Created
January 30, 2019 16:09
-
-
Save wvxvw/030c309b6febf60016ac9b5d7ca82e00 to your computer and use it in GitHub Desktop.
Generic top-level setup.py
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 -*- | |
import os | |
import sys | |
import subprocess | |
import shutil | |
from tempfile import TemporaryDirectory | |
from glob import glob | |
from multiprocessing.dummy import Pool as ThreadPool | |
from setuptools import setup, Command | |
try: | |
from setuptools.package_index import parse_requirement_arg | |
except ImportError: | |
from setuptools.command.easy_install import parse_requirement_arg | |
from setuptools.command.install import install | |
try: | |
from pip._internal.commands.install import InstallCommand | |
except ImportError: | |
from pip.commands.install import InstallCommand | |
class Install(install): | |
''' | |
Customized setuptools install command which uses pip. | |
''' | |
def stream_logs(self, args): | |
cmd, cwd, prefix, td = args | |
process = subprocess.Popen( | |
cmd, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.STDOUT, | |
cwd=cwd, | |
) | |
for line in iter(process.stdout.readline, b''): | |
sys.stdout.buffer.write(prefix) | |
sys.stdout.buffer.write(line) | |
sdist = glob('{}/dist/*.tar.gz'.format(cwd))[0] | |
dst = os.path.join(td, os.path.basename(sdist)) | |
shutil.copy(sdist, dst) | |
return dst | |
def extract_package_names(self): | |
return [ | |
parse_requirement_arg(req).name | |
for req in self.distribution.install_requires | |
] | |
def run(self, *args, **kwargs): | |
requires = self.extract_package_names() | |
setup_dir = os.path.dirname(os.path.realpath(__file__)) | |
packages = glob('{}/packages/*'.format(setup_dir)) | |
pool = ThreadPool(len(packages)) | |
with TemporaryDirectory() as td: | |
cmd_argss = [ | |
( | |
['python', './setup.py', 'sdist'], | |
d, | |
(os.path.basename(d) + ': ').encode('utf-8'), | |
td, | |
) | |
for d in packages | |
if os.path.basename(d) in requires | |
] | |
sdists = ['--find-links', td] + pool.map(self.stream_logs, cmd_argss) | |
self.distribution.install_requires = [] | |
cmd = InstallCommand() | |
cmd.main(sdists) | |
super().run(*args, **kwargs) | |
setup( | |
packages=[], | |
name='$name', | |
version='0.0.1', | |
description='$description', | |
author='$author', | |
author_email='$email', | |
url='TBD', | |
license='PROPRIETARY', | |
cmdclass={ | |
'install': Install, | |
}, | |
dependency_links=[ | |
'file://' + os.path.join(os.getcwd(), 'packages'), | |
], | |
install_requires=[ | |
'$packages', | |
], | |
setup_requires=['wheel', 'pip >= 18.0'], | |
zip_safe=False, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment