Skip to content
This repository was archived by the owner on Mar 15, 2026. It is now read-only.
Closed
Changes from 1 commit
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
Prev Previous commit
Fix case where local package name is the same as package on pip registry
  • Loading branch information
dominusmi committed May 14, 2021
commit 532667fcefeec0221758a43ecb6c1e4976b3db40
49 changes: 40 additions & 9 deletions aws_lambda/aws_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,13 @@ def get_handler_filename(handler):


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,
Expand All @@ -457,6 +464,29 @@ def _install_package(package, path):
]
)

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 Down Expand Up @@ -484,17 +514,18 @@ def _filter_blacklist(package):
_install_package(package, path)
except subprocess.CalledProcessError as e:
try:
print("Failed install of {package} ".format(package=package))
# 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.findall("egg=([\w+_-]+)", package)
if captures:
alternative = captures[0]
print("Trying to install with alternative name {alternative}".format(alternative=alternative))
_install_package(alternative, path)

except subprocess.CalledProcessError as e:
pass
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)
Expand Down