|
| 1 | +import glob |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | + |
| 5 | +tocheck = ['stable'] + [f'{ver}.{minver}.{minminver}' |
| 6 | + for ver in range(6, -1, -1) |
| 7 | + for minver in range(6,-1,-1) |
| 8 | + for minminver in range(6,-1, -1)] |
| 9 | +toignore = tocheck + ['mpl-probscale', 'mplt_examples', 'mpl_toolkits', |
| 10 | + 'plot_directive', 'xkcd', 'sitemap.xml', 'robots.tex', |
| 11 | + 'CNAME', '.git'] |
| 12 | + |
| 13 | + |
| 14 | +def findlast(fname, tocheck): |
| 15 | + """ |
| 16 | + Check the directories listed in ``tocheck`` to see if they have |
| 17 | + ``fname`` in them. Return the first one found, or None |
| 18 | + """ |
| 19 | + for t in tocheck: |
| 20 | + if os.path.exists(t + '/'+ fname): |
| 21 | + return t |
| 22 | + else: |
| 23 | + return None |
| 24 | + |
| 25 | + |
| 26 | +html_redirect = """ |
| 27 | +<!DOCTYPE HTML> |
| 28 | +<html lang="en"> |
| 29 | + <head> |
| 30 | + <meta charset="utf-8"> |
| 31 | + <meta http-equiv="refresh" content="0;url=https://matplotlib.org%s" /> |
| 32 | + <link rel="canonical" href="https://matplotlib.org%s" /> |
| 33 | + </head> |
| 34 | + <body> |
| 35 | + <h1> |
| 36 | + The page been moved to <a href="https://matplotlib.org%s"</a> |
| 37 | + </h1> |
| 38 | + </body> |
| 39 | +</html> |
| 40 | +""" |
| 41 | + |
| 42 | +def do_links(root0): |
| 43 | + """ |
| 44 | + Either soft link a file at the top level to its newest position, |
| 45 | + or make an html redirect. |
| 46 | + """ |
| 47 | + for root, dirs, files in os.walk(root0): |
| 48 | + for name in files: |
| 49 | + fullname = root + '/' + name |
| 50 | + last = findlast(fullname, tocheck) |
| 51 | + if last is not None: |
| 52 | + os.remove('./'+fullname) |
| 53 | + if name.endswith(('.htm', '.html')): |
| 54 | + # make an html redirect. |
| 55 | + print('Rewriting HTML:', fullname, 'in', last) |
| 56 | + with open(fullname, 'w') as fout: |
| 57 | + oldname = '/' + last + '/' + fullname |
| 58 | + st = html_redirect % (oldname, oldname, oldname) |
| 59 | + fout.write(st) |
| 60 | + else: |
| 61 | + # soft link |
| 62 | + # Need to do these relative to where the link is |
| 63 | + # so if it is a level down `ln -s ../3.1.1/boo/who boo/who` |
| 64 | + depth = root.count('/') |
| 65 | + for i in range(depth): |
| 66 | + last = '../' + last |
| 67 | + print('Linking', fullname, 'to', last) |
| 68 | + os.symlink(last + '/' + fullname, fullname) |
| 69 | + print(dirs) |
| 70 | + for d in dirs: |
| 71 | + do_links(d) |
| 72 | + |
| 73 | + |
| 74 | +# Main routine. We need to run as `do_links` so it can be run recursively |
| 75 | +# on the directory structure... |
| 76 | +for entry in os.scandir('./'): |
| 77 | + if entry.is_dir(): |
| 78 | + if not (entry.name in toignore): |
| 79 | + do_links(entry.name) |
0 commit comments