Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: modules/adblockplus/files/web/static/deploy_script.py

Issue 29777652: #6145 - Introduce deploy script for websites (Closed)
Patch Set: For comment 7 Created June 6, 2018, 12:32 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/env python
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):
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)
67 d = os.path.join(dst, item)
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,
85 required=True,
86 help='Hash of the commit to deploy')
87 parser.add_argument('--source', action='store', type=str,
88 required=True,
89 help='The source where files will be downloaded')
90 parser.add_argument('--website', action='store', type=str,
91 help='The name of the website [e.g. help.eyeo.com]')
92 args = parser.parse_args()
93 hash = args.hash
94 source = args.source
95 url_file = '{0}/{1}.tar.gz'.format(source, hash)
96 url_md5 = '{0}/{1}.md5'.format(source, hash)
97 tmp_dir = tempfile.mkdtemp()
98 try:
99 down_file = download(url_file, tmp_dir)
100 down_md5 = download(url_md5, tmp_dir)
101 if calculate_md5(down_file) == read_md5(down_md5):
102 untar(down_file, tmp_dir)
103 hash_directory = os.path.join(tmp_dir, hash)
104 destination = os.path.join('/var/www/', args.website)
105 dcmp = dircmp(destination, hash_directory)
106 print 'Deploying files'
107 deploy_files(dcmp)
108 else:
109 sys.exit("Hashes don't match")
110 except Exception as e:
111 sys.exit(e)
112 finally:
113 shutil.rmtree(tmp_dir)
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld