def _save_with_backup(file_path, text): try: os.rename(file_path, '{}.bak'.format(file_path)) except (OSError, IOError) as o: IOOSErrorGracefulFail( o, "Could not rename file {0} to {0}.bak".format(file_path)) save_text_to_file(text, file_path)
def _get_file_text(file_path): try: with open(file_path, 'r') as txtfile: return txtfile.read() except (OSError, IOError) as o: IOOSErrorGracefulFail(o, "Cannot read or decode {} \n".format(file_path))
recursive_copy_dir, IOOSErrorGracefulFail, save_files_copied) from stagelib.replace import do_variable_replace # Set default values for tunables (primarily only interesting for testing) CORE_FILES_DIR = "src" CEPH_RELEASES_DIR = "ceph-releases" STAGING_DIR = getEnvVar('STAGING_DIR') # Start with empty staging dir so there are no previous artifacts try: if os.path.isdir(STAGING_DIR): shutil.rmtree(STAGING_DIR) os.makedirs(STAGING_DIR, mode=0o755) except (OSError, IOError) as o: IOOSErrorGracefulFail( o, 'Could not delete and recreate staging dir: {}'.format(STAGING_DIR)) LOG_FILE = os.path.join(STAGING_DIR, "stage.log") loglevel = logging.INFO # If DEBUG env var is set to anything (including empty string) except '0', log debug text if 'DEBUG' in os.environ and not os.environ['DEBUG'] == '0': loglevel = logging.DEBUG logging.basicConfig(filename=LOG_FILE, level=loglevel, format='%(levelname)5s: %(message)s') # Build dependency on python3 for `replace.py`. Looking to py2.7 deprecation in 2020. if sys.version_info[0] < 3: print('This must be run with Python 3+') sys.exit(1)