Last active
October 29, 2015 10:43
-
-
Save dholth/e17f243085dcc539813f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
Build a pip-compatible sdist for the flit package in the current directory. | |
""" | |
import flit, zipfile, os.path | |
def build_sdist(): | |
# build wheel | |
flit.main(['wheel']) | |
# find newly created dist | |
wheel_filename = sorted(os.listdir('dist'), | |
key=lambda filename: os.stat(os.path.join('dist', filename)).st_mtime)[-1] | |
wheel_path = os.path.join('dist', wheel_filename) | |
def excluded(x): | |
return x.startswith('.') or x in ('dist', 'build', 'setup.py', __file__) | |
included = [x for x in os.listdir('.') if not excluded(x)] | |
sdist_name = '-'.join(wheel_filename.split('-')[:2]) + '.zip' | |
sdist_path = os.path.join('dist', sdist_name) | |
if os.path.isfile(sdist_path): | |
os.unlink(sdist_path) | |
zipfile.main(['-c', sdist_path] + included) | |
# Copy the .dist-info directory from the wheel into the sdist | |
with zipfile.ZipFile(sdist_path, 'a', zipfile.ZIP_DEFLATED) as sdist_zip, \ | |
zipfile.ZipFile(wheel_path, 'r') as wheel_zip: | |
for info in wheel_zip.filelist: | |
if info.filename.partition('/')[0].endswith('.dist-info'): | |
sdist_zip.writestr(info, wheel_zip.read(info.filename)) | |
sdist_zip.writestr('setup.py', SETUP_PY) | |
# PKG-INFO may also be required for pypi to recognize the archive | |
SETUP_PY = """\ | |
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# Adapted from https://bitbucket.org/dholth/setup-requires | |
import sys, os, subprocess, pkg_resources | |
sys.path[0:0] = ['setup-requires'] | |
pkg_resources.working_set.add_entry('setup-requires') | |
def get_requirements(): | |
specifiers = [ 'flit' ] | |
for specifier in specifiers: | |
try: | |
pkg_resources.require(specifier) | |
except pkg_resources.DistributionNotFound: | |
yield specifier | |
to_install = list(get_requirements()) | |
if to_install: | |
subprocess.call([sys.executable, "-m", "pip", "install", | |
"-t", "setup-requires"] + to_install) | |
# | |
# Implement the parts of setup.py used by pip, for backwards compatibility: | |
# | |
import shutil | |
from os.path import join | |
# Find this dist from a dist-info directory in the root of this sdist | |
dist = list(pkg_resources.find_on_path(None, '.'))[0] | |
PKG_INFO = \"""\ | |
Metadata-Version: 1.1 | |
Name: {} | |
Version: {} | |
\""" | |
def write_egg_info(base_path): | |
\""" | |
Write enough egg-info for pip to determine our requirements; just the | |
name, version, and the requirements for the current environment. | |
\""" | |
egg_info = '{}-{}.egg-info'.format(dist.project_name, dist.version) | |
egg_info_dir = join(base_path, egg_info) | |
os.mkdir(egg_info_dir) | |
with open(join(egg_info_dir, 'PKG-INFO'), 'wb+') as pkg_info: | |
pkg_info.write(PKG_INFO.format(dist.project_name, dist.version) | |
.encode('utf-8')) | |
with open(join(egg_info_dir, 'requires.txt'), 'wb+') as requires: | |
requires.write('\\n'.join(str(requirement) | |
for requirement in dist.requires()) | |
.encode('utf-8')) | |
if sys.argv[1] == 'egg_info': | |
# ['-c', 'egg_info', '--egg-base', 'pip-egg-info'] | |
egg_base = sys.argv[-1] | |
write_egg_info(egg_base) | |
elif sys.argv[1] == 'bdist_wheel': | |
# ['-c', 'bdist_wheel', '-d', '/random/temp/directory'] | |
import flit | |
flit.main(['wheel']) | |
for filename in os.listdir('dist'): | |
if filename.endswith('.whl'): | |
shutil.move(os.path.join('dist', filename), sys.argv[-1]) | |
else: | |
raise NotImplementedError() | |
""".encode('utf-8') | |
if __name__ == "__main__": | |
build_sdist() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# Adapted from https://bitbucket.org/dholth/setup-requires | |
import sys, os, subprocess, pkg_resources | |
sys.path[0:0] = ['setup-requires'] | |
pkg_resources.working_set.add_entry('setup-requires') | |
def get_requirements(): | |
specifiers = [ 'flit' ] | |
for specifier in specifiers: | |
try: | |
pkg_resources.require(specifier) | |
except pkg_resources.DistributionNotFound: | |
yield specifier | |
to_install = list(get_requirements()) | |
if to_install: | |
subprocess.call([sys.executable, "-m", "pip", "install", | |
"-t", "setup-requires"] + to_install) | |
# | |
# Implement the parts of setup.py used by pip, for backwards compatibility: | |
# | |
import shutil | |
from os.path import join | |
# Find this dist from a dist-info directory in the root of this sdist | |
dist = list(pkg_resources.find_on_path(None, '.'))[0] | |
PKG_INFO = """Metadata-Version: 1.1 | |
Name: {} | |
Version: {} | |
""" | |
def write_egg_info(base_path): | |
""" | |
Write enough egg-info for pip to determine our requirements; just the | |
name, version, and the requirements for the current environment. | |
""" | |
egg_info = '{}-{}.egg-info'.format(dist.project_name, dist.version) | |
egg_info_dir = join(base_path, egg_info) | |
os.mkdir(egg_info_dir) | |
with open(join(egg_info_dir, 'PKG-INFO'), 'wb+') as pkg_info: | |
pkg_info.write(PKG_INFO.format(dist.project_name, dist.version) | |
.encode('utf-8')) | |
with open(join(egg_info_dir, 'requires.txt'), 'wb+') as requires: | |
requires.write('\n'.join(str(requirement) | |
for requirement in dist.requires()) | |
.encode('utf-8')) | |
if sys.argv[1] == 'egg_info': | |
# ['-c', 'egg_info', '--egg-base', 'pip-egg-info'] | |
egg_base = sys.argv[-1] | |
write_egg_info(egg_base) | |
elif sys.argv[1] == 'bdist_wheel': | |
# ['-c', 'bdist_wheel', '-d', '/random/temp/directory'] | |
import flit | |
flit.main(['wheel']) | |
for filename in os.listdir('dist'): | |
if filename.endswith('.whl'): | |
shutil.move(os.path.join('dist', filename), sys.argv[-1]) | |
else: | |
raise NotImplementedError() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment