|
| 1 | +import argparse |
1 | 2 | import glob
|
| 3 | +import logging |
| 4 | +import multiprocessing |
2 | 5 | import os
|
3 |
| -import subprocess |
4 | 6 | import pathlib
|
5 |
| -import tempfile |
6 | 7 | import re
|
7 |
| -import logging |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | +import tempfile |
| 11 | + |
8 | 12 |
|
9 | 13 | """
|
10 | 14 | This script does two things that improve the website organization.
|
@@ -152,30 +156,47 @@ def update_canonical(fullname, last):
|
152 | 156 | os.rename(fout.name, fullname)
|
153 | 157 |
|
154 | 158 |
|
155 |
| -def main(): |
| 159 | +if __name__ == "__main__": |
| 160 | + |
| 161 | + parser = argparse.ArgumentParser(description='Optional app description') |
| 162 | + |
| 163 | + parser.add_argument('--np', type=int, help='Number of processors to use') |
| 164 | + parser.add_argument('--no_canonicals', help='do not do canonical links', |
| 165 | + action="store_true") |
| 166 | + parser.add_argument('--no_redirects', help='do not do redirects links', |
| 167 | + action="store_true") |
| 168 | + |
| 169 | + args = parser.parse_args() |
| 170 | + if args.np: |
| 171 | + np = args.np |
| 172 | + else: |
| 173 | + np = None |
| 174 | + |
156 | 175 | # html redirect or soft link most things in the top-level directory that
|
157 | 176 | # are not other modules or versioned docs.
|
158 |
| - for entry in os.scandir('./'): |
159 |
| - if not (entry.name in toignore): |
160 |
| - if entry.is_dir(): |
161 |
| - do_links(entry.name) |
162 |
| - elif entry.name.endswith(('.htm', '.html')): |
163 |
| - fullname = entry.name |
164 |
| - last = findlast(fullname, tocheck) |
165 |
| - _log.debug(f'Checking: {fullname} found {last}') |
166 |
| - if last is not None: |
167 |
| - os.remove('./'+fullname) |
168 |
| - _log.info(f'Rewriting HTML: {fullname} in {last}') |
169 |
| - with open(fullname, 'w') as fout: |
170 |
| - oldname = '/' + os.path.join(last, fullname) |
171 |
| - st = html_redirect % (oldname, oldname, oldname) |
172 |
| - fout.write(st) |
173 |
| - _log.info('Done links and redirects') |
174 |
| - # change the canonical url for all html to the newest version in the docs: |
175 |
| - |
176 |
| - pool = multiprocessing.Pool() |
177 |
| - pool.map(do_canonicals, tocheck[1:]) |
178 |
| - pool.close() |
| 177 | + if not args.no_redirects: |
| 178 | + for entry in os.scandir('./'): |
| 179 | + if not (entry.name in toignore): |
| 180 | + if entry.is_dir(): |
| 181 | + do_links(entry.name) |
| 182 | + elif entry.name.endswith(('.htm', '.html')): |
| 183 | + fullname = entry.name |
| 184 | + last = findlast(fullname, tocheck) |
| 185 | + _log.debug(f'Checking: {fullname} found {last}') |
| 186 | + if last is not None: |
| 187 | + os.remove('./'+fullname) |
| 188 | + _log.info(f'Rewriting HTML: {fullname} in {last}') |
| 189 | + with open(fullname, 'w') as fout: |
| 190 | + oldname = '/' + os.path.join(last, fullname) |
| 191 | + st = html_redirect % (oldname, oldname, oldname) |
| 192 | + fout.write(st) |
| 193 | + _log.info('Done links and redirects') |
179 | 194 |
|
180 |
| -if __name__ == "__main__": |
181 |
| - main() |
| 195 | + # change the canonical url for all html to the newest version in the docs: |
| 196 | + if not args.no_canonicals: |
| 197 | + if np is not None: |
| 198 | + with multiprocessing.Pool(np) as pool: |
| 199 | + pool.map(do_canonicals, tocheck[1:]) |
| 200 | + else: |
| 201 | + for t in tocheck[1:]: |
| 202 | + do_canonicals(t) |
0 commit comments