Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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
Commits on Mar 5, 2012
  1. Spencer Rathbun
Commits on Mar 9, 2012
  1. Spencer Rathbun
  2. Spencer Rathbun
Commits on May 1, 2012
  1. Spencer Rathbun

    Using shlex as a lexer, and we are parsing the return tokens. This is…

    srathbun authored
    … better than before, but it still fails some cases.
  2. Spencer Rathbun

    All my test {read: use} cases are functional. Properly works for = si…

    srathbun authored
    …gns, pipes, and quotes. The quotes need more testing of edge cases.
Commits on May 2, 2012
  1. Spencer Rathbun

    Non posix mode does not work for ghostscript. If a filename has a quo…

    srathbun authored
    …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.
This page is out of date. Refresh to see the latest.
Showing with 23 additions and 18 deletions.
  1. +23 −18 envoy/core.py
41 envoy/core.py
View
@@ -11,12 +11,14 @@
import shlex
import subprocess
import threading
+import platform
__version__ = '0.0.2'
__license__ = 'MIT'
__author__ = 'Kenneth Reitz'
+POSIX = False if platform.system() == 'Windows' else True
class Command(object):
def __init__(self, cmd):
@@ -132,35 +134,38 @@ def __repr__(self):
return '<Response>'
-def expand_args(command):
+def expand_args(command, posix=None):
"""Parses command strings and returns a Popen-ready list."""
# Prepare arguments.
if isinstance(command, basestring):
- splitter = shlex.shlex(command, posix=True)
- splitter.whitespace = '|'
- splitter.whitespace_split = True
- command = []
-
- while True:
- token = splitter.get_token()
- if token:
- command.append(token)
- else:
- break
+ item = []
+ cmdlist = []
- command = map(shlex.split, command)
+ if posix is not None:
+ p = posix
+ else:
+ p = POSIX
+ 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
+ s.whitespace_split = True
+ 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.
+ if tok == '|':
+ cmdlist.append(item)
+ item = []
+ else:
+ item.append(tok)
+ cmdlist.append(item)
- return command
+ return cmdlist
-def run(command, data=None, timeout=None, env=None):
+def run(command, data=None, timeout=None, env=None, posix=None):
"""Executes a given commmand and returns Response.
Blocks until process is complete, or timeout is reached.
"""
- command = expand_args(command)
+ command = expand_args(command, posix=posix)
history = []
for c in command:
@@ -187,11 +192,11 @@ def run(command, data=None, timeout=None, env=None):
return r
-def connect(command, data=None, env=None):
+def connect(command, data=None, env=None, posix=None):
"""Spawns a new process from the given command."""
# TODO: support piped commands
- command_str = expand_args(command).pop()
+ command_str = expand_args(command, posix=posix).pop()
environ = dict(os.environ).update(env or {})
process = subprocess.Popen(command_str,
Something went wrong with that request. Please try again.