Skip to content

Commit d3c3a08

Browse files
committed
Merge pull request rackerlabs#29 from martinb3/existing_virtualenv
Allow use of existing virtualenv
2 parents abf7e64 + 946cf3f commit d3c3a08

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ To specify an alternative profile that has been defined in `~/.aws/credentials`
4747
lambda-uploader --profile=alternative-profile
4848
```
4949

50+
To specify an alternative, prexisting virtualenv use the `--virtualenv` parameter.
51+
```shell
52+
lambda-uploader --virtualenv=~/.virtualenv/my_custom_virtualenv
53+
```
54+
5055
If you would prefer to upload another way you can tell the uploader to ignore the upload.
5156
This will create a package and leave it in the project directory.
5257
```shell

README.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ To specify an alternative profile that has been defined in
6363
6464
lambda-uploader --profile=alternative-profile
6565
66+
To specify an alternative, prexisting virtualenv use the ``--virtualenv`` parameter.
67+
68+
.. code:: shell
69+
70+
lambda-uploader --virtualenv=~/.virtualenv/my_custom_virtualenv
71+
6672
If you would prefer to upload another way you can tell the uploader to
6773
ignore the upload. This will create a package and leave it in the
6874
project directory.

lambda_uploader/package.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,38 @@
2626
ZIPFILE_NAME = 'lambda_function.zip'
2727

2828

29-
def build_package(path, requirements):
30-
pkg = Package(path)
29+
def build_package(path, requirements, virtualenv=None):
30+
pkg = Package(path, virtualenv)
3131

3232
pkg.clean_workspace()
3333
pkg.clean_zipfile()
3434
pkg.prepare_workspace()
35-
pkg.install_requirements(requirements)
35+
if virtualenv:
36+
if not os.path.isdir(virtualenv):
37+
raise Exception("supplied virtualenv %s not found" % virtualenv)
38+
LOG.info("Using existing virtualenv found in %s" % virtualenv)
39+
else:
40+
LOG.info('Building new virtualenv and installing requirements')
41+
pkg.prepare_virtualenv()
42+
pkg.install_requirements(requirements)
3643
pkg.package()
3744
return pkg
3845

3946

4047
class Package(object):
41-
def __init__(self, path):
48+
def __init__(self, path, virtualenv=None):
4249
self._path = path
4350
self._temp_workspace = os.path.join(path,
4451
TEMP_WORKSPACE_NAME)
4552
self.zip_file = os.path.join(path, ZIPFILE_NAME)
4653

47-
self._pkg_venv = os.path.join(self._temp_workspace, 'venv')
48-
self._venv_pip = 'bin/pip'
49-
if sys.platform == 'win32' or sys.platform == 'cygwin':
50-
self._venv_pip = 'Scripts\pip.exe'
54+
if virtualenv:
55+
self._pkg_venv = virtualenv
56+
else:
57+
self._pkg_venv = os.path.join(self._temp_workspace, 'venv')
58+
self._venv_pip = 'bin/pip'
59+
if sys.platform == 'win32' or sys.platform == 'cygwin':
60+
self._venv_pip = 'Scripts\pip.exe'
5161

5262
def clean_workspace(self):
5363
if os.path.isdir(self._temp_workspace):
@@ -61,6 +71,7 @@ def prepare_workspace(self):
6171
# Setup temporary workspace
6272
os.mkdir(self._temp_workspace)
6373

74+
def prepare_virtualenv(self):
6475
proc = Popen(["virtualenv", self._pkg_venv], stdout=PIPE, stderr=PIPE)
6576
stdout, stderr = proc.communicate()
6677
LOG.debug("Virtualenv stdout: %s" % stdout)

lambda_uploader/shell.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def _execute(args):
5050
cfg = config.Config(pth, args.config, role=args.role)
5151

5252
_print('Building Package')
53-
pkg = package.build_package(pth, cfg.requirements)
53+
pkg = package.build_package(pth, cfg.requirements, args.virtualenv)
5454

5555
if not args.no_clean:
5656
pkg.clean_workspace()
@@ -97,6 +97,9 @@ def main(arv=None):
9797
action='store_const',
9898
help='publish an upload to an immutable version',
9999
const=True)
100+
parser.add_argument('--virtualenv', '-e',
101+
help='use specified virtualenv instead of making one',
102+
default=None)
100103
parser.add_argument('--role', dest='role',
101104
default=getenv('LAMBDA_UPLOADER_ROLE'),
102105
help=('IAM role to assign the lambda function, '

test/test_package.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def test_prepare_workspace():
3939

4040
pkg = package.Package(TESTING_TEMP_DIR)
4141
pkg.prepare_workspace()
42+
pkg.prepare_virtualenv()
4243
assert path.isdir(temp_workspace)
4344
assert path.isdir(path.join(temp_workspace, 'venv'))
4445
if sys.platform == 'win32' or sys.platform == 'cygwin':
@@ -63,6 +64,11 @@ def test_install_requirements():
6364
assert path.isdir(path.join(site_packages, '_pytest'))
6465

6566

67+
def test_existing_virtualenv():
68+
pkg = package.Package(TESTING_TEMP_DIR, 'abc')
69+
assert pkg._pkg_venv == 'abc'
70+
71+
6672
def test_package():
6773
pkg = package.Package(TESTING_TEMP_DIR)
6874
pkg.package()

0 commit comments

Comments
 (0)