Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: function to remove requirements.txt/setup.py constraints #21278

Closed
FRidh opened this issue Dec 19, 2016 · 8 comments
Closed

Python: function to remove requirements.txt/setup.py constraints #21278

FRidh opened this issue Dec 19, 2016 · 8 comments
Labels
0.kind: enhancement 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md 6.topic: python

Comments

@FRidh
Copy link
Member

FRidh commented Dec 19, 2016

Issue description

Python developers often pin their dependencies to exact versions. This is bad practice and most of the times absolutely not necessary either.

Typically we use substituteInPlace to remove the dependency requirement. It would be useful to have a function that would strip such exact requirements.

I've also opened an issue at setuptools with a request for a flag that ignores version pinning.
pypa/setuptools#894

@FRidh FRidh changed the title Python: function to remove requirements.txt/setup.py exact versions Python: function to remove requirements.txt/setup.py constraints Dec 19, 2016
@FRidh
Copy link
Member Author

FRidh commented Dec 21, 2016

Example why we should have this: #21321

@johbo
Copy link
Contributor

johbo commented Dec 22, 2016

I can see the need in regards to the setup.py file. I've myself sometimes patched away a specific version pinning in a setup.py.

How do you see the use related to a requirements.txt file? Wouldn't this typically be used in a scenario to generate nix code e.g. via pip2nix or pypi2nix?

@FRidh
Copy link
Member Author

FRidh commented Dec 23, 2016

Actually, many developers wrongly load the requirements.txt file and add the items to install_requires. Therefore, a method to just filter the install_requires and setup_requires values should be sufficient.

That's also why I opened an issue on the setuptools trackers, since I think it's best implemented there.

@mmahut
Copy link
Member

mmahut commented Aug 9, 2019

Are there any updates to this issue, please?

@FRidh
Copy link
Member Author

FRidh commented Feb 18, 2020

Reading pypa/setuptools#894 again. The buildPhase will produce a wheel, and that will contain the constraints (in METADATA, the Requires-Dist rows). We can thus fix it at our side by patching the wheel. Would also need to fix then the hash for METADATA in RECORD. The are tools to manipulate wheels. This can be implemented in a hook.

@danbst
Copy link
Contributor

danbst commented Feb 18, 2020

continuing my line of though with sed patching, I came with following hook:

function pythonJailbreak {
    local file="$1"; shift

    for dep in $@; do
        if [[ "$file" =~ requirements*.txt ]]; then
            sed -r -i "s/$dep[><=].*?/'$dep'/g" $file
        else
            sed -r -i "s/['\"]$dep[><=].*?['\"]/'$dep'/g" $file
        fi
    done
}

function toJailbreak {
    local file="$1"
    if [[ "$file" =~ requirements* ]]; then
        sed -rn "s/([a-zA-Z0-9+-]+)[><=].*/\1/p" $file
    else
        sed -rn "s/.*['\"]([a-zA-Z0-9_-]+)[><=].*['\"].*/\1/p" $file
    fi
}

function jailbreakHook {
    if [[ "$doJailbreak" != "false" ]]; then
        if [ -f setup.py ]; then
            pythonJailbreak setup.py $(toJailbreak setup.py)
        fi
        for reqtxt in $(ls -d requirements*.txt); do
            pythonJailbreak "$reqtxt" $(toJailbreak $reqtxt)
        done
    fi
}

It jailbreaks ~95% of dependencies for apache-airflow, so is not "production-grade", but package is built and tested fine.

@stale

This comment was marked as off-topic.

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Aug 16, 2020
@Artturin
Copy link
Member

Added by #170813

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0.kind: enhancement 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md 6.topic: python
Projects
None yet
Development

No branches or pull requests

5 participants