Created
March 28, 2013 14:53
-
-
Save ernado/5263761 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import datetime | |
import shutil | |
template_name = 'LABN_216' | |
template_path = os.path.join('template', template_name) | |
def path(filename): | |
return os.path.join(template_path, filename) | |
class CreationError(Exception): | |
pass | |
def create(project_name): | |
if os.path.exists(project_name): | |
if raw_input('Folder %s already exists. Delete folder? [Y/n]: ' % project_name) in ['Y', 'y', '']: | |
shutil.rmtree(project_name) | |
print 'Folder deleted' | |
else: | |
print 'Creation cancelled' | |
exit(0) | |
os.mkdir(project_name) | |
print 'Folder %s created' % project_name | |
replace_values = { | |
'$date$': datetime.datetime.now().strftime('%d.%m.%Y %H:%M'), | |
template_name: project_name | |
} | |
replacements = 0 | |
files_processed = 0 | |
for filename in os.listdir(template_path): | |
new_filename = filename.replace(template_name, project_name) | |
source_file = open(path(filename)) | |
print 'Processing %s -> %s' % (filename, new_filename) | |
new_file = open(os.path.join(project_name, new_filename), 'w') | |
for l in source_file.readlines(): | |
new_line = l | |
for replacement in replace_values.iterkeys(): | |
new_line = new_line.replace(replacement, replace_values[replacement]) | |
if new_line != l: | |
replacements += 1 | |
new_file.write(new_line) | |
source_file.close() | |
new_file.close() | |
files_processed += 1 | |
print 'Project %s created' % project_name | |
print 'Replacements: %d' % replacements | |
print 'Files processed: %d' % files_processed | |
print 'Creating date: %s' % replace_values['$date$'] | |
if __name__ == '__main__': | |
if len(sys.argv) not in [2, 3]: | |
print 'usage: python labstrap.py {number}' | |
exit(0) | |
if len(sys.argv) == 3: | |
project_name = sys.argv[-1] | |
print 'Creating project %s' % project_name | |
create(project_name) | |
exit(0) | |
lab_variant = 16 | |
lab_group = 2 | |
lab_number = sys.argv[1] | |
lab_name = 'LAB%s_%s%s' % (lab_number, lab_group, lab_variant) | |
print 'Creating lab %s' % lab_name | |
create(lab_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment