@@ -33,7 +33,7 @@ def _get_xdg_cache_dir():
33
33
cache_dir = os .path .expanduser ('~/.cache' )
34
34
if cache_dir .startswith ('~/' ): # Expansion failed.
35
35
return None
36
- return os . path . join (cache_dir , 'matplotlib' )
36
+ return pathlib . Path (cache_dir , 'matplotlib' )
37
37
38
38
39
39
def get_fd_hash (fd ):
@@ -74,7 +74,7 @@ def download_or_cache(url, sha):
74
74
def get_from_cache (local_fn ):
75
75
if cache_dir is None :
76
76
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 ())
78
78
if get_fd_hash (buf ) != sha :
79
79
return None
80
80
buf .seek (0 )
@@ -83,11 +83,10 @@ def get_from_cache(local_fn):
83
83
def write_cache (local_fn , data ):
84
84
if cache_dir is None :
85
85
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 )
88
87
old_pos = data .tell ()
89
88
data .seek (0 )
90
- with open (cache_filename , "xb" ) as fout :
89
+ with open (cache_dir / local_fn , "xb" ) as fout :
91
90
fout .write (data .read ())
92
91
data .seek (old_pos )
93
92
@@ -107,10 +106,9 @@ def write_cache(local_fn, data):
107
106
file_sha = get_fd_hash (file_contents )
108
107
109
108
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 } " )
114
112
115
113
try :
116
114
write_cache (sha , file_contents )
@@ -483,17 +481,17 @@ class FreeType(SetupPackage):
483
481
def add_flags (self , ext ):
484
482
ext .sources .insert (0 , 'src/checkdep_freetype2.c' )
485
483
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 } ' )
488
486
# Statically link to the locally-built freetype.
489
487
# 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' ))
491
489
if sys .platform == 'win32' :
492
490
libfreetype = 'libfreetype.lib'
493
491
else :
494
492
libfreetype = 'libfreetype.a'
495
493
ext .extra_objects .insert (
496
- 0 , os . path . join (src_path , 'objs' , '.libs' , libfreetype ))
494
+ 0 , str (src_path / 'objs' / '.libs' / libfreetype ))
497
495
ext .define_macros .append (('FREETYPE_BUILD_TYPE' , 'local' ))
498
496
else :
499
497
pkg_config_setup_extension (
@@ -511,8 +509,7 @@ def do_custom_build(self):
511
509
if not options .get ('local_freetype' ):
512
510
return
513
511
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 } ' )
516
513
517
514
# We've already built freetype
518
515
if sys .platform == 'win32' :
@@ -521,12 +518,11 @@ def do_custom_build(self):
521
518
libfreetype = 'libfreetype.a'
522
519
523
520
# 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 ():
526
522
return
527
523
528
524
# do we need to download / load the source from cache?
529
- if not os . path . exists (src_path ):
525
+ if not src_path . exists ():
530
526
os .makedirs ('build' , exist_ok = True )
531
527
532
528
url_fmts = [
@@ -535,7 +531,7 @@ def do_custom_build(self):
535
531
('https://download.savannah.gnu.org/releases/freetype'
536
532
'/{tarball}' )
537
533
]
538
- tarball = 'freetype-{0 }.tar.gz' . format ( LOCAL_FREETYPE_VERSION )
534
+ tarball = f 'freetype-{ LOCAL_FREETYPE_VERSION } .tar.gz'
539
535
540
536
target_urls = [
541
537
url_fmt .format (version = LOCAL_FREETYPE_VERSION ,
@@ -550,22 +546,20 @@ def do_custom_build(self):
550
546
except Exception :
551
547
pass
552
548
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." )
558
553
559
- print ("Extracting {}" . format ( tarball ) )
554
+ print (f "Extracting { tarball } " )
560
555
# just to be sure
561
556
tar_contents .seek (0 )
562
557
with tarfile .open (tarball , mode = "r:gz" ,
563
558
fileobj = tar_contents ) as tgz :
564
559
tgz .extractall ("build" )
565
560
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
569
563
env = {** os .environ ,
570
564
"CFLAGS" : "{} -fPIC" .format (os .environ .get ("CFLAGS" , "" ))}
571
565
subprocess .check_call (
@@ -575,16 +569,7 @@ def do_custom_build(self):
575
569
subprocess .check_call (["make" ], env = env , cwd = src_path )
576
570
else :
577
571
# 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 )
588
573
import distutils .msvc9compiler as msvc
589
574
# FreeType has no build profile for 2014, so we don't bother.
590
575
vc = 'vc2010'
@@ -594,18 +579,21 @@ def do_custom_build(self):
594
579
if vcvarsall is None :
595
580
raise RuntimeError ('Microsoft VS 2010 required' )
596
581
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
+ """ )
599
590
subprocess .check_call ([str (cmdfile .resolve ())],
600
591
shell = True , cwd = src_path )
601
592
# Move to the corresponding Unix build path.
602
- pathlib . Path (src_path , "objs/ .libs" ).mkdir ()
593
+ (src_path / "objs" / " .libs" ).mkdir ()
603
594
# 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" )
609
597
610
598
611
599
class FT2Font (SetupPackage ):
0 commit comments