3333 from urllib .request import FancyURLopener
3434except ImportError :
3535 from urllib import FancyURLopener
36+ from urlparse import urlparse
3637
3738import argparse
3839from appdirs import user_data_dir
@@ -799,7 +800,7 @@ def prepare_build_environment(self, user_sdk_dir, user_ndk_dir,
799800 if not exists (self .ndk_platform ):
800801 warning ('ndk_platform doesn\' t exist' )
801802 ok = False
802-
803+
803804 virtualenv = None
804805 if virtualenv is None :
805806 virtualenv = sh .which ('virtualenv2' )
@@ -826,26 +827,48 @@ def prepare_build_environment(self, user_sdk_dir, user_ndk_dir,
826827 if not self .cython :
827828 ok = False
828829 warning ("Missing requirement: cython is not installed" )
829-
830+
830831 # Modify the path so that sh finds modules appropriately
831832 py_platform = sys .platform
832833 if py_platform in ['linux2' , 'linux3' ]:
833834 py_platform = 'linux'
834835 if self .ndk_ver == 'r5b' :
835836 toolchain_prefix = 'arm-eabi'
836- toolchain_version = '4.4.0'
837- elif self .ndk_ver [:2 ] in ('r7' , 'r8' ):
837+ elif self .ndk_ver [:2 ] in ('r7' , 'r8' , 'r9' ):
838838 toolchain_prefix = 'arm-linux-androideabi'
839- toolchain_version = '4.4.3'
840- elif self .ndk_ver [:2 ] == 'r9' :
841- toolchain_prefix = 'arm-linux-androideabi'
842- toolchain_version = '4.8'
843839 elif self .ndk_ver [:3 ] == 'r10' :
844840 toolchain_prefix = 'arm-linux-androideabi'
845- toolchain_version = '4.9'
846841 else :
847842 warning ('Error: NDK not supported by these tools?' )
848843 exit (1 )
844+
845+ toolchain_versions = []
846+ toolchain_path = join ( self .ndk_dir , 'toolchains' )
847+ if os .path .isdir (toolchain_path ):
848+ toolchain_contents = os .listdir (toolchain_path )
849+ for toolchain_content in toolchain_contents :
850+ if toolchain_content .startswith (toolchain_prefix ) and os .path .isdir (os .path .join (toolchain_path , toolchain_content )):
851+ toolchain_version = toolchain_content [len (toolchain_prefix )+ 1 :]
852+ debug ("Found toolchain version: %s" % (toolchain_version ))
853+ toolchain_versions .append (toolchain_version )
854+ else :
855+ warning ('Could not find toolchain subdirectory!' )
856+ ok = False
857+ toolchain_versions .sort ()
858+
859+ toolchain_versions_gcc = []
860+ for toolchain_version in toolchain_versions :
861+ if toolchain_version [0 ].isdigit (): # GCC toolchains begin with a number
862+ toolchain_versions_gcc .append (toolchain_version )
863+
864+ if toolchain_versions :
865+ info ('Found the following toolchain versions: %s' % (repr (toolchain_versions )))
866+ info ('Picking the latest gcc toolchain, here %s' % (repr (toolchain_versions_gcc [- 1 ])))
867+ toolchain_version = toolchain_versions_gcc [- 1 ]
868+ else :
869+ warning ('Could not find any toolchain for %s!' % (toolchain_prefix ))
870+ ok = False
871+
849872 self .toolchain_prefix = toolchain_prefix
850873 self .toolchain_version = toolchain_version
851874 environ ['PATH' ] = ('{ndk_dir}/toolchains/{toolchain_prefix}-{toolchain_version}/'
@@ -1338,51 +1361,74 @@ def versioned_url(self):
13381361 return None
13391362 return self .url .format (version = self .version )
13401363
1341- def download_file (self , url , filename , cwd = None ):
1364+ def download_file (self , url , target , cwd = None ):
13421365 """
1343- (internal) Download an ``url`` to a ``filename ``.
1366+ (internal) Download an ``url`` to a ``target ``.
13441367 """
13451368 if not url :
13461369 return
1347- def report_hook (index , blksize , size ):
1348- if size <= 0 :
1349- progression = '{0} bytes' .format (index * blksize )
1350- else :
1351- progression = '{0:.2f}%' .format (
1352- index * blksize * 100. / float (size ))
1353- stdout .write ('- Download {}\r ' .format (progression ))
1354- stdout .flush ()
1370+ info ('Downloading {} from {}' .format (self .name , url ))
13551371
13561372 if cwd :
1357- filename = join (cwd , filename )
1358- if exists (filename ):
1359- unlink (filename )
1373+ target = join (cwd , target )
13601374
1361- info ('Downloading {} from {}' .format (self .name , url ))
1362- urlretrieve (url , filename , report_hook )
1363- return filename
1375+ parsed_url = urlparse (url )
1376+ if parsed_url .scheme in ('http' , 'https' ):
1377+ def report_hook (index , blksize , size ):
1378+ if size <= 0 :
1379+ progression = '{0} bytes' .format (index * blksize )
1380+ else :
1381+ progression = '{0:.2f}%' .format (
1382+ index * blksize * 100. / float (size ))
1383+ stdout .write ('- Download {}\r ' .format (progression ))
1384+ stdout .flush ()
1385+
1386+ if exists (target ):
1387+ unlink (target )
1388+
1389+ urlretrieve (url , target , report_hook )
1390+ return target
1391+ elif parsed_url .scheme in ('git' ,):
1392+ if os .path .isdir (target ):
1393+ with current_directory (self .get_build_dir (arch .arch )):
1394+ shprint (sh .git , 'pull' , '--all' )
1395+ else :
1396+ shprint (sh .git , 'clone' , '--recursive' , url , target )
1397+ return target
13641398
1365- def extract_file (self , filename , cwd ):
1399+ def extract_source (self , source , cwd ):
13661400 """
1367- (internal) Extract the `filename ` into the directory `cwd`.
1401+ (internal) Extract the `source ` into the directory `cwd`.
13681402 """
1369- if not filename :
1403+ if not source :
13701404 return
1371- info ("Extract {} into {}" .format (filename , cwd ))
1372- if filename .endswith (".tgz" ) or filename .endswith (".tar.gz" ):
1373- shprint (sh .tar , "-C" , cwd , "-xvzf" , filename )
1405+ if os .path .isfile (source ):
1406+ info ("Extract {} into {}" .format (source , cwd ))
1407+
1408+ if source .endswith (".tgz" ) or source .endswith (".tar.gz" ):
1409+ shprint (sh .tar , "-C" , cwd , "-xvzf" , source )
1410+
1411+ elif source .endswith (".tbz2" ) or source .endswith (".tar.bz2" ):
1412+ shprint (sh .tar , "-C" , cwd , "-xvjf" , source )
1413+
1414+ elif source .endswith (".zip" ):
1415+ zf = zipfile .ZipFile (source )
1416+ zf .extractall (path = cwd )
1417+ zf .close ()
1418+
1419+ else :
1420+ warning ("Error: cannot extract, unrecognized extension for {}" .format (
1421+ source ))
1422+ raise Exception ()
13741423
1375- elif filename . endswith ( ".tbz2" ) or filename . endswith ( ".tar.bz2" ):
1376- shprint ( sh . tar , "-C" , cwd , "-xvjf" , filename )
1424+ elif os . path . isdir ( source ):
1425+ info ( "Copying {} into {}" . format ( source , cwd ) )
13771426
1378- elif filename .endswith (".zip" ):
1379- zf = zipfile .ZipFile (filename )
1380- zf .extractall (path = cwd )
1381- zf .close ()
1427+ shprint (sh .cp , '-a' , source , cwd )
13821428
13831429 else :
1384- warning ("Error: cannot extract, unrecognized extension for {}" .format (
1385- filename ))
1430+ warning ("Error: cannot extract or copy , unrecognized path {}" .format (
1431+ source ))
13861432 raise Exception ()
13871433
13881434 # def get_archive_rootdir(self, filename):
@@ -1606,32 +1652,37 @@ def unpack(self, arch):
16061652 # AND: Could use tito's get_archive_rootdir here
16071653 if not exists (directory_name ) or not isdir (directory_name ):
16081654 extraction_filename = join (self .ctx .packages_path , self .name , filename )
1609- if (extraction_filename .endswith ('.tar.gz' ) or
1610- extraction_filename .endswith ('.tgz' )):
1611- sh .tar ('xzf' , extraction_filename )
1612- root_directory = shprint (
1613- sh .tar , 'tzf' , extraction_filename ).stdout .decode (
1614- 'utf-8' ).split ('\n ' )[0 ].strip ('/' )
1615- if root_directory != directory_name :
1616- shprint (sh .mv , root_directory , directory_name )
1617- elif (extraction_filename .endswith ('.tar.bz2' ) or
1618- extraction_filename .endswith ('.tbz2' )):
1619- info ('Extracting {} at {}' .format (extraction_filename , filename ))
1620- sh .tar ('xjf' , extraction_filename )
1621- root_directory = sh .tar ('tjf' , extraction_filename ).stdout .decode (
1622- 'utf-8' ).split ('\n ' )[0 ].strip ('/' )
1623- if root_directory != directory_name :
1624- shprint (sh .mv , root_directory , directory_name )
1625- elif extraction_filename .endswith ('.zip' ):
1626- sh .unzip (extraction_filename )
1627- import zipfile
1628- fileh = zipfile .ZipFile (extraction_filename , 'r' )
1629- root_directory = fileh .filelist [0 ].filename .strip ('/' )
1630- if root_directory != directory_name :
1631- shprint (sh .mv , root_directory , directory_name )
1655+ if os .path .isfile (extraction_filename ):
1656+ if (extraction_filename .endswith ('.tar.gz' ) or
1657+ extraction_filename .endswith ('.tgz' )):
1658+ sh .tar ('xzf' , extraction_filename )
1659+ root_directory = shprint (
1660+ sh .tar , 'tzf' , extraction_filename ).stdout .decode (
1661+ 'utf-8' ).split ('\n ' )[0 ].strip ('/' )
1662+ if root_directory != directory_name :
1663+ shprint (sh .mv , root_directory , directory_name )
1664+ elif (extraction_filename .endswith ('.tar.bz2' ) or
1665+ extraction_filename .endswith ('.tbz2' )):
1666+ info ('Extracting {} at {}' .format (extraction_filename , filename ))
1667+ sh .tar ('xjf' , extraction_filename )
1668+ root_directory = sh .tar ('tjf' , extraction_filename ).stdout .decode (
1669+ 'utf-8' ).split ('\n ' )[0 ].strip ('/' )
1670+ if root_directory != directory_name :
1671+ shprint (sh .mv , root_directory , directory_name )
1672+ elif extraction_filename .endswith ('.zip' ):
1673+ sh .unzip (extraction_filename )
1674+ import zipfile
1675+ fileh = zipfile .ZipFile (extraction_filename , 'r' )
1676+ root_directory = fileh .filelist [0 ].filename .strip ('/' )
1677+ if root_directory != directory_name :
1678+ shprint (sh .mv , root_directory , directory_name )
1679+ else :
1680+ raise Exception ('Could not extract {} download, it must be .zip, '
1681+ '.tar.gz or .tar.bz2' )
1682+ elif os .path .isdir (extraction_filename ):
1683+ os .symlink (extraction_filename , directory_name )
16321684 else :
1633- raise Exception ('Could not extract {} download, it must be .zip, '
1634- '.tar.gz or .tar.bz2' )
1685+ raise Exception ('Given path is neither a file nor a directory: {}' .format (extraction_filename ))
16351686
16361687 else :
16371688 info ('{} is already unpacked, skipping' .format (self .name ))
0 commit comments