Commit 9c2f9bf7 authored by Chandan Singh's avatar Chandan Singh
Browse files

Merge branch 'chiaratolentino/fix-pip-source-regex' into 'master'

plugins/sources/pip.py: Accomodate characters '-','.','_' for packages

See merge request !914
parents 7f79b9ce 8d7cf806
Loading
Loading
Loading
Loading
Loading
+19 −4
Original line number Diff line number Diff line
@@ -96,7 +96,7 @@ _PYTHON_VERSIONS = [
# Names of source distribution archives must be of the form
# '%{package-name}-%{version}.%{extension}'.
_SDIST_RE = re.compile(
    r'^([a-zA-Z0-9]+?)-(.+).(?:tar|tar.bz2|tar.gz|tar.xz|tar.Z|zip)$',
    r'^([\w.-]+?)-((?:[\d.]+){2,})\.(?:tar|tar.bz2|tar.gz|tar.xz|tar.Z|zip)$',
    re.IGNORECASE)


@@ -225,12 +225,27 @@ class PipSource(Source):
    def _parse_sdist_names(self, basedir):
        reqs = []
        for f in os.listdir(basedir):
            pkg_match = _SDIST_RE.match(f)
            if pkg_match:
                reqs.append(pkg_match.groups())
            pkg = _match_package_name(f)
            if pkg is not None:
                reqs.append(pkg)

        return sorted(reqs)


# Extract the package name and version of a source distribution
#
# Args:
#    filename (str): Filename of the source distribution
#
# Returns:
#    (tuple): A tuple of (package_name, version)
#
def _match_package_name(filename):
    pkg_match = _SDIST_RE.match(filename)
    if pkg_match is None:
        return None
    return pkg_match.groups()


def setup():
    return PipSource
+35 −12
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ import pytest
from buildstream import _yaml

from tests.testutils import cli_integration as cli
from tests.testutils.python_repo import setup_pypi_repo
from tests.testutils.integration import assert_contains


@@ -17,12 +18,21 @@ DATA_DIR = os.path.join(


@pytest.mark.datafiles(DATA_DIR)
def test_pip_source_import(cli, tmpdir, datafiles):
def test_pip_source_import(cli, tmpdir, datafiles, setup_pypi_repo):
    project = os.path.join(datafiles.dirname, datafiles.basename)
    checkout = os.path.join(cli.directory, 'checkout')
    element_path = os.path.join(project, 'elements')
    element_name = 'pip/hello.bst'

    # check that exotically named packages are imported correctly
    myreqs_packages = ['hellolib']
    packages = ['app2', 'app.3', 'app-4', 'app_5', 'app.no.6', 'app-no-7', 'app_no_8']

    # create mock pypi repository
    pypi_repo = os.path.join(project, 'files', 'pypi-repo')
    os.makedirs(pypi_repo, exist_ok=True)
    setup_pypi_repo(myreqs_packages + packages, pypi_repo)

    element = {
        'kind': 'import',
        'sources': [
@@ -32,9 +42,9 @@ def test_pip_source_import(cli, tmpdir, datafiles):
            },
            {
                'kind': 'pip',
                'url': 'file://{}'.format(os.path.realpath(os.path.join(project, 'files', 'pypi-repo'))),
                'url': 'file://{}'.format(os.path.realpath(pypi_repo)),
                'requirements-files': ['myreqs.txt'],
                'packages': ['app2']
                'packages': packages
            }
        ]
    }
@@ -51,16 +61,31 @@ def test_pip_source_import(cli, tmpdir, datafiles):
    assert result.exit_code == 0

    assert_contains(checkout, ['/.bst_pip_downloads',
                               '/.bst_pip_downloads/HelloLib-0.1.tar.gz',
                               '/.bst_pip_downloads/App2-0.1.tar.gz'])
                               '/.bst_pip_downloads/hellolib-0.1.tar.gz',
                               '/.bst_pip_downloads/app2-0.1.tar.gz',
                               '/.bst_pip_downloads/app.3-0.1.tar.gz',
                               '/.bst_pip_downloads/app-4-0.1.tar.gz',
                               '/.bst_pip_downloads/app_5-0.1.tar.gz',
                               '/.bst_pip_downloads/app.no.6-0.1.tar.gz',
                               '/.bst_pip_downloads/app-no-7-0.1.tar.gz',
                               '/.bst_pip_downloads/app_no_8-0.1.tar.gz'])


@pytest.mark.datafiles(DATA_DIR)
def test_pip_source_build(cli, tmpdir, datafiles):
def test_pip_source_build(cli, tmpdir, datafiles, setup_pypi_repo):
    project = os.path.join(datafiles.dirname, datafiles.basename)
    element_path = os.path.join(project, 'elements')
    element_name = 'pip/hello.bst'

    # check that exotically named packages are imported correctly
    myreqs_packages = ['hellolib']
    packages = ['app2', 'app.3', 'app-4', 'app_5', 'app.no.6', 'app-no-7', 'app_no_8']

    # create mock pypi repository
    pypi_repo = os.path.join(project, 'files', 'pypi-repo')
    os.makedirs(pypi_repo, exist_ok=True)
    setup_pypi_repo(myreqs_packages + packages, pypi_repo)

    element = {
        'kind': 'manual',
        'depends': ['base.bst'],
@@ -71,15 +96,14 @@ def test_pip_source_build(cli, tmpdir, datafiles):
            },
            {
                'kind': 'pip',
                'url': 'file://{}'.format(os.path.realpath(os.path.join(project, 'files', 'pypi-repo'))),
                'url': 'file://{}'.format(os.path.realpath(pypi_repo)),
                'requirements-files': ['myreqs.txt'],
                'packages': ['app2']
                'packages': packages
            }
        ],
        'config': {
            'install-commands': [
                'pip3 install --no-index --prefix %{install-root}/usr .bst_pip_downloads/*.tar.gz',
                'chmod +x app1.py',
                'install app1.py %{install-root}/usr/bin/'
            ]
        }
@@ -95,5 +119,4 @@ def test_pip_source_build(cli, tmpdir, datafiles):

    result = cli.run(project=project, args=['shell', element_name, '/usr/bin/app1.py'])
    assert result.exit_code == 0
    assert result.output == """Hello App1!
"""
    assert result.output == "Hello App1! This is hellolib\n"
−769 B

File deleted.

+0 −8
Original line number Diff line number Diff line
<html>
  <head>
    <title>Links for app1</title>
  </head>
  <body>
    <a href='App2-0.1.tar.gz'>App2-0.1.tar.gz</a><br />
  </body>
</html>
−734 B

File deleted.

Loading