def enable_file_logging(self, toolname, num_logs=50, log_dir=None): """ Find a tmp location for file logging """ from c_misc import backup_file import c_path if log_dir is None: log_dir = '' for tmp_path in [os.getenv('TEMP'), os.getenv('TMP')]: if c_path.validate_dir(tmp_path): log_dir = c_path.join(tmp_path, '{0}_logs'.format(toolname)) break else: self.warning( 'CANNOT FIND A LOCAL TEMP DIRECTORY TO CREATE LOGS') # Set Log Folder flids = [] if log_dir: log_dir = c_path.normalize(log_dir) log_file = c_path.join(log_dir, '{0}_log.txt'.format(toolname)) log_file_debug = c_path.join(log_dir, '{0}_log_debug.txt'.format(toolname)) # Create the logs directory retval, reterr = True, '' try: c_path.create_dir(log_dir) except Exception as e: retval, reterr = False, str(e) if not retval: self.warning( 'CANNOT CREATE DIRECTORY FOR LOGGING FILE: {0}\nPath: {1}'. format(reterr, log_dir)) elif num_logs > 1 and c_path.validate_file( log_file) and not backup_file(log_file, num_logs): self.warning( 'FAILED TO BACKUP LOGGING FILE: \nPath: {0}'.format( log_file)) elif num_logs > 1 and c_path.validate_file( log_file_debug) and not backup_file( log_file_debug, num_logs): self.warning( 'FAILED TO BACKUP DEBUG LOGGING FILE: \nPath: {0}'.format( log_file_debug)) else: flids.append(self.add_file_logger(log_file, self.INFO, 'w')) self.info('Logging to {0}'.format(log_file)) # Only create debug file if debug is enabled if self.verbosity < self.INFO: flids.append( self.add_file_logger(log_file_debug, self.verbosity, 'w')) self.debug('Debug logging to {0}'.format(log_file_debug)) return flids
def enable_file_logging(self, toolname, num_logs=50, log_dir=None): """ Find a tmp location for file logging """ from c_misc import backup_file import c_path if log_dir is None: log_dir = '' for tmp_path in [os.getenv('TEMP'), os.getenv('TMP')]: if c_path.validate_dir(tmp_path): log_dir = c_path.join(tmp_path, '{0}_logs'.format(toolname)) break else: self.warning('CANNOT FIND A LOCAL TEMP DIRECTORY TO CREATE LOGS') # Set Log Folder if log_dir: log_file = c_path.join(log_dir, '{0}_log.txt'.format(toolname)) log_file_debug = c_path.join(log_dir, '{0}_log_debug.txt'.format(toolname)) # Create the logs directory retval, reterr = True, '' try: c_path.create_dir(log_dir); except Exception as e: retval, reterr = False, str(e) if not retval: self.warning('CANNOT CREATE DIRECTORY FOR LOGGING FILE: {0}\n Path: {1}'.format(reterr, log_dir)) elif num_logs > 1 and c_path.validate_file(log_file) and not backup_file(log_file, num_logs): self.warning('FAILED TO BACKUP LOGGING FILE: \n Path: {0}'.format(log_file)) elif num_logs > 1 and c_path.validate_file(log_file_debug) and not backup_file(log_file_debug, num_logs): self.warning('FAILED TO BACKUP DEBUG LOGGING FILE: \n Path: {0}'.format(log_file_debug)) else: self.add_file_logger(log_file, self.INFO, 'w') self.info('Logging to {0}'.format(log_file)) # Only create debug file if debug is enabled if self.verbosity < self.INFO: self.add_file_logger(log_file_debug, self.verbosity, 'w') self.debug('Debug logging to {0}'.format(log_file_debug)) return log_file, log_file_debug return '', ''
def zipDir(directory, zipfilename): """create zip package from files in directory""" if c_path.validate_dir_write(directory): zip_handler = zipfile.ZipFile(zipfilename, 'w') try: for root, dirs, files in os.walk(directory): for entry in files: if not entry.endswith('.zip'): logger.debug('add to zip: ' + entry) zip_handler.write(c_path.join(root, entry), entry) finally: zip_handler.close() else: raise RuntimeError('cannot write to directory: ' + str(directory))
def getMD5Directory(directory): """ Return md5 checksum for a directory. """ hashValue = '' try: hashtotal = hashlib.md5() for eachFolder, eachSubFolder, eachFiles in os.walk(directory): for eachFile in eachFiles: fd = open(c_path.join(eachFolder, eachFile), 'r') contents = fd.read() fd.close() hashtotal.update(contents) hashValue = hashtotal.hexdigest() except Exception: raise CoreError(CoreErrorCode.GENERIC_FAILURE, 'Getting hash: {0}'.format(sys.exc_info()[1])) return hashValue
def store_debug_data_to_file(file_name, data=None, debug_dir=None): if debug_dir is None or data is None: return store_data_to_file(c_path.join(debug_dir, file_name), data)
Given a file base name, returns the list of possible binary name on the current system. For eg, on windows, this will add the '.exe' .. data:: packaged_bin_folder Name of the folder under the bin directory to be used on the current system. """ import os import sys import c_path DIR_PATH = os.path.dirname(os.path.abspath(__file__)) BIN_PATH = c_path.join(DIR_PATH, '..', '..', '..', 'bin') if sys.platform.startswith('linux'): bin_names = lambda x: [x] packaged_bin_folder = c_path.join(BIN_PATH, 'LIN') elif sys.platform == 'win32': bin_names = lambda x: [x + '.exe'] packaged_bin_folder = c_path.join(BIN_PATH, 'WIN') elif sys.platform == 'cygwin': bin_names = lambda x: [x, x + '.exe'] packaged_bin_folder = c_path.join(BIN_PATH, 'WIN') elif sys.platform == 'darwin': bin_names = lambda x: [x] packaged_bin_folder = c_path.join(BIN_PATH, 'MAC', '10.8.5') import os os.environ['DYLD_LIBRARY_PATH'] = packaged_bin_folder