OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 |
| 3 # This file is part of Adblock Plus <https://adblockplus.org/>, |
| 4 # Copyright (C) 2006-2016 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 os |
| 19 import shutil |
| 20 import subprocess |
| 21 |
| 22 from sitescripts.utils import get_config |
| 23 |
| 24 def read_projects(config): |
| 25 projects = {} |
| 26 for key, value in config.items("docs"): |
| 27 key_parts = key.split("_", 1) |
| 28 if len(key_parts) < 2: |
| 29 continue |
| 30 project_name, field_name = key_parts |
| 31 if field_name not in {"repository", "target_directory", "command"}: |
| 32 continue |
| 33 projects.setdefault(project_name, {})[field_name] = value |
| 34 return projects |
| 35 |
| 36 def sync_sources(sources_dir, repository_url): |
| 37 if os.path.exists(sources_dir): |
| 38 subprocess.check_call(["hg", "pull", "--quiet", |
| 39 "--rev", "master", |
| 40 "--repository", sources_dir]) |
| 41 subprocess.check_call(["hg", "update", "--quiet", |
| 42 "--rev", "master"]) |
| 43 else: |
| 44 subprocess.check_call(["hg", "clone", "--quiet", |
| 45 "--updaterev", "master", |
| 46 repository_url, sources_dir]) |
| 47 |
| 48 def replace_dir(source_dir, target_dir): |
| 49 if not os.path.exists(target_dir): |
| 50 parent_dir = os.path.dirname(target_dir) |
| 51 try: |
| 52 os.makedirs(parent_dir) |
| 53 except OSError: |
| 54 pass |
| 55 os.rename(source_dir, target_dir) |
| 56 else: |
| 57 old_target_dir = target_dir.rstrip(os.path.sep) + ".old" |
| 58 shutil.rmtree(old_target_dir, ignore_errors=True) |
| 59 os.rename(target_dir, old_target_dir) |
| 60 os.rename(source_dir, target_dir) |
| 61 shutil.rmtree(old_target_dir) |
| 62 |
| 63 def run_generation_command(command, sources_dir, output_dir): |
| 64 shutil.rmtree(output_dir, ignore_errors=True) |
| 65 command = command.format(output_dir=output_dir) |
| 66 subprocess.check_call(command, shell=True, cwd=sources_dir) |
| 67 |
| 68 def generate_docs(projects, config): |
| 69 temp_directory = config.get("docs", "temp_directory") |
| 70 try: |
| 71 os.makedirs(temp_directory) |
| 72 except OSError: |
| 73 pass |
| 74 |
| 75 for name, data in projects.iteritems(): |
| 76 sources_dir = os.path.join(temp_directory, name) |
| 77 sync_sources(sources_dir, data["repository"]) |
| 78 output_dir = sources_dir.rstrip(os.path.sep) + ".docs" |
| 79 run_generation_command(data["command"], sources_dir, output_dir) |
| 80 replace_dir(output_dir, data["target_directory"]) |
| 81 |
| 82 if __name__ == "__main__": |
| 83 config = get_config() |
| 84 projects = read_projects(config) |
| 85 generate_docs(projects, config) |
OLD | NEW |