def logfile_open(scripttype): """ Returns an open logfile with a timestamp. It is left to the calling function to write to the file and close it. This function will terminate the calling function if an IOError occurs while opening the file. Parameters: 1. scripttype: A string describing the installation script that is being run. """ rundir = os.getcwd() logsdir = "/install/logs/" prefix = "install_" + scripttype + "_" dt = datetime.datetime date_suffix = "null" try: date_suffix = datestring_functions.datestring(dt) except ValueError as ve: print "ValueError:\n %s" % ve print "Bad datetime object, could not generate logfile name." print "Script will terminate." exit(1) # Assumes rundir is not terminated with a "/" logfile_path = rundir + logsdir + prefix + date_suffix + ".log" try: logfile = open(logfile_path, "w") return logfile except IOError as ioe: print "IOError:\n %s" % ioe print "Could not open logfile at %s for writing." % logfile_path print "Script will terminate." exit(1)
def logfile_open(scripttype): """ Returns an open logfile with a timestamp. It is left to the calling function to write to the file and close it. This function will terminate the calling function if an IOError occurs while opening the file. Parameters: 1. scripttype: A string describing the installation script that is being run. """ rundir = os.getcwd() logsdir = "/install/logs/" prefix = "install_" + scripttype + "_" dt = datetime.datetime date_suffix = "null" logfile = None try: date_suffix = datestring_functions.datestring(dt) # Assumes rundir is not terminated with a "/" logfile_path = rundir + logsdir + prefix + date_suffix + ".log" try: logfile = open(logfile_path, 'w') except IOError as ioe: print "IOError:\n %s" % ioe print "Could not open logfile at %s for writing." % logfile_path print "Script will terminate." logfile = None except ValueError as ve: print "ValueError:\n %s" % ve print "Bad datetime object, could not generate logfile name." print "Script will terminate." logfile = None return logfile
def logfile_open(scripttype): """ Returns an open logfile with a timestamp. It is left to the calling function to write to the file and close it. This function will return None if an IOError occurs while opening the file. Parameters: 1. scripttype: A string describing the installation script that is being run. """ # Build file path to logs directory based on location of this file runpath = os.path.dirname(os.path.realpath(__file__)) pathdirs = runpath.split(os.sep) assembled_path = "" i = 0 # Assume the last part is the filename while i < len(pathdirs): if i == 0: assembled_path = pathdirs[i] else: assembled_path = assembled_path + os.sep + pathdirs[i] i = i + 1 logsdir = assembled_path + os.sep + "logs" prefix = "install_" + scripttype + "_" dt = datetime.datetime date_suffix = "null" logfile = None try: date_suffix = datestring_functions.datestring(dt) logfile_path = logsdir + os.sep + prefix + date_suffix + ".log" try: result = subprocess.check_call( shlex.split("touch %s" % logfile_path)) if result == 0: logfile = open(logfile_path, 'a') elif result == 1: print "Could not create logfile at %s." % logfile_path print "Script will terminate." logfile = None except IOError as ioe: print "IOError:\n %s" % ioe print "Could not open logfile at %s for writing." % logfile_path print "Script will terminate." logfile = None except ValueError as ve: print "ValueError:\n %s" % ve print "Bad datetime object, could not generate logfile name." print "Script will terminate." logfile = None return logfile
def logfile_open(scripttype): """ Returns an open logfile with a timestamp. It is left to the calling function to write to the file and close it. This function will return None if an IOError occurs while opening the file. Parameters: 1. scripttype: A string describing the installation script that is being run. """ # Build file path to logs directory based on location of this file runpath = os.path.dirname(os.path.realpath(__file__)) pathdirs = runpath.split(os.sep) assembled_path = "" i = 0 # Assume the last part is the filename while i < len(pathdirs): if i == 0: assembled_path = pathdirs[i] else: assembled_path = assembled_path + os.sep + pathdirs[i] i = i + 1 logsdir = assembled_path + os.sep + "logs" prefix = "install_" + scripttype + "_" dt = datetime.datetime date_suffix = "null" logfile = None try: date_suffix = datestring_functions.datestring(dt) logfile_path = logsdir + os.sep + prefix + date_suffix + ".log" try: result = subprocess.check_call(shlex.split("touch %s" % logfile_path)) if result == 0: logfile = open(logfile_path, 'a') elif result == 1: print "Could not create logfile at %s." % logfile_path print "Script will terminate." logfile = None except IOError as ioe: print "IOError:\n %s" % ioe print "Could not open logfile at %s for writing." % logfile_path print "Script will terminate." logfile = None except ValueError as ve: print "ValueError:\n %s" % ve print "Bad datetime object, could not generate logfile name." print "Script will terminate." logfile = None return logfile