scanpypi 31 KB

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