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

Use Virtual Environment for Python Deps #2294

Merged
merged 17 commits into from
Jan 10, 2022
Merged

Use Virtual Environment for Python Deps #2294

merged 17 commits into from
Jan 10, 2022

Conversation

lindluni
Copy link
Contributor

@lindluni lindluni commented Jan 4, 2022

Fixes issues with python deps

Proposed Changes

  1. ...
  2. ...
  3. ...

Readiness Checklist

Author/Contributor

  • If documentation is needed for this change, has that been included in this pull request

Reviewing Maintainer

  • Label as breaking if this is a large fundamental change
  • Label as either automation, bug, documentation, enhancement, infrastructure, or performance

@admiralAwkbar admiralAwkbar self-assigned this Jan 4, 2022
@admiralAwkbar admiralAwkbar added automation related to helping the project operate more efficiently dependencies Pull requests that update a dependency file labels Jan 4, 2022
@admiralAwkbar
Copy link
Collaborator

@lindluni took some liberties on your bash script :)

@admiralAwkbar
Copy link
Collaborator

@ferrarimarco this should help with #2224

@lindluni
Copy link
Contributor Author

lindluni commented Jan 4, 2022

@admiralAwkbar I'll fix the issue later tonight. I need to build outside of virtualenv, otherwise it tries to link against that interpreter instead of the one installed in the container

@@ -0,0 +1,13 @@
ansible-lint[core]==5.2.1
Copy link
Contributor

@jalaziz jalaziz Jan 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend a separate requirements.txt for each dependency.

This will fail to upgrade certain dependencies that have fixed dependencies on each other (like snakefmt).

Also, black below is a regression compared to the current working version.

Dependabot works with directories, so you could have a python folder with a .txt for each tool, then do something like this in build-python-binaries.sh:

while read -r line; do
  # Install dependency
  pipx install "${line}"
done < ./python/*.txt

@jalaziz
Copy link
Contributor

jalaziz commented Jan 5, 2022

With this we can also remove the self-contained black binary install.

@ferrarimarco
Copy link
Collaborator

Maybe I'm missing the point, but how is this going to help? If dependencies are conflicting during updates, they're going to conflict during the installation as well, right?

@jalaziz
Copy link
Contributor

jalaziz commented Jan 5, 2022

Maybe I'm missing the point, but how is this going to help? If dependencies are conflicting during updates, they're going to conflict during the installation as well, right?

They won't because pipx installs each tool in its own virtualenv. Separation of the dependencies would allow dependabot to upgrade each tool independently. If they're in the same requirements.txt, then dependabot will try to ensure that the versions don't conflict and may hold back updates.

@lindluni lindluni changed the title Build static python depenencies Use Virtual Environment for Python Deps Jan 6, 2022
@lindluni
Copy link
Contributor Author

lindluni commented Jan 6, 2022

Best I could figure pipx doesn't support requirement files, it runs purely npx. Switched to virtual environments instead. Can't say python is in any way shape or form my forte, but this works. Need to come back and revisit the size by pruning and reusing where we can, and need to update the README for adding new deps.

@lindluni
Copy link
Contributor Author

lindluni commented Jan 6, 2022

I also need to add doc on updating the deps

@jalaziz
Copy link
Contributor

jalaziz commented Jan 6, 2022

Best I could figure pipx doesn't support requirement files...

It doesn't, but it does support the standard python requirements specifier. pipx should be sufficient because you should only need to install one tool at a time and not have to worry about the transitive dependencies.

One thing I noticed is that the there are multiple requirements listed per tool. Why is that? Instead of fixing transitive dependencies, it's probably better to just let pip / pipx / whatever figure it out.

Lastly, instead of having a directory per-tool with a requirements.txt, it would be a lot simpler to have one python-dependencies folder with a <tool>.txt for each tool. Dependabot doesn't seem care what the file is named as long as it ends with .txt. This would simplify the dependabot configuration considerably.

EDIT: Looking at the dependabot code again, it's not clear if separate requirements files in the same directory will result in dependabot trying to resolve each file separately or as one giant unit. I can probably come up with a quick test to verify and report back.

Can't say python is in any way shape or form my forte, but this works.

I'm happy to help if that would be useful.

Also, my apologies to Github staff if I'm stepping out of line with these comments, just happened to have looked into this already and I think it could be simpler.

@lindluni
Copy link
Contributor Author

lindluni commented Jan 6, 2022

@jalaziz the issue with not fixing the transitive deps is you never know if a transitive dep has a vulnerability in it. Dependabot can only tell if the package listed has a vulnerability. So if I fail to list the transitive deps, we are at the mercy of the upstream project to patch the vulnerability, and we will never know if we are consuming vulnerable dependencies because the upstream failed to patch something.

It lets us make informed decisions about the dependencies we are consuming, to remove them, fork them, or to help patch an upstream project when we can.

And by no means are you out of line. We are a community and we are here to learn from each other.

@lindluni
Copy link
Contributor Author

lindluni commented Jan 6, 2022

I'm also not saying my approach is best, good, or anything of the sort, I personally think the use of virtual environments is far too heavy, my first go around was to actually compile all the projects with pyinstaller, but ansible-core was a nightmare to do that with

@jalaziz
Copy link
Contributor

jalaziz commented Jan 6, 2022

@jalaziz the issue with not fixing the transitive deps is you never know if a transitive dep has a vulnerability in it. Dependabot can only tell if the package listed has a vulnerability. So if I fail to list the transitive deps, we are at the mercy of the upstream project to patch the vulnerability, and we will never know if we are consuming vulnerable dependencies because the upstream failed to patch something.

It lets us make informed decisions about the dependencies we are consuming, to remove them, fork them, or to help patch an upstream project when we can.

Yeah, that's totally fair. I was actually wondering if Dependabot would actually do the right thing after my comment and started questioning it myself. In that case, yeah, virtualenvs are probably a safe bet.

I'm also not saying my approach is best, good, or anything of the sort, I personally think the use of virtual environments is far too heavy, my first go around was to actually compile all the projects with pyinstaller, but ansible-core was a nightmare to do that with

Oh lord, I can imagine. That being said, virtualenvs may not be too bad. If setup correctly, virtual envs will share the system's python through symlinks. That would be roughly equivalent or better to what you'd be getting with PyInstaller (minus potential compression and other small optimizations).

@jalaziz
Copy link
Contributor

jalaziz commented Jan 6, 2022

Also, I think the Dependabot config will definitely need updating now as I don't believe it recurses into subdirectories (dependabot/dependabot-core#2178 and dependabot/dependabot-core#1015).

@lindluni
Copy link
Contributor Author

lindluni commented Jan 6, 2022

Still need to update dependabot, and add the readme, but this is ready for everyone's thoughts

auto-merge was automatically disabled January 10, 2022 21:23

Rebase failed

Copy link
Collaborator

@admiralAwkbar admiralAwkbar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dank fixes all around

@admiralAwkbar admiralAwkbar merged commit fc6c5b3 into main Jan 10, 2022
@admiralAwkbar admiralAwkbar deleted the python_deps branch January 10, 2022 21:23
@jalaziz
Copy link
Contributor

jalaziz commented Jan 10, 2022

Looks like the dependabot update was still missing from this PR. Should I open a new PR?

@admiralAwkbar
Copy link
Collaborator

@jalaziz PR's are always welcome :)
this open got out of hand lol

sarahc23 pushed a commit to 23andMe/super-linter that referenced this pull request May 6, 2022
* Build static python depenencies

* Address linting

* Fix copy path

* cleaner

* Stage virtual environments

* Update Dockerfile to support virtual environments

* Remove old python builds

* Remove unnecessary RUN step

* Fix merge conflicts

* Remove test checking for PIP packages

We use virtual environments and no longer install the packages
via pip directly in the image. It should be enough that the version
tests check for the existence already and that the version
comes back correctly.

* Remove binary installation of black

* cleaner

* Remove pip

* pretty

Co-authored-by: Admiral Awkbar <admiralawkbar@github.com>
sarahc23 pushed a commit to 23andMe/super-linter that referenced this pull request May 6, 2022
* Build static python depenencies

* Address linting

* Fix copy path

* cleaner

* Stage virtual environments

* Update Dockerfile to support virtual environments

* Remove old python builds

* Remove unnecessary RUN step

* Fix merge conflicts

* Remove test checking for PIP packages

We use virtual environments and no longer install the packages
via pip directly in the image. It should be enough that the version
tests check for the existence already and that the version
comes back correctly.

* Remove binary installation of black

* cleaner

* Remove pip

* pretty

Co-authored-by: Admiral Awkbar <admiralawkbar@github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automation related to helping the project operate more efficiently dependencies Pull requests that update a dependency file
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants