Skip to content

Instantly share code, notes, and snippets.

@takluyver
Created February 4, 2016 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takluyver/7c66429a8e7f2b9388b0 to your computer and use it in GitHub Desktop.
Save takluyver/7c66429a8e7f2b9388b0 to your computer and use it in GitHub Desktop.
flit sdist
import io
from pathlib import Path
from posixpath import join as pjoin
import tarfile
from flit import common, inifile
SETUP = """\
#!/usr/bin/env python
from distutils.core import setup
setup(name={name!r},
version={version!r},
description={description!r},
author={author!r},
author_email={author_email!r},
url={url!r},
{extra}
)
"""
PKG_INFO = """\
Metadata-Version: 1.1
Name: {name}
Version: {version}
Summary: {summary}
Home-page: {home_page}
Author: {author}
Author-email: {author_email}
"""
def exclude_pycache(tarinfo):
if ('/__pycache__' in tarinfo.name) or tarinfo.name.endswith('.pyc'):
return None
return tarinfo
def make_sdist(ini_path=Path('flit.ini')):
ini_info = inifile.read_pkg_ini(ini_path)
module = common.Module(ini_info['module'], ini_path.parent)
metadata = common.make_metadata(module, ini_info)
target = ini_path.parent / 'dist' / '{}-{}.tar.gz'.format(metadata.name,
metadata.version)
tf = tarfile.open(str(target), mode='w:gz')
tf_dir = '{}-{}'.format(metadata.name, metadata.version)
srcdir = str(ini_path.parent)
tf.add(str(module.path), arcname=pjoin(tf_dir, module.path.name), filter=exclude_pycache)
tf.add(pjoin(srcdir, 'docs'), arcname=pjoin(tf_dir, 'docs'), filter=exclude_pycache)
tf.add(pjoin(srcdir, 'tests'), arcname=pjoin(tf_dir, 'tests'), filter=exclude_pycache)
tf.add(pjoin(srcdir, 'LICENSE'), arcname=pjoin(tf_dir, 'LICENSE'))
tf.add(pjoin(srcdir, 'README.rst'), arcname=pjoin(tf_dir, 'README.rst'))
tf.add(str(ini_path), arcname=pjoin(tf_dir, 'flit.ini'))
extra = []
if module.is_package:
# TODO: subpackages
extra.append("packages={!r}".format([module.name]))
else:
extra.append("py_modules={!r}".format([module.name]))
# TODO: Dependencies, scripts
setup_py = SETUP.format(
name=metadata.name,
version=metadata.version,
description=metadata.summary,
author=metadata.author,
author_email=metadata.author_email,
url=metadata.home_page,
extra='\n '.join(extra),
).encode('utf-8')
ti = tarfile.TarInfo(pjoin(tf_dir, 'setup.py'))
ti.size = len(setup_py)
tf.addfile(ti, io.BytesIO(setup_py))
pkg_info = PKG_INFO.format(
name=metadata.name,
version=metadata.version,
summary=metadata.summary,
home_page=metadata.home_page,
author=metadata.author,
author_email=metadata.author_email,
).encode('utf-8')
ti = tarfile.TarInfo(pjoin(tf_dir, 'PKG-INFO'))
ti.size = len(pkg_info)
tf.addfile(ti, io.BytesIO(pkg_info))
tf.close()
print("Built", target)
print("Upload with twine:")
print(" twine upload", target)
if __name__ == '__main__':
make_sdist()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment