Skip to content

Commit

Permalink
Trac #34454: sage --package create: Add option --source wheel
Browse files Browse the repository at this point in the history
Utility for creating wheel packages (#34450)

URL: https://trac.sagemath.org/34454
Reported by: mkoeppe
Ticket author(s): Matthias Koeppe
Reviewer(s): Kwankyu Lee
  • Loading branch information
Release Manager committed Sep 27, 2022
2 parents 4722394 + d18cfa4 commit 187ef46
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
20 changes: 18 additions & 2 deletions build/sage_bootstrap/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,20 @@ def fix_checksum(self, package_name):
def create(self, package_name, version=None, tarball=None, pkg_type=None, upstream_url=None,
description=None, license=None, upstream_contact=None, pypi=False, source='normal'):
"""
Create a normal package
Create a package
$ sage --package create foo --version 1.3 --tarball FoO-VERSION.tar.gz --type experimental
$ sage --package create scikit_spatial --pypi --type optional
$ sage --package create torch --pypi --source pip --type optional
$ sage --package create jupyterlab_markup --pypi --source wheel --type optional
"""
if '-' in package_name:
raise ValueError('package names must not contain dashes, use underscore instead')
if pypi:
pypi_version = PyPiVersion(package_name)
pypi_version = PyPiVersion(package_name, source=source)
if source == 'normal':
if not tarball:
# Guess the general format of the tarball name.
Expand All @@ -281,6 +289,14 @@ def create(self, package_name, version=None, tarball=None, pkg_type=None, upstre
# Use a URL from pypi.io instead of the specific URL received from the PyPI query
# because it follows a simple pattern.
upstream_url = 'https://pypi.io/packages/source/{0:1.1}/{0}/{1}'.format(package_name, tarball)
elif source == 'wheel':
if not tarball:
tarball = pypi_version.tarball.replace(pypi_version.version, 'VERSION')
if not tarball.endswith('-none-any.whl'):
raise ValueError('Only platform-independent wheels can be used for wheel packages, got {0}'.format(tarball))
if not version:
version = pypi_version.version
upstream_url = 'https://pypi.io/packages/py3/{0:1.1}/{0}/{1}'.format(package_name, tarball)
if not description:
description = pypi_version.summary
if not license:
Expand Down
2 changes: 1 addition & 1 deletion build/sage_bootstrap/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def make_parser():
'package_name', default=None, type=str,
help='Package name.')
parser_create.add_argument(
'--source', type=str, default='normal', help='Package source (one of normal, script, pip)')
'--source', type=str, default='normal', help='Package source (one of normal, wheel, script, pip)')
parser_create.add_argument(
'--version', type=str, default=None, help='Package version')
parser_create.add_argument(
Expand Down
7 changes: 6 additions & 1 deletion build/sage_bootstrap/creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ def set_python_data_and_scripts(self, pypi_package_name=None, source='normal'):
If ``source`` is ``"normal"``, write the files ``spkg-install.in`` and
``install-requires.txt``.
If ``source`` is ``"wheel"``, write the file ``install-requires.txt``.
If ``source`` is ``"pip"``, write the file ``requirements.txt``.
"""
if pypi_package_name is None:
Expand All @@ -99,10 +101,13 @@ def set_python_data_and_scripts(self, pypi_package_name=None, source='normal'):
f.write('cd src\nsdh_pip_install .\n')
with open(os.path.join(self.path, 'install-requires.txt'), 'w+') as f:
f.write('{0}\n'.format(pypi_package_name))
elif source == 'wheel':
with open(os.path.join(self.path, 'install-requires.txt'), 'w+') as f:
f.write('{0}\n'.format(pypi_package_name))
elif source == 'pip':
with open(os.path.join(self.path, 'requirements.txt'), 'w+') as f:
f.write('{0}\n'.format(pypi_package_name))
elif source == 'script':
pass
else:
raise ValueError('package source must be one of normal, script, or pip')
raise ValueError('package source must be one of normal, script, pip, or wheel')
14 changes: 9 additions & 5 deletions build/sage_bootstrap/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ class PyPiError(Exception):

class PyPiVersion(object):

def __init__(self, package_name):
def __init__(self, package_name, source='normal'):
self.name = package_name
self.json = self._get_json()
# Replace provided name with the canonical name
self.name = self.json['info']['name']
if source == 'wheel':
self.python_version = 'py3'
else:
self.python_version = 'source'

def _get_json(self):
response = urllib.urlopen(self.json_url)
Expand All @@ -65,19 +69,19 @@ def url(self):
Return the source url
"""
for download in self.json['urls']:
if download['python_version'] == 'source':
if download['python_version'] == self.python_version:
return download['url']
raise PyPiError('No source url for %s found', self.name)
raise PyPiError('No %s url for %s found', self.python_version, self.name)

@property
def tarball(self):
"""
Return the source tarball name
"""
for download in self.json['urls']:
if download['python_version'] == 'source':
if download['python_version'] == self.python_version:
return download['filename']
raise PyPiError('No source url for %s found', self.name)
raise PyPiError('No %s url for %s found', self.python_version, self.name)

@property
def package_url(self):
Expand Down
8 changes: 6 additions & 2 deletions src/doc/en/developer/packaging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ set the ``upstream_url`` field in ``checksums.ini`` described above.
For Python packages available from PyPI, you can use::
[user@localhost]$ sage -package create scikit_spatial --pypi --type optional
[user@localhost]$ sage --package create scikit_spatial --pypi --type optional
This automatically downloads the most recent version from PyPI and also
obtains most of the necessary information by querying PyPI.
Expand All @@ -1117,7 +1117,11 @@ in the file ``install-requires.txt``.
To create a pip package rather than a normal package, you can use::
[user@localhost]$ sage -package create scikit_spatial --pypi --source pip --type optional
[user@localhost]$ sage --package create scikit_spatial --pypi --source pip --type optional
To create a wheel package rather than a normal package, you can use::
[user@localhost]$ sage --package create scikit_spatial --pypi --source wheel --type optional
.. _section-manual-build:
Expand Down

0 comments on commit 187ef46

Please sign in to comment.