Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 63 additions & 12 deletions aws_lambda/aws_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys
import time
from collections import defaultdict
import re

from shutil import copy
from shutil import copyfile
Expand Down Expand Up @@ -442,6 +443,50 @@ def get_handler_filename(handler):
return "{0}.py".format(module_name)


def _install_package(package, path):
""" Runs python pip install $(package) -t $(path) --ignore-installed command

:param str package:
Name of the package or directory to the local src
:param str path:
Path to copy installed pip packages to.
"""
subprocess.check_call(
[
sys.executable,
"-m",
"pip",
"install",
package,
"-t",
path,
"--ignore-installed",
]
)

def _install_local_package(package, path):
""" Install a local package and install it. Uses `pip show` to find the
local location
:param str package:
Name of the local package
:param str path:
Path to copy installed pip packages to.
"""
print(f"Running pip show {package}")
proc = subprocess.run([sys.executable, "-m", "pip", "show", package], stdout=subprocess.PIPE)
if not proc.stdout:
return False

out = str(proc.stdout, "utf-8")
captures = re.search("Location: ([^ \n]+)", out)
if not captures:
return False

directory = captures.groups()[0]
_install_package(directory, path)
return True


def _install_packages(path, packages):
"""Install all packages listed to the target directory.

Expand All @@ -463,26 +508,32 @@ def _filter_blacklist(package):
if package.startswith("-e "):
package = package.replace("-e ", "")


print("Installing {package}".format(package=package))
subprocess.check_call(
[
sys.executable,
"-m",
"pip",
"install",
package,
"-t",
path,
"--ignore-installed",
]
)
try:
_install_package(package, path)
except subprocess.CalledProcessError as e:
try:
# editable local package are can also be named as `-e git+git@...#egg=$(local_name)`
# this try attempts to find the egg name, and install it
captures = re.search("egg=([\w+_-]+)", package)
if not captures:
raise Exception()
alternative = captures.groups()[0]
if not _install_local_package(alternative, path):
raise Exception()

except Exception as e:
print(e)
print("Failed to install of {package} ".format(package=package))
print(
"Install directory contents are now: {directory}".format(
directory=os.listdir(path)
)
)



def pip_install_to_target(path, requirements=None, local_package=None):
"""For a given active virtualenv, gather all installed pip packages then
copy (re-install) them to the path provided.
Expand Down