Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # This file is part of the Adblock Plus infrastructure | 3 # This file is part of the Adblock Plus infrastructure |
4 # Copyright (C) 2018-present eyeo GmbH | 4 # Copyright (C) 2018-present eyeo GmbH |
5 # | 5 # |
6 # Adblock Plus is free software: you can redistribute it and/or modify | 6 # Adblock Plus is free software: you can redistribute it and/or modify |
7 # it under the terms of the GNU General Public License version 3 as | 7 # it under the terms of the GNU General Public License version 3 as |
8 # published by the Free Software Foundation. | 8 # published by the Free Software Foundation. |
9 # | 9 # |
10 # Adblock Plus is distributed in the hope that it will be useful, | 10 # Adblock Plus is distributed in the hope that it will be useful, |
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 # GNU General Public License for more details. | 13 # GNU General Public License for more details. |
14 # | 14 # |
15 # You should have received a copy of the GNU General Public License | 15 # You should have received a copy of the GNU General Public License |
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 |
18 import argparse | 18 import argparse |
19 from filecmp import dircmp | 19 from filecmp import dircmp |
20 import hashlib | 20 import hashlib |
21 import os | 21 import os |
22 import sys | 22 import sys |
23 import shutil | 23 import shutil |
24 import tarfile | 24 import tarfile |
25 import tempfile | 25 import tempfile |
26 import urllib | 26 import urllib |
27 | 27 |
28 | 28 |
29 _doc = """This script MUST be renamed in the form of deploy_$WEBSITE, e.g. | 29 __doc__ = """This script MUST be renamed in the form of $WEBSITE, e.g. |
mathias
2018/06/18 19:58:38
Why don't you use __doc__?
f.lopez
2018/06/18 22:47:43
Acknowledged.
| |
30 deploy_help.eyeo.com, --name must be provided in order to fetch the | 30 help.eyeo.com, --name must be provided in order to fetch the |
31 files, expected files to be fetched are $NAME.tar.gz and $NAME.md5 in | 31 files, expected files to be fetched are $NAME.tar.gz and $NAME.md5 in |
32 order to compare the hashes. --source must be an URL, e.g. | 32 order to compare the hashes. --source must be an URL, e.g. |
33 https://helpcenter.eyeofiles.com""" | 33 https://helpcenter.eyeofiles.com""" |
34 | 34 |
35 | 35 |
36 def download(url, temporary_directory): | 36 def download(url, temporary_directory): |
37 file_name = url.split('/')[-1] | 37 file_name = url.split('/')[-1] |
38 absolute_file_path = os.path.join(temporary_directory, file_name) | 38 absolute_file_path = os.path.join(temporary_directory, file_name) |
39 print 'Downloading: ' + file_name | 39 print 'Downloading: ' + file_name |
40 urllib.urlretrieve(url, absolute_file_path) | 40 urllib.urlretrieve(url, absolute_file_path) |
41 return absolute_file_path | 41 return absolute_file_path |
42 | 42 |
43 | 43 |
44 def calculate_md5(file): | 44 def calculate_md5(file): |
45 with open(file) as f: | 45 with open(file) as file_handle: |
mathias
2018/06/18 19:58:39
You can `open(file) as handle` or something simila
f.lopez
2018/06/18 22:47:42
Acknowledged.
| |
46 data = f.read() | 46 data = file_handle.read() |
47 md5_result = hashlib.md5(data).hexdigest() | 47 md5_result = hashlib.md5(data).hexdigest() |
48 return md5_result.strip() | 48 return md5_result.strip() |
49 | 49 |
50 | 50 |
51 def read_md5(file): | 51 def read_md5(file): |
52 with open(file) as f: | 52 with open(file) as file_handle: |
53 md5_result = f.readline() | 53 md5_result = file_handle.readline() |
54 return md5_result.strip() | 54 return md5_result.strip() |
55 | 55 |
56 | 56 |
57 def untar(tar_file, temporary_directory): | 57 def untar(tar_file, temporary_directory): |
58 if tarfile.is_tarfile(tar_file): | 58 if tarfile.is_tarfile(tar_file): |
59 with tarfile.open(tar_file, 'r:gz') as tar: | 59 with tarfile.open(tar_file, 'r:gz') as tar: |
60 tar.extractall(temporary_directory) | 60 tar.extractall(temporary_directory) |
61 | 61 |
62 | 62 |
63 def remove_tree(to_remove): | 63 def remove_tree(to_remove): |
(...skipping 26 matching lines...) Expand all Loading... | |
90 for item in source_items: | 90 for item in source_items: |
91 source_path = os.path.join(source, item) | 91 source_path = os.path.join(source, item) |
92 destination_path = os.path.join(destination, item) | 92 destination_path = os.path.join(destination, item) |
93 if os.path.isdir(source_path): | 93 if os.path.isdir(source_path): |
94 copytree(source_path, destination_path) | 94 copytree(source_path, destination_path) |
95 else: | 95 else: |
96 shutil.copy2(source_path, destination_path) | 96 shutil.copy2(source_path, destination_path) |
97 | 97 |
98 | 98 |
99 if __name__ == '__main__': | 99 if __name__ == '__main__': |
100 website = os.path.basename(__file__)[len("deploy_"):] | 100 website = os.path.basename(__file__) |
101 parser = argparse.ArgumentParser( | 101 parser = argparse.ArgumentParser( |
102 description="""Fetch a compressed archive in the form of $NAME.tar.gz | 102 description="""Fetch a compressed archive in the form of $NAME.tar.gz |
103 and deploy it to /var/www/{0} folder""".format(website), | 103 and deploy it to /var/www/{0} folder""".format(website), |
104 epilog=_doc, | 104 epilog=__doc__, |
105 ) | 105 ) |
106 parser.add_argument('--name', action='store', type=str, required=True, | 106 parser.add_argument('--name', action='store', type=str, required=True, |
107 help='Name of the tarball to deploy') | 107 help='Name of the tarball to deploy') |
108 parser.add_argument('--source', action='store', type=str, required=True, | 108 parser.add_argument('--source', action='store', type=str, required=True, |
109 help='The source where files will be downloaded') | 109 help='The source where files will be downloaded') |
110 arguments = parser.parse_args() | 110 arguments = parser.parse_args() |
111 name = arguments.name | 111 name = arguments.name |
112 source = arguments.source | 112 source = arguments.source |
113 url_file = '{0}/{1}.tar.gz'.format(source, name) | 113 url_file = '{0}/{1}.tar.gz'.format(source, name) |
114 url_md5 = '{0}/{1}.md5'.format(source, name) | 114 url_md5 = '{0}/{1}.md5'.format(source, name) |
115 temporary_directory = tempfile.mkdtemp() | 115 temporary_directory = tempfile.mkdtemp() |
116 try: | 116 try: |
117 downloaded_file = download(url_file, temporary_directory) | 117 downloaded_file = download(url_file, temporary_directory) |
118 downloaded_md5 = download(url_md5, temporary_directory) | 118 downloaded_md5 = download(url_md5, temporary_directory) |
119 if calculate_md5(downloaded_file) == read_md5(downloaded_md5): | 119 if calculate_md5(downloaded_file) == read_md5(downloaded_md5): |
120 untar(downloaded_file, temporary_directory) | 120 untar(downloaded_file, temporary_directory) |
121 name_directory = os.path.join(temporary_directory, name) | 121 tarball_directory = os.path.join(temporary_directory, name) |
mathias
2018/06/18 19:58:38
This name is a bit irritating (and was a bit irrit
f.lopez
2018/06/18 22:47:42
Acknowledged.
| |
122 destination = os.path.join('/var/www/', website) | 122 destination = os.path.join('/var/www/', website) |
123 directory_comparison = dircmp(destination, name_directory) | 123 directory_comparison = dircmp(destination, tarball_directory) |
124 print 'Deploying files' | 124 print 'Deploying files' |
125 deploy_files(directory_comparison) | 125 deploy_files(directory_comparison) |
126 else: | 126 else: |
127 sys.exit("Hashes don't match") | 127 error_message = """{0}.tar.gz md5 computation doesn't match {0}.md5 |
mathias
2018/06/18 19:58:39
Sorry for not noticing this earlier, but this erro
f.lopez
2018/06/18 22:47:43
Acknowledged.
| |
128 contents""".format(name) | |
129 sys.exit(error_message) | |
128 except Exception as error: | 130 except Exception as error: |
129 sys.exit(error) | 131 sys.exit(error) |
130 finally: | 132 finally: |
131 shutil.rmtree(temporary_directory) | 133 shutil.rmtree(temporary_directory) |
LEFT | RIGHT |