Skip to content

Commit

Permalink
Use python specified as runtime in the virtualenv
Browse files Browse the repository at this point in the history
  • Loading branch information
azatoth committed Apr 25, 2018
1 parent 9d5de3c commit 5c30b4b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
39 changes: 24 additions & 15 deletions lambda_uploader/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@


def build_package(path, requires, virtualenv=None, ignore=None,
extra_files=None, zipfile_name=ZIPFILE_NAME):
extra_files=None, zipfile_name=ZIPFILE_NAME,
pyexec=None):
'''Builds the zip file and creates the package with it'''
pkg = Package(path, zipfile_name)
pkg = Package(path, zipfile_name, pyexec)

if extra_files:
for fil in extra_files:
Expand All @@ -59,7 +60,7 @@ def create_package(path, zipfile_name=ZIPFILE_NAME):


class Package(object):
def __init__(self, path, zipfile_name=ZIPFILE_NAME):
def __init__(self, path, zipfile_name=ZIPFILE_NAME, pyexec=None):
self._path = path
self._temp_workspace = os.path.join(path,
TEMP_WORKSPACE_NAME)
Expand All @@ -68,6 +69,7 @@ def __init__(self, path, zipfile_name=ZIPFILE_NAME):
self._virtualenv = None
self._skip_virtualenv = False
self._requirements = None
self._pyexec = pyexec
self._requirements_file = os.path.join(self._path, "requirements.txt")
self._extra_files = []

Expand Down Expand Up @@ -172,7 +174,9 @@ def _build_new_virtualenv(self):
if sys.platform == 'win32' or sys.platform == 'cygwin':
self._venv_pip = 'Scripts\pip.exe'

proc = Popen(["virtualenv", "-p", _python_executable(),
python_exe = self._python_executable()

proc = Popen(["virtualenv", "-p", python_exe,
self._pkg_venv], stdout=PIPE, stderr=PIPE)
stdout, stderr = proc.communicate()
LOG.debug("Virtualenv stdout: %s" % stdout)
Expand All @@ -184,6 +188,22 @@ def _build_new_virtualenv(self):
else:
raise Exception('cannot build a new virtualenv when asked to omit')

def _python_executable(self):
if self._pyexec is not None:
python_exe = find_executable(self._pyexec)
if not python_exe:
raise Exception('Unable to locate {} executable'
.format(self._pyexec))
else:
python_exe = find_executable('python2')
if not python_exe:
python_exe = find_executable('python')

if not python_exe:
raise Exception('Unable to locate python executable')

return python_exe

def _install_requirements(self):
'''
Create a new virtualenvironment and install requirements
Expand Down Expand Up @@ -293,14 +313,3 @@ def _isfile(path):
if not path:
return False
return os.path.isfile(path)


def _python_executable():
python_exe = find_executable('python2')
if not python_exe:
python_exe = find_executable('python')

if not python_exe:
raise Exception('Unable to locate python executable')

return python_exe
2 changes: 1 addition & 1 deletion lambda_uploader/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def _execute(args):
if args.extra_files:
extra_files = args.extra_files
pkg = package.build_package(pth, requirements,
venv, cfg.ignore, extra_files)
venv, cfg.ignore, extra_files, pyexec=cfg.runtime)

if not args.no_clean:
pkg.clean_workspace()
Expand Down

0 comments on commit 5c30b4b

Please sign in to comment.