Skip to content
This repository

Checks platform.system to determine POSIX mode #25

Closed
wants to merge 6 commits into from

1 participant

Spencer Rathbun
Spencer Rathbun

This fixes a quoting issue with the shlex module in expand_args on windows. Under posix mode, words in " are expanded. So, 'cat "c:/program files/test.txt"' gets passed in as ['cat', 'c:/program files/test.txt']. With the double quotes removed, the underlying windows os sees two arguments, and fails to find either.

I've added a variable POSIX to hold True or False based upon the results from platform.system(). Anything that needs to check posix mode should use this value.

srathbun added some commits
Spencer Rathbun srathbun Added platform check to determine posix mode for expanding arguments. f542146
Spencer Rathbun srathbun Very dirty fix for expand_args parsing issue. See kennethreitz#26 (co…
…mment) for details
050cb3c
Spencer Rathbun srathbun The rest of the dirty pipe fix dea91b8
Spencer Rathbun srathbun Using shlex as a lexer, and we are parsing the return tokens. This is…
… better than before, but it still fails some cases.
0549050
Spencer Rathbun srathbun All my test {read: use} cases are functional. Properly works for = si…
…gns, pipes, and quotes. The quotes need more testing of edge cases.
2a17c68
Spencer Rathbun srathbun Non posix mode does not work for ghostscript. If a filename has a quo…
…te surrounding it, gs does not strip it out. Since " is invalid for filenames on windows, this does not work.

I've configured it to only parse once over the input string, thus avoiding the original reason for switching to Non posix mode.

Also, it is set up to allow setting of posix mode manually if the user needs to.

Non posix mode still handles quotes slightly oddly, in that if a command such as -I"c:/program files/gs/gs9.05/fonts" is input, two tokens are returned.

This can be fixed, if necessary, in Non posix mode since tokens are examined individually, instead of just using shlex.split() and getting a list.
95fb52c
Spencer Rathbun srathbun closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 6 unique commits by 1 author.

Mar 05, 2012
Spencer Rathbun srathbun Added platform check to determine posix mode for expanding arguments. f542146
Mar 09, 2012
Spencer Rathbun srathbun Very dirty fix for expand_args parsing issue. See kennethreitz#26 (co…
…mment) for details
050cb3c
Spencer Rathbun srathbun The rest of the dirty pipe fix dea91b8
May 01, 2012
Spencer Rathbun srathbun Using shlex as a lexer, and we are parsing the return tokens. This is…
… better than before, but it still fails some cases.
0549050
Spencer Rathbun srathbun All my test {read: use} cases are functional. Properly works for = si…
…gns, pipes, and quotes. The quotes need more testing of edge cases.
2a17c68
May 02, 2012
Spencer Rathbun srathbun Non posix mode does not work for ghostscript. If a filename has a quo…
…te surrounding it, gs does not strip it out. Since " is invalid for filenames on windows, this does not work.

I've configured it to only parse once over the input string, thus avoiding the original reason for switching to Non posix mode.

Also, it is set up to allow setting of posix mode manually if the user needs to.

Non posix mode still handles quotes slightly oddly, in that if a command such as -I"c:/program files/gs/gs9.05/fonts" is input, two tokens are returned.

This can be fixed, if necessary, in Non posix mode since tokens are examined individually, instead of just using shlex.split() and getting a list.
95fb52c
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 23 additions and 18 deletions. Show diff stats Hide diff stats

  1. +23 18 envoy/core.py
41 envoy/core.py
@@ -11,12 +11,14 @@
11 11 import shlex
12 12 import subprocess
13 13 import threading
  14 +import platform
14 15
15 16
16 17 __version__ = '0.0.2'
17 18 __license__ = 'MIT'
18 19 __author__ = 'Kenneth Reitz'
19 20
  21 +POSIX = False if platform.system() == 'Windows' else True
20 22
21 23 class Command(object):
22 24 def __init__(self, cmd):
@@ -132,35 +134,38 @@ def __repr__(self):
132 134 return '<Response>'
133 135
134 136
135   -def expand_args(command):
  137 +def expand_args(command, posix=None):
136 138 """Parses command strings and returns a Popen-ready list."""
137 139
138 140 # Prepare arguments.
139 141 if isinstance(command, basestring):
140   - splitter = shlex.shlex(command, posix=True)
141   - splitter.whitespace = '|'
142   - splitter.whitespace_split = True
143   - command = []
144   -
145   - while True:
146   - token = splitter.get_token()
147   - if token:
148   - command.append(token)
149   - else:
150   - break
  142 + item = []
  143 + cmdlist = []
151 144
152   - command = map(shlex.split, command)
  145 + if posix is not None:
  146 + p = posix
  147 + else:
  148 + p = POSIX
  149 + s = shlex.shlex(command, posix=p) # POSIX mode can now be manually set, in case you are using a *nix util on windows I.E. ghostscript
  150 + s.whitespace_split = True
  151 + for tok in s: # this is almost identical to what shlex.split does, only we start a new list if we see a pipe by itself. All lists get packaged up together, and we are good.
  152 + if tok == '|':
  153 + cmdlist.append(item)
  154 + item = []
  155 + else:
  156 + item.append(tok)
  157 + cmdlist.append(item)
153 158
154   - return command
  159 + return cmdlist
155 160
156 161
157   -def run(command, data=None, timeout=None, env=None):
  162 +def run(command, data=None, timeout=None, env=None, posix=None):
158 163 """Executes a given commmand and returns Response.
159 164
160 165 Blocks until process is complete, or timeout is reached.
161 166 """
162 167
163   - command = expand_args(command)
  168 + command = expand_args(command, posix=posix)
164 169
165 170 history = []
166 171 for c in command:
@@ -187,11 +192,11 @@ def run(command, data=None, timeout=None, env=None):
187 192 return r
188 193
189 194
190   -def connect(command, data=None, env=None):
  195 +def connect(command, data=None, env=None, posix=None):
191 196 """Spawns a new process from the given command."""
192 197
193 198 # TODO: support piped commands
194   - command_str = expand_args(command).pop()
  199 + command_str = expand_args(command, posix=posix).pop()
195 200 environ = dict(os.environ).update(env or {})
196 201
197 202 process = subprocess.Popen(command_str,

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.