2
1

scanpypi 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. #!/usr/bin/env python
  2. """
  3. Utility for building Buildroot packages for existing PyPI packages
  4. Any package built by scanpypi should be manually checked for
  5. errors.
  6. """
  7. from __future__ import print_function
  8. from __future__ import absolute_import
  9. import argparse
  10. import json
  11. import six.moves.urllib.request
  12. import six.moves.urllib.error
  13. import six.moves.urllib.parse
  14. import sys
  15. import os
  16. import shutil
  17. import tarfile
  18. import zipfile
  19. import errno
  20. import hashlib
  21. import re
  22. import textwrap
  23. import tempfile
  24. import imp
  25. from functools import wraps
  26. from six.moves import map
  27. from six.moves import zip
  28. from six.moves import input
  29. if six.PY2:
  30. import StringIO
  31. else:
  32. import io
  33. BUF_SIZE = 65536
  34. try:
  35. import spdx_lookup as liclookup
  36. except ImportError:
  37. # spdx_lookup is not installed
  38. print('spdx_lookup module is not installed. This can lead to an '
  39. 'inaccurate licence detection. Please install it via\n'
  40. 'pip install spdx_lookup')
  41. liclookup = None
  42. def setup_decorator(func, method):
  43. """
  44. Decorator for distutils.core.setup and setuptools.setup.
  45. Puts the arguments with which setup is called as a dict
  46. Add key 'method' which should be either 'setuptools' or 'distutils'.
  47. Keyword arguments:
  48. func -- either setuptools.setup or distutils.core.setup
  49. method -- either 'setuptools' or 'distutils'
  50. """
  51. @wraps(func)
  52. def closure(*args, **kwargs):
  53. # Any python packages calls its setup function to be installed.
  54. # Argument 'name' of this setup function is the package's name
  55. BuildrootPackage.setup_args[kwargs['name']] = kwargs
  56. BuildrootPackage.setup_args[kwargs['name']]['method'] = method
  57. return closure
  58. # monkey patch
  59. import setuptools # noqa E402
  60. setuptools.setup = setup_decorator(setuptools.setup, 'setuptools')
  61. import distutils # noqa E402
  62. distutils.core.setup = setup_decorator(setuptools.setup, 'distutils')
  63. def find_file_upper_case(filenames, path='./'):
  64. """
  65. List generator:
  66. Recursively find files that matches one of the specified filenames.
  67. Returns a relative path starting with path argument.
  68. Keyword arguments:
  69. filenames -- List of filenames to be found
  70. path -- Path to the directory to search
  71. """
  72. for root, dirs, files in os.walk(path):
  73. for file in files:
  74. if file.upper() in filenames:
  75. yield (os.path.join(root, file))
  76. def pkg_buildroot_name(pkg_name):
  77. """
  78. Returns the Buildroot package name for the PyPI package pkg_name.
  79. Remove all non alphanumeric characters except -
  80. Also lowers the name and adds 'python-' suffix
  81. Keyword arguments:
  82. pkg_name -- String to rename
  83. """
  84. name = re.sub('[^\w-]', '', pkg_name.lower())
  85. prefix = 'python-'
  86. pattern = re.compile('^(?!' + prefix + ')(.+?)$')
  87. name = pattern.sub(r'python-\1', name)
  88. return name
  89. class DownloadFailed(Exception):
  90. pass
  91. class BuildrootPackage():
  92. """This class's methods are not meant to be used individually please
  93. use them in the correct order:
  94. __init__
  95. download_package
  96. extract_package
  97. load_module
  98. get_requirements
  99. create_package_mk
  100. create_hash_file
  101. create_config_in
  102. """
  103. setup_args = {}
  104. def __init__(self, real_name, pkg_folder):
  105. self.real_name = real_name
  106. self.buildroot_name = pkg_buildroot_name(self.real_name)
  107. self.pkg_dir = os.path.join(pkg_folder, self.buildroot_name)
  108. self.mk_name = self.buildroot_name.upper().replace('-', '_')
  109. self.as_string = None
  110. self.md5_sum = None
  111. self.metadata = None
  112. self.metadata_name = None
  113. self.metadata_url = None
  114. self.pkg_req = None
  115. self.setup_metadata = None
  116. self.tmp_extract = None
  117. self.used_url = None
  118. self.filename = None
  119. self.url = None
  120. self.version = None
  121. self.license_files = []
  122. def fetch_package_info(self):
  123. """
  124. Fetch a package's metadata from the python package index
  125. """
  126. self.metadata_url = 'https://pypi.org/pypi/{pkg}/json'.format(
  127. pkg=self.real_name)
  128. try:
  129. pkg_json = six.moves.urllib.request.urlopen(self.metadata_url).read().decode()
  130. except six.moves.urllib.error.HTTPError as error:
  131. print('ERROR:', error.getcode(), error.msg, file=sys.stderr)
  132. print('ERROR: Could not find package {pkg}.\n'
  133. 'Check syntax inside the python package index:\n'
  134. 'https://pypi.python.org/pypi/ '
  135. .format(pkg=self.real_name))
  136. raise
  137. except six.moves.urllib.error.URLError:
  138. print('ERROR: Could not find package {pkg}.\n'
  139. 'Check syntax inside the python package index:\n'
  140. 'https://pypi.python.org/pypi/ '
  141. .format(pkg=self.real_name))
  142. raise
  143. self.metadata = json.loads(pkg_json)
  144. self.version = self.metadata['info']['version']
  145. self.metadata_name = self.metadata['info']['name']
  146. def download_package(self):
  147. """
  148. Download a package using metadata from pypi
  149. """
  150. download = None
  151. try:
  152. self.metadata['urls'][0]['filename']
  153. except IndexError:
  154. print(
  155. 'Non-conventional package, ',
  156. 'please check carefully after creation')
  157. self.metadata['urls'] = [{
  158. 'packagetype': 'sdist',
  159. 'url': self.metadata['info']['download_url'],
  160. 'digests': None}]
  161. # In this case, we can't get the name of the downloaded file
  162. # from the pypi api, so we need to find it, this should work
  163. urlpath = six.moves.urllib.parse.urlparse(
  164. self.metadata['info']['download_url']).path
  165. # urlparse().path give something like
  166. # /path/to/file-version.tar.gz
  167. # We use basename to remove /path/to
  168. self.metadata['urls'][0]['filename'] = os.path.basename(urlpath)
  169. for download_url in self.metadata['urls']:
  170. if 'bdist' in download_url['packagetype']:
  171. continue
  172. try:
  173. print('Downloading package {pkg} from {url}...'.format(
  174. pkg=self.real_name, url=download_url['url']))
  175. download = six.moves.urllib.request.urlopen(download_url['url'])
  176. except six.moves.urllib.error.HTTPError as http_error:
  177. download = http_error
  178. else:
  179. self.used_url = download_url
  180. self.as_string = download.read()
  181. if not download_url['digests']['md5']:
  182. break
  183. self.md5_sum = hashlib.md5(self.as_string).hexdigest()
  184. if self.md5_sum == download_url['digests']['md5']:
  185. break
  186. if download is None:
  187. raise DownloadFailed('Failed to download package {pkg}: '
  188. 'No source archive available'
  189. .format(pkg=self.real_name))
  190. elif download.__class__ == six.moves.urllib.error.HTTPError:
  191. raise download
  192. self.filename = self.used_url['filename']
  193. self.url = self.used_url['url']
  194. def extract_package(self, tmp_path):
  195. """
  196. Extract the package contents into a directrory
  197. Keyword arguments:
  198. tmp_path -- directory where you want the package to be extracted
  199. """
  200. if six.PY2:
  201. as_file = StringIO.StringIO(self.as_string)
  202. else:
  203. as_file = io.BytesIO(self.as_string)
  204. if self.filename[-3:] == 'zip':
  205. with zipfile.ZipFile(as_file) as as_zipfile:
  206. tmp_pkg = os.path.join(tmp_path, self.buildroot_name)
  207. try:
  208. os.makedirs(tmp_pkg)
  209. except OSError as exception:
  210. if exception.errno != errno.EEXIST:
  211. print("ERROR: ", exception.strerror, file=sys.stderr)
  212. return
  213. print('WARNING:', exception.strerror, file=sys.stderr)
  214. print('Removing {pkg}...'.format(pkg=tmp_pkg))
  215. shutil.rmtree(tmp_pkg)
  216. os.makedirs(tmp_pkg)
  217. as_zipfile.extractall(tmp_pkg)
  218. pkg_filename = self.filename.split(".zip")[0]
  219. else:
  220. with tarfile.open(fileobj=as_file) as as_tarfile:
  221. tmp_pkg = os.path.join(tmp_path, self.buildroot_name)
  222. try:
  223. os.makedirs(tmp_pkg)
  224. except OSError as exception:
  225. if exception.errno != errno.EEXIST:
  226. print("ERROR: ", exception.strerror, file=sys.stderr)
  227. return
  228. print('WARNING:', exception.strerror, file=sys.stderr)
  229. print('Removing {pkg}...'.format(pkg=tmp_pkg))
  230. shutil.rmtree(tmp_pkg)
  231. os.makedirs(tmp_pkg)
  232. as_tarfile.extractall(tmp_pkg)
  233. pkg_filename = self.filename.split(".tar")[0]
  234. tmp_extract = '{folder}/{name}'
  235. self.tmp_extract = tmp_extract.format(
  236. folder=tmp_pkg,
  237. name=pkg_filename)
  238. def load_setup(self):
  239. """
  240. Loads the corresponding setup and store its metadata
  241. """
  242. current_dir = os.getcwd()
  243. os.chdir(self.tmp_extract)
  244. sys.path.append(self.tmp_extract)
  245. s_file, s_path, s_desc = imp.find_module('setup', [self.tmp_extract])
  246. setup = imp.load_module('setup', s_file, s_path, s_desc)
  247. try:
  248. self.setup_metadata = self.setup_args[self.metadata_name]
  249. except KeyError:
  250. # This means setup was not called which most likely mean that it is
  251. # called through the if __name__ == '__main__' directive.
  252. # In this case, we can only pray that it is called through a
  253. # function called main() in setup.py.
  254. setup.main() # Will raise AttributeError if not found
  255. self.setup_metadata = self.setup_args[self.metadata_name]
  256. # Here we must remove the module the hard way.
  257. # We must do this because of a very specific case: if a package calls
  258. # setup from the __main__ but does not come with a 'main()' function,
  259. # for some reason setup.main() will successfully call the main
  260. # function of a previous package...
  261. sys.modules.pop('setup', None)
  262. del setup
  263. os.chdir(current_dir)
  264. sys.path.remove(self.tmp_extract)
  265. def get_requirements(self, pkg_folder):
  266. """
  267. Retrieve dependencies from the metadata found in the setup.py script of
  268. a pypi package.
  269. Keyword Arguments:
  270. pkg_folder -- location of the already created packages
  271. """
  272. if 'install_requires' not in self.setup_metadata:
  273. self.pkg_req = None
  274. return set()
  275. self.pkg_req = self.setup_metadata['install_requires']
  276. self.pkg_req = [re.sub('([-.\w]+).*', r'\1', req)
  277. for req in self.pkg_req]
  278. # get rid of commented lines and also strip the package strings
  279. self.pkg_req = [item.strip() for item in self.pkg_req
  280. if len(item) > 0 and item[0] != '#']
  281. req_not_found = self.pkg_req
  282. self.pkg_req = list(map(pkg_buildroot_name, self.pkg_req))
  283. pkg_tuples = list(zip(req_not_found, self.pkg_req))
  284. # pkg_tuples is a list of tuples that looks like
  285. # ('werkzeug','python-werkzeug') because I need both when checking if
  286. # dependencies already exist or are already in the download list
  287. req_not_found = set(
  288. pkg[0] for pkg in pkg_tuples
  289. if not os.path.isdir(pkg[1])
  290. )
  291. return req_not_found
  292. def __create_mk_header(self):
  293. """
  294. Create the header of the <package_name>.mk file
  295. """
  296. header = ['#' * 80 + '\n']
  297. header.append('#\n')
  298. header.append('# {name}\n'.format(name=self.buildroot_name))
  299. header.append('#\n')
  300. header.append('#' * 80 + '\n')
  301. header.append('\n')
  302. return header
  303. def __create_mk_download_info(self):
  304. """
  305. Create the lines refering to the download information of the
  306. <package_name>.mk file
  307. """
  308. lines = []
  309. version_line = '{name}_VERSION = {version}\n'.format(
  310. name=self.mk_name,
  311. version=self.version)
  312. lines.append(version_line)
  313. targz = self.filename.replace(
  314. self.version,
  315. '$({name}_VERSION)'.format(name=self.mk_name))
  316. targz_line = '{name}_SOURCE = {filename}\n'.format(
  317. name=self.mk_name,
  318. filename=targz)
  319. lines.append(targz_line)
  320. if self.filename not in self.url:
  321. # Sometimes the filename is in the url, sometimes it's not
  322. site_url = self.url
  323. else:
  324. site_url = self.url[:self.url.find(self.filename)]
  325. site_line = '{name}_SITE = {url}'.format(name=self.mk_name,
  326. url=site_url)
  327. site_line = site_line.rstrip('/') + '\n'
  328. lines.append(site_line)
  329. return lines
  330. def __create_mk_setup(self):
  331. """
  332. Create the line refering to the setup method of the package of the
  333. <package_name>.mk file
  334. There are two things you can use to make an installer
  335. for a python package: distutils or setuptools
  336. distutils comes with python but does not support dependencies.
  337. distutils is mostly still there for backward support.
  338. setuptools is what smart people use,
  339. but it is not shipped with python :(
  340. """
  341. lines = []
  342. setup_type_line = '{name}_SETUP_TYPE = {method}\n'.format(
  343. name=self.mk_name,
  344. method=self.setup_metadata['method'])
  345. lines.append(setup_type_line)
  346. return lines
  347. def __get_license_names(self, license_files):
  348. """
  349. Try to determine the related license name.
  350. There are two possibilities. Either the script tries to
  351. get license name from package's metadata or, if spdx_lookup
  352. package is available, the script compares license files with
  353. SPDX database.
  354. """
  355. license_line = ''
  356. if liclookup is None:
  357. license_dict = {
  358. 'Apache Software License': 'Apache-2.0',
  359. 'BSD License': 'FIXME: please specify the exact BSD version',
  360. 'European Union Public Licence 1.0': 'EUPL-1.0',
  361. 'European Union Public Licence 1.1': 'EUPL-1.1',
  362. "GNU General Public License": "GPL",
  363. "GNU General Public License v2": "GPL-2.0",
  364. "GNU General Public License v2 or later": "GPL-2.0+",
  365. "GNU General Public License v3": "GPL-3.0",
  366. "GNU General Public License v3 or later": "GPL-3.0+",
  367. "GNU Lesser General Public License v2": "LGPL-2.1",
  368. "GNU Lesser General Public License v2 or later": "LGPL-2.1+",
  369. "GNU Lesser General Public License v3": "LGPL-3.0",
  370. "GNU Lesser General Public License v3 or later": "LGPL-3.0+",
  371. "GNU Library or Lesser General Public License": "LGPL-2.0",
  372. "ISC License": "ISC",
  373. "MIT License": "MIT",
  374. "Mozilla Public License 1.0": "MPL-1.0",
  375. "Mozilla Public License 1.1": "MPL-1.1",
  376. "Mozilla Public License 2.0": "MPL-2.0",
  377. "Zope Public License": "ZPL"
  378. }
  379. regexp = re.compile('^License :* *.* *:+ (.*)( \(.*\))?$')
  380. classifiers_licenses = [regexp.sub(r"\1", lic)
  381. for lic in self.metadata['info']['classifiers']
  382. if regexp.match(lic)]
  383. licenses = [license_dict[x] if x in license_dict else x for x in classifiers_licenses]
  384. if not len(licenses):
  385. print('WARNING: License has been set to "{license}". It is most'
  386. ' likely wrong, please change it if need be'.format(
  387. license=', '.join(licenses)))
  388. licenses = [self.metadata['info']['license']]
  389. license_line = '{name}_LICENSE = {license}\n'.format(
  390. name=self.mk_name,
  391. license=', '.join(licenses))
  392. else:
  393. license_names = []
  394. for license_file in license_files:
  395. with open(license_file) as lic_file:
  396. match = liclookup.match(lic_file.read())
  397. if match is not None and match.confidence >= 90.0:
  398. license_names.append(match.license.id)
  399. else:
  400. license_names.append("FIXME: license id couldn't be detected")
  401. if len(license_names) > 0:
  402. license_line = ('{name}_LICENSE ='
  403. ' {names}\n'.format(
  404. name=self.mk_name,
  405. names=', '.join(license_names)))
  406. return license_line
  407. def __create_mk_license(self):
  408. """
  409. Create the lines referring to the package's license informations of the
  410. <package_name>.mk file
  411. The license's files are found by searching the package (case insensitive)
  412. for files named license, license.txt etc. If more than one license file
  413. is found, the user is asked to select which ones he wants to use.
  414. """
  415. lines = []
  416. filenames = ['LICENCE', 'LICENSE', 'LICENSE.RST', 'LICENSE.TXT',
  417. 'COPYING', 'COPYING.TXT']
  418. self.license_files = list(find_file_upper_case(filenames, self.tmp_extract))
  419. lines.append(self.__get_license_names(self.license_files))
  420. license_files = [license.replace(self.tmp_extract, '')[1:]
  421. for license in self.license_files]
  422. if len(license_files) > 0:
  423. if len(license_files) > 1:
  424. print('More than one file found for license:',
  425. ', '.join(license_files))
  426. license_files = [filename
  427. for index, filename in enumerate(license_files)]
  428. license_file_line = ('{name}_LICENSE_FILES ='
  429. ' {files}\n'.format(
  430. name=self.mk_name,
  431. files=' '.join(license_files)))
  432. lines.append(license_file_line)
  433. else:
  434. print('WARNING: No license file found,'
  435. ' please specify it manually afterwards')
  436. license_file_line = '# No license file found\n'
  437. return lines
  438. def __create_mk_requirements(self):
  439. """
  440. Create the lines referring to the dependencies of the of the
  441. <package_name>.mk file
  442. Keyword Arguments:
  443. pkg_name -- name of the package
  444. pkg_req -- dependencies of the package
  445. """
  446. lines = []
  447. dependencies_line = ('{name}_DEPENDENCIES ='
  448. ' {reqs}\n'.format(
  449. name=self.mk_name,
  450. reqs=' '.join(self.pkg_req)))
  451. lines.append(dependencies_line)
  452. return lines
  453. def create_package_mk(self):
  454. """
  455. Create the lines corresponding to the <package_name>.mk file
  456. """
  457. pkg_mk = '{name}.mk'.format(name=self.buildroot_name)
  458. path_to_mk = os.path.join(self.pkg_dir, pkg_mk)
  459. print('Creating {file}...'.format(file=path_to_mk))
  460. lines = self.__create_mk_header()
  461. lines += self.__create_mk_download_info()
  462. lines += self.__create_mk_setup()
  463. lines += self.__create_mk_license()
  464. lines.append('\n')
  465. lines.append('$(eval $(python-package))')
  466. lines.append('\n')
  467. with open(path_to_mk, 'w') as mk_file:
  468. mk_file.writelines(lines)
  469. def create_hash_file(self):
  470. """
  471. Create the lines corresponding to the <package_name>.hash files
  472. """
  473. pkg_hash = '{name}.hash'.format(name=self.buildroot_name)
  474. path_to_hash = os.path.join(self.pkg_dir, pkg_hash)
  475. print('Creating {filename}...'.format(filename=path_to_hash))
  476. lines = []
  477. if self.used_url['digests']['md5'] and self.used_url['digests']['sha256']:
  478. hash_header = '# md5, sha256 from {url}\n'.format(
  479. url=self.metadata_url)
  480. lines.append(hash_header)
  481. hash_line = '{method}\t{digest} {filename}\n'.format(
  482. method='md5',
  483. digest=self.used_url['digests']['md5'],
  484. filename=self.filename)
  485. lines.append(hash_line)
  486. hash_line = '{method}\t{digest} {filename}\n'.format(
  487. method='sha256',
  488. digest=self.used_url['digests']['sha256'],
  489. filename=self.filename)
  490. lines.append(hash_line)
  491. if self.license_files:
  492. lines.append('# Locally computed sha256 checksums\n')
  493. for license_file in self.license_files:
  494. sha256 = hashlib.sha256()
  495. with open(license_file, 'rb') as lic_f:
  496. while True:
  497. data = lic_f.read(BUF_SIZE)
  498. if not data:
  499. break
  500. sha256.update(data)
  501. hash_line = '{method}\t{digest} {filename}\n'.format(
  502. method='sha256',
  503. digest=sha256.hexdigest(),
  504. filename=license_file.replace(self.tmp_extract, '')[1:])
  505. lines.append(hash_line)
  506. with open(path_to_hash, 'w') as hash_file:
  507. hash_file.writelines(lines)
  508. def create_config_in(self):
  509. """
  510. Creates the Config.in file of a package
  511. """
  512. path_to_config = os.path.join(self.pkg_dir, 'Config.in')
  513. print('Creating {file}...'.format(file=path_to_config))
  514. lines = []
  515. config_line = 'config BR2_PACKAGE_{name}\n'.format(
  516. name=self.mk_name)
  517. lines.append(config_line)
  518. bool_line = '\tbool "{name}"\n'.format(name=self.buildroot_name)
  519. lines.append(bool_line)
  520. if self.pkg_req:
  521. for dep in self.pkg_req:
  522. dep_line = '\tselect BR2_PACKAGE_{req} # runtime\n'.format(
  523. req=dep.upper().replace('-', '_'))
  524. lines.append(dep_line)
  525. lines.append('\thelp\n')
  526. help_lines = textwrap.wrap(self.metadata['info']['summary'], 62,
  527. initial_indent='\t ',
  528. subsequent_indent='\t ')
  529. # make sure a help text is terminated with a full stop
  530. if help_lines[-1][-1] != '.':
  531. help_lines[-1] += '.'
  532. # \t + two spaces is 3 char long
  533. help_lines.append('')
  534. help_lines.append('\t ' + self.metadata['info']['home_page'])
  535. help_lines = [x + '\n' for x in help_lines]
  536. lines += help_lines
  537. with open(path_to_config, 'w') as config_file:
  538. config_file.writelines(lines)
  539. def main():
  540. # Building the parser
  541. parser = argparse.ArgumentParser(
  542. description="Creates buildroot packages from the metadata of "
  543. "an existing PyPI packages and include it "
  544. "in menuconfig")
  545. parser.add_argument("packages",
  546. help="list of packages to be created",
  547. nargs='+')
  548. parser.add_argument("-o", "--output",
  549. help="""
  550. Output directory for packages.
  551. Default is ./package
  552. """,
  553. default='./package')
  554. args = parser.parse_args()
  555. packages = list(set(args.packages))
  556. # tmp_path is where we'll extract the files later
  557. tmp_prefix = 'scanpypi-'
  558. pkg_folder = args.output
  559. tmp_path = tempfile.mkdtemp(prefix=tmp_prefix)
  560. try:
  561. for real_pkg_name in packages:
  562. package = BuildrootPackage(real_pkg_name, pkg_folder)
  563. print('buildroot package name for {}:'.format(package.real_name),
  564. package.buildroot_name)
  565. # First we download the package
  566. # Most of the info we need can only be found inside the package
  567. print('Package:', package.buildroot_name)
  568. print('Fetching package', package.real_name)
  569. try:
  570. package.fetch_package_info()
  571. except (six.moves.urllib.error.URLError, six.moves.urllib.error.HTTPError):
  572. continue
  573. if package.metadata_name.lower() == 'setuptools':
  574. # setuptools imports itself, that does not work very well
  575. # with the monkey path at the begining
  576. print('Error: setuptools cannot be built using scanPyPI')
  577. continue
  578. try:
  579. package.download_package()
  580. except six.moves.urllib.error.HTTPError as error:
  581. print('Error: {code} {reason}'.format(code=error.code,
  582. reason=error.reason))
  583. print('Error downloading package :', package.buildroot_name)
  584. print()
  585. continue
  586. # extract the tarball
  587. try:
  588. package.extract_package(tmp_path)
  589. except (tarfile.ReadError, zipfile.BadZipfile):
  590. print('Error extracting package {}'.format(package.real_name))
  591. print()
  592. continue
  593. # Loading the package install info from the package
  594. try:
  595. package.load_setup()
  596. except ImportError as err:
  597. if 'buildutils' in err.message:
  598. print('This package needs buildutils')
  599. else:
  600. raise
  601. continue
  602. except AttributeError as error:
  603. print('Error: Could not install package {pkg}: {error}'.format(
  604. pkg=package.real_name, error=error))
  605. continue
  606. # Package requirement are an argument of the setup function
  607. req_not_found = package.get_requirements(pkg_folder)
  608. req_not_found = req_not_found.difference(packages)
  609. packages += req_not_found
  610. if req_not_found:
  611. print('Added packages \'{pkgs}\' as dependencies of {pkg}'
  612. .format(pkgs=", ".join(req_not_found),
  613. pkg=package.buildroot_name))
  614. print('Checking if package {name} already exists...'.format(
  615. name=package.pkg_dir))
  616. try:
  617. os.makedirs(package.pkg_dir)
  618. except OSError as exception:
  619. if exception.errno != errno.EEXIST:
  620. print("ERROR: ", exception.message, file=sys.stderr)
  621. continue
  622. print('Error: Package {name} already exists'
  623. .format(name=package.pkg_dir))
  624. del_pkg = input(
  625. 'Do you want to delete existing package ? [y/N]')
  626. if del_pkg.lower() == 'y':
  627. shutil.rmtree(package.pkg_dir)
  628. os.makedirs(package.pkg_dir)
  629. else:
  630. continue
  631. package.create_package_mk()
  632. package.create_hash_file()
  633. package.create_config_in()
  634. print()
  635. # printing an empty line for visual confort
  636. finally:
  637. shutil.rmtree(tmp_path)
  638. if __name__ == "__main__":
  639. main()