def test_decide_paths_for_local_source(tmpdir):
    scm_source = fsutil.parse_path_or_url('.')
    with tmpdir.as_cwd():
        paths = fsutil.decide_paths(scm_source)
        assert paths.src_dir == tmpdir
        assert paths.build_dir == tmpdir.join('build')
        assert paths.dist_dir == tmpdir.join('dist')
        assert os.path.dirname(paths.zip_path) == tmpdir.join('dist')

        paths = fsutil.decide_paths(scm_source, work_dir='deploy')
        work_dir = tmpdir.join('deploy')
        assert paths.src_dir == tmpdir
        assert paths.build_dir == work_dir.join('build')
        assert paths.dist_dir == work_dir.join('dist')
        assert os.path.dirname(paths.zip_path) == work_dir.join('dist')
def test_decide_paths_for_github_url(tmpdir):
    git_url = 'https://github.com/ento/ento.git'
    scm_source = fsutil.parse_path_or_url(git_url)
    with tmpdir.as_cwd():
        paths = fsutil.decide_paths(scm_source)
        work_dir = tmpdir.join('vendor')
        assert paths.src_dir == work_dir.join('src', 'ento', 'ento')
        assert paths.build_dir == work_dir.join('build', 'ento', 'ento')
        assert paths.dist_dir == work_dir.join('dist')
        assert paths.zip_path == work_dir.join('dist', 'ento-ento.zip')

        paths = fsutil.decide_paths(scm_source, work_dir='deploy')
        work_dir = tmpdir.join('deploy')
        assert paths.src_dir == work_dir.join('src', 'ento', 'ento')
        assert paths.build_dir == work_dir.join('build', 'ento', 'ento')
        assert paths.dist_dir == work_dir.join('dist')
        assert paths.zip_path == work_dir.join('dist', 'ento-ento.zip')
Beispiel #3
0
def test_deps_file(tmpdir):
    with tmpdir.as_cwd():
        scm_source = fsutil.parse_path_or_url('https://gist.github.com/hello.git')
        paths = fsutil.decide_paths(scm_source)
        fsutil.ensure_dirs(paths)

        site_packages_path = py.path.local(paths.build_dir).join(
            'env', 'lib', 'python2.7', 'site-packages')

        fsutil.mkdir_p(str(site_packages_path))

        site_packages_path.mkdir('mypackage').join('hello.txt').write('hello')

        deps_file = py.path.local('deps.txt')
        deps_file.write('\n'.join(['mypackage/hello.txt', '../ghost']))

        archive.make_archive(paths, deps_file=str(deps_file))

        with ZipFile(paths.zip_path) as zipfile:
            assert len(zipfile.namelist()) == 1
            with zipfile.open('mypackage/hello.txt') as f:
                assert f.read().decode('utf-8') == 'hello'
def test_repo_and_local_source_files(tmpdir):
    with tmpdir.as_cwd():
        scm_source = fsutil.parse_path_or_url('https://gist.github.com/hello.git')
        paths = fsutil.decide_paths(scm_source)
        fsutil.ensure_dirs(paths)
        fsutil.mkdir_p(paths.src_dir)

        with py.path.local(paths.src_dir).as_cwd():
            py.path.local().join('hello.txt').write('repo')

        # this should not be added to the archive
        py.path.local().join('hello.txt').write('local')

        archive.make_archive(paths, None, repo_source_files='hello.txt')

        with ZipFile(paths.zip_path) as zipfile:
            with zipfile.open('hello.txt') as f:
                assert f.read().decode('utf-8') == 'repo'

        archive.make_archive(paths, None, local_source_files=[('hello.txt', 'dest.txt')])

        with ZipFile(paths.zip_path) as zipfile:
            with zipfile.open('dest.txt') as f:
                assert f.read().decode('utf-8') == 'local'
Beispiel #5
0
def main(source, repo_source_files, requirements_file, local_source_file,
         work_dir, runtime):
    """
    Bundle up a deployment package for AWS Lambda.

    From your local filesystem:

    \b
        $ make-lambda-package .
        ...
        dist/lambda-package.zip

    Or from a remote git repository:

    \b
        $ make-lambda-package https://github.com/NoRedInk/make-lambda-package.git
        ...
        vendor/dist/NoRedInk-make-lambda-package.zip

    Use # fragment to specify a commit or a branch:

    \b
        $ make-lambda-package https://github.com/NoRedInk/make-lambda-package.git#v1.0.0

    Dependencies specified with --requirements-file will built using a docker container
    that replicates AWS Lambda's execution environment, so that extension modules
    are correctly packaged.

    When packaging a local source, --work-dir defaults to `.`:

    \b
    * ./build will hold a virtualenv for building dependencies if specified.
    * ./dist is where the zipped package will be saved

    When packaging a remote source, --work-dir defaults to `./vendor`.
    """
    scm_source = fsutil.parse_path_or_url(source)
    paths = fsutil.decide_paths(scm_source, work_dir)

    if requirements_file:
        with open(os.devnull, 'w') as devnull:
            docker_retcode = subprocess.call(['docker', '--help'],
                                             stdout=devnull)
        if docker_retcode != 0:
            raise click.UsageError(
                "`docker` command doesn't seem to be available. "
                "It's required to package dependencies.")

    if not (requirements_file or repo_source_files or local_source_file):
        click.secho(
            'Warning: without --repo-source-files, --requirements-file, '
            'or --local-source-file, nothing will be included in the zip file. '
            'Assuming you have good reasons to do this and proceeding.',
            fg='yellow')

    fsutil.ensure_dirs(paths)

    if isinstance(scm_source, fsutil.RemoteSource):
        click.echo('Fetching repo..')
        scm.fetch_repo(scm_source.url, scm_source.ref, paths.src_dir)

    deps_file = None
    if requirements_file:
        click.echo('Building deps..')
        deps_file = deps.build_deps(paths, requirements_file, runtime)

    click.echo('Creating zip file..')
    archive.make_archive(paths, runtime, repo_source_files, local_source_file,
                         deps_file)

    click.echo(os.path.relpath(paths.zip_path, os.getcwd()))
def test_parse_path_or_url_local_path():
    path = './ento/ento.git'
    assert fsutil.parse_path_or_url(path).path == path
def test_parse_path_or_url_commit_like():
    git_url = 'https://github.com/ento/ento.git'
    assert fsutil.parse_path_or_url(git_url).ref == 'master'
    assert fsutil.parse_path_or_url(git_url + '#').ref == 'master'
    assert fsutil.parse_path_or_url(git_url + '#f0a1b2').ref == 'f0a1b2'
    assert fsutil.parse_path_or_url(git_url + '#f0a1b2').url == git_url