3131_log = logging .getLogger ("make_redirect_links" )
3232
3333
34- tocheck = ["stable" ] + [
35- f"{ major } .{ minor } .{ micro } "
34+ tocheck = [pathlib . Path ( "stable" ) ] + [
35+ pathlib . Path ( f"{ major } .{ minor } .{ micro } " )
3636 for major in range (6 , - 1 , - 1 )
3737 for minor in range (6 , - 1 , - 1 )
3838 for micro in range (6 , - 1 , - 1 )
39- if pathlib .Path (f"{ major } .{ minor } .{ micro } " ).exists ()
4039]
4140
42- toignore = tocheck + [
41+ toignore = tocheck + [pathlib . Path ( p ) for p in [
4342 "mpl-probscale" ,
4443 "mpl_examples" ,
4544 "mpl_toolkits" ,
4948 "robots.txt" ,
5049 "CNAME" ,
5150 ".git" ,
52- ]
51+ ]]
5352
5453logging .basicConfig (level = logging .DEBUG )
5554
@@ -60,16 +59,15 @@ def findlast(fname, tocheck, *, _cache={}):
6059 Check the directories listed in ``tocheck`` to see if they have
6160 ``fname`` in them. Return the first one found, or None
6261 """
63- p = pathlib .Path (fname )
64- if p in _cache :
65- return _cache [p ]
62+ if fname in _cache :
63+ return _cache [fname ]
6664 for t in tocheck :
67- pnew = pathlib . Path ( t , p )
65+ pnew = t / fname
6866 if pnew .exists ():
69- _cache [p ] = t
67+ _cache [fname ] = t
7068 return t
7169 else :
72- _cache [p ] = None
70+ _cache [fname ] = None
7371 return None
7472
7573
@@ -111,32 +109,29 @@ def do_links(root0):
111109 _log .info (f"Doing links on { root0 } " )
112110 for root , dirs , files in os .walk (root0 ):
113111 for name in files :
114- fullname = os . path . join (root , name )
112+ fullname = pathlib . Path (root , name )
115113 last = findlast (fullname , tocheck )
116114 _log .debug (f"Checking: { fullname } found { last } " )
117- depth = root .count ("/" )
118115 if last is not None :
119- os .remove (fullname )
116+ fullname .unlink ()
117+ oldname = last / fullname
118+ # Need to do these relative to where the final is, but note
119+ # that `Path.relative_to` does not allow '.' as a common path
120+ # prefix, so we need to use `os.path.relpath` instead.
121+ relpath = os .path .relpath (oldname , start = fullname .parent )
120122 if name .endswith ((".htm" , ".html" )):
121123 # make an html redirect.
122124 _log .info (f"Rewriting HTML: { fullname } in { last } " )
123- with open (fullname , "w" ) as fout :
124- oldname = os .path .join (last , fullname )
125+ with fullname .open ("w" ) as fout :
125126 st = html_redirect .format (
126- newurl = "../" * ( depth + 1 ) + oldname ,
127+ newurl = relpath ,
127128 canonical = oldname ,
128129 )
129130 fout .write (st )
130131 else :
131132 # soft link
132- # Need to do these relative to where the link is
133- # so if it is a level down `ln -s ../3.1.1/boo/who boo/who`
134- last = os .path .join (".." , last )
135- for i in range (depth ):
136- last = os .path .join (".." , last )
137- oldname = os .path .join (last , fullname )
138133 _log .info (f"Linking { fullname } to { oldname } " )
139- os . symlink ( oldname , fullname )
134+ fullname . symlink_to ( relpath )
140135
141136
142137def do_canonicals (dname ):
@@ -145,16 +140,12 @@ def do_canonicals(dname):
145140 to the newest version.
146141 """
147142 _log .debug (f"Walking { dname } " )
148- for root , dirs , files in os .walk (dname ):
149- for name in files :
150- fullname = os .path .join (root , name )
151- p = pathlib .Path (fullname )
152- _log .debug (f"Checking { fullname } " )
153- if name .endswith ((".htm" , ".html" )):
154- basename = pathlib .Path (* p .parts [1 :])
155- last = findlast (basename , tocheck )
156- if last is not None :
157- update_canonical (fullname , last , dname == tocheck [1 ])
143+ for fullname in dname .rglob ("*.html" ):
144+ _log .debug (f"Checking { fullname } " )
145+ basename = pathlib .Path (* fullname .parts [1 :])
146+ last = findlast (basename , tocheck )
147+ if last is not None :
148+ update_canonical (fullname , last , dname == tocheck [1 ])
158149
159150
160151def update_canonical (fullname , last , newest ):
@@ -168,15 +159,14 @@ def update_canonical(fullname, last, newest):
168159 Note that if for some reason there are more than one canonical link
169160 this will change all of them.
170161 """
171- p = pathlib .Path (fullname )
172162 pre = "https://matplotlib.org/"
173- pnew = pathlib . Path ( last , * p .parts [1 :])
163+ pnew = last . joinpath ( * fullname .parts [1 :])
174164 newcanon = f"{ pre } { str (pnew )} "
175- _log .info (f"{ p } to { pre } { str (pnew )} " )
165+ _log .info (f"{ fullname } to { pre } { str (pnew )} " )
176166 rec = re .compile (b'<link rel="canonical" href=".*"' )
177167 with tempfile .NamedTemporaryFile (delete = False ) as fout :
178168 found = False
179- with open (fullname , "rb" ) as fin :
169+ with fullname . open ("rb" ) as fin :
180170 for line in fin :
181171 if not found and b'<link rel="canonical"' in line :
182172 new = f'<link rel="canonical" href="{ newcanon } "'
@@ -188,11 +178,12 @@ def update_canonical(fullname, last, newest):
188178 # add a warning right under:
189179 fout .write (line )
190180 line = next (fin )
191- if last == 'stable' :
192- new = warn_banner_exists .format (version = p .parts [0 ],
193- url = newcanon )
181+ if last == tocheck [0 ]:
182+ new = warn_banner_exists .format (
183+ version = fullname .parts [0 ],
184+ url = newcanon )
194185 else :
195- new = warn_banner_old .format (version = p .parts [0 ])
186+ new = warn_banner_old .format (version = fullname .parts [0 ])
196187 fout .write (new .encode ("utf-8" ))
197188 if b'<div id="olddocs-message">' not in line :
198189 # write the line out if it wasn't an olddocs-message:
@@ -221,25 +212,25 @@ def update_canonical(fullname, last, newest):
221212 np = None
222213
223214 # figure out the newest version and trim tocheck at the same time:
224- tocheck = [ t for t in tocheck if os . path . exists (t )]
215+ tocheck = tuple ( p for p in tocheck if p . exists ())
225216 print (tocheck )
226217
227218 # html redirect or soft link most things in the top-level directory that
228219 # are not other modules or versioned docs.
229220 if not args .no_redirects :
230221 for entry in os .scandir ("." ):
231- if entry .name not in toignore :
222+ fullname = pathlib .Path (entry .name )
223+ if fullname not in toignore :
232224 if entry .is_dir ():
233225 do_links (entry .name )
234- elif entry .name .endswith ((".htm" , ".html" )):
235- fullname = entry .name
226+ elif fullname .suffix == ".html" :
236227 last = findlast (fullname , tocheck )
237228 _log .debug (f"Checking: { fullname } found { last } " )
238229 if last is not None :
239- os . remove ( fullname )
230+ fullname . unlink ( )
240231 _log .info (f"Rewriting HTML: { fullname } in { last } " )
241- with open (fullname , "w" ) as fout :
242- oldname = os . path . join ( last , fullname )
232+ with fullname . open ("w" ) as fout :
233+ oldname = last / fullname
243234 st = html_redirect .format (newurl = oldname ,
244235 canonical = oldname )
245236 fout .write (st )
0 commit comments