Skip to content

Instantly share code, notes, and snippets.

@joaodaher joaodaher/setup.py

Last active Mar 31, 2020
Embed
What would you like to do?
Pipfile to setup.py install_requires
import json
import os
from setuptools import find_packages, setup
def read(f):
return open(f, 'r', encoding='utf-8').read()
def parse_requirements():
def _as_setup(all_items, s):
requirements = []
dependencies = []
for name, version in all_items.items():
if isinstance(version, str):
if version == '*':
line = name
else:
line = f'{name}{version}'
requirements.append(line)
else:
extras = ','.join(version.get('extras', []))
extra_str = f'[{extras}]' if extras else ''
line = f'{name}{extra_str}'
v = version['version']
if v != '*':
line = f'{line}{v}'
requirements.append(line)
if 'index' in version:
repo_url = s[version['index']]
dependencies.append(f"{repo_url}/{name}")
return requirements, dependencies
pipfile_path = os.path.join(os.path.dirname(__file__), 'Pipfile')
with open(pipfile_path, 'r') as f:
parsed = _parse_toml(f.read())
sources = {item['name']: item['url'] for item in parsed['source']}
install_packages, install_dependencies = _as_setup(parsed['packages'], sources)
test_packages, test_dependencies = _as_setup(parsed['dev-packages'], sources)
return (
install_packages,
test_packages,
list(set(install_dependencies + test_dependencies)),
parsed['requires'],
)
def _parse_toml(s):
lines = iter(s.split('\n'))
def _parse_item(item):
key, value = item.split('=', 1)
value = value.strip()
if value.startswith('{'):
for tag in ['version', 'index', 'extras']:
value = value.replace(f'{tag} = ', f'"{tag}": ')
final_value = json.loads(value)
else:
final_value = value.strip().replace("\"", '').replace("'", '')
return {key.strip(): final_value}
def _parse_source():
_source = {}
while 1:
_line = next(lines)
if not _line:
break
_source.update(_parse_item(item=_line))
return _source
def _parse_packages():
_pkgs = {}
while 1:
_line = next(lines)
if not _line:
break
_pkgs.update(_parse_item(item=_line))
return _pkgs
sources = []
packages = {}
dev_packages = {}
require_system = {}
try:
while 1:
line = next(lines)
if line == '[[source]]':
sources.append(_parse_source())
elif line == '[packages]':
packages = _parse_packages()
elif line == '[dev-packages]':
dev_packages = _parse_packages()
elif line == '[requires]':
require_system = _parse_packages()
except StopIteration:
pass
return {
'source': sources,
'packages': packages,
'dev-packages': dev_packages,
'requires': require_system,
}
install, dev_install, dependencies, requires = parse_requirements()
setup(
name='<add pkg name here>',
version='<add version here>',
description='<Add description here>',
long_description=read('README.md'),
long_description_content_type='text/markdown',
author='Joao Daher',
author_email='joao@daher.dev',
packages=find_packages(exclude=['test_*']),
data_files=[('.', ['Pipfile'])],
include_package_data=True,
install_requires=install,
tests_require=dev_install,
dependency_links=dependencies,
python_requires=">=" + requires['python_version'],
zip_safe=False,
classifiers=[
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: ' + requires['python_version'],
]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.