Skip to content

Commit ef14fcb

Browse files
authored
Merge pull request #14988 from anntzer/pathlibsetup
BLD: pathlibify/fstringify setup/setupext.
2 parents 965540c + a5d4da0 commit ef14fcb

File tree

2 files changed

+54
-69
lines changed

2 files changed

+54
-69
lines changed

setup.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
""".format('.'.join(str(n) for n in min_version)),
2121
sys.exit(error)
2222

23-
import os
23+
from pathlib import Path
2424
import shutil
2525
from zipfile import ZipFile
2626

@@ -116,52 +116,49 @@ def build_extensions(self):
116116

117117

118118
def _download_jquery_to(dest):
119-
if os.path.exists(os.path.join(dest, "jquery-ui-1.12.1")):
120-
return
121-
122-
# If we are installing from an sdist, use the already downloaded jquery-ui
123-
sdist_src = os.path.join(
124-
"lib/matplotlib/backends/web_backend", "jquery-ui-1.12.1")
125-
if os.path.exists(sdist_src):
126-
shutil.copytree(sdist_src, os.path.join(dest, "jquery-ui-1.12.1"))
127-
return
128-
129119
# Note: When bumping the jquery-ui version, also update the versions in
130120
# single_figure.html and all_figures.html.
131121
url = "https://jqueryui.com/resources/download/jquery-ui-1.12.1.zip"
132-
sha = 'f8233674366ab36b2c34c577ec77a3d70cac75d2e387d8587f3836345c0f624d'
133-
if not os.path.exists(os.path.join(dest, "jquery-ui-1.12.1")):
134-
os.makedirs(dest, exist_ok=True)
122+
sha = "f8233674366ab36b2c34c577ec77a3d70cac75d2e387d8587f3836345c0f624d"
123+
name = Path(url).stem
124+
if (dest / name).exists():
125+
return
126+
# If we are installing from an sdist, use the already downloaded jquery-ui.
127+
sdist_src = Path("lib/matplotlib/backends/web_backend", name)
128+
if sdist_src.exists():
129+
shutil.copytree(sdist_src, dest / name)
130+
return
131+
if not (dest / name).exists():
132+
dest.mkdir(parents=True, exist_ok=True)
135133
try:
136134
buff = download_or_cache(url, sha)
137135
except Exception:
138-
raise IOError("Failed to download jquery-ui. Please download " +
139-
"{url} and extract it to {dest}.".format(
140-
url=url, dest=dest))
136+
raise IOError(f"Failed to download jquery-ui. Please download "
137+
f"{url} and extract it to {dest}.")
141138
with ZipFile(buff) as zf:
142139
zf.extractall(dest)
143140

144141

145142
# Relying on versioneer's implementation detail.
146143
class sdist_with_jquery(cmdclass['sdist']):
147144
def make_release_tree(self, base_dir, files):
148-
super(sdist_with_jquery, self).make_release_tree(base_dir, files)
145+
super().make_release_tree(base_dir, files)
149146
_download_jquery_to(
150-
os.path.join(base_dir, "lib/matplotlib/backends/web_backend/"))
147+
Path(base_dir, "lib/matplotlib/backends/web_backend/"))
151148

152149

153150
# Affects install and bdist_wheel.
154151
class install_lib_with_jquery(InstallLibCommand):
155152
def run(self):
156-
super(install_lib_with_jquery, self).run()
153+
super().run()
157154
_download_jquery_to(
158-
os.path.join(self.install_dir, "matplotlib/backends/web_backend/"))
155+
Path(self.install_dir, "matplotlib/backends/web_backend/"))
159156

160157

161158
class develop_with_jquery(DevelopCommand):
162159
def run(self):
163-
super(develop_with_jquery, self).run()
164-
_download_jquery_to("lib/matplotlib/backends/web_backend/")
160+
super().run()
161+
_download_jquery_to(Path("lib/matplotlib/backends/web_backend/"))
165162

166163

167164
cmdclass['sdist'] = sdist_with_jquery

setupext.py

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def _get_xdg_cache_dir():
3333
cache_dir = os.path.expanduser('~/.cache')
3434
if cache_dir.startswith('~/'): # Expansion failed.
3535
return None
36-
return os.path.join(cache_dir, 'matplotlib')
36+
return pathlib.Path(cache_dir, 'matplotlib')
3737

3838

3939
def get_fd_hash(fd):
@@ -74,7 +74,7 @@ def download_or_cache(url, sha):
7474
def get_from_cache(local_fn):
7575
if cache_dir is None:
7676
raise Exception("no cache dir")
77-
buf = BytesIO(pathlib.Path(cache_dir, local_fn).read_bytes())
77+
buf = BytesIO((cache_dir / local_fn).read_bytes())
7878
if get_fd_hash(buf) != sha:
7979
return None
8080
buf.seek(0)
@@ -83,11 +83,10 @@ def get_from_cache(local_fn):
8383
def write_cache(local_fn, data):
8484
if cache_dir is None:
8585
raise Exception("no cache dir")
86-
cache_filename = os.path.join(cache_dir, local_fn)
87-
os.makedirs(cache_dir, exist_ok=True)
86+
cache_dir.mkdir(parents=True, exist_ok=True)
8887
old_pos = data.tell()
8988
data.seek(0)
90-
with open(cache_filename, "xb") as fout:
89+
with open(cache_dir / local_fn, "xb") as fout:
9190
fout.write(data.read())
9291
data.seek(old_pos)
9392

@@ -107,10 +106,9 @@ def write_cache(local_fn, data):
107106
file_sha = get_fd_hash(file_contents)
108107

109108
if file_sha != sha:
110-
raise Exception(("The download file does not match the "
111-
"expected sha. {url} was expected to have "
112-
"{sha} but it had {file_sha}").format(
113-
sha=sha, file_sha=file_sha, url=url))
109+
raise Exception(
110+
f"The download file does not match the expected sha. {url} was "
111+
f"expected to have {sha} but it had {file_sha}")
114112

115113
try:
116114
write_cache(sha, file_contents)
@@ -483,17 +481,17 @@ class FreeType(SetupPackage):
483481
def add_flags(self, ext):
484482
ext.sources.insert(0, 'src/checkdep_freetype2.c')
485483
if options.get('local_freetype'):
486-
src_path = os.path.join(
487-
'build', 'freetype-{0}'.format(LOCAL_FREETYPE_VERSION))
484+
src_path = pathlib.Path(
485+
'build', f'freetype-{LOCAL_FREETYPE_VERSION}')
488486
# Statically link to the locally-built freetype.
489487
# This is certainly broken on Windows.
490-
ext.include_dirs.insert(0, os.path.join(src_path, 'include'))
488+
ext.include_dirs.insert(0, str(src_path / 'include'))
491489
if sys.platform == 'win32':
492490
libfreetype = 'libfreetype.lib'
493491
else:
494492
libfreetype = 'libfreetype.a'
495493
ext.extra_objects.insert(
496-
0, os.path.join(src_path, 'objs', '.libs', libfreetype))
494+
0, str(src_path / 'objs' / '.libs' / libfreetype))
497495
ext.define_macros.append(('FREETYPE_BUILD_TYPE', 'local'))
498496
else:
499497
pkg_config_setup_extension(
@@ -511,8 +509,7 @@ def do_custom_build(self):
511509
if not options.get('local_freetype'):
512510
return
513511

514-
src_path = os.path.join(
515-
'build', 'freetype-{0}'.format(LOCAL_FREETYPE_VERSION))
512+
src_path = pathlib.Path('build', f'freetype-{LOCAL_FREETYPE_VERSION}')
516513

517514
# We've already built freetype
518515
if sys.platform == 'win32':
@@ -521,12 +518,11 @@ def do_custom_build(self):
521518
libfreetype = 'libfreetype.a'
522519

523520
# bailing because it is already built
524-
if os.path.isfile(os.path.join(
525-
src_path, 'objs', '.libs', libfreetype)):
521+
if (src_path / 'objs' / '.libs' / libfreetype).is_file():
526522
return
527523

528524
# do we need to download / load the source from cache?
529-
if not os.path.exists(src_path):
525+
if not src_path.exists():
530526
os.makedirs('build', exist_ok=True)
531527

532528
url_fmts = [
@@ -535,7 +531,7 @@ def do_custom_build(self):
535531
('https://download.savannah.gnu.org/releases/freetype'
536532
'/{tarball}')
537533
]
538-
tarball = 'freetype-{0}.tar.gz'.format(LOCAL_FREETYPE_VERSION)
534+
tarball = f'freetype-{LOCAL_FREETYPE_VERSION}.tar.gz'
539535

540536
target_urls = [
541537
url_fmt.format(version=LOCAL_FREETYPE_VERSION,
@@ -550,22 +546,20 @@ def do_custom_build(self):
550546
except Exception:
551547
pass
552548
else:
553-
raise IOError("Failed to download FreeType. Please download "
554-
"one of {target_urls} and extract it into "
555-
"{src_path} at the top-level of the source "
556-
"repository".format(
557-
target_urls=target_urls, src_path=src_path))
549+
raise IOError(
550+
f"Failed to download FreeType. Please download one of "
551+
f"{target_urls} and extract it into {src_path} at the "
552+
f"top-level of the source repository.")
558553

559-
print("Extracting {}".format(tarball))
554+
print(f"Extracting {tarball}")
560555
# just to be sure
561556
tar_contents.seek(0)
562557
with tarfile.open(tarball, mode="r:gz",
563558
fileobj=tar_contents) as tgz:
564559
tgz.extractall("build")
565560

566-
print("Building freetype in {}".format(src_path))
567-
if sys.platform != 'win32':
568-
# compilation on all other platforms than windows
561+
print(f"Building freetype in {src_path}")
562+
if sys.platform != 'win32': # compilation on non-windows
569563
env = {**os.environ,
570564
"CFLAGS": "{} -fPIC".format(os.environ.get("CFLAGS", ""))}
571565
subprocess.check_call(
@@ -575,16 +569,7 @@ def do_custom_build(self):
575569
subprocess.check_call(["make"], env=env, cwd=src_path)
576570
else:
577571
# compilation on windows
578-
shutil.rmtree(str(pathlib.Path(src_path, "objs")),
579-
ignore_errors=True)
580-
FREETYPE_BUILD_CMD = r"""
581-
call "%ProgramFiles%\Microsoft SDKs\Windows\v7.0\Bin\SetEnv.Cmd" ^
582-
/Release /{xXX} /xp
583-
call "{vcvarsall}" {xXX}
584-
set MSBUILD=C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe
585-
%MSBUILD% "builds\windows\{vc20xx}\freetype.sln" ^
586-
/t:Clean;Build /p:Configuration="Release";Platform={WinXX}
587-
"""
572+
shutil.rmtree(pathlib.Path(src_path, "objs"), ignore_errors=True)
588573
import distutils.msvc9compiler as msvc
589574
# FreeType has no build profile for 2014, so we don't bother.
590575
vc = 'vc2010'
@@ -594,18 +579,21 @@ def do_custom_build(self):
594579
if vcvarsall is None:
595580
raise RuntimeError('Microsoft VS 2010 required')
596581
cmdfile = pathlib.Path("build/build_freetype.cmd")
597-
cmdfile.write_text(FREETYPE_BUILD_CMD.format(
598-
vc20xx=vc, WinXX=WinXX, xXX=xXX, vcvarsall=vcvarsall))
582+
cmdfile.write_text(fr"""
583+
call "%ProgramFiles%\Microsoft SDKs\Windows\v7.0\Bin\SetEnv.Cmd" ^
584+
/Release /{xXX} /xp
585+
call "{vcvarsall}" {xXX}
586+
set MSBUILD=C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe
587+
%MSBUILD% "builds\windows\{vc}\freetype.sln" ^
588+
/t:Clean;Build /p:Configuration="Release";Platform={WinXX}
589+
""")
599590
subprocess.check_call([str(cmdfile.resolve())],
600591
shell=True, cwd=src_path)
601592
# Move to the corresponding Unix build path.
602-
pathlib.Path(src_path, "objs/.libs").mkdir()
593+
(src_path / "objs" / ".libs").mkdir()
603594
# Be robust against change of FreeType version.
604-
lib_path, = (pathlib.Path(src_path, "objs", vc, xXX)
605-
.glob("freetype*.lib"))
606-
shutil.copy2(
607-
str(lib_path),
608-
str(pathlib.Path(src_path, "objs/.libs/libfreetype.lib")))
595+
lib_path, = (src_path / "objs" / vc / xXX).glob("freetype*.lib")
596+
shutil.copy2(lib_path, src_path / "objs/.libs/libfreetype.lib")
609597

610598

611599
class FT2Font(SetupPackage):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy