| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
|
mathias
2018/06/07 21:52:00
According to our legal department all source files
f.lopez
2018/06/18 18:41:30
Acknowledged.
| |
| 2 | |
| 3 import argparse | |
| 4 from filecmp import dircmp | |
| 5 import hashlib | |
| 6 import os | |
| 7 import sys | |
| 8 import shutil | |
| 9 import tarfile | |
| 10 import tempfile | |
| 11 import urllib | |
| 12 | |
| 13 | |
| 14 def download(url, tmp_dir): | |
| 15 file_name = url.split('/')[-1] | |
| 16 abs_file_name = os.path.join(tmp_dir, file_name) | |
| 17 print 'Downloading: ' + file_name | |
| 18 urllib.urlretrieve(url, abs_file_name) | |
| 19 return abs_file_name | |
| 20 | |
| 21 | |
| 22 def calculate_md5(file): | |
| 23 with open(file) as f: | |
| 24 data = f.read() | |
| 25 md5_result = hashlib.md5(data).hexdigest() | |
| 26 return md5_result.strip() | |
| 27 | |
| 28 | |
| 29 def read_md5(file): | |
| 30 with open(file) as f: | |
| 31 md5_result = f.readline() | |
| 32 return md5_result.strip() | |
| 33 | |
| 34 | |
| 35 def untar(tar_file, tmp_dir): | |
| 36 if tarfile.is_tarfile(tar_file): | |
| 37 with tarfile.open(tar_file, 'r:gz') as tar: | |
| 38 tar.extractall(tmp_dir) | |
| 39 | |
| 40 | |
| 41 def remove_tree(to_remove): | |
| 42 if os.path.exists(to_remove): | |
| 43 if os.path.isdir(to_remove): | |
| 44 shutil.rmtree(to_remove) | |
| 45 else: | |
| 46 os.remove(to_remove) | |
| 47 | |
| 48 | |
| 49 def deploy_files(dcmp): | |
| 50 for name in dcmp.diff_files: | |
| 51 copytree(dcmp.right, dcmp.left) | |
| 52 for name in dcmp.left_only: | |
| 53 remove_tree(os.path.join(dcmp.left, name)) | |
| 54 for name in dcmp.right_only: | |
| 55 copytree(dcmp.right, dcmp.left) | |
| 56 for sub_dcmp in dcmp.subdirs.values(): | |
| 57 deploy_files(sub_dcmp) | |
| 58 | |
| 59 | |
| 60 def copytree(src, dst): | |
|
mathias
2018/06/07 21:52:00
Why don't you use shutil.copytree? And why isn't t
f.lopez
2018/06/18 18:41:29
Acknowledged.
| |
| 61 if not os.path.exists(dst): | |
| 62 os.makedirs(dst) | |
| 63 shutil.copystat(src, dst) | |
| 64 lst = os.listdir(src) | |
| 65 for item in lst: | |
| 66 s = os.path.join(src, item) | |
|
mathias
2018/06/07 21:52:00
So `s` is an abbreviation for `src` which is of co
f.lopez
2018/06/18 18:41:29
Acknowledged.
| |
| 67 d = os.path.join(dst, item) | |
|
mathias
2018/06/07 21:52:00
And it continues. At least you are consistent.
f.lopez
2018/06/18 18:41:29
Acknowledged.
| |
| 68 if os.path.isdir(s): | |
| 69 copytree(s, d) | |
| 70 else: | |
| 71 shutil.copy2(s, d) | |
| 72 | |
| 73 | |
| 74 if __name__ == '__main__': | |
| 75 parser = argparse.ArgumentParser( | |
| 76 description="""Fetch a compressed archive in the form of $HASH.tar.gz | |
| 77 and deploy it to /var/www/$WEBSITE folder""", | |
| 78 epilog="""--hash must be provided in order to fetch the files, | |
| 79 expected files to be fetched are $HASH.tar.gz and $HASH.md5 in | |
| 80 order to compare the hashes. | |
| 81 --source must be an URL, e.g. | |
| 82 https://helpcenter.eyeofiles.com""", | |
| 83 ) | |
| 84 parser.add_argument('--hash', action='store', type=str, required=True, | |
|
mathias
2018/06/07 21:52:00
Is this sure to always be a hash? Wouldn't `--revi
f.lopez
2018/06/18 18:41:30
Acknowledged.
| |
| 85 help='Hash of the commit to deploy') | |
| 86 parser.add_argument('--source', action='store', type=str, required=True, | |
| 87 help='The source where files will be downloaded') | |
| 88 parser.add_argument('--website', action='store', type=str, | |
|
mathias
2018/06/07 21:52:00
Why does option even exist? IT should not be possi
f.lopez
2018/06/18 18:41:30
Acknowledged.
| |
| 89 help='The name of the website [e.g. help.eyeo.com]') | |
| 90 args = parser.parse_args() | |
| 91 hash = args.hash | |
| 92 source = args.source | |
| 93 url_file = '{0}/{1}.tar.gz'.format(source, hash) | |
| 94 url_md5 = '{0}/{1}.md5'.format(source, hash) | |
| 95 tmp_dir = tempfile.mkdtemp() | |
| 96 try: | |
| 97 down_file = download(url_file, tmp_dir) | |
| 98 down_md5 = download(url_md5, tmp_dir) | |
| 99 if calculate_md5(down_file) == read_md5(down_md5): | |
| 100 untar(down_file, tmp_dir) | |
| 101 hash_directory = os.path.join(tmp_dir, hash) | |
| 102 destination = os.path.join('/var/www/', args.website) | |
| 103 dcmp = dircmp(destination, hash_directory) | |
| 104 print 'Deploying files' | |
| 105 deploy_files(dcmp) | |
| 106 else: | |
| 107 sys.exit("Hashes don't match") | |
| 108 except Exception as e: | |
| 109 sys.exit(e) | |
| 110 finally: | |
| 111 shutil.rmtree(tmp_dir) | |
| OLD | NEW |