@@ -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
3939def 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
611599class FT2Font (SetupPackage ):
0 commit comments