|
| 1 | +importglob |
| 2 | +importos |
| 3 | +importsubprocess |
| 4 | + |
| 5 | +tocheck= ['stable']+ [f'{ver}.{minver}.{minminver}' |
| 6 | +forverinrange(6,-1,-1) |
| 7 | +forminverinrange(6,-1,-1) |
| 8 | +forminminverinrange(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 | +deffindlast(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 | +fortintocheck: |
| 20 | +ifos.path.exists(t+'/'+fname): |
| 21 | +returnt |
| 22 | +else: |
| 23 | +returnNone |
| 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 | +defdo_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 | +forroot,dirs,filesinos.walk(root0): |
| 48 | +fornameinfiles: |
| 49 | +fullname=root+'/'+name |
| 50 | +last=findlast(fullname,tocheck) |
| 51 | +iflastisnotNone: |
| 52 | +os.remove('./'+fullname) |
| 53 | +ifname.endswith(('.htm','.html')): |
| 54 | +# make an html redirect. |
| 55 | +print('Rewriting HTML:',fullname,'in',last) |
| 56 | +withopen(fullname,'w')asfout: |
| 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 | +foriinrange(depth): |
| 66 | +last='../'+last |
| 67 | +print('Linking',fullname,'to',last) |
| 68 | +os.symlink(last+'/'+fullname,fullname) |
| 69 | +print(dirs) |
| 70 | +fordindirs: |
| 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 | +forentryinos.scandir('./'): |
| 77 | +ifentry.is_dir(): |
| 78 | +ifnot (entry.nameintoignore): |
| 79 | +do_links(entry.name) |