| OLD | NEW | 
|---|
| 1 # coding: utf-8 | 1 # coding: utf-8 | 
| 2 | 2 | 
| 3 # This file is part of the Adblock Plus web scripts, | 3 # This file is part of the Adblock Plus web scripts, | 
| 4 # Copyright (C) 2006-2013 Eyeo GmbH | 4 # Copyright (C) 2006-2013 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 os, re, subprocess, tempfile, shutil | 18 import os, re, subprocess, tempfile, shutil, zipfile | 
| 19 from sitescripts.utils import get_config, setupStderr | 19 from StringIO import StringIO | 
| 20 from sitescripts.subscriptions.combineSubscriptions import combineSubscriptions | 20 from ...utils import get_config, setupStderr | 
|  | 21 from ..combineSubscriptions import combine_subscriptions | 
| 21 | 22 | 
| 22 if __name__ == '__main__': | 23 class MercurialSource: | 
|  | 24   _prefix = "./" | 
|  | 25 | 
|  | 26   def __init__(self, repo): | 
|  | 27     command = ["hg", "-R", repo, "archive", "-r", "default", | 
|  | 28         "-t", "uzip", "-p", ".", "-"] | 
|  | 29     data = subprocess.check_output(command) | 
|  | 30     self._archive = zipfile.ZipFile(StringIO(data), mode="r") | 
|  | 31 | 
|  | 32   def close(self): | 
|  | 33     self._archive.close() | 
|  | 34 | 
|  | 35   def read_file(self, filename): | 
|  | 36     return self._archive.read(self._prefix + filename).decode("utf-8") | 
|  | 37 | 
|  | 38   def list_top_level_files(self): | 
|  | 39     for filename in self._archive.namelist(): | 
|  | 40       filename = filename[len(self._prefix):] | 
|  | 41       if "/" not in filename: | 
|  | 42         yield filename | 
|  | 43 | 
|  | 44 if __name__ == "__main__": | 
| 23   setupStderr() | 45   setupStderr() | 
| 24 | 46 | 
| 25   sourceRepos = {} | 47   source_repos = {} | 
| 26   for option, value in get_config().items('subscriptionDownloads'): | 48   for option, value in get_config().items("subscriptionDownloads"): | 
| 27     if option.endswith('_repository'): | 49     if option.endswith("_repository"): | 
| 28       sourceRepos[re.sub(r'_repository$', '', option)] = value | 50       source_repos[re.sub(r"_repository$", "", option)] = MercurialSource(value) | 
| 29   destDir = get_config().get('subscriptionDownloads', 'outdir') |  | 
| 30 | 51 | 
| 31   sourceTemp = {} | 52   basedir = get_config().get("subscriptionDownloads", "outdir") | 
| 32   destTemp = None | 53   destination = tempfile.mkdtemp(prefix="data.", dir=basedir) | 
| 33   try: | 54   try: | 
| 34     destTemp = tempfile.mkdtemp() | 55     combine_subscriptions(source_repos, destination) | 
| 35     for repoName, repoDir in sourceRepos.iteritems(): | 56   except: | 
| 36       tempDir = tempfile.mkdtemp() | 57     shutil.rmtree(destination, ignore_errors=True) | 
| 37       sourceTemp[repoName] = tempDir | 58     raise | 
| 38       subprocess.check_call(['hg', 'archive', '-R', repoDir, '-r', 'default', te
    mpDir]) |  | 
| 39     subprocess.check_call(['rsync', '-a', '--delete', destDir + os.path.sep, des
    tTemp]) |  | 
| 40     combineSubscriptions(sourceTemp, destTemp) |  | 
| 41     subprocess.check_call(['rsync', '-au', '--delete', destTemp + os.path.sep, d
    estDir]) |  | 
| 42   finally: | 59   finally: | 
| 43     for tempDir in sourceTemp.itervalues(): | 60     for source in source_repos.itervalues(): | 
| 44       if os.path.exists(tempDir): | 61       source.close() | 
| 45         shutil.rmtree(tempDir, True) | 62 | 
| 46     if destTemp and os.path.exists(destTemp): | 63   symbolic_link = os.path.join(basedir, "data") | 
| 47       shutil.rmtree(destTemp, True) | 64   symbolic_link_tmp = os.path.join(basedir, "data~") | 
|  | 65   orig_data = None | 
|  | 66   if os.path.islink(symbolic_link): | 
|  | 67     orig_data = os.path.join(basedir, os.readlink(symbolic_link)) | 
|  | 68 | 
|  | 69   os.symlink(os.path.relpath(destination, basedir), symbolic_link_tmp) | 
|  | 70   os.rename(symbolic_link_tmp, symbolic_link) | 
|  | 71 | 
|  | 72   if orig_data: | 
|  | 73     shutil.rmtree(orig_data) | 
| OLD | NEW | 
|---|