Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdf8ed61

Browse files
committed
Use pathlib more in redirect script.
1 parent1ff05eb commitdf8ed61

File tree

1 file changed

+40
-49
lines changed

1 file changed

+40
-49
lines changed

‎_websiteutils/make_redirects_links.py

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,14 @@
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
formajorinrange(6,-1,-1)
3737
forminorinrange(6,-1,-1)
3838
formicroinrange(6,-1,-1)
39-
ifpathlib.Path(f"{major}.{minor}.{micro}").exists()
4039
]
4140

42-
toignore=tocheck+ [
41+
toignore=tocheck+ [pathlib.Path(p)forpin [
4342
"mpl-probscale",
4443
"mpl_examples",
4544
"mpl_toolkits",
@@ -49,7 +48,7 @@
4948
"robots.txt",
5049
"CNAME",
5150
".git",
52-
]
51+
]]
5352

5453
logging.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-
ifpin_cache:
65-
return_cache[p]
62+
iffnamein_cache:
63+
return_cache[fname]
6664
fortintocheck:
67-
pnew=pathlib.Path(t,p)
65+
pnew=t/fname
6866
ifpnew.exists():
69-
_cache[p]=t
67+
_cache[fname]=t
7068
returnt
7169
else:
72-
_cache[p]=None
70+
_cache[fname]=None
7371
returnNone
7472

7573

@@ -111,32 +109,29 @@ def do_links(root0):
111109
_log.info(f"Doing links on{root0}")
112110
forroot,dirs,filesinos.walk(root0):
113111
fornameinfiles:
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
iflastisnotNone:
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
ifname.endswith((".htm",".html")):
121123
# make an html redirect.
122124
_log.info(f"Rewriting HTML:{fullname} in{last}")
123-
withopen(fullname,"w")asfout:
124-
oldname=os.path.join(last,fullname)
125+
withfullname.open("w")asfout:
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-
foriinrange(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

142137
defdo_canonicals(dname):
@@ -145,16 +140,12 @@ def do_canonicals(dname):
145140
to the newest version.
146141
"""
147142
_log.debug(f"Walking{dname}")
148-
forroot,dirs,filesinos.walk(dname):
149-
fornameinfiles:
150-
fullname=os.path.join(root,name)
151-
p=pathlib.Path(fullname)
152-
_log.debug(f"Checking{fullname}")
153-
ifname.endswith((".htm",".html")):
154-
basename=pathlib.Path(*p.parts[1:])
155-
last=findlast(basename,tocheck)
156-
iflastisnotNone:
157-
update_canonical(fullname,last,dname==tocheck[1])
143+
forfullnameindname.rglob("*.html"):
144+
_log.debug(f"Checking{fullname}")
145+
basename=pathlib.Path(*fullname.parts[1:])
146+
last=findlast(basename,tocheck)
147+
iflastisnotNone:
148+
update_canonical(fullname,last,dname==tocheck[1])
158149

159150

160151
defupdate_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
withtempfile.NamedTemporaryFile(delete=False)asfout:
178168
found=False
179-
withopen(fullname,"rb")asfin:
169+
withfullname.open("rb")asfin:
180170
forlineinfin:
181171
ifnotfoundandb'<link rel="canonical"'inline:
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-
iflast=='stable':
192-
new=warn_banner_exists.format(version=p.parts[0],
193-
url=newcanon)
181+
iflast==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
ifb'<div id="olddocs-message">'notinline:
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=[tfortintocheckifos.path.exists(t)]
215+
tocheck=tuple(pforpintocheckifp.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
ifnotargs.no_redirects:
230221
forentryinos.scandir("."):
231-
ifentry.namenotintoignore:
222+
fullname=pathlib.Path(entry.name)
223+
iffullnamenotintoignore:
232224
ifentry.is_dir():
233225
do_links(entry.name)
234-
elifentry.name.endswith((".htm",".html")):
235-
fullname=entry.name
226+
eliffullname.suffix==".html":
236227
last=findlast(fullname,tocheck)
237228
_log.debug(f"Checking:{fullname} found{last}")
238229
iflastisnotNone:
239-
os.remove(fullname)
230+
fullname.unlink()
240231
_log.info(f"Rewriting HTML:{fullname} in{last}")
241-
withopen(fullname,"w")asfout:
242-
oldname=os.path.join(last,fullname)
232+
withfullname.open("w")asfout:
233+
oldname=last/fullname
243234
st=html_redirect.format(newurl=oldname,
244235
canonical=oldname)
245236
fout.write(st)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp