Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 62 additions & 6 deletions src/python_inspector/setup_py_live_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@
#
"""Generate requirements from `setup.py` and `requirements-devel.txt`."""

import ast
import os
import re
import sys

try:
import configparser
except ImportError: # pragma: no cover
import ConfigParser as configparser

import distutils.core

import mock
import setuptools
from commoncode.command import pushd
Expand Down Expand Up @@ -54,11 +56,65 @@ def iter_requirements(level, extras, setup_file):
setup_requires = {}
# change directory to setup.py path
with pushd(os.path.dirname(setup_file)):
with mock.patch.object(setuptools, "setup") as mock_setup:
sys.path.append(os.path.dirname(setup_file))
g = {"__file__": setup_file, "__name__": "__main__"}
with open(setup_file) as sf:
exec(sf.read(), g)
with open(setup_file) as sf:
file_contents = sf.read()
node = ast.parse(file_contents)
asnames = {}
imports = []
for elem in ast.walk(node):
# save the asnames to parse aliases later
if isinstance(elem, ast.Import):
for n in elem.names:
asnames[(n.asname if n.asname is not None else n.name)] = n.name
for elem in ast.walk(node):
# for function imports, e.g. from setuptools import setup; setup()
if isinstance(elem, ast.ImportFrom) and "setup" in [e.name for e in elem.names]:
imports.append(elem.module)
# for module imports, e.g. import setuptools; setuptools.setup(...)
elif (
isinstance(elem, ast.Expr)
and isinstance(elem.value, ast.Call)
and isinstance(elem.value.func, ast.Attribute)
and isinstance(elem.value.func.value, ast.Name)
and elem.value.func.attr == "setup"
):
name = elem.value.func.value.id
if name in asnames.keys():
name = asnames[name]
imports.append(name)
# for module imports, e.g. import disttools.core; disttools.core.setup(...)
elif (
isinstance(elem, ast.Expr)
and isinstance(elem.value, ast.Call)
and isinstance(elem.value.func, ast.Attribute)
and isinstance(elem.value.func.value, ast.Attribute)
and elem.value.func.attr == "setup"
):
name = (
str(elem.value.func.value.value.id) + "." + str(elem.value.func.value.attr)
)
if name in asnames.keys():
name = asnames[name]
imports.append(name)
setup_providers = [i for i in imports if i in ["distutils.core", "setuptools"]]
if len(setup_providers) == 0:
print(
f"Warning: unable to recognize setup provider in {setup_file}: "
"defaulting to 'distutils.core'."
)
setup_provider = "distutils.core"
elif len(setup_providers) == 1:
setup_provider = setup_providers[0]
else:
print(
f"Warning: ambiguous setup provider in {setup_file}: candidates are {setup_providers}"
"defaulting to 'distutils.core'."
)
setup_provider = "distutils.core"
with mock.patch.object(eval(setup_provider), "setup") as mock_setup:
sys.path.append(os.path.dirname(setup_file))
g = {"__file__": setup_file, "__name__": "__main__"}
exec(file_contents, g)
sys.path.pop()
# removing the assertion `assert g["setup"]`` since this is not true for all cases
# for example when setuptools.setup() is called instead of setup()
Expand Down
45 changes: 23 additions & 22 deletions tests/data/azure-devops.req-310-expected.json

Large diffs are not rendered by default.

45 changes: 23 additions & 22 deletions tests/data/azure-devops.req-38-expected.json

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions tests/data/frozen-requirements.txt-expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -6013,12 +6013,12 @@
"type": "pypi",
"namespace": null,
"name": "pip",
"version": "23.0",
"version": "23.0.1",
"qualifiers": {},
"subpath": null,
"primary_language": "Python",
"description": "The PyPA recommended tool for installing Python packages.\npip - The Python Package Installer\n==================================\n\n.. image:: https://img.shields.io/pypi/v/pip.svg\n :target: https://pypi.org/project/pip/\n\n.. image:: https://readthedocs.org/projects/pip/badge/?version=latest\n :target: https://pip.pypa.io/en/latest\n\npip is the `package installer`_ for Python. You can use pip to install packages from the `Python Package Index`_ and other indexes.\n\nPlease take a look at our documentation for how to install and use pip:\n\n* `Installation`_\n* `Usage`_\n\nWe release updates regularly, with a new version every 3 months. Find more details in our documentation:\n\n* `Release notes`_\n* `Release process`_\n\nIn pip 20.3, we've `made a big improvement to the heart of pip`_; `learn more`_. We want your input, so `sign up for our user experience research studies`_ to help us do it right.\n\n**Note**: pip 21.0, in January 2021, removed Python 2 support, per pip's `Python 2 support policy`_. Please migrate to Python 3.\n\nIf you find bugs, need help, or want to talk to the developers, please use our mailing lists or chat rooms:\n\n* `Issue tracking`_\n* `Discourse channel`_\n* `User IRC`_\n\nIf you want to get involved head over to GitHub to get the source code, look at our development documentation and feel free to jump on the developer mailing lists and chat rooms:\n\n* `GitHub page`_\n* `Development documentation`_\n* `Development IRC`_\n\nCode of Conduct\n---------------\n\nEveryone interacting in the pip project's codebases, issue trackers, chat\nrooms, and mailing lists is expected to follow the `PSF Code of Conduct`_.\n\n.. _package installer: https://packaging.python.org/guides/tool-recommendations/\n.. _Python Package Index: https://pypi.org\n.. _Installation: https://pip.pypa.io/en/stable/installation/\n.. _Usage: https://pip.pypa.io/en/stable/\n.. _Release notes: https://pip.pypa.io/en/stable/news.html\n.. _Release process: https://pip.pypa.io/en/latest/development/release-process/\n.. _GitHub page: https://github.com/pypa/pip\n.. _Development documentation: https://pip.pypa.io/en/latest/development\n.. _made a big improvement to the heart of pip: https://pyfound.blogspot.com/2020/11/pip-20-3-new-resolver.html\n.. _learn more: https://pip.pypa.io/en/latest/user_guide/#changes-to-the-pip-dependency-resolver-in-20-3-2020\n.. _sign up for our user experience research studies: https://pyfound.blogspot.com/2020/03/new-pip-resolver-to-roll-out-this-year.html\n.. _Python 2 support policy: https://pip.pypa.io/en/latest/development/release-process/#python-2-support\n.. _Issue tracking: https://github.com/pypa/pip/issues\n.. _Discourse channel: https://discuss.python.org/c/packaging\n.. _User IRC: https://kiwiirc.com/nextclient/#ircs://irc.libera.chat:+6697/pypa\n.. _Development IRC: https://kiwiirc.com/nextclient/#ircs://irc.libera.chat:+6697/pypa-dev\n.. _PSF Code of Conduct: https://github.com/pypa/.github/blob/main/CODE_OF_CONDUCT.md",
"release_date": "2023-01-30T23:10:52",
"release_date": "2023-02-17T18:31:51",
"parties": [
{
"type": "person",
Expand All @@ -6044,11 +6044,11 @@
"Topic :: Software Development :: Build Tools"
],
"homepage_url": "https://pip.pypa.io/",
"download_url": "https://files.pythonhosted.org/packages/ab/43/508c403c38eeaa5fc86516eb13bb470ce77601b6d2bbcdb16e26328d0a15/pip-23.0-py3-none-any.whl",
"size": 2056044,
"download_url": "https://files.pythonhosted.org/packages/07/51/2c0959c5adf988c44d9e1e0d940f5b074516ecc87e96b1af25f59de9ba38/pip-23.0.1-py3-none-any.whl",
"size": 2055563,
"sha1": null,
"md5": "911a8561240e5b46c50b7e16834d42dd",
"sha256": "b5f88adff801f5ef052bcdef3daa31b55eb67b0fccd6d0106c206fa248e0463c",
"md5": "83b53b599a1644afe7e76debe4a62bcc",
"sha256": "236bcb61156d76c4b8a05821b988c7b8c35bf0da28a4b614e8d6ab5212c25c6f",
"sha512": null,
"bug_tracking_url": null,
"code_view_url": "https://github.com/pypa/pip",
Expand All @@ -6068,20 +6068,20 @@
"dependencies": [],
"repository_homepage_url": null,
"repository_download_url": null,
"api_data_url": "https://pypi.org/pypi/pip/23.0/json",
"api_data_url": "https://pypi.org/pypi/pip/23.0.1/json",
"datasource_id": null,
"purl": "pkg:pypi/[email protected]"
"purl": "pkg:pypi/[email protected].1"
},
{
"type": "pypi",
"namespace": null,
"name": "pip",
"version": "23.0",
"version": "23.0.1",
"qualifiers": {},
"subpath": null,
"primary_language": "Python",
"description": "The PyPA recommended tool for installing Python packages.\npip - The Python Package Installer\n==================================\n\n.. image:: https://img.shields.io/pypi/v/pip.svg\n :target: https://pypi.org/project/pip/\n\n.. image:: https://readthedocs.org/projects/pip/badge/?version=latest\n :target: https://pip.pypa.io/en/latest\n\npip is the `package installer`_ for Python. You can use pip to install packages from the `Python Package Index`_ and other indexes.\n\nPlease take a look at our documentation for how to install and use pip:\n\n* `Installation`_\n* `Usage`_\n\nWe release updates regularly, with a new version every 3 months. Find more details in our documentation:\n\n* `Release notes`_\n* `Release process`_\n\nIn pip 20.3, we've `made a big improvement to the heart of pip`_; `learn more`_. We want your input, so `sign up for our user experience research studies`_ to help us do it right.\n\n**Note**: pip 21.0, in January 2021, removed Python 2 support, per pip's `Python 2 support policy`_. Please migrate to Python 3.\n\nIf you find bugs, need help, or want to talk to the developers, please use our mailing lists or chat rooms:\n\n* `Issue tracking`_\n* `Discourse channel`_\n* `User IRC`_\n\nIf you want to get involved head over to GitHub to get the source code, look at our development documentation and feel free to jump on the developer mailing lists and chat rooms:\n\n* `GitHub page`_\n* `Development documentation`_\n* `Development IRC`_\n\nCode of Conduct\n---------------\n\nEveryone interacting in the pip project's codebases, issue trackers, chat\nrooms, and mailing lists is expected to follow the `PSF Code of Conduct`_.\n\n.. _package installer: https://packaging.python.org/guides/tool-recommendations/\n.. _Python Package Index: https://pypi.org\n.. _Installation: https://pip.pypa.io/en/stable/installation/\n.. _Usage: https://pip.pypa.io/en/stable/\n.. _Release notes: https://pip.pypa.io/en/stable/news.html\n.. _Release process: https://pip.pypa.io/en/latest/development/release-process/\n.. _GitHub page: https://github.com/pypa/pip\n.. _Development documentation: https://pip.pypa.io/en/latest/development\n.. _made a big improvement to the heart of pip: https://pyfound.blogspot.com/2020/11/pip-20-3-new-resolver.html\n.. _learn more: https://pip.pypa.io/en/latest/user_guide/#changes-to-the-pip-dependency-resolver-in-20-3-2020\n.. _sign up for our user experience research studies: https://pyfound.blogspot.com/2020/03/new-pip-resolver-to-roll-out-this-year.html\n.. _Python 2 support policy: https://pip.pypa.io/en/latest/development/release-process/#python-2-support\n.. _Issue tracking: https://github.com/pypa/pip/issues\n.. _Discourse channel: https://discuss.python.org/c/packaging\n.. _User IRC: https://kiwiirc.com/nextclient/#ircs://irc.libera.chat:+6697/pypa\n.. _Development IRC: https://kiwiirc.com/nextclient/#ircs://irc.libera.chat:+6697/pypa-dev\n.. _PSF Code of Conduct: https://github.com/pypa/.github/blob/main/CODE_OF_CONDUCT.md",
"release_date": "2023-01-30T23:10:56",
"release_date": "2023-02-17T18:31:56",
"parties": [
{
"type": "person",
Expand All @@ -6107,11 +6107,11 @@
"Topic :: Software Development :: Build Tools"
],
"homepage_url": "https://pip.pypa.io/",
"download_url": "https://files.pythonhosted.org/packages/b5/16/5e24bf63cff51dcc169f43bd43b86b005c49941e09cc3482a5b370db239e/pip-23.0.tar.gz",
"size": 2082372,
"download_url": "https://files.pythonhosted.org/packages/6b/8b/0b16094553ecc680e43ded8f920c3873b01b1da79a54274c98f08cb29fca/pip-23.0.1.tar.gz",
"size": 2082217,
"sha1": null,
"md5": "5d41b9679adae9362026b1753b543cb2",
"sha256": "aee438284e82c8def684b0bcc50b1f6ed5e941af97fa940e83e2e8ef1a59da9b",
"md5": "f988022baba70f483744cc8c0e584010",
"sha256": "cd015ea1bfb0fcef59d8a286c1f8bebcb983f6317719d415dc5351efb7cd7024",
"sha512": null,
"bug_tracking_url": null,
"code_view_url": "https://github.com/pypa/pip",
Expand All @@ -6131,9 +6131,9 @@
"dependencies": [],
"repository_homepage_url": null,
"repository_download_url": null,
"api_data_url": "https://pypi.org/pypi/pip/23.0/json",
"api_data_url": "https://pypi.org/pypi/pip/23.0.1/json",
"datasource_id": null,
"purl": "pkg:pypi/[email protected]"
"purl": "pkg:pypi/[email protected].1"
},
{
"type": "pypi",
Expand Down Expand Up @@ -10940,7 +10940,7 @@
{
"key": "pip",
"package_name": "pip",
"installed_version": "23.0",
"installed_version": "23.0.1",
"dependencies": []
}
]
Expand Down
40 changes: 40 additions & 0 deletions tests/data/setup-distutils-asnames.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Copyright 2018 Matthew Aynalem

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import distutils.core as dts, os
from setuptools import find_packages

dts.setup(
name='packer.py',
version='0.3.0',
author='Matthew Aynalem',
author_email='[email protected]',
packages=['packerpy'],
url='https://github.com/mayn/packer.py',
license='Apache License 2.0',
description='packer.py - python library to run hashicorp packer CLI commands',
keywords="hashicorp packer",
install_requires=[
],
classifiers=[
'License :: OSI Approved :: Apache Software License',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
],
)
40 changes: 40 additions & 0 deletions tests/data/setup-distutils-qualifiedfct.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Copyright 2018 Matthew Aynalem

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import distutils.core
from setuptools import find_packages

distutils.core.setup(
name='packer.py',
version='0.3.0',
author='Matthew Aynalem',
author_email='[email protected]',
packages=['packerpy'],
url='https://github.com/mayn/packer.py',
license='Apache License 2.0',
description='packer.py - python library to run hashicorp packer CLI commands',
keywords="hashicorp packer",
install_requires=[
],
classifiers=[
'License :: OSI Approved :: Apache Software License',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
],
)
40 changes: 40 additions & 0 deletions tests/data/setup-distutils.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Copyright 2018 Matthew Aynalem

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from distutils.core import setup
from setuptools import find_packages

setup(
name='packer.py',
version='0.3.0',
author='Matthew Aynalem',
author_email='[email protected]',
packages=['packerpy'],
url='https://github.com/mayn/packer.py',
license='Apache License 2.0',
description='packer.py - python library to run hashicorp packer CLI commands',
keywords="hashicorp packer",
install_requires=[
],
classifiers=[
'License :: OSI Approved :: Apache Software License',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
],
)
77 changes: 77 additions & 0 deletions tests/data/setup-qualifiedfct.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# -*- coding: utf-8 -*-
#
# This file is part of Requirements-Builder
# Copyright (C) 2015, 2016, 2017, 2018, 2019, 2020 CERN.
#
# Requirements-Builder is free software; you can redistribute it and/or
# modify it under the terms of the Revised BSD License; see LICENSE
# file for more details.
#
"""Build requirements files from setup.py requirements."""

import os

import setuptools

# Get the version string. Cannot be done with import!
g = {}

install_requires = [
'click>=6.1.0',
'mock>=1.3.0',
]

tests_require = [
'check-manifest>=0.25',
'coverage>=4.0',
'isort>=4.0.0',
'pydocstyle>=1.0.0',
'pytest-cache>=1.0',
'pytest-cov>=2.0.0',
'pytest-pep8>=1.0.6',
'pytest>=2.8.0',
]

extras_require = {
'docs': [
'Sphinx>=2.4',
],
'tests': tests_require,
}

extras_require['all'] = extras_require['tests'] + extras_require['docs']

setup_requires = ['pytest-runner>=2.6.2', ]

setuptools.setup(
name='requirements-builder',
version="0.1.0",
description=__doc__,
long_description='\n\n',
author="Invenio Collaboration",
author_email='[email protected]',
url='https://github.com/inveniosoftware/requirements-builder',
entry_points={
'console_scripts':
["requirements-builder = requirements_builder.cli:cli"]
},
packages=['requirements_builder', ],
include_package_data=True,
extras_require=extras_require,
install_requires=install_requires,
setup_requires=setup_requires,
tests_require=tests_require,
license='BSD',
zip_safe=False,
keywords='requirements-builder',
classifiers=[
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'Natural Language :: English',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
],
)
Loading