@@ -86,7 +86,7 @@ def download_or_cache(url, sha):
86
86
file_sha = _get_hash (data )
87
87
if file_sha != sha :
88
88
raise Exception (
89
- f"The download file does not match the expected sha. { url } was "
89
+ f"The downloaded file does not match the expected sha. { url } was "
90
90
f"expected to have { sha } but it had { file_sha } " )
91
91
92
92
if cache_dir is not None : # Try to cache the downloaded file.
@@ -100,6 +100,31 @@ def download_or_cache(url, sha):
100
100
return BytesIO (data )
101
101
102
102
103
+ def get_and_extract_tarball (urls , sha , dirname ):
104
+ toplevel = Path ("build" , dirname )
105
+ if not toplevel .exists (): # Download it or load it from cache.
106
+ Path ("build" ).mkdir (exist_ok = True )
107
+ for url in urls :
108
+ try :
109
+ tar_contents = download_or_cache (url , sha )
110
+ break
111
+ except Exception :
112
+ pass
113
+ else :
114
+ raise IOError (
115
+ f"Failed to download any of the following: { urls } . "
116
+ f"Please download one of these urls and extract it into "
117
+ f"'build/' at the top-level of the source repository." )
118
+ print ("Extracting {}" .format (urllib .parse .urlparse (url ).path ))
119
+ with tarfile .open (fileobj = tar_contents , mode = "r:gz" ) as tgz :
120
+ if os .path .commonpath (tgz .getnames ()) != dirname :
121
+ raise IOError (
122
+ f"The downloaded tgz file was expected to have { dirname } "
123
+ f"as sole top-level directory, but that is not the case" )
124
+ tgz .extractall ("build" )
125
+ return toplevel
126
+
127
+
103
128
# SHA256 hashes of the FreeType tarballs
104
129
_freetype_hashes = {
105
130
'2.6.1' :
@@ -515,7 +540,7 @@ def add_qhull_flags(ext):
515
540
if options .get ("system_qhull" ):
516
541
ext .libraries .append ("qhull" )
517
542
else :
518
- qhull_path = Path (f'extern /qhull-{ LOCAL_QHULL_VERSION } /src' )
543
+ qhull_path = Path (f'build /qhull-{ LOCAL_QHULL_VERSION } /src' )
519
544
ext .include_dirs .insert (0 , str (qhull_path ))
520
545
ext .sources .extend (map (str , sorted (qhull_path .glob ('libqhull_r/*.c' ))))
521
546
if sysconfig .get_config_var ("LIBM" ) == "-lm" :
@@ -560,46 +585,24 @@ def do_custom_build(self, env):
560
585
if options .get ('system_freetype' ):
561
586
return
562
587
563
- src_path = Path ('build' , f'freetype-{ LOCAL_FREETYPE_VERSION } ' )
588
+ tarball = f'freetype-{ LOCAL_FREETYPE_VERSION } .tar.gz'
589
+ src_path = get_and_extract_tarball (
590
+ urls = [
591
+ (f'https://downloads.sourceforge.net/project/freetype'
592
+ f'/freetype2/{ LOCAL_FREETYPE_VERSION } /{ tarball } ' ),
593
+ (f'https://download.savannah.gnu.org/releases/freetype'
594
+ f'/{ tarball } ' )
595
+ ],
596
+ sha = LOCAL_FREETYPE_HASH ,
597
+ dirname = f'freetype-{ LOCAL_FREETYPE_VERSION } ' ,
598
+ )
564
599
565
- # We've already built freetype
566
600
if sys .platform == 'win32' :
567
601
libfreetype = 'libfreetype.lib'
568
602
else :
569
603
libfreetype = 'libfreetype.a'
570
-
571
- # bailing because it is already built
572
604
if (src_path / 'objs' / '.libs' / libfreetype ).is_file ():
573
- return
574
-
575
- # do we need to download / load the source from cache?
576
- if not src_path .exists ():
577
- os .makedirs ('build' , exist_ok = True )
578
-
579
- tarball = f'freetype-{ LOCAL_FREETYPE_VERSION } .tar.gz'
580
- target_urls = [
581
- (f'https://downloads.sourceforge.net/project/freetype'
582
- f'/freetype2/{ LOCAL_FREETYPE_VERSION } /{ tarball } ' ),
583
- (f'https://download.savannah.gnu.org/releases/freetype'
584
- f'/{ tarball } ' )
585
- ]
586
-
587
- for tarball_url in target_urls :
588
- try :
589
- tar_contents = download_or_cache (tarball_url ,
590
- LOCAL_FREETYPE_HASH )
591
- break
592
- except Exception :
593
- pass
594
- else :
595
- raise IOError (
596
- f"Failed to download FreeType. Please download one of "
597
- f"{ target_urls } and extract it into { src_path } at the "
598
- f"top-level of the source repository." )
599
-
600
- print (f"Extracting { tarball } " )
601
- with tarfile .open (fileobj = tar_contents , mode = "r:gz" ) as tgz :
602
- tgz .extractall ("build" )
605
+ return # Bail out because we have already built FreeType.
603
606
604
607
print (f"Building freetype in { src_path } " )
605
608
if sys .platform != 'win32' : # compilation on non-windows
0 commit comments