def rmdir(dir_path): """ Remove an entire (empty or non empty) folder. """ logger.info('About to remove folder %s...' % dir_path) try: shutil.rmtree(dir_path) logger.info('Folder succesfully removed.') status = 0 except Exception as e: logger.error('Could not remove folder (%s)' % e) status = 1 return status
def mv(source, dest): """Move a file. Return 0 upon succesfull operation, 1 otherwise. """ logger.info('About to move %s to %s...' % (source, dest)) try: shutil.move(source, dest) logger.info('File succesfully copied.') status = 0 except Exception as e: logger.error('Could not move file (%s)' % e) status = 1 return status
def mkdir(dir_path): """Create a directory (unless it already exists). Return 0 upon succesfull operation, 1 otherwise. """ if not os.path.exists(dir_path): logger.info('About to create folder %s...' % dir_path) try: os.makedirs(dir_path) logger.info('Folder succesfully created.') status = 0 except Exception as e: logger.error('Could not create folder (%s)' % e) status = 1 return status
def rm(file_path): """ Remove a file. Return 0 upon succesfull operation, 1 otherwise. """ logger.info('About to remove file %s...' % file_path) if not os.path.exists(file_path): logger.info('File is not there, giving up...') return 0 try: os.remove(file_path) logger.info('File succesfully removed.') status = 0 except Exception as e: logger.error('Could not remove file (%s)' % e) status = 1 return status
def cmd(cmd, verbose=False, log_file_path=None, log_file_mode='w', dry_run=False): """ Exec a system command. This uses subprocess internally and returns the subprocess status code (if the dry_run option is true the function will just print the command out through the logger and returns happily). By default the stdout and the stderr are redirected into subprocess pipes so that the output can be effectively used by the logger. It the log_file_path parameter is different from None the stdout is redirected to file instead. The rules are: * if verbose is True the command output is logged onto the terminal one \ line at a time; * if the status code is zero we just aknowledge that before returning it; * upon error we try and log out both the error code and the error message. """ logger.info('About to execute "%s"...' % cmd) if dry_run: logger.info('Just kidding (dry run).') return 0 err = subprocess.PIPE if log_file_path is not None: out = open(log_file_path, log_file_mode) else: out = subprocess.PIPE process = subprocess.Popen(cmd, stdout = out, stderr = err, shell = True) errorCode = process.wait() if verbose: if log_file_path is None: output = process.stdout.read().strip('\n') else: output = open(log_file_path).read().strip('\n') for line in output.split('\n'): logger.info(line) if not errorCode: logger.info('Command executed with status code %d.' % errorCode) else: logger.error('Command returned status code %d.' % errorCode) logger.error('Full error message following...\n%s' %\ process.stderr.read().strip('\n')) return errorCode
def cp(source, dest, create_tree = False): """Copy a file. Return 0 upon succesfull operation, 1 otherwise. """ logger.info('About to copy %s to %s...' % (source, dest)) destFolder = os.path.dirname(dest) if not os.path.exists(destFolder) and createTree: mkdir(destFolder) try: if os.path.isdir(source): shutil.copytree(source, dest) else: shutil.copy(source, dest) logger.info('File succesfully copied.') status = 0 except Exception as e: logger.error('Could not copy file (%s)' % e) status = 1 return status
def cp(source, dest, create_tree=False): """Copy a file. Return 0 upon succesfull operation, 1 otherwise. """ logger.info('About to copy %s to %s...' % (source, dest)) destFolder = os.path.dirname(dest) if not os.path.exists(destFolder) and createTree: mkdir(destFolder) try: if os.path.isdir(source): shutil.copytree(source, dest) else: shutil.copy(source, dest) logger.info('File succesfully copied.') status = 0 except Exception as e: logger.error('Could not copy file (%s)' % e) status = 1 return status