def test_with_src_module_dir(): poetry = Poetry.create(project('source_package')) builder = SdistBuilder(poetry, NullVenv(), NullIO()) # Check setup.py setup = builder.build_setup() setup_ast = ast.parse(setup) setup_ast.body = [n for n in setup_ast.body if isinstance(n, ast.Assign)] ns = {} exec(compile(setup_ast, filename="setup.py", mode="exec"), ns) assert ns['package_dir'] == {'': 'src'} assert ns['packages'] == [ 'package_src', ] builder.build() sdist = fixtures_dir / 'source_package' / 'dist' / 'package-src-0.1.tar.gz' assert sdist.exists() tar = tarfile.open(str(sdist), 'r') assert 'package-src-0.1/src/package_src/__init__.py' in tar.getnames() assert 'package-src-0.1/src/package_src/module.py' in tar.getnames()
def test_make_setup(): poetry = Poetry.create(project("complete")) builder = SdistBuilder(poetry, NullEnv(), NullIO()) setup = builder.build_setup() setup_ast = ast.parse(setup) setup_ast.body = [n for n in setup_ast.body if isinstance(n, ast.Assign)] ns = {} exec(compile(setup_ast, filename="setup.py", mode="exec"), ns) assert ns["packages"] == [ "my_package", "my_package.sub_pkg1", "my_package.sub_pkg2", ] assert ns["install_requires"] == [ "cachy[msgpack]>=0.2.0,<0.3.0", "cleo>=0.6,<0.7" ] assert ns["entry_points"] == { "console_scripts": [ "extra-script = my_package.extra:main[time]", "my-2nd-script = my_package:main2", "my-script = my_package:main", ] } assert ns["extras_require"] == {"time": ["pendulum>=1.4,<2.0"]}
def test_make_setup(): poetry = Poetry.create(project('complete')) builder = SdistBuilder(poetry, NullVenv(), NullIO()) setup = builder.build_setup() setup_ast = ast.parse(setup) setup_ast.body = [n for n in setup_ast.body if isinstance(n, ast.Assign)] ns = {} exec(compile(setup_ast, filename="setup.py", mode="exec"), ns) assert ns['packages'] == [ 'my_package', 'my_package.sub_pkg1', 'my_package.sub_pkg2' ] assert ns['install_requires'] == [ 'cleo (>=0.6.0.0,<0.7.0.0)' ] assert ns['entry_points'] == { 'console_scripts': [ 'my-script = my_package:main', 'my-2nd-script = my_package:main2', ] } assert ns['extras_require'] == { 'time': [ 'pendulum (>=1.4.0.0,<2.0.0.0)' ] }
def test_with_src_module_dir(): poetry = Poetry.create(project("source_package")) builder = SdistBuilder(poetry, NullVenv(), NullIO()) # Check setup.py setup = builder.build_setup() setup_ast = ast.parse(setup) setup_ast.body = [n for n in setup_ast.body if isinstance(n, ast.Assign)] ns = {} exec(compile(setup_ast, filename="setup.py", mode="exec"), ns) assert ns["package_dir"] == {"": "src"} assert ns["packages"] == ["package_src"] builder.build() sdist = fixtures_dir / "source_package" / "dist" / "package-src-0.1.tar.gz" assert sdist.exists() tar = tarfile.open(str(sdist), "r") assert "package-src-0.1/src/package_src/__init__.py" in tar.getnames() assert "package-src-0.1/src/package_src/module.py" in tar.getnames()
def test_default_with_excluded_data(mocker): # Patch git module to return specific excluded files p = mocker.patch("poetry.vcs.git.Git.get_ignored_files") p.return_value = [ ( ( Path(__file__).parent / "fixtures" / "default_with_excluded_data" / "my_package" / "data" / "sub_data" / "data2.txt" ) .relative_to(project("default_with_excluded_data")) .as_posix() ) ] poetry = Factory().create_poetry(project("default_with_excluded_data")) builder = SdistBuilder(poetry, NullEnv(), NullIO()) # Check setup.py setup = builder.build_setup() setup_ast = ast.parse(setup) setup_ast.body = [n for n in setup_ast.body if isinstance(n, ast.Assign)] ns = {} exec(compile(setup_ast, filename="setup.py", mode="exec"), ns) assert "package_dir" not in ns assert ns["packages"] == ["my_package"] assert ns["package_data"] == { "": ["*"], "my_package": ["data/*", "data/sub_data/data3.txt"], } builder.build() sdist = ( fixtures_dir / "default_with_excluded_data" / "dist" / "my-package-1.2.3.tar.gz" ) assert sdist.exists() with tarfile.open(str(sdist), "r") as tar: names = tar.getnames() assert len(names) == len(set(names)) assert "my-package-1.2.3/LICENSE" in names assert "my-package-1.2.3/README.rst" in names assert "my-package-1.2.3/my_package/__init__.py" in names assert "my-package-1.2.3/my_package/data/data1.txt" in names assert "my-package-1.2.3/pyproject.toml" in names assert "my-package-1.2.3/setup.py" in names assert "my-package-1.2.3/PKG-INFO" in names # all last modified times should be set to a valid timestamp for tarinfo in tar.getmembers(): assert 0 < tarinfo.mtime
def test_package_with_include(mocker): # Patch git module to return specific excluded files p = mocker.patch("poetry.vcs.git.Git.get_ignored_files") p.return_value = [ str( Path(__file__).parent / "fixtures" / "with-include" / "extra_dir" / "vcs_excluded.txt" ), str( Path(__file__).parent / "fixtures" / "with-include" / "extra_dir" / "sub_pkg" / "vcs_excluded.txt" ), ] poetry = Poetry.create(project("with-include")) builder = SdistBuilder(poetry, NullEnv(), NullIO()) # Check setup.py setup = builder.build_setup() setup_ast = ast.parse(setup) setup_ast.body = [n for n in setup_ast.body if isinstance(n, ast.Assign)] ns = {} exec(compile(setup_ast, filename="setup.py", mode="exec"), ns) assert "package_dir" not in ns assert ns["packages"] == ["extra_dir", "extra_dir.sub_pkg", "package_with_include"] assert ns["package_data"] == {"": ["*"]} assert ns["modules"] == ["my_module"] builder.build() sdist = fixtures_dir / "with-include" / "dist" / "with-include-1.2.3.tar.gz" assert sdist.exists() with tarfile.open(str(sdist), "r") as tar: names = tar.getnames() assert len(names) == len(set(names)) assert "with-include-1.2.3/LICENSE" in names assert "with-include-1.2.3/README.rst" in names assert "with-include-1.2.3/extra_dir/__init__.py" in names assert "with-include-1.2.3/extra_dir/vcs_excluded.txt" in names assert "with-include-1.2.3/extra_dir/sub_pkg/__init__.py" in names assert "with-include-1.2.3/extra_dir/sub_pkg/vcs_excluded.txt" not in names assert "with-include-1.2.3/my_module.py" in names assert "with-include-1.2.3/notes.txt" in names assert "with-include-1.2.3/package_with_include/__init__.py" in names assert "with-include-1.2.3/pyproject.toml" in names assert "with-include-1.2.3/setup.py" in names assert "with-include-1.2.3/PKG-INFO" in names
def test_excluded_subpackage(): poetry = Factory().create_poetry(project("excluded_subpackage")) builder = SdistBuilder(poetry, NullEnv(), NullIO()) setup = builder.build_setup() setup_ast = ast.parse(setup) setup_ast.body = [n for n in setup_ast.body if isinstance(n, ast.Assign)] ns = {} exec(compile(setup_ast, filename="setup.py", mode="exec"), ns) assert ns["packages"] == ["example"]
def make_setup_if_not_exists(): ''' If python package without setup.py (for example Poetry) ''' if not os.path.exists('setup.py') and os.path.exists('setup.cfg'): from poetry.masonry.builders.sdist import SdistBuilder from poetry.factory import Factory factory = Factory() poetry = factory.create_poetry('.') sdist_builder = SdistBuilder(poetry, None, None) setuppy_blob = sdist_builder.build_setup() with open('setup.py', 'wb') as unit: unit.write(setuppy_blob) unit.write(b'\n# This setup.py was autogenerated using poetry.\n') pass
# poetry when there is no setup.py and an extension needs to be compiled. # See https://github.com/python-poetry/poetry/issues/1516. Running this # script creates a setup.py filled out with information generated by # poetry when parsing the pyproject.toml. import os import sys # If there is a global installation of poetry, prefer that. poetry_python_lib = os.path.expanduser('~/.poetry/lib') sys.path.append(os.path.realpath(poetry_python_lib)) try: from poetry.masonry.builders.sdist import SdistBuilder from poetry.factory import Factory except (ImportError, ModuleNotFoundError) as ee: raise ImportError('install poetry by doing pip install poetry to use ' f'this script: {ee}') # Generate a Poetry object that knows about the metadata in pyproject.toml factory = Factory() poetry = factory.create_poetry(os.path.dirname(__file__)) # Use the SdistBuilder to genrate a blob for setup.py sdist_builder = SdistBuilder(poetry, None, None) setuppy_blob = sdist_builder.build_setup() with open('setup.py', 'wb') as unit: unit.write(setuppy_blob) unit.write(b'\n# This setup.py was autogenerated using poetry.\n')
#!/usr/bin/env python3 import os import sys poetry_path = os.path.expanduser('~/.poetry/lib') sys.path.append(os.path.realpath(poetry_path)) from poetry.masonry.builders.sdist import SdistBuilder from poetry.factory import Factory poetry = Factory().create_poetry(os.getcwd()) builder = SdistBuilder(poetry, None, None) setup = builder.build_setup() with open('setup.py', 'wb') as fd: fd.write(setup)