Skip to content

Commit

Permalink
Merge pull request fluidattacks#1247 from dsalaza4/main
Browse files Browse the repository at this point in the history
feat(back): fluidattacks#1171 update make python lock
  • Loading branch information
dsalaza4 authored Jan 10, 2024
2 parents a948152 + 79ea498 commit 4b36aaa
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 142 deletions.
37 changes: 22 additions & 15 deletions docs/src/api/builtins/utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,36 @@ m github:fluidattacks/makes@24.01 /utils/makeNodeJsLock \

## makePythonLock

You can generate a `sourcesYaml` for
[makePythonPypiEnvironment](/api/extensions/python/#makepythonpypienvironment)
You can generate a `poetry.lock` for
[makePythonEnvironment](/api/extensions/python/#makepythonenvironment)
like this:

```bash
m github:fluidattacks/makes@24.01 /utils/makePythonLock \
"${python_version}" \
"${dependencies_yaml}" \
"${sources_yaml}"
m github:fluidattacks/makes@24.01 /utils/makePythonLock "${project}"
```

- Supported `python_version`s are: `3.9`, `3.10`, `3.11` and `3.12`.
- `dependencies_yaml` is the **absolute path** to a YAML file
mapping [PyPI](https://pypi.org/) packages to version constraints.
- `project` is the **absolute path** to a Python project
containing a `pyproject.toml` file.
Example:

```yaml
Django: "3.2.*"
psycopg2: "2.9.1"
```
```toml
[tool.poetry]
name = "test"
version = "0.1.0"
description = ""
authors = ["Your Name <you@example.com>"]
readme = "README.md"

- `sources_yaml` is the **absolute path**
to a file were the script will output results.
[tool.poetry.dependencies]
python = "^3.11"
Django = "3.2.0"
psycopg2 = "2.9.1"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
```

## makeRubyLock

Expand Down
119 changes: 8 additions & 111 deletions makes/utils/makePythonLock/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,119 +1,16 @@
# shellcheck shell=bash

function main {
declare -A depWithLinks
local python_version="${1}"
local deps_yaml="${2}"
local sources_yaml="${3:-sources.yaml}"
local pyproject_toml='{
"build-system": {
"build-backend": "poetry.core.masonry.api"
},
"build-system": {
"requires": [ "poetry-core>=1.0.0" ]
},
"tool": {
"poetry": {
"authors": [ ],
"dependencies": ($deps * { python: $python_version }),
"description": "",
"name": "pyproject-toml-for-make-python-pypi-mirror",
"version": "1"
}
}
}'
local implementations39=(any cp39 py2.py3 py3 source)
local implementations310=(any cp310 py2.py3 py3 source)
local implementations311=(any cp311 py2.py3 py3 source)
local implementations312=(any cp312 py2.py3 py3 source)
local project="${1}"

true \
&& case "${python_version}" in
3.9) python=__argPy39__ && implementations=("${implementations39[@]}") ;;
3.10) python=__argPy310__ && implementations=("${implementations310[@]}") ;;
3.11) python=__argPy311__ && implementations=("${implementations311[@]}") ;;
3.12) python=__argPy312__ && implementations=("${implementations312[@]}") ;;
*) critical Python version not supported: "${python_version}" ;;
esac \
&& info Generating manifest: \
: && if ! ls "${project}/pyproject.toml" &> /dev/null; then
error "Provided route must contain a pyproject.toml file"
fi \
&& info "Generating poetry.lock" \
&& cd "$(mktemp -d)" \
&& jq -enrS \
--argjson deps "$(yj -yj < "${deps_yaml}")" \
--arg python_version "${python_version}.*" \
"${pyproject_toml}" \
| yj -jt | tee pyproject.toml \
&& poetry env use "${python}/bin/python" \
&& poetry lock -vv \
&& yj -tj \
< poetry.lock > poetry.lock.json \
&& jq -erS '.package[] | "https://pypi.org/pypi/\(.name)/\(.version)/json"' \
< poetry.lock.json > endpoints.lst \
&& jq -erS \
--arg python_version "${python_version}" \
'{
closure: [ .package[] | {key: .name, value: .version} ] | from_entries,
links: [],
python: $python_version
}' \
< poetry.lock.json > sources.json \
&& mapfile -t endpoints < endpoints.lst \
&& for endpoint in "${endpoints[@]}"; do
curl -L "${endpoint}" -s | jq -erS .urls[] > data.json \
&& jq -erS .filename < data.json > files.lst \
&& jq -erS .python_version < data.json > python_versions.lst \
&& jq -erS .url < data.json > urls.lst \
&& mapfile -t files < files.lst \
&& mapfile -t python_versions < python_versions.lst \
&& mapfile -t urls < urls.lst \
&& for ((index = 0; index < "${#files[@]}"; index++)); do
file="${files[${index}]}" \
&& case "${file}" in
*.egg) continue ;;
*.exe) continue ;;
*-win32.whl) continue ;;
*-win_amd64.whl) continue ;;
*-win_arm64.whl) continue ;;
*) ;;
esac \
&& if ! in_array "${python_versions[${index}]}" "${implementations[@]}"; then
case "${file}" in
*-abi3-*.whl) ;;
*) continue ;;
esac
fi \
&& url="${urls[${index}]}" \
&& sha256="$(nix-prefetch-url --name "${file}" --type sha256 "${url}")" \
&& jq -ersS \
--arg file "${file}" \
--arg sha256 "${sha256}" \
--arg url "${url}" \
'.[0] | .links += [{
name: $file,
sha256: $sha256,
url: $url
}]' \
sources.json \
> sources2.json \
&& mv sources2.json sources.json \
&& IFS='/' read -ra ADDR <<< "$endpoint" \
&& depWithLinks["${ADDR[-3]}"]="" \
|| critical Unable to download "${file}"
done
done \
&& jq -erS \
--argjson depsWithLinks "$(echo "${!depWithLinks[@]}" | jq -Rc \
'split(" ") | map({(.): .}) | add')" \
'.closure |= with_entries(select(.key as $k | $depsWithLinks | has($k)))' \
sources.json > sources_filtered.json \
&& mv sources_filtered.json sources.json \
&& yj -jy < sources.json > "${sources_yaml}" \
&& info Generated a sources file at "${sources_yaml}"
}

function in_array {
for elem in "${@:2}"; do
if test "${1}" = "${elem}"; then return 0; fi
done && return 1
&& cp "${project}/pyproject.toml" . \
&& poetry lock \
&& cp "poetry.lock" "${project}/poetry.lock"
}

main "${@}"
18 changes: 2 additions & 16 deletions makes/utils/makePythonLock/main.nix
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
{
__nixpkgs__,
makePythonVersion,
makeScript,
...
}:
makeScript {
entrypoint = ./entrypoint.sh;
name = "python-pypi2nix";
replace = {
__argPy39__ = makePythonVersion "3.9";
__argPy310__ = makePythonVersion "3.10";
__argPy311__ = makePythonVersion "3.11";
__argPy312__ = makePythonVersion "3.12";
};
searchPaths.bin = [
__nixpkgs__.curl
__nixpkgs__.git
__nixpkgs__.jq
__nixpkgs__.nix
__nixpkgs__.poetry
__nixpkgs__.yj
];
name = "make-python-lock";
searchPaths.bin = [__nixpkgs__.poetry];
}

0 comments on commit 4b36aaa

Please sign in to comment.