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