pexpect helpful hints - find EOF with child.readline() and also own computer backup code

Multi tool use
Multi tool use












1















I have used this website over a hundred times and it has helped me so much with my coding (in python, arduino, terminal commands and Window's prompt). I thought I would put up some knowledge that I found, for things that Stack overflow could not help me with but my be helpful for others in a similar situation. So have a look at the code below. I hope if helps people with creating their own backup code. I am most proud with the "while 'rn' in output" part of the below code. :



    output = child0.readline()
while 'rn' in output:
msg.log(output.replace('rn', ''), logMode + 's')
output = child0.readline()


This helps find the EOF when the program has finished running. Hence you can output the terminal program's output as the program is running.



I will be adding a Windows version to this code too. Possibly with robocopy.



Any questions with the below code, please do not hesitate to ask. NB: I changed people's names and removed my username and passwords.



#!/usr/bin/python

# Written by irishcream24, amateur coder


import subprocess
import sys
import os.path
import logAndError # my own library to handle errors and log events
from inspect import currentframe as CF # help with logging
from inspect import getframeinfo as GFI # help with logging
import threading
import fcntl
import pexpect
import time
import socket
import time as t



from sys import platform
if platform == "win32":
import msvcrt
portSearch = "Uno"
portResultPosition = 1
elif platform == "darwin":
portSearch = "usb"
portResultPosition = 0
else:
print 'Unknown operating system'
print 'Ending Program...'
sys.exit()



# Check if another instance of the program is running, if so, then stop second.
pid_file = 'program.pid'
fp = open(pid_file, 'w')
try:
fcntl.lockf(fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
# another instance is running
print "Program already running, stopping the second instance..."
sys.exit(1)



# Determine where main program files are stored
directory = os.path.dirname(os.path.realpath(__file__))



# To print stderr to both screen and file
errCounter = 0
exitFlag = [0]
class tee:
def __init__(self, _fd1, _fd2):
self.fd1 = _fd1
self.fd2 = _fd2

def __del__(self):
if self.fd1 != sys.stdout and self.fd1 != sys.stderr :
self.fd1.close()
if self.fd2 != sys.stdout and self.fd2 != sys.stderr :
self.fd2.close()

def write(self, text):
global errCounter
global exitFlag
if errCounter == 0:
self.fd1.write('%s: ' %t.strftime("%d/%m/%y %H:%M"))
self.fd2.write('%s: ' %t.strftime("%d/%m/%y %H:%M"))
errCounter = 1
exitFlag[0] = 1
self.fd1.write(text)
self.fd2.write(text)

def flush(self):
self.fd1.flush()
self.fd2.flush()


# Error and log handling
errMode = 'pf' # p = print to screen, f = print to file, e = end program
errorFileAddress = '%s/errorFile.txt' %directory
outputlog = open(errorFileAddress, "a")
sys.stderr = tee(sys.stderr, outputlog)
logFileAddress = '%s/log.txt' %directory
logMode = 'pf' # p = print to screen, f = print to file
msg = logAndError.logAndError(errorFileAddress, logFileAddress)


# Set computer to be backed up
sourceComputer = 'DebbieMac'
try:
sourceComputer = sys.argv[1]
except:
print 'No source arguement given.'

if sourceComputer == 'SamMac' or sourceComputer == 'DebbieMac' or sourceComputer == 'mediaCentre' or sourceComputer == 'garageComputer':
pass
else:
msg.error('incorrect source computer supplied!', errMode, GFI(CF()).lineno, exitFlag)
sys.exit()


# Source and destination setup
backupRoute = 'network'
try:
backupRoute = sys.argv[2]
except:
print 'No back up route arguement given.'

if backupRoute == 'network' or backupRoute == 'direct' or backupRoute == 'testNetwork' or backupRoute == 'testDirect':
pass
else:
msg.error('incorrect backup route supplied!', errMode, GFI(CF()).lineno, exitFlag)
sys.exit()


# Source, destination and exclude dictionaries
v = {
'SamMac network source' : '/Users/SamJones',
'SamMac network destination' : '/Volumes/Seagate/Sam_macbook_backup/Backups',
'SamMac direct source' : '/Users/SamJones',
'SamMac direct destination' : '/Volumes/Seagate Backup Plus Drive/Sam_macbook_backup/Backups',
'SamMac testNetwork source' : '/Users/SamJones/Documents/Arduino/arduino_sketches-master',
'SamMac testNetwork destination' : '/Volumes/Seagate/Sam_macbook_backup/Arduino',
'SamMac exclude' : ['.*', '.Trash', 'Library', 'Pictures'],
'DebbieMac network source' : '/Users/DebbieJones',
'DebbieMac network destination' : '/Volumes/Seagate/Debbie_macbook_backup/Backups',
'DebbieMac direct source' : '/Users/DebbieJones',
'DebbieMac direct destination' : '/Volumes/Seagate Backup Plus Drive/Debbie_macbook_backup/Backups',
'DebbieMac testNetwork source': '/Users/DebbieJones/testFolder',
'DebbieMac testNetwork destination' : '/Volumes/Seagate/Debbie_macbook_backup',
'DebbieMac testDirect source' : '/Users/DebbieJones/testFolder',
'DebbieMac testDirect destination' : '/Volumes/Seagate Backup Plus Drive/Debbie_macbook_backup',
'DebbieMac exclude' : ['.*', '.Trash', 'Library', 'Pictures']
}



# Main threading code
class mainThreadClass(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
PIDMessage = 'Starting backup PID: %s'%os.getpid()
msg.log(PIDMessage, logMode)
mainThread()
msg.log('Process completed successfullyn', logMode)



def mainThread():

if platform == "win32":
pass

elif platform == "darwin":

if 'network' in backupRoute:
# Connect to SeagateBackup
if os.path.ismount('/Volumes/Seagate') == False:
msg.log('Mounting Seagate Backup Hub', logMode)
commandM = 'mount volume'
smbPoint = '"smb://username:password@mediacentre/Seagate"'
childM = pexpect.spawn("%s '%s %s'" %('osascript -e', commandM, smbPoint), timeout = None)
childM.expect(pexpect.EOF)
else:
msg.log('Seagate already mounted', logMode)


# Use rsync to backup files
commandR = 'rsync -avb '
for s in v['%s exclude' %sourceComputer]:
commandR = commandR + "--exclude '%s' " %s
commandR = commandR + '--delete --backup-dir="../PreviousBackups/%s" ' %time.strftime("%d-%m-%y %H%M")

commandR = commandR + '%s %s' %(v['%s %s source' %(sourceComputer, backupRoute)], v['%s %s destination' %(sourceComputer, backupRoute)])
msg.log(commandR, logMode)
msg.log('Running rsync...rsync output below', logMode)
child0 = pexpect.spawn(commandR, timeout = None)

# Handling command output
# If no 'rn' in readline() output, then EOF reached
output = child0.readline()
while 'rn' in output:
msg.log(output.replace('rn', ''), logMode + 's')
output = child0.readline()

return


if __name__ == '__main__':
# Create new threads
threadMain = mainThreadClass()
# Start new Threads
threadMain.start()




logAndError.py



# to handle errors

import time
import sys
import threading


class logAndError:
def __init__(self, errorAddress, logAddress):
self.errorAddress = errorAddress
self.logAddress = logAddress
self.lock = threading.RLock()


def error(self, message, errMode, lineNumber=None, exitFlag=[0]):

message = '%s: %s' %(time.strftime("%d/%m/%y %H:%M"), message)

# p = print to screen, f = print to file, e = end program
if 'p' in errMode:
print message
if 'f' in errMode and 'e' not in errMode:
errorFile = open(self.errorAddress, 'a')
errorFile.write('%sn' %message)
errorFile.close()
return


def log(self, logmsg, logMode):
with self.lock:
logmsg2 = '%s: %s' %(time.strftime("%d/%m/%y %H:%M"), logmsg)
if 'p' in logMode:
# s = simple (no date stamp)
if 's' in logMode:
print logmsg
else:
print logmsg2
if 'f' in logMode:
if 's' in logMode:
logFile = open(self.logAddress, 'a')
logFile.write('%sn' %logmsg)
logFile.close()
else:
logFile = open(self.logAddress, 'a')
logFile.write('%sn' %logmsg2)
logFile.close()
return









share|improve this question



























    1















    I have used this website over a hundred times and it has helped me so much with my coding (in python, arduino, terminal commands and Window's prompt). I thought I would put up some knowledge that I found, for things that Stack overflow could not help me with but my be helpful for others in a similar situation. So have a look at the code below. I hope if helps people with creating their own backup code. I am most proud with the "while 'rn' in output" part of the below code. :



        output = child0.readline()
    while 'rn' in output:
    msg.log(output.replace('rn', ''), logMode + 's')
    output = child0.readline()


    This helps find the EOF when the program has finished running. Hence you can output the terminal program's output as the program is running.



    I will be adding a Windows version to this code too. Possibly with robocopy.



    Any questions with the below code, please do not hesitate to ask. NB: I changed people's names and removed my username and passwords.



    #!/usr/bin/python

    # Written by irishcream24, amateur coder


    import subprocess
    import sys
    import os.path
    import logAndError # my own library to handle errors and log events
    from inspect import currentframe as CF # help with logging
    from inspect import getframeinfo as GFI # help with logging
    import threading
    import fcntl
    import pexpect
    import time
    import socket
    import time as t



    from sys import platform
    if platform == "win32":
    import msvcrt
    portSearch = "Uno"
    portResultPosition = 1
    elif platform == "darwin":
    portSearch = "usb"
    portResultPosition = 0
    else:
    print 'Unknown operating system'
    print 'Ending Program...'
    sys.exit()



    # Check if another instance of the program is running, if so, then stop second.
    pid_file = 'program.pid'
    fp = open(pid_file, 'w')
    try:
    fcntl.lockf(fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
    except IOError:
    # another instance is running
    print "Program already running, stopping the second instance..."
    sys.exit(1)



    # Determine where main program files are stored
    directory = os.path.dirname(os.path.realpath(__file__))



    # To print stderr to both screen and file
    errCounter = 0
    exitFlag = [0]
    class tee:
    def __init__(self, _fd1, _fd2):
    self.fd1 = _fd1
    self.fd2 = _fd2

    def __del__(self):
    if self.fd1 != sys.stdout and self.fd1 != sys.stderr :
    self.fd1.close()
    if self.fd2 != sys.stdout and self.fd2 != sys.stderr :
    self.fd2.close()

    def write(self, text):
    global errCounter
    global exitFlag
    if errCounter == 0:
    self.fd1.write('%s: ' %t.strftime("%d/%m/%y %H:%M"))
    self.fd2.write('%s: ' %t.strftime("%d/%m/%y %H:%M"))
    errCounter = 1
    exitFlag[0] = 1
    self.fd1.write(text)
    self.fd2.write(text)

    def flush(self):
    self.fd1.flush()
    self.fd2.flush()


    # Error and log handling
    errMode = 'pf' # p = print to screen, f = print to file, e = end program
    errorFileAddress = '%s/errorFile.txt' %directory
    outputlog = open(errorFileAddress, "a")
    sys.stderr = tee(sys.stderr, outputlog)
    logFileAddress = '%s/log.txt' %directory
    logMode = 'pf' # p = print to screen, f = print to file
    msg = logAndError.logAndError(errorFileAddress, logFileAddress)


    # Set computer to be backed up
    sourceComputer = 'DebbieMac'
    try:
    sourceComputer = sys.argv[1]
    except:
    print 'No source arguement given.'

    if sourceComputer == 'SamMac' or sourceComputer == 'DebbieMac' or sourceComputer == 'mediaCentre' or sourceComputer == 'garageComputer':
    pass
    else:
    msg.error('incorrect source computer supplied!', errMode, GFI(CF()).lineno, exitFlag)
    sys.exit()


    # Source and destination setup
    backupRoute = 'network'
    try:
    backupRoute = sys.argv[2]
    except:
    print 'No back up route arguement given.'

    if backupRoute == 'network' or backupRoute == 'direct' or backupRoute == 'testNetwork' or backupRoute == 'testDirect':
    pass
    else:
    msg.error('incorrect backup route supplied!', errMode, GFI(CF()).lineno, exitFlag)
    sys.exit()


    # Source, destination and exclude dictionaries
    v = {
    'SamMac network source' : '/Users/SamJones',
    'SamMac network destination' : '/Volumes/Seagate/Sam_macbook_backup/Backups',
    'SamMac direct source' : '/Users/SamJones',
    'SamMac direct destination' : '/Volumes/Seagate Backup Plus Drive/Sam_macbook_backup/Backups',
    'SamMac testNetwork source' : '/Users/SamJones/Documents/Arduino/arduino_sketches-master',
    'SamMac testNetwork destination' : '/Volumes/Seagate/Sam_macbook_backup/Arduino',
    'SamMac exclude' : ['.*', '.Trash', 'Library', 'Pictures'],
    'DebbieMac network source' : '/Users/DebbieJones',
    'DebbieMac network destination' : '/Volumes/Seagate/Debbie_macbook_backup/Backups',
    'DebbieMac direct source' : '/Users/DebbieJones',
    'DebbieMac direct destination' : '/Volumes/Seagate Backup Plus Drive/Debbie_macbook_backup/Backups',
    'DebbieMac testNetwork source': '/Users/DebbieJones/testFolder',
    'DebbieMac testNetwork destination' : '/Volumes/Seagate/Debbie_macbook_backup',
    'DebbieMac testDirect source' : '/Users/DebbieJones/testFolder',
    'DebbieMac testDirect destination' : '/Volumes/Seagate Backup Plus Drive/Debbie_macbook_backup',
    'DebbieMac exclude' : ['.*', '.Trash', 'Library', 'Pictures']
    }



    # Main threading code
    class mainThreadClass(threading.Thread):
    def __init__(self):
    threading.Thread.__init__(self)
    def run(self):
    PIDMessage = 'Starting backup PID: %s'%os.getpid()
    msg.log(PIDMessage, logMode)
    mainThread()
    msg.log('Process completed successfullyn', logMode)



    def mainThread():

    if platform == "win32":
    pass

    elif platform == "darwin":

    if 'network' in backupRoute:
    # Connect to SeagateBackup
    if os.path.ismount('/Volumes/Seagate') == False:
    msg.log('Mounting Seagate Backup Hub', logMode)
    commandM = 'mount volume'
    smbPoint = '"smb://username:password@mediacentre/Seagate"'
    childM = pexpect.spawn("%s '%s %s'" %('osascript -e', commandM, smbPoint), timeout = None)
    childM.expect(pexpect.EOF)
    else:
    msg.log('Seagate already mounted', logMode)


    # Use rsync to backup files
    commandR = 'rsync -avb '
    for s in v['%s exclude' %sourceComputer]:
    commandR = commandR + "--exclude '%s' " %s
    commandR = commandR + '--delete --backup-dir="../PreviousBackups/%s" ' %time.strftime("%d-%m-%y %H%M")

    commandR = commandR + '%s %s' %(v['%s %s source' %(sourceComputer, backupRoute)], v['%s %s destination' %(sourceComputer, backupRoute)])
    msg.log(commandR, logMode)
    msg.log('Running rsync...rsync output below', logMode)
    child0 = pexpect.spawn(commandR, timeout = None)

    # Handling command output
    # If no 'rn' in readline() output, then EOF reached
    output = child0.readline()
    while 'rn' in output:
    msg.log(output.replace('rn', ''), logMode + 's')
    output = child0.readline()

    return


    if __name__ == '__main__':
    # Create new threads
    threadMain = mainThreadClass()
    # Start new Threads
    threadMain.start()




    logAndError.py



    # to handle errors

    import time
    import sys
    import threading


    class logAndError:
    def __init__(self, errorAddress, logAddress):
    self.errorAddress = errorAddress
    self.logAddress = logAddress
    self.lock = threading.RLock()


    def error(self, message, errMode, lineNumber=None, exitFlag=[0]):

    message = '%s: %s' %(time.strftime("%d/%m/%y %H:%M"), message)

    # p = print to screen, f = print to file, e = end program
    if 'p' in errMode:
    print message
    if 'f' in errMode and 'e' not in errMode:
    errorFile = open(self.errorAddress, 'a')
    errorFile.write('%sn' %message)
    errorFile.close()
    return


    def log(self, logmsg, logMode):
    with self.lock:
    logmsg2 = '%s: %s' %(time.strftime("%d/%m/%y %H:%M"), logmsg)
    if 'p' in logMode:
    # s = simple (no date stamp)
    if 's' in logMode:
    print logmsg
    else:
    print logmsg2
    if 'f' in logMode:
    if 's' in logMode:
    logFile = open(self.logAddress, 'a')
    logFile.write('%sn' %logmsg)
    logFile.close()
    else:
    logFile = open(self.logAddress, 'a')
    logFile.write('%sn' %logmsg2)
    logFile.close()
    return









    share|improve this question

























      1












      1








      1








      I have used this website over a hundred times and it has helped me so much with my coding (in python, arduino, terminal commands and Window's prompt). I thought I would put up some knowledge that I found, for things that Stack overflow could not help me with but my be helpful for others in a similar situation. So have a look at the code below. I hope if helps people with creating their own backup code. I am most proud with the "while 'rn' in output" part of the below code. :



          output = child0.readline()
      while 'rn' in output:
      msg.log(output.replace('rn', ''), logMode + 's')
      output = child0.readline()


      This helps find the EOF when the program has finished running. Hence you can output the terminal program's output as the program is running.



      I will be adding a Windows version to this code too. Possibly with robocopy.



      Any questions with the below code, please do not hesitate to ask. NB: I changed people's names and removed my username and passwords.



      #!/usr/bin/python

      # Written by irishcream24, amateur coder


      import subprocess
      import sys
      import os.path
      import logAndError # my own library to handle errors and log events
      from inspect import currentframe as CF # help with logging
      from inspect import getframeinfo as GFI # help with logging
      import threading
      import fcntl
      import pexpect
      import time
      import socket
      import time as t



      from sys import platform
      if platform == "win32":
      import msvcrt
      portSearch = "Uno"
      portResultPosition = 1
      elif platform == "darwin":
      portSearch = "usb"
      portResultPosition = 0
      else:
      print 'Unknown operating system'
      print 'Ending Program...'
      sys.exit()



      # Check if another instance of the program is running, if so, then stop second.
      pid_file = 'program.pid'
      fp = open(pid_file, 'w')
      try:
      fcntl.lockf(fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
      except IOError:
      # another instance is running
      print "Program already running, stopping the second instance..."
      sys.exit(1)



      # Determine where main program files are stored
      directory = os.path.dirname(os.path.realpath(__file__))



      # To print stderr to both screen and file
      errCounter = 0
      exitFlag = [0]
      class tee:
      def __init__(self, _fd1, _fd2):
      self.fd1 = _fd1
      self.fd2 = _fd2

      def __del__(self):
      if self.fd1 != sys.stdout and self.fd1 != sys.stderr :
      self.fd1.close()
      if self.fd2 != sys.stdout and self.fd2 != sys.stderr :
      self.fd2.close()

      def write(self, text):
      global errCounter
      global exitFlag
      if errCounter == 0:
      self.fd1.write('%s: ' %t.strftime("%d/%m/%y %H:%M"))
      self.fd2.write('%s: ' %t.strftime("%d/%m/%y %H:%M"))
      errCounter = 1
      exitFlag[0] = 1
      self.fd1.write(text)
      self.fd2.write(text)

      def flush(self):
      self.fd1.flush()
      self.fd2.flush()


      # Error and log handling
      errMode = 'pf' # p = print to screen, f = print to file, e = end program
      errorFileAddress = '%s/errorFile.txt' %directory
      outputlog = open(errorFileAddress, "a")
      sys.stderr = tee(sys.stderr, outputlog)
      logFileAddress = '%s/log.txt' %directory
      logMode = 'pf' # p = print to screen, f = print to file
      msg = logAndError.logAndError(errorFileAddress, logFileAddress)


      # Set computer to be backed up
      sourceComputer = 'DebbieMac'
      try:
      sourceComputer = sys.argv[1]
      except:
      print 'No source arguement given.'

      if sourceComputer == 'SamMac' or sourceComputer == 'DebbieMac' or sourceComputer == 'mediaCentre' or sourceComputer == 'garageComputer':
      pass
      else:
      msg.error('incorrect source computer supplied!', errMode, GFI(CF()).lineno, exitFlag)
      sys.exit()


      # Source and destination setup
      backupRoute = 'network'
      try:
      backupRoute = sys.argv[2]
      except:
      print 'No back up route arguement given.'

      if backupRoute == 'network' or backupRoute == 'direct' or backupRoute == 'testNetwork' or backupRoute == 'testDirect':
      pass
      else:
      msg.error('incorrect backup route supplied!', errMode, GFI(CF()).lineno, exitFlag)
      sys.exit()


      # Source, destination and exclude dictionaries
      v = {
      'SamMac network source' : '/Users/SamJones',
      'SamMac network destination' : '/Volumes/Seagate/Sam_macbook_backup/Backups',
      'SamMac direct source' : '/Users/SamJones',
      'SamMac direct destination' : '/Volumes/Seagate Backup Plus Drive/Sam_macbook_backup/Backups',
      'SamMac testNetwork source' : '/Users/SamJones/Documents/Arduino/arduino_sketches-master',
      'SamMac testNetwork destination' : '/Volumes/Seagate/Sam_macbook_backup/Arduino',
      'SamMac exclude' : ['.*', '.Trash', 'Library', 'Pictures'],
      'DebbieMac network source' : '/Users/DebbieJones',
      'DebbieMac network destination' : '/Volumes/Seagate/Debbie_macbook_backup/Backups',
      'DebbieMac direct source' : '/Users/DebbieJones',
      'DebbieMac direct destination' : '/Volumes/Seagate Backup Plus Drive/Debbie_macbook_backup/Backups',
      'DebbieMac testNetwork source': '/Users/DebbieJones/testFolder',
      'DebbieMac testNetwork destination' : '/Volumes/Seagate/Debbie_macbook_backup',
      'DebbieMac testDirect source' : '/Users/DebbieJones/testFolder',
      'DebbieMac testDirect destination' : '/Volumes/Seagate Backup Plus Drive/Debbie_macbook_backup',
      'DebbieMac exclude' : ['.*', '.Trash', 'Library', 'Pictures']
      }



      # Main threading code
      class mainThreadClass(threading.Thread):
      def __init__(self):
      threading.Thread.__init__(self)
      def run(self):
      PIDMessage = 'Starting backup PID: %s'%os.getpid()
      msg.log(PIDMessage, logMode)
      mainThread()
      msg.log('Process completed successfullyn', logMode)



      def mainThread():

      if platform == "win32":
      pass

      elif platform == "darwin":

      if 'network' in backupRoute:
      # Connect to SeagateBackup
      if os.path.ismount('/Volumes/Seagate') == False:
      msg.log('Mounting Seagate Backup Hub', logMode)
      commandM = 'mount volume'
      smbPoint = '"smb://username:password@mediacentre/Seagate"'
      childM = pexpect.spawn("%s '%s %s'" %('osascript -e', commandM, smbPoint), timeout = None)
      childM.expect(pexpect.EOF)
      else:
      msg.log('Seagate already mounted', logMode)


      # Use rsync to backup files
      commandR = 'rsync -avb '
      for s in v['%s exclude' %sourceComputer]:
      commandR = commandR + "--exclude '%s' " %s
      commandR = commandR + '--delete --backup-dir="../PreviousBackups/%s" ' %time.strftime("%d-%m-%y %H%M")

      commandR = commandR + '%s %s' %(v['%s %s source' %(sourceComputer, backupRoute)], v['%s %s destination' %(sourceComputer, backupRoute)])
      msg.log(commandR, logMode)
      msg.log('Running rsync...rsync output below', logMode)
      child0 = pexpect.spawn(commandR, timeout = None)

      # Handling command output
      # If no 'rn' in readline() output, then EOF reached
      output = child0.readline()
      while 'rn' in output:
      msg.log(output.replace('rn', ''), logMode + 's')
      output = child0.readline()

      return


      if __name__ == '__main__':
      # Create new threads
      threadMain = mainThreadClass()
      # Start new Threads
      threadMain.start()




      logAndError.py



      # to handle errors

      import time
      import sys
      import threading


      class logAndError:
      def __init__(self, errorAddress, logAddress):
      self.errorAddress = errorAddress
      self.logAddress = logAddress
      self.lock = threading.RLock()


      def error(self, message, errMode, lineNumber=None, exitFlag=[0]):

      message = '%s: %s' %(time.strftime("%d/%m/%y %H:%M"), message)

      # p = print to screen, f = print to file, e = end program
      if 'p' in errMode:
      print message
      if 'f' in errMode and 'e' not in errMode:
      errorFile = open(self.errorAddress, 'a')
      errorFile.write('%sn' %message)
      errorFile.close()
      return


      def log(self, logmsg, logMode):
      with self.lock:
      logmsg2 = '%s: %s' %(time.strftime("%d/%m/%y %H:%M"), logmsg)
      if 'p' in logMode:
      # s = simple (no date stamp)
      if 's' in logMode:
      print logmsg
      else:
      print logmsg2
      if 'f' in logMode:
      if 's' in logMode:
      logFile = open(self.logAddress, 'a')
      logFile.write('%sn' %logmsg)
      logFile.close()
      else:
      logFile = open(self.logAddress, 'a')
      logFile.write('%sn' %logmsg2)
      logFile.close()
      return









      share|improve this question














      I have used this website over a hundred times and it has helped me so much with my coding (in python, arduino, terminal commands and Window's prompt). I thought I would put up some knowledge that I found, for things that Stack overflow could not help me with but my be helpful for others in a similar situation. So have a look at the code below. I hope if helps people with creating their own backup code. I am most proud with the "while 'rn' in output" part of the below code. :



          output = child0.readline()
      while 'rn' in output:
      msg.log(output.replace('rn', ''), logMode + 's')
      output = child0.readline()


      This helps find the EOF when the program has finished running. Hence you can output the terminal program's output as the program is running.



      I will be adding a Windows version to this code too. Possibly with robocopy.



      Any questions with the below code, please do not hesitate to ask. NB: I changed people's names and removed my username and passwords.



      #!/usr/bin/python

      # Written by irishcream24, amateur coder


      import subprocess
      import sys
      import os.path
      import logAndError # my own library to handle errors and log events
      from inspect import currentframe as CF # help with logging
      from inspect import getframeinfo as GFI # help with logging
      import threading
      import fcntl
      import pexpect
      import time
      import socket
      import time as t



      from sys import platform
      if platform == "win32":
      import msvcrt
      portSearch = "Uno"
      portResultPosition = 1
      elif platform == "darwin":
      portSearch = "usb"
      portResultPosition = 0
      else:
      print 'Unknown operating system'
      print 'Ending Program...'
      sys.exit()



      # Check if another instance of the program is running, if so, then stop second.
      pid_file = 'program.pid'
      fp = open(pid_file, 'w')
      try:
      fcntl.lockf(fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
      except IOError:
      # another instance is running
      print "Program already running, stopping the second instance..."
      sys.exit(1)



      # Determine where main program files are stored
      directory = os.path.dirname(os.path.realpath(__file__))



      # To print stderr to both screen and file
      errCounter = 0
      exitFlag = [0]
      class tee:
      def __init__(self, _fd1, _fd2):
      self.fd1 = _fd1
      self.fd2 = _fd2

      def __del__(self):
      if self.fd1 != sys.stdout and self.fd1 != sys.stderr :
      self.fd1.close()
      if self.fd2 != sys.stdout and self.fd2 != sys.stderr :
      self.fd2.close()

      def write(self, text):
      global errCounter
      global exitFlag
      if errCounter == 0:
      self.fd1.write('%s: ' %t.strftime("%d/%m/%y %H:%M"))
      self.fd2.write('%s: ' %t.strftime("%d/%m/%y %H:%M"))
      errCounter = 1
      exitFlag[0] = 1
      self.fd1.write(text)
      self.fd2.write(text)

      def flush(self):
      self.fd1.flush()
      self.fd2.flush()


      # Error and log handling
      errMode = 'pf' # p = print to screen, f = print to file, e = end program
      errorFileAddress = '%s/errorFile.txt' %directory
      outputlog = open(errorFileAddress, "a")
      sys.stderr = tee(sys.stderr, outputlog)
      logFileAddress = '%s/log.txt' %directory
      logMode = 'pf' # p = print to screen, f = print to file
      msg = logAndError.logAndError(errorFileAddress, logFileAddress)


      # Set computer to be backed up
      sourceComputer = 'DebbieMac'
      try:
      sourceComputer = sys.argv[1]
      except:
      print 'No source arguement given.'

      if sourceComputer == 'SamMac' or sourceComputer == 'DebbieMac' or sourceComputer == 'mediaCentre' or sourceComputer == 'garageComputer':
      pass
      else:
      msg.error('incorrect source computer supplied!', errMode, GFI(CF()).lineno, exitFlag)
      sys.exit()


      # Source and destination setup
      backupRoute = 'network'
      try:
      backupRoute = sys.argv[2]
      except:
      print 'No back up route arguement given.'

      if backupRoute == 'network' or backupRoute == 'direct' or backupRoute == 'testNetwork' or backupRoute == 'testDirect':
      pass
      else:
      msg.error('incorrect backup route supplied!', errMode, GFI(CF()).lineno, exitFlag)
      sys.exit()


      # Source, destination and exclude dictionaries
      v = {
      'SamMac network source' : '/Users/SamJones',
      'SamMac network destination' : '/Volumes/Seagate/Sam_macbook_backup/Backups',
      'SamMac direct source' : '/Users/SamJones',
      'SamMac direct destination' : '/Volumes/Seagate Backup Plus Drive/Sam_macbook_backup/Backups',
      'SamMac testNetwork source' : '/Users/SamJones/Documents/Arduino/arduino_sketches-master',
      'SamMac testNetwork destination' : '/Volumes/Seagate/Sam_macbook_backup/Arduino',
      'SamMac exclude' : ['.*', '.Trash', 'Library', 'Pictures'],
      'DebbieMac network source' : '/Users/DebbieJones',
      'DebbieMac network destination' : '/Volumes/Seagate/Debbie_macbook_backup/Backups',
      'DebbieMac direct source' : '/Users/DebbieJones',
      'DebbieMac direct destination' : '/Volumes/Seagate Backup Plus Drive/Debbie_macbook_backup/Backups',
      'DebbieMac testNetwork source': '/Users/DebbieJones/testFolder',
      'DebbieMac testNetwork destination' : '/Volumes/Seagate/Debbie_macbook_backup',
      'DebbieMac testDirect source' : '/Users/DebbieJones/testFolder',
      'DebbieMac testDirect destination' : '/Volumes/Seagate Backup Plus Drive/Debbie_macbook_backup',
      'DebbieMac exclude' : ['.*', '.Trash', 'Library', 'Pictures']
      }



      # Main threading code
      class mainThreadClass(threading.Thread):
      def __init__(self):
      threading.Thread.__init__(self)
      def run(self):
      PIDMessage = 'Starting backup PID: %s'%os.getpid()
      msg.log(PIDMessage, logMode)
      mainThread()
      msg.log('Process completed successfullyn', logMode)



      def mainThread():

      if platform == "win32":
      pass

      elif platform == "darwin":

      if 'network' in backupRoute:
      # Connect to SeagateBackup
      if os.path.ismount('/Volumes/Seagate') == False:
      msg.log('Mounting Seagate Backup Hub', logMode)
      commandM = 'mount volume'
      smbPoint = '"smb://username:password@mediacentre/Seagate"'
      childM = pexpect.spawn("%s '%s %s'" %('osascript -e', commandM, smbPoint), timeout = None)
      childM.expect(pexpect.EOF)
      else:
      msg.log('Seagate already mounted', logMode)


      # Use rsync to backup files
      commandR = 'rsync -avb '
      for s in v['%s exclude' %sourceComputer]:
      commandR = commandR + "--exclude '%s' " %s
      commandR = commandR + '--delete --backup-dir="../PreviousBackups/%s" ' %time.strftime("%d-%m-%y %H%M")

      commandR = commandR + '%s %s' %(v['%s %s source' %(sourceComputer, backupRoute)], v['%s %s destination' %(sourceComputer, backupRoute)])
      msg.log(commandR, logMode)
      msg.log('Running rsync...rsync output below', logMode)
      child0 = pexpect.spawn(commandR, timeout = None)

      # Handling command output
      # If no 'rn' in readline() output, then EOF reached
      output = child0.readline()
      while 'rn' in output:
      msg.log(output.replace('rn', ''), logMode + 's')
      output = child0.readline()

      return


      if __name__ == '__main__':
      # Create new threads
      threadMain = mainThreadClass()
      # Start new Threads
      threadMain.start()




      logAndError.py



      # to handle errors

      import time
      import sys
      import threading


      class logAndError:
      def __init__(self, errorAddress, logAddress):
      self.errorAddress = errorAddress
      self.logAddress = logAddress
      self.lock = threading.RLock()


      def error(self, message, errMode, lineNumber=None, exitFlag=[0]):

      message = '%s: %s' %(time.strftime("%d/%m/%y %H:%M"), message)

      # p = print to screen, f = print to file, e = end program
      if 'p' in errMode:
      print message
      if 'f' in errMode and 'e' not in errMode:
      errorFile = open(self.errorAddress, 'a')
      errorFile.write('%sn' %message)
      errorFile.close()
      return


      def log(self, logmsg, logMode):
      with self.lock:
      logmsg2 = '%s: %s' %(time.strftime("%d/%m/%y %H:%M"), logmsg)
      if 'p' in logMode:
      # s = simple (no date stamp)
      if 's' in logMode:
      print logmsg
      else:
      print logmsg2
      if 'f' in logMode:
      if 's' in logMode:
      logFile = open(self.logAddress, 'a')
      logFile.write('%sn' %logmsg)
      logFile.close()
      else:
      logFile = open(self.logAddress, 'a')
      logFile.write('%sn' %logmsg2)
      logFile.close()
      return






      pexpect






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 30 '18 at 20:31









      irishcream24irishcream24

      61




      61
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53981198%2fpexpect-helpful-hints-find-eof-with-child-readline-and-also-own-computer-bac%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53981198%2fpexpect-helpful-hints-find-eof-with-child-readline-and-also-own-computer-bac%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          7bbyktUTj3sYn3sBrIqBuVC egxj1
          Qj q3ip8UNrdjdheRew0ZnTj9vpjG XY mixEk2nmb,AdLLdrzsEy0EErZI9OcyV rmLy6edMZOpZ

          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas