Skip to content
Snippets Groups Projects
Commit 5450ea62 authored by Karl-Hermann Wieners's avatar Karl-Hermann Wieners
Browse files

Changed backup handling for easier comparison of versions

* Changed to use directories for backup, leaving file names unchanged
* Added README file to backup
parent 34b1e6f0
No related branches found
No related tags found
No related merge requests found
...@@ -71,9 +71,7 @@ def expand_template(template_dict, template_name): ...@@ -71,9 +71,7 @@ def expand_template(template_dict, template_name):
def expand_template_file(template_dict, template_names, expanded_name, backup_name): def expand_template_file(template_dict, template_names, expanded_name, backup_name):
'''Replace keywords in template file using the given dictionary.''' '''Replace keywords in template file using the given dictionary.'''
if os.path.exists(expanded_name): move_file_to_backup(expanded_name, backup_name)
feedback.info("renaming '%s' to '%s'", expanded_name, backup_name)
os.rename(expanded_name, backup_name)
expanded_file = open(expanded_name, 'w') expanded_file = open(expanded_name, 'w')
try: try:
for line in template_env.select_template(template_names).generate(template_dict): for line in template_env.select_template(template_names).generate(template_dict):
...@@ -84,7 +82,13 @@ def expand_template_file(template_dict, template_names, expanded_name, backup_na ...@@ -84,7 +82,13 @@ def expand_template_file(template_dict, template_names, expanded_name, backup_na
expanded_file.close() expanded_file.close()
chmod_plus_x(expanded_name) chmod_plus_x(expanded_name)
def move_file_to_backup(file_name, backup_name):
'''If given file name exists, move it to the backup location'''
if os.path.exists(file_name):
feedback.info("renaming '%s' to '%s'", file_name, backup_name)
os.rename(file_name, backup_name)
# Namelist formatting # Namelist formatting
...@@ -244,14 +248,14 @@ except ExpConfigError as error: ...@@ -244,14 +248,14 @@ except ExpConfigError as error:
# Create directory for scripts if it doesn't exist # Create directory for scripts if it doesn't exist
script_dir = config['SCRIPT_DIR'] script_dir = config['SCRIPT_DIR']
print "Script directory: '"+script_dir+"'" print "Script directory: '"+script_dir+"'"
backup_dir = os.path.join(script_dir, 'backup') time_stamp = strftime("%Y%m%d%H%M%S")
time_stamp = strftime("%Y-%m-%dT%H:%M:%S") backup_dir = os.path.join(script_dir, 'backup', time_stamp)
if not os.path.isdir(script_dir): if not os.path.isdir(script_dir):
os.makedirs(script_dir) os.makedirs(script_dir)
else: else:
feedback.warning("script directory already exists, moving existing scripts to backup") feedback.warning("script directory already exists, moving existing scripts to backup")
if not os.path.isdir(backup_dir): if not os.path.isdir(backup_dir):
os.mkdir(backup_dir) os.makedirs(backup_dir)
# Create directory for output data if it doesn't exist # Create directory for output data if it doesn't exist
data_dir = config['DATA_DIR'] data_dir = config['DATA_DIR']
...@@ -344,11 +348,12 @@ for subjob, subconfig in config['jobs'].iteritems(): ...@@ -344,11 +348,12 @@ for subjob, subconfig in config['jobs'].iteritems():
template_job) ), template_job) ),
get_exp_script_name(config.experiment_id, subjob), get_exp_script_name(config.experiment_id, subjob),
os.path.join(backup_dir, os.path.join(backup_dir,
config.experiment_id+'.'+subjob+ config.experiment_id+'.'+subjob))
'~'+time_stamp))
# Create README file from experiment description # Create README file from experiment description
move_file_to_backup(os.path.join(script_dir, 'README'),
os.path.join(backup_dir, 'README'))
readme_file = open(os.path.join(script_dir, 'README'), 'w') readme_file = open(os.path.join(script_dir, 'README'), 'w')
readme_file.write(config['EXP_DESCRIPTION'] + '\n') readme_file.write(config['EXP_DESCRIPTION'] + '\n')
readme_file.close() readme_file.close()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment