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

Side by Side Diff: sitescripts/extensions/pad/__init__.py

Issue 5723465818570752: Issue 520 - Generate PAD files for download portals when updating download links (Closed)
Patch Set: Addressed comments Created May 26, 2014, 12:04 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
OLDNEW
(Empty)
1 # This file is part of the Adblock Plus web scripts,
2 # Copyright (C) 2006-2014 Eyeo GmbH
3 #
4 # Adblock Plus is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation.
7 #
8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
15
16 import subprocess
17 import os
18 import re
19 import time
20 from datetime import datetime
21 from urlparse import urljoin
22
23 from sitescripts.utils import get_template
24 from sitescripts.extensions.android import get_min_android_version
25 from sitescripts.extensions.pad.language import iso2pad
26 from sitescripts.extensions.pad.validation import validate_pad
27
28 OS_WINDOWS = ['Win7 x32', 'Win7 x64', 'WinVista', 'WinVista x64', 'WinXP']
29 OS_LINUX = ['Linux']
30 OS_MAC = ['Mac OS X']
31 OS_ANDROID = ['Android']
32
33 class PadFile:
34 def __init__(self, repo, version, download_url):
35 self.repo = repo
36 self.version = version
37 self.download_url = re.sub(r'(?<=^http)s(?=:)', '', download_url)
38
39 for i, (filename, version) in enumerate(repo.getDownloads()):
40 self.first_release = (i == 0)
41
42 if version == self.version:
43 self.download_filename = filename
44
45 @property
46 def release_status(self):
47 if self.first_release:
48 return 'New Release'
49
50 if self.version.rstrip('.0').count('.') < 2:
51 return 'Major Update'
52
53 return 'Minor Update'
54
55 @property
56 def release_date(self):
57 command = ['hg', 'log', '-l', '1', self.download_filename, '--template', '{d ate}']
58 result = subprocess.check_output(command, cwd=self.repo.downloadsRepo)
59 timestamp, offset = re.match(r'(.*)([-+].*)', result).groups()
60 return datetime(*time.gmtime(float(timestamp) + float(offset))[:6])
61
62 @property
63 def download_size(self):
64 return len(subprocess.check_output(
65 ['hg', 'cat', self.download_filename],
66 cwd=self.repo.downloadsRepo
67 ))
68
69 @property
70 def browser_min_version(self):
71 metadata = self.repo.readMetadata(self.version)
72 compat_option = getattr(self, 'compat_option', self.repo.type)
73 return metadata.get('compat', compat_option)).split('/')[0].rstrip('.0')
74
75 @property
76 def languages(self):
77 files = self.repo.listContents(self.version)
78 languages = set()
79 skipped = set()
80
81 for filename in files:
82 match = re.match(self.translation_files_regex, filename)
83
84 if match:
85 groups = match.groupdict()
86
87 # support translation files that vary from the naming
88 # scheme like the default translation for Android
89 for k, v in groups.iteritems():
90 if k.startswith('is_') and v is not None:
91 code = k[3:]
92 break
93 else:
94 code = groups['code']
95
96 # support .incomplete files like on Firefox
97 if groups.get('skip') is not None:
98 skipped.add(code)
99 continue
100
101 languages.add(code)
102
103 return iso2pad(languages.difference(skipped))
104
105 def write(self):
106 template = get_template(self.repo.padTemplate)
107 filename = self.repo.basename + '.xml'
108
109 pad = template.render({
110 'type': self.repo.type,
111 'browser_name': self.browser_name,
112 'browser_min_version': self.browser_min_version,
113 'version': self.version,
114 'release_date': self.release_date,
115 'release_status': self.release_status,
116 'os_support': ','.join(self.os_support),
117 'language': ','.join(self.languages),
118 'download_size': self.download_size,
119 'download_url': self.download_url,
120 'pad_url': urljoin(self.repo.padURL, filename),
121 }).encode('utf-8')
122
123 path = os.path.join(self.repo.padDirectory, filename)
124 validate_pad(pad, path)
125
126 with open(path, 'wb') as file:
127 file.write(pad)
128
129 @staticmethod
130 def forRepository(repo, *args, **kwargs):
131 if repo.type == 'gecko':
132 return FirefoxPadFile(repo, *args, **kwargs)
133 if repo.type == 'chrome':
134 return ChromePadFile(repo, *args, **kwargs)
135 if repo.type == 'opera':
136 return OperaPadFile(repo, *args, **kwargs)
137 if repo.type == 'safari':
138 return SafariPadFile(repo, *args, **kwargs)
139 if repo.type == 'ie':
140 return InternetExplorerPadFile(repo, *args, **kwargs)
141 if repo.type == 'android':
142 return AndroidPadFile(repo, *args, **kwargs)
143
144 raise Exception('unknown repository type %r' % repo.type)
145
146 class FirefoxPadFile(PadFile):
147 browser_name = 'Mozilla Firefox'
148 os_support = OS_WINDOWS + OS_MAC + OS_LINUX + OS_ANDROID
149 translation_files_regex = r'chrome\/locale\/(?P<code>.+?)\/(?P<skip>\.incomple te$)?'
150 compat_option = 'firefox'
151
152 class ChromePadFile(PadFile):
153 browser_name = 'Google Chrome'
154 os_support = OS_WINDOWS + OS_MAC + OS_LINUX
155 translation_files_regex = r'_locales\/(?P<code>.+?)\/'
156
157 class OperaPadFile(PadFile):
158 browser_name = 'Opera'
159 browser_min_version = '17'
160 os_support = OS_WINDOWS + OS_MAC + OS_LINUX
161 translation_files_regex = ChromePadFile.translation_files_regex
162
163 class SafariPadFile(PadFile):
164 borwser_name = 'Safari'
165 browser_min_version = '6'
166 os_support = OS_MAC
167 translation_files_regex = ChromePadFile.translation_files_regex
168
169 class InternetExplorerPadFile(PadFile):
170 browser_name = 'Internet Explorer'
171 browser_min_version = '8'
172 os_support = OS_WINDOWS
173 translation_files_regex = r'locales\/(?P<code>.+)\.ini$'
174
175 class AndroidPadFile(PadFile):
176 browser_name = 'Android'
177 os_support = OS_ANDROID
178 translation_files_regex = r'res\/(?:raw|values)(?:-(?P<code>.+?)|(?P<is_en>))\ /'
179
180 @property
181 def browser_min_version(self):
182 return get_min_android_version(self.repo, self.version)
OLDNEW
« no previous file with comments | « sitescripts/extensions/bin/updateDownloadLinks.py ('k') | sitescripts/extensions/pad/language.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld