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

make script compatible to pypy / having scripts in bin #95

Closed
wants to merge 5 commits into from

Conversation

wamserma
Copy link
Contributor

I had a take on #30
Works for me, had no issues so far.

thebjorn added a commit that referenced this pull request Nov 30, 2017
…pied into most scripts with varying levels of fidelity. This is an attempt to collect these into one file, and also to make the work in #95 easier to implement.
@thebjorn
Copy link
Collaborator

Hi @wamserma and thank you for the PR, it's really appreciated.

I see a few problems that needs to be fixed before I can merge this.

pypy --version >nul 2>&1

This will always succeed for users that have a (default) pypy installed on %PATH%, so it isn't a valid test for whether the current virtualenv is using pypy.

As an aside: if errorlevel might not function as you're expecting (https://ss64.com/nt/errorlevel.html)

Escaping env vars: You are defining a couple of environment variables that escape to the user's environment after the script is run (e.g. PYTHON_EXE, LIB_DIR). That is bad, e.g. if a user has LIB_DIR pointing to their c++ lib folder and calls cdsitepackages.. suddenly the environment variable is gone (or points to a different directory).

We have a number of environment variables that historically have escaped (e.g. _LAST_DIR, PYHOME, WORKON_HOME, etc.). These must stay for backwards compatibility reasons. For everything else we need to clean the variables from the environment before exiting. The method I have chosen involves using a per script prefix:

    set "vwfooscript.my_var=%~1"
    set "vwfooscript.my_other_var=%~2"

and then clean them up at the end using a for loop:

:cleanup
    for /f "usebackq delims==" %%v in (`set vwfooscript.`) do @set "%%v="
    goto:eof

(I've started to document some of this on the docs branch if you're interested, e.g.: https://github.com/davidmarble/virtualenvwrapper-win/blob/docs/docs/codingstyle.rst)

Duplication: There's a lot of duplicated setup code. This isn't your fault (at all), but your additions certainly puts a spotlight on this problem. For a while I've toyed with creating a script that would run the setup code and could be called from every other script.

I've just pushed such a script now (scripts/vwenv.bat). It's not used anywhere yet, but maybe it would make this PR cleaner..?

NB: As per now, the vwenv.bat script doesn't account for the situation where virtualenvwrapper-win has been installed in a pypy-based Python. (I see you're detecting this in a couple of places).

Bookkeeping: You should add a bullet in CHANGELOG.rst (it can be as simple as "added pypy support @wamserma ")

@wamserma
Copy link
Contributor Author

wamserma commented Nov 30, 2017

Mmmh yes, this was just a quick batch edit to get things rolling for me. To be honest, I always assumed that one was checking for the availability of pypy.exe outside of the VirtualEnv.
Regarding the escaping vars, I was reluctant to use SETLOCAL as I expected things to break then.
Pretty much the same thing with the duplicated code: I wanted to get things running quickly. How about using some sort of preprocessor to plug things together?
I also wasn't able to test this properly. I simply wanted to have some comfort for virtualenv on a system where I only unzipped pypy and added it to the path.
For now, I will add the bullet to CHANGELOG.rst and look into the other issues as I have time.

Edit: Regarding errorlevel, I know that this is a GTE-check. I'll put it on the review-list, though.

@thebjorn
Copy link
Collaborator

thebjorn commented Dec 1, 2017

To be honest, I always assumed that one was checking for the availability of pypy.exe outside of the VirtualEnv.

There are two situations that need to be handled with respect to pypy/python.

  1. the py(py|thon) environment where virtualenvwrapper-win itself is installed
  2. the py(py|thon) version installed in a virtualenv.

In a few situations virtualenvwrapper-win uses py(py|thon) to execute commands that are unruly in .bat. Great care is taken so the Python code used is universal (can be ran on all supported Python versions). Up until now the python.exe used is whichever is found first on the path. It can be either of case 1 or 2 above, but since the Python code to be run is universal it hasn't mattered.

Apparently it is possible to run post-install code (re. checking for pypy from the outside)[1], but that only solves half the problem.

Regarding the escaping vars, I was reluctant to use SETLOCAL as I expected things to break then.

Yes, stay away from setlocal whenever possible. Using a name that is unique and unsetting it at the end is preferrable.

How about using some sort of preprocessor to plug things together?

That is certainly a possibility, and one that I've contemplated a couple of times. The natural choice for preprocessor would be Jinja2 if you're interested in going this route. It would also require an extra step during the build/publish(/test?) phase.

For now, I will add the bullet to CHANGELOG.rst and look into the other issues as I have time.

Excellent. The only thing that's coming from me in the near/medium future is a full hook implementation, but it shouldn't interfere with this PR.

[1] https://stackoverflow.com/a/36902139/75103

davidfraser pushed a commit to j5int/virtualenvwrapper-win that referenced this pull request Dec 27, 2017
* upstream/master:
  upversion
  debug leftover
  Typo fix in CHANGELOG.rst
  Getting ready for next release..
  Publish the vwenv.bat script.
  It's starting to be a lot of setup that needs to be done, and it's copied into most scripts with varying levels of fidelity.  This is an attempt to collect these into one file, and also to make the work in davidmarble#95 easier to implement.
  Push 1.2.4 to PyPI.
  Fix workon, rmvirtualenv, and mkvirtualenv -a when virtualenv and/or project contain spaces. This fixes davidmarble#89.
  Remove debug echo.
  Fix 3 failing tests (test-code issues).
  "Number" headers in CHANGELOG similar to README (the files are concatenated and must match).
  Updates to add2virtualenv to fix davidmarble#93.
  Tests for add2virtualenv and setprojectdir.
  Convert tabs to spaces, fix som issues with spaces in paths, and exit with a distinct error level for each error.
  Make sure we call cleanup, even on a successful run, and set ERRORLEVEL to zero.
  Missing return from error_msg.
  Add support for -h, --help switches, and fix davidmarble#92.
  Add support for -h --help options, and fix davidmarble#92.
  tests for mkproject
  Add publishing step that checks syntax of the reStructuredText in README.rst.
  updated mkproject to follow coding style of mkvirtualenv.  updated readme and changelog as requested.
  New commands need to be installed..
  Add virtualenvwrapper.bat command: Print a list of commands and their descriptions as basic help output. (The linux version has it: https://virtualenvwrapper.readthedocs.io/en/latest/command_ref.html#virtualenvwrapper)
  Added top-comment and usage block to whereis.bat Changed some awkward verbiage in README.rst
  Missed a goto:eof..
  Fix a number of issues with spaces in virtualenv names in rmvirtualenv.bat and simplified the removal algorithm.
  Allow spaces in virtualenv names.
  Start next version.
  Added mkproject script (with better default PROJECT_HOME) to setup.py
  upversion
  Added invoke tasks.py file.
  ignore PyCharm files
  Grab fix for path with env-var issue from @nakedmin2017 (nakedmind2017@de4ada2)
  virtualenv doesn't need an equal sign for parameters with double dashes, fixes davidmarble#86.
  Fix for davidmarble#85.
  Fix other potential dir-with-spaces problems.
  Test for davidmarble#85
  Add -k option to run_tests.bat for running single tests.
  Fix (?) PyPI barfing on readme.
  Bump Version
  Release notes for v1.2.2
  Implement -a, -i, and -r options to mkvirtualenv.
  This is a little bit of a hefty rewrite, but it's needed to be able to handle some options, and let other options through to virtualenv.
  Start of test-suite.
@wamserma
Copy link
Contributor Author

As I've moved to greener pastures (mostly non-Windows) I'm abandoning this PR, releasing it to the public domain.
If someone cares, pick it up. Otherwise it may rest in peace.

@wamserma wamserma closed this Sep 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants