├── README └── deploy.py /README: -------------------------------------------------------------------------------- 1 | Simple script that copies files from one directory to another. 2 | 3 | Currently used to move new code from a staging environment to a production environment. 4 | 5 | Might be enhanced in the future to move files from a local dev box to a remote box. Or check out from git to deploy, etc. 6 | -------------------------------------------------------------------------------- /deploy.py: -------------------------------------------------------------------------------- 1 | import sys, os, shutil 2 | 3 | # Config settings 4 | restart_apache_cmd = 'apache2/bin/restart' 5 | staging_root = 'staging/htdocs/' 6 | production_root = 'production/htdocs/' 7 | files_to_ignore = [] 8 | extensions_to_ignore = ['.pyc'] 9 | 10 | 11 | def sizeof_fmt(num): 12 | for x in ['bytes','KB','MB','GB','TB']: 13 | if num < 1024.0: 14 | return "%3.3f%s" % (num, x) 15 | num /= 1024.0 16 | 17 | files_to_copy = {} 18 | 19 | for root, dir, files in os.walk(staging_root): 20 | for file in files: 21 | if file not in files_to_ignore and os.path.splitext(file)[1] not in extensions_to_ignore: 22 | staging_filename = os.path.join(root, file) 23 | production_filename = staging_filename.replace(staging_root, production_root) 24 | 25 | if not os.path.exists(production_filename) or os.path.getsize(staging_filename) != os.path.getsize(production_filename): 26 | if os.path.exists(production_filename): 27 | print file + ' is ' + sizeof_fmt(os.path.getsize(staging_filename)) + ' on staging, but ' + sizeof_fmt(os.path.getsize(production_filename)) + ' on production' 28 | #print os.system('diff ' + staging_filename + ' ' + production_filename) 29 | else: 30 | print file + ' not found on production' 31 | 32 | files_to_copy[staging_filename] = production_filename 33 | 34 | print '' 35 | print 'Total number of files to update: ' + str(len(files_to_copy)) 36 | print 'd to diff, Y to copy, Enter to skip, x to exit' 37 | 38 | for staging_filename in files_to_copy: 39 | print '' 40 | production_filename = files_to_copy[staging_filename] 41 | 42 | question = 'Copy ' + os.path.basename(staging_filename) + ' to ' + production_filename + '? ' 43 | if os.path.exists(production_filename): 44 | question = 'Overwrite ' + production_filename + '? ' 45 | 46 | input = raw_input(question) 47 | 48 | if input.lower() == 'x': 49 | sys.exit() 50 | 51 | if input.lower() == 'd': 52 | if os.path.exists(production_filename): 53 | print os.system('diff ' + staging_filename + ' ' + production_filename) 54 | input = raw_input(question) 55 | else: 56 | print 'No file on production to diff against' 57 | 58 | if input == 'Y': 59 | print 'Copying ' + staging_filename + ' to ' + production_filename 60 | shutil.copy2(staging_filename, production_filename) 61 | 62 | input = raw_input('Restart Apache? ') 63 | 64 | if input == 'Y': 65 | os.system(restart_apache_cmd) 66 | --------------------------------------------------------------------------------