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: remove $out/bin/__pycache__ in fixup phase #30915

Closed
wants to merge 1 commit into from

Conversation

casey
Copy link
Contributor

@casey casey commented Oct 29, 2017

Motivation for this change

This fixes #29896.

Things done
  • [x ] Tested using sandboxing (nix.useSandbox on NixOS, or option build-use-sandbox in nix.conf on non-NixOS)
  • Built on platform(s)
    • [x ] NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nox --run "nox-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Fits CONTRIBUTING.md.

Tested by creating a minimal configuration.nix which, when built, produced result/sw/bin/__pycache__.

Then rebased this diff on top of it and rebuilt, and __pycache__ was gone.

I'm slightly concerned about testing here. This seems like a safe diff, but it potentially effects every python package. Also, this will likely lead to everything under the sun getting rebuilt, so it's hard to test fully.

@casey casey requested a review from FRidh as a code owner October 29, 2017 01:41
@adisbladis
Copy link
Member

These files could be avoided in the first place by creating an environment variable called $PYTHONDONTWRITEBYTECODE.

Maybe this is a better fix?

@casey
Copy link
Contributor Author

casey commented Oct 29, 2017

My concern with that is that we actually want .pyc files for libraries, since they speed up execution. (They also speed up execution for binaries, but they shouldn't be in bin.)

Happy to do it with an environment variable though, if that's not a concern.

@adisbladis
Copy link
Member

I think the env var approach should be fine as there are different abstractions for libraries and applications (buildPythonPackage and buildPythonApplication).

@FRidh FRidh self-assigned this Oct 29, 2017
@FRidh
Copy link
Member

FRidh commented Oct 29, 2017

I think the env var approach should be fine as there are different abstractions for libraries and applications (buildPythonPackage and buildPythonApplication).

No, that won't be sufficient. All Python packages can provide executables. The reason @casey opened this is exactly because of certain libraries that distribute executables ending with .py.

@@ -29,4 +29,8 @@ attrs // {

runHook postInstall
'';

fixupPhase = attrs.fixupPhase or ''
rm -rf $out/bin/__pycache__
Copy link
Member

Choose a reason for hiding this comment

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

Bytecode produces by 2.x and 3.x is different.

$ ls -Al $(nix-build -A python2.pkgs.docutils)/bin
total 132
-r-xr-xr-x 1 root root  483 Jan  1  1970 rst2html5.py
-r--r--r-- 1 root root  745 Jan  1  1970 rst2html5.pyc
-r-xr-xr-x 1 root root 1620 Jan  1  1970 .rst2html5.py-wrapped
-r-xr-xr-x 1 root root  482 Jan  1  1970 rst2html.py
-r--r--r-- 1 root root  619 Jan  1  1970 rst2html.pyc
-r-xr-xr-x 1 root root 1073 Jan  1  1970 .rst2html.py-wrapped
-r-xr-xr-x 1 root root  483 Jan  1  1970 rst2latex.py
-r--r--r-- 1 root root  749 Jan  1  1970 rst2latex.pyc
...
$ ls -Al $(nix-build -A python3.pkgs.docutils)/bin
total 92
dr-xr-xr-x 2 root root 4096 Jan  1  1970 __pycache__
-r-xr-xr-x 1 root root  483 Jan  1  1970 rst2html5.py
-r-xr-xr-x 1 root root 1621 Jan  1  1970 .rst2html5.py-wrapped
-r-xr-xr-x 1 root root  482 Jan  1  1970 rst2html.py
-r-xr-xr-x 1 root root 1074 Jan  1  1970 .rst2html.py-wrapped
-r-xr-xr-x 1 root root  483 Jan  1  1970 rst2latex.py
...

Both cases need to be handled.

Then, there is the matter of where to put this function. Whoever uses buildPythonPackage, should be able to use this. Even with format = "other";. Therefore, I suggest putting this in the postFixup of mk-python-derivation.nix.

@FRidh
Copy link
Member

FRidh commented Oct 29, 2017

It would also be good to consider #29086. In that issue the suggestion is made that all of our language-specific builders should be more modular, so you could just pick parts.

@grahamc
Copy link
Member

grahamc commented Oct 29, 2017

@GrahamcOfBorg python3Packages.requests

Copy link

@GrahamcOfBorg GrahamcOfBorg left a comment

Choose a reason for hiding this comment

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

    from _pytest.config import (
  File "/nix/store/i2dm10k5b1qch07pgjfmq8hkpqbsihin-python3.6-pytest-3.2.3/lib/python3.6/site-packages/_pytest/config.py", line 9, in <module>
    import py
ModuleNotFoundError: No module named 'py'
builder for ‘/nix/store/rw83k17p9c3cp1ya7vs5rbl9vx9z02hi-python3.6-cffi-1.11.2.drv’ failed with exit code 1
cannot build derivation ‘/nix/store/6g25mvsb9n88z8mhrsabhjrs15ws2pw3-python3.6-cryptography-2.0.3.drv’: 5 dependencies couldn't be built
cannot build derivation ‘/nix/store/dh42gky3kpyj6frgvf1jfc7670qfbfp9-python3.6-pyOpenSSL-17.2.0.drv’: 2 dependencies couldn't be built
cannot build derivation ‘/nix/store/99ygxj0sfinc5scx7iyzz9pkikvd9814-python3.6-urllib3-1.22.drv’: 2 dependencies couldn't be built
cannot build derivation ‘/nix/store/cw4jmxc1q6svlms210qjaxrjjynw28mw-python3.6-requests-2.18.4.drv’: 2 dependencies couldn't be built
error: build of ‘/nix/store/cw4jmxc1q6svlms210qjaxrjjynw28mw-python3.6-requests-2.18.4.drv’ failed

@FRidh
Copy link
Member

FRidh commented Oct 29, 2017

It fails to build because this fixupPhase now replaces the default one, which wraps Python programs.

@casey
Copy link
Contributor Author

casey commented Oct 29, 2017

@FRidh If I put this in postFixup, will it still have the same problem, since it will replace the default postFixup phase?

@FRidh
Copy link
Member

FRidh commented Oct 30, 2017

As I wrote before it needs to go in postFixup of mk-python-derivation.nix. If you read that part you'll see that issue has been handled there ;)

@casey
Copy link
Contributor Author

casey commented Nov 13, 2017

Unfortunately I'm going traveling for a few months, and my nix box is now languishing in a self-storage unit, so I won't be able to work on this until I get back. I'm going to close it. Hopefully I get a chance to re-open it, or some kind soul will be able to pick it up before I get the chance.

@casey casey closed this Nov 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Python: bytecode in /bin
5 participants