pip module fails on Windows because of quoting
salt -G 'os:Windows' pip.install mako
gives:
pid:
1112
retcode:
2
stderr:
stdout:
Exception:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\pip\basecommand.py", line 134, in main
status = self.run(options, args)
File "C:\Python27\lib\site-packages\pip\commands\install.py", line 215, in run
InstallRequirement.from_line(name, None))
File "C:\Python27\lib\site-packages\pip\req.py", line 129, in from_line
return cls(req, comes_from, url=url, prereleases=prereleases)
File "C:\Python27\lib\site-packages\pip\req.py", line 44, in __init__
req = pkg_resources.Requirement.parse(req)
File "build\bdist.win32\egg\pkg_resources.py", line 2700, in parse
reqs = list(parse_requirements(s))
File "build\bdist.win32\egg\pkg_resources.py", line 2626, in parse_requirements
raise ValueError("Missing distribution spec", line)
ValueError: ('Missing distribution spec', "'mako'")
Storing complete log in C:\Windows\system32\config\systemprofile\pip\pip.log
In this changeset fcbed9a, quoting was added to package names with the "!r" format flag. Packages are then appended to the pip command as strings enclosed in single quotes:
" 'packagename' "
On linux, this works fine. On Windows, the Package resource API (lib/site-packages/pkg_resources.py) cannot properly parse package names quoted like this, hence raising this "Missing distribution spec" error.
I am not sure what would be the right way to fix this. Using the "!s" flag instead of the "!r" flag would fix this, but could potentially break something else. I am not fully aware of the all the pip module subtleties, so I cannot for sure propose a solution.
Thanks for your detective work here! We do need to make sure we don't introduce any regressions with this change, but we definitely need to do something different for windows.
@basepi - using '!r' is definitely wrong here for all possible cases. It runs repr() on the path so you'll get single quotes and backslash escaping which will break things on linux too. E.g:
>>> a='{0!r}'.format('/tmp/path\ with\ spaces')
>>> subprocess.call(['mkdir', a])
mkdir: cannot create directory `\'/tmp/path\\\\ with\\\\ spaces\'': No such file or directory
1
>>> a='{0}'.format('/tmp/path\ with\ spaces')
>>> subprocess.call(['mkdir', a])
0
Subprocess already quotes cmd line args correctly for you:
>>> subprocess.list2cmdline(['mkdir', a])
'mkdir "/tmp/path\\ with\\ spaces"'
@s0undt3ch - Look like your changes in https://github.com/saltstack/salt/commit/baebb2f064c8c5cbd1fd9a59aa7cf8848793f298 added the '!r' - was there any particular reason for this?
The --extra-index-url option is also broken by this.
@otrempe Do you think you could give this another try on 2014.7.5? I know that @twangboy has been doing a lot of work lately on getting some of these windows bugs cleaned up. See the documentation changes in the pull request above for more info.
Hi @rallytime
Unfortunately, I didn't play with SaltStack since I left my previous job. Hence I don't have a SaltStack installation to play with to confirm this is properly fixed. However, I am pushing hard to promote SaltStack at my new job!
@otrempe No problem! I'm going to go ahead a close this then.
When people make it across this issue and think there is still more work to be done here, leave a comment and we will be happy to re-open. Thanks!
This still appears to be broken in 2015.5.3:
test-virtualenv:
virtualenv:
- managed
- name: C:\testvenv
- venv_bin: C:/Python27/Scripts/virtualenv.exe
- no_use_wheel: True
- use_wheel: False
- extra_index_url: {{ pillar['testvenv']['pip_extra_index_url'] }}
- system_site_packages: True
- requirements: salt://testvenv/client/files/requirements-windows.txt
Pip fails due to the double escaping of the backslashes for requirements.txt:
2015-08-24 15:55:29,090 [salt.loaded.int.module.cmdmod][INFO ][2064] Executing command "C:\\\\testvenv\\\\Scripts\\\\pip.exe install --requirement='c:\\\\salt\\\\var\\\\cache\\\\salt\\\\minion\\\\files\\\\base\\\\testvenv/client/files/requirements-windows.txt' --extra-index-url='http://devpi/testindex/dev/+simple/'" in directory 'C:\\Windows\\system32\\config\\systemprofile'
2015-08-24 15:55:29,418 [salt.loaded.int.module.cmdmod][ERROR ][2064] Command "C:\\\\testvenv\\\\Scripts\\\\pip.exe install --requirement='c:\\\\salt\\\\var\\\\cache\\\\salt\\\\minion\\\\files\\\\base\\\\testvenv/client/files/requirements-windows.txt' --extra-index-url='http://devpi/testindex/dev/+simple/'" failed with return code: 1
2015-08-24 15:55:29,418 [salt.loaded.int.module.cmdmod][ERROR ][2064] stdout: Could not open requirements file: [Errno 22] invalid mode ('r') or filename: "'c:\\\\salt\\\\var\\\\cache\\\\salt\\\\minion\\\\files\\\\base\\\\testvenv/client/files/requirements-windows.txt'"
Storing complete log in C:\Windows\system32\config\systemprofile\pip\pip.log
Removing all of the !r instances in modules/pip.py and using subprocess.list2cmdline to join the command line fixes this.
Thanks for the comment, @olliewalsh. I've re-opened and triaged it accordingly.
I'm working on this right now as part of a larger audit eliminating use of the '!r' conversion flag.
Fixed in https://github.com/saltstack/salt/pull/26617.
There is no need to use subprocess.list2cmdline(), as subprocess.Popen (which we use to run commands) is designed to accept a list of arguments.
Sweet. subprocess.Popen uses list2cmdline under the hood so it's pretty much the same code path but much cleaner
Yep, we're moving towards switching to passing commands to cmd.* functions as lists of arguments in the execution module code wherever possible too, because doing that both gets rid of the need to worry about quoting arguments to ensure they're logically separated when subprocess.Popen() is invoked, and fixes issues like the one you saw here with Windows.
Fix was merged, closing.