|
| 1 | +importglob |
| 2 | +importos |
| 3 | +importsubprocess |
| 4 | +importpathlib |
| 5 | +importtempfile |
| 6 | +importre |
| 7 | +importlogging |
| 8 | + |
| 9 | +""" |
| 10 | +This script does two things that improve the website organization. |
| 11 | +
|
| 12 | +First, we used to host in the root of the webpage, but have now moved to |
| 13 | +``/stable/``. We do not want obsolete links to link to nothing (or that has |
| 14 | +been our policy), so we currently just keep the old version at the top level. |
| 15 | +Here, instead, we either softlink to the newest version, or replace the file by |
| 16 | +an html refresh redirect. |
| 17 | +
|
| 18 | +Second, it changes the canonical link in each html file to the newest version |
| 19 | +found of the html file (including stable if its in the latest version.) |
| 20 | +
|
| 21 | +This script takes a while, and is desctructive, so should probably be run on a |
| 22 | +branch and pushed as a PR so it can easily be reverted. |
| 23 | +""" |
| 24 | + |
| 25 | +_log=logging.getLogger('make_redirect_links') |
| 26 | + |
| 27 | + |
| 28 | +tocheck= ['stable']+ [f'{ver}.{minver}.{minminver}' |
| 29 | +forverinrange(6,-1,-1) |
| 30 | +forminverinrange(6,-1,-1) |
| 31 | +forminminverinrange(6,-1,-1)] |
| 32 | + |
| 33 | +toignore=tocheck+ ['mpl-probscale','mpl_examples','mpl_toolkits', |
| 34 | +'_webpageutils''plot_directive','xkcd','sitemap.xml', |
| 35 | +'robots.txt','CNAME','.git'] |
| 36 | + |
| 37 | +logging.basicConfig(level=logging.DEBUG) |
| 38 | + |
| 39 | + |
| 40 | +deffindlast(fname,tocheck): |
| 41 | +""" |
| 42 | + Check the directories listed in ``tocheck`` to see if they have |
| 43 | + ``fname`` in them. Return the first one found, or None |
| 44 | + """ |
| 45 | +p=pathlib.Path(fname) |
| 46 | +fortintocheck: |
| 47 | +pnew=pathlib.Path(t,*p.parts[:]) |
| 48 | +ifos.path.exists(pnew ): |
| 49 | +returnt |
| 50 | +else: |
| 51 | +returnNone |
| 52 | + |
| 53 | +html_redirect=""" |
| 54 | +<!DOCTYPE HTML> |
| 55 | +<html lang="en"> |
| 56 | + <head> |
| 57 | + <meta charset="utf-8"> |
| 58 | + <meta http-equiv="refresh" content="0;url=https://matplotlib.org%s" /> |
| 59 | + <link rel="canonical" href="https://matplotlib.org%s" /> |
| 60 | + </head> |
| 61 | + <body> |
| 62 | + <h1> |
| 63 | + The page been moved to <a href="https://matplotlib.org%s"</a> |
| 64 | + </h1> |
| 65 | + </body> |
| 66 | +</html> |
| 67 | +""" |
| 68 | + |
| 69 | + |
| 70 | +defdo_links(root0): |
| 71 | +""" |
| 72 | + Either soft link a file at the top level to its newest position, |
| 73 | + or make an html redirect if it is an html file. |
| 74 | + """ |
| 75 | +_log.info(f'Doing links on{root0}') |
| 76 | +forroot,dirs,filesinos.walk(root0): |
| 77 | +fornameinfiles: |
| 78 | +fullname=os.path.join(root,name) |
| 79 | +last=findlast(fullname,tocheck) |
| 80 | +_log.debug(f'Checking:{fullname} found{last}') |
| 81 | +iflastisnotNone: |
| 82 | +os.remove('./'+fullname) |
| 83 | +ifname.endswith(('.htm','.html')): |
| 84 | +# make an html redirect. |
| 85 | +_log.info(f'Rewriting HTML:{fullname} in{last}') |
| 86 | +withopen(fullname,'w')asfout: |
| 87 | +oldname='/'+os.path.join(last,fullname) |
| 88 | +st=html_redirect% (oldname,oldname,oldname) |
| 89 | +fout.write(st) |
| 90 | +else: |
| 91 | +# soft link |
| 92 | +# Need to do these relative to where the link is |
| 93 | +# so if it is a level down `ln -s ../3.1.1/boo/who boo/who` |
| 94 | +last=os.path.join('..',last) |
| 95 | +depth=root.count('/') |
| 96 | +foriinrange(depth): |
| 97 | +last=os.path.join('..',last) |
| 98 | +oldname=os.path.join(last,fullname) |
| 99 | +_log.info(f'Linking{fullname} to{oldname}') |
| 100 | +os.symlink(oldname,fullname) |
| 101 | +fordindirs: |
| 102 | +do_links(d) |
| 103 | + |
| 104 | + |
| 105 | +defdo_canonicals(dname): |
| 106 | +""" |
| 107 | + For each html file in the versioned docs, make the canonical link point |
| 108 | + to the newest version. |
| 109 | + """ |
| 110 | +_log.debug(f'Walking{dname}') |
| 111 | +forroot,dirs,filesinos.walk(dname): |
| 112 | +fornameinfiles: |
| 113 | +fullname=os.path.join(root,name) |
| 114 | +p=pathlib.Path(fullname) |
| 115 | +_log.debug(f'Checking{fullname}') |
| 116 | +ifname.endswith(('.htm','.html')): |
| 117 | +basename=pathlib.Path(*p.parts[1:]) |
| 118 | +last=findlast(basename,tocheck) |
| 119 | +iflastisnotNone: |
| 120 | +update_canonical(fullname,last) |
| 121 | + |
| 122 | +fordindirs: |
| 123 | +_log.info(f'DIR:{d}') |
| 124 | +do_canonicals(os.path.join(dname,d)) |
| 125 | + |
| 126 | + |
| 127 | +defupdate_canonical(fullname,last): |
| 128 | +""" |
| 129 | + Change the canonical link in *fullname* to the same link in the |
| 130 | + version given by *last*. We do this with a regexp to prevent |
| 131 | + removing any other content on a line that has the canonical link. |
| 132 | +
|
| 133 | + Note that if for some reason there are more than one canonical link |
| 134 | + this will change all of them. |
| 135 | + """ |
| 136 | +p=pathlib.Path(fullname) |
| 137 | +pre='https://matplotlib.org/' |
| 138 | +pnew=pathlib.Path(last,*p.parts[1:]) |
| 139 | +newcanon=f'{pre+str(pnew)}' |
| 140 | +_log.info(f'{p} to{pre+str(pnew)}') |
| 141 | +withtempfile.NamedTemporaryFile(delete=False)asfout: |
| 142 | +withopen(fullname,'r')asfin: |
| 143 | +forlineinfin: |
| 144 | +if'<link rel="canonical"'inline: |
| 145 | +ll=re.sub('<link rel="canonical" href=".*"', |
| 146 | +f'<link rel="canonical" href="{newcanon}"', |
| 147 | +line) |
| 148 | +_log.debug(f'new{line}->{ll}') |
| 149 | +fout.write(bytes(ll,encoding='utf-8')) |
| 150 | +else: |
| 151 | +fout.write(bytes(line,encoding='utf-8')) |
| 152 | +os.rename(fout.name,fullname) |
| 153 | + |
| 154 | + |
| 155 | +defmain(): |
| 156 | +# html redirect or soft link most things in the top-level directory that |
| 157 | +# are not other modules or versioned docs. |
| 158 | +forentryinos.scandir('./'): |
| 159 | +ifentry.is_dir(): |
| 160 | +ifnot (entry.nameintoignore): |
| 161 | +do_links(entry.name) |
| 162 | +_log.info('Done links and redirects') |
| 163 | + |
| 164 | +# change the canonical url for all html to the newest version in the docs: |
| 165 | +forentryinos.scandir('./'): |
| 166 | +ifentry.is_dir()and (entry.nameintocheck[1:]): |
| 167 | +_log.info(f'TOP DIR:{entry.name}') |
| 168 | +do_canonicals(entry.name) |
| 169 | + |
| 170 | +if__name__=="__main__": |
| 171 | +main() |