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: Use of a different name convention for the script Created July 4, 2018, 2:12 p.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 # This file is part of the Adblock Plus infrastructure
4 # Copyright (C) 2018-present eyeo GmbH
5 #
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
8 # published by the Free Software Foundation.
9 #
10 # Adblock Plus is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
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/>.
17
18 import argparse
19 from filecmp import dircmp
20 import hashlib
21 import os
22 import sys
23 import shutil
24 import tarfile
25 import tempfile
26 import urllib
27
28
29 __doc__ = """This script MUST be renamed in the form of $WEBSITE, e.g.
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
32 order to compare the hashes. --source must be an URL, e.g.
33 https://helpcenter.eyeofiles.com"""
34
35
36 def download(url, temporary_directory):
37 file_name = url.split('/')[-1]
38 absolute_file_path = os.path.join(temporary_directory, file_name)
39 print 'Downloading: ' + file_name
40 urllib.urlretrieve(url, absolute_file_path)
41 return absolute_file_path
42
43
44 def calculate_md5(file):
45 with open(file) as file_handle:
46 data = file_handle.read()
47 md5_result = hashlib.md5(data).hexdigest()
48 return md5_result.strip()
49
50
51 def read_md5(file):
52 with open(file) as file_handle:
53 md5_result = file_handle.readline()
54 return md5_result.strip()
55
56
57 def untar(tar_file, temporary_directory):
58 if tarfile.is_tarfile(tar_file):
59 with tarfile.open(tar_file, 'r:gz') as tar:
60 tar.extractall(temporary_directory)
61
62
63 def remove_tree(to_remove):
64 if os.path.exists(to_remove):
65 if os.path.isdir(to_remove):
66 shutil.rmtree(to_remove)
67 else:
68 os.remove(to_remove)
69
70
71 def deploy_files(directory_comparison):
72 for name in directory_comparison.diff_files:
73 copytree(directory_comparison.right, directory_comparison.left)
74 for name in directory_comparison.left_only:
75 remove_tree(os.path.join(directory_comparison.left, name))
76 for name in directory_comparison.right_only:
77 copytree(directory_comparison.right, directory_comparison.left)
78 for subdirectory_comparison in directory_comparison.subdirs.values():
79 deploy_files(subdirectory_comparison)
80
81
82 # shutil.copytree copies a tree but the destination directory MUST NOT exist
83 # this might break the site for the duration of the files being deployed
84 # for more info read: https://docs.python.org/2/library/shutil.html
85 def copytree(source, destination):
86 if not os.path.exists(destination):
87 os.makedirs(destination)
88 shutil.copystat(source, destination)
89 source_items = os.listdir(source)
90 for item in source_items:
91 source_path = os.path.join(source, item)
92 destination_path = os.path.join(destination, item)
93 if os.path.isdir(source_path):
94 copytree(source_path, destination_path)
95 else:
96 shutil.copy2(source_path, destination_path)
97
98
99 if __name__ == '__main__':
100 website = os.path.basename(__file__)
101 parser = argparse.ArgumentParser(
102 description="""Fetch a compressed archive in the form of $NAME.tar.gz
103 and deploy it to /var/www/{0} folder""".format(website),
104 epilog=__doc__,
105 )
106 parser.add_argument('--name', action='store', type=str, required=True,
107 help='Name of the tarball to deploy')
108 parser.add_argument('--source', action='store', type=str, required=True,
109 help='The source where files will be downloaded')
110 arguments = parser.parse_args()
111 name = arguments.name
112 source = arguments.source
113 url_file = '{0}/{1}.tar.gz'.format(source, name)
114 url_md5 = '{0}/{1}.md5'.format(source, name)
115 temporary_directory = tempfile.mkdtemp()
116 try:
117 downloaded_file = download(url_file, temporary_directory)
118 downloaded_md5 = download(url_md5, temporary_directory)
119 if calculate_md5(downloaded_file) == read_md5(downloaded_md5):
120 untar(downloaded_file, temporary_directory)
121 tarball_directory = os.path.join(temporary_directory, name)
122 destination = os.path.join('/var/www/', website)
123 directory_comparison = dircmp(destination, tarball_directory)
124 print 'Deploying files'
125 deploy_files(directory_comparison)
126 else:
127 error_message = """{0}.tar.gz md5 computation doesn't match {0}.md5
128 contents""".format(name)
129 sys.exit(error_message)
130 except Exception as error:
131 sys.exit(error)
132 finally:
133 shutil.rmtree(temporary_directory)
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