Download a Python package and its dependencies for local use. Parameters ---------- python_version: str Python version to use. Ex: "3.8.5" or "3.9" package_name: str Name of the module to download. Ex: "msticpy" package_version: str Version of the mo
(
python_version: str, package_name: str, package_version: str, host_directory: str
)
| 20 | |
| 21 | |
| 22 | def download_python_package( |
| 23 | python_version: str, package_name: str, package_version: str, host_directory: str |
| 24 | ): |
| 25 | """ |
| 26 | Download a Python package and its dependencies for local use. |
| 27 | |
| 28 | Parameters |
| 29 | ---------- |
| 30 | python_version: str |
| 31 | Python version to use. Ex: "3.8.5" or "3.9" |
| 32 | package_name: str |
| 33 | Name of the module to download. Ex: "msticpy" |
| 34 | package_version: str |
| 35 | Version of the module to download. Ex: "1.0.0" |
| 36 | host_directory: str |
| 37 | Directory containing tar.gz files |
| 38 | |
| 39 | """ |
| 40 | os.makedirs(host_directory, exist_ok=True) |
| 41 | try: |
| 42 | # Generate a unique tag based on the current timestamp |
| 43 | image_tag = f"{python_version}:{int(time.time())}" |
| 44 | |
| 45 | # get base name if module name includes additional dependencies |
| 46 | module_base_name = package_name.split("[", maxsplit=1)[0] |
| 47 | |
| 48 | pipstring = f"{package_name}=={package_version}" if package_version else package_name |
| 49 | |
| 50 | # Define Dockerfile content |
| 51 | dockerfile_content = f""" |
| 52 | FROM python:{python_version} |
| 53 | |
| 54 | WORKDIR /{python_version} |
| 55 | |
| 56 | RUN apt-get update && \\ |
| 57 | apt-get install -y zip && \\ |
| 58 | rm -rf /var/lib/apt/lists/* |
| 59 | |
| 60 | ENV PACKAGE_NAME="{package_name}" |
| 61 | ENV PIP_STRING="{pipstring}" |
| 62 | |
| 63 | RUN pip download "$PIP_STRING" -d /{python_version} |
| 64 | |
| 65 | RUN for file in *.tar.gz; do \\ |
| 66 | if [ -f "$file" ]; then \\ |
| 67 | pip wheel "$file" -w /{python_version}; \\ |
| 68 | rm -f "$file"; \\ |
| 69 | fi; \\ |
| 70 | done |
| 71 | |
| 72 | |
| 73 | RUN zip -j /{python_version}/py{python_version}_$PACKAGE_NAME.zip /{python_version}/*.whl |
| 74 | |
| 75 | # Remove the wheel files |
| 76 | RUN rm -f /{python_version}/*.whl |
| 77 | RUN rm -f /{python_version}/*.tar.gz |
| 78 | |
| 79 | ENTRYPOINT ["echo", "Docker tasks completed."] |
no test coverage detected