OLD | NEW |
(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 = ['Windows 8', '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 = download_url |
| 38 |
| 39 self.first_release = True |
| 40 for i, (filename, version) in enumerate(repo.getDownloads()): |
| 41 if i > 0: |
| 42 self.first_release = False |
| 43 |
| 44 if version == self.version: |
| 45 self.download_filename = filename |
| 46 |
| 47 @property |
| 48 def release_status(self): |
| 49 if self.first_release: |
| 50 return 'New Release' |
| 51 |
| 52 if self.version.rstrip('.0').count('.') < 2: |
| 53 return 'Major Update' |
| 54 |
| 55 return 'Minor Update' |
| 56 |
| 57 @property |
| 58 def release_date(self): |
| 59 command = ['hg', 'log', '-l', '1', self.download_filename, '--template', '{d
ate}'] |
| 60 result = subprocess.check_output(command, cwd=self.repo.downloadsRepo) |
| 61 timestamp, offset = re.match(r'(.*)([-+].*)', result).groups() |
| 62 return datetime(*time.gmtime(float(timestamp) + float(offset))[:6]) |
| 63 |
| 64 @property |
| 65 def download_size(self): |
| 66 return len(subprocess.check_output( |
| 67 ['hg', 'cat', self.download_filename], |
| 68 cwd=self.repo.downloadsRepo |
| 69 )) |
| 70 |
| 71 @property |
| 72 def browser_min_version(self): |
| 73 metadata = self.repo.readMetadata(self.version) |
| 74 compat_option = getattr(self, 'compat_option', self.repo.type) |
| 75 return metadata.get('compat', compat_option).split('/')[0].rstrip('.0') |
| 76 |
| 77 @property |
| 78 def languages(self): |
| 79 files = self.repo.listContents(self.version) |
| 80 languages = set() |
| 81 skipped = set() |
| 82 |
| 83 for filename in files: |
| 84 match = re.match(self.translation_files_regex, filename) |
| 85 |
| 86 if match: |
| 87 groups = match.groupdict() |
| 88 |
| 89 # support translation files that vary from the naming |
| 90 # scheme like the default translation for Android |
| 91 for k, v in groups.iteritems(): |
| 92 if k.startswith('is_') and v is not None: |
| 93 code = k[3:] |
| 94 break |
| 95 else: |
| 96 code = groups['code'] |
| 97 |
| 98 # support .incomplete files like on Firefox |
| 99 if groups.get('skip') is not None: |
| 100 skipped.add(code) |
| 101 continue |
| 102 |
| 103 languages.add(code) |
| 104 |
| 105 return iso2pad(languages.difference(skipped)) |
| 106 |
| 107 def write(self): |
| 108 template = get_template(self.repo.padTemplate) |
| 109 basename = self.repo.basename |
| 110 filename = basename + '.xml' |
| 111 |
| 112 pad = template.render({ |
| 113 'name': self.repo.name, |
| 114 'type': self.repo.type, |
| 115 'basename': basename, |
| 116 'browser_name': self.browser_name, |
| 117 'browser_min_version': self.browser_min_version, |
| 118 'version': self.version, |
| 119 'release_date': self.release_date, |
| 120 'release_status': self.release_status, |
| 121 'os_support': ','.join(self.os_support), |
| 122 'language': ','.join(self.languages), |
| 123 'download_size': self.download_size, |
| 124 'download_url': self.download_url, |
| 125 'pad_url': urljoin(self.repo.padURL, filename), |
| 126 }).encode('utf-8') |
| 127 |
| 128 path = os.path.join(self.repo.padDirectory, filename) |
| 129 validate_pad(pad, path) |
| 130 |
| 131 with open(path, 'wb') as file: |
| 132 file.write(pad) |
| 133 |
| 134 @staticmethod |
| 135 def forRepository(repo, *args, **kwargs): |
| 136 if repo.type == 'gecko': |
| 137 return FirefoxPadFile(repo, *args, **kwargs) |
| 138 if repo.type == 'chrome': |
| 139 return ChromePadFile(repo, *args, **kwargs) |
| 140 if repo.type == 'opera': |
| 141 return OperaPadFile(repo, *args, **kwargs) |
| 142 if repo.type == 'safari': |
| 143 return SafariPadFile(repo, *args, **kwargs) |
| 144 if repo.type == 'ie': |
| 145 return InternetExplorerPadFile(repo, *args, **kwargs) |
| 146 if repo.type == 'android': |
| 147 return AndroidPadFile(repo, *args, **kwargs) |
| 148 |
| 149 raise Exception('unknown repository type %r' % repo.type) |
| 150 |
| 151 class FirefoxPadFile(PadFile): |
| 152 browser_name = 'Mozilla Firefox' |
| 153 os_support = OS_WINDOWS + OS_MAC + OS_LINUX + OS_ANDROID |
| 154 translation_files_regex = r'chrome\/locale\/(?P<code>.+?)\/(?P<skip>\.incomple
te$)?' |
| 155 compat_option = 'firefox' |
| 156 |
| 157 class ChromePadFile(PadFile): |
| 158 browser_name = 'Google Chrome' |
| 159 os_support = OS_WINDOWS + OS_MAC + OS_LINUX |
| 160 translation_files_regex = r'_locales\/(?P<code>.+?)\/' |
| 161 |
| 162 class OperaPadFile(PadFile): |
| 163 browser_name = 'Opera' |
| 164 browser_min_version = '17' |
| 165 os_support = OS_WINDOWS + OS_MAC + OS_LINUX |
| 166 translation_files_regex = ChromePadFile.translation_files_regex |
| 167 |
| 168 class SafariPadFile(PadFile): |
| 169 browser_name = 'Safari' |
| 170 browser_min_version = '6' |
| 171 os_support = OS_MAC |
| 172 translation_files_regex = ChromePadFile.translation_files_regex |
| 173 |
| 174 class InternetExplorerPadFile(PadFile): |
| 175 browser_name = 'Internet Explorer' |
| 176 browser_min_version = '8' |
| 177 os_support = OS_WINDOWS |
| 178 translation_files_regex = r'locales\/(?P<code>.+)\.ini$' |
| 179 |
| 180 class AndroidPadFile(PadFile): |
| 181 browser_name = 'Android' |
| 182 os_support = OS_ANDROID |
| 183 translation_files_regex = r'res\/(?:raw|values)(?:-(?P<code>.+?)|(?P<is_en>))\
/' |
| 184 |
| 185 @property |
| 186 def browser_min_version(self): |
| 187 return get_min_android_version(self.repo, self.version) |
OLD | NEW |