Shell quoting as part of the standard library?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David M. Wilson

    Shell quoting as part of the standard library?

    Hello fellow users!

    I've been using Python in a couple of different environments for a few
    years now. I have quite often found that I have needed to deal with
    filenames which may contain characters that confuse the UNIX shell
    (space and double quote in particular).

    Quite a while ago I came up with two simple functions which allowed me
    to safely build a shell command line quickly. I've used the functions
    hundreds of times by now, and find myself constantly cut/pasting them
    into new projects. It is very likely that someone other than myself
    would find these functions to be useful, and I was wondering if such a
    thing might find a home in the standard library.



    Does this sort of thing have a home in the standard library? It
    doesn't really apply across platforms, which is my only real concern.
    Where should it be added to? shutil?

    If you are thinking of commenting on the way shell_quote_str ing always
    quotes, regardless of whether it is needed, please don't. If it acted
    in a more human-friendly manner then it would be called
    shell_friendly_ quote_string, or something even more insane than that.
    :)

    Thanks,


    David.
  • Peter Otten

    #2
    Re: Shell quoting as part of the standard library?

    David M. Wilson wrote:
    [color=blue]
    > I've been using Python in a couple of different environments for a few
    > years now. I have quite often found that I have needed to deal with
    > filenames which may contain characters that confuse the UNIX shell
    > (space and double quote in particular).
    >
    > Quite a while ago I came up with two simple functions which allowed me
    > to safely build a shell command line quickly. I've used the functions
    > hundreds of times by now, and find myself constantly cut/pasting them
    > into new projects. It is very likely that someone other than myself
    > would find these functions to be useful, and I was wondering if such a
    > thing might find a home in the standard library.
    >
    > http://botanicus.net/dw/tmp/shellquote.py
    >
    > Does this sort of thing have a home in the standard library? It
    > doesn't really apply across platforms, which is my only real concern.
    > Where should it be added to? shutil?[/color]
    [color=blue][color=green][color=darkred]
    >>> from commands import mkarg
    >>> mkarg("one silver $")[/color][/color][/color]
    " 'one silver $'"[color=blue][color=green][color=darkred]
    >>> mkarg("one's own $")[/color][/color][/color]
    ' "one\'s own \\$"'

    So something very similar for argument preparation is already there. The
    joining, as far as I know, currently has to be done manually:
    [color=blue][color=green][color=darkred]
    >>> "".join([mkarg(a) for a in ["a a", "$'"]])[/color][/color][/color]
    ' \'a a\' "\\$\'"'

    Peter

    Comment

    • Francis Avila

      #3
      Re: Shell quoting as part of the standard library?

      David M. Wilson wrote in message
      <99dce321.03121 31439.1612dee2@ posting.google. com>...[color=blue]
      >Hello fellow users!
      >
      >I've been using Python in a couple of different environments for a few
      >years now. I have quite often found that I have needed to deal with
      >filenames which may contain characters that confuse the UNIX shell
      >(space and double quote in particular).[/color]

      Isn't this the more general problem of escaping shell metachars in
      user-supplied input?

      Why don't you just let the shell deal with it?

      $ cat > do_st_evil
      #! /bin/sh
      echo "Gotcha!"
      ^D
      $ chmod u+x do_st_evil
      $ python
      Python 2.2.1 (#1, Apr 21 2002, 08:38:44)
      [GCC 2.95.4 20011002 (Debian prerelease)] on linux2
      Type "help", "copyright" , "credits" or "license" for more information[color=blue][color=green][color=darkred]
      >>> import os
      >>> badness = '$(./do_st_evil)'
      >>> os.popen('echo '+badness).read ()[/color][/color][/color]
      'Gotcha!\n'[color=blue][color=green][color=darkred]
      >>> os.environ['shellvar'] = badness
      >>> os.popen('echo $shellvar').rea d()[/color][/color][/color]
      '$(./do_st_evil)\n'

      --
      Francis Avila

      Comment

      • David M. Wilson

        #4
        Re: Shell quoting as part of the standard library?

        Peter Otten <__peter__@web. de> wrote...
        [color=blue][color=green][color=darkred]
        > >>> from commands import mkarg
        > >>> mkarg("one silver $")[/color][/color]
        > " 'one silver $'"[color=green][color=darkred]
        > >>> mkarg("one's own $")[/color][/color]
        > ' "one\'s own \\$"'[/color]

        I don't get it. I just don't get it. I'm really starting to doubt I
        have any initiative whatsoever. :)

        Ok, yet another mystery "why hasn't python got that" solved. Thanks!


        David.

        Comment

        • Oren Tirosh

          #5
          Re: Shell quoting as part of the standard library?

          On Sat, Dec 13, 2003 at 02:39:52PM -0800, David M. Wilson wrote:[color=blue]
          > Hello fellow users!
          >
          > I've been using Python in a couple of different environments for a few
          > years now. I have quite often found that I have needed to deal with
          > filenames which may contain characters that confuse the UNIX shell
          > (space and double quote in particular).[/color]

          The best solution is often to bypass the shell (os.system) and execute
          the external command you want directly with os.spawnv. In this case the
          arguments are passed as a list of strings rather than a space-separated
          string and therefore need no quoting.

          Oren

          Comment

          • David M. Wilson

            #6
            Re: Shell quoting as part of the standard library?

            "Francis Avila" <francisgavila@ yahoo.com> wrote...
            [color=blue]
            > Why don't you just let the shell deal with it?
            >
            > ...
            >[/color]

            That is a very simple solution I have never seen used before. However,
            it does not solve my original problem. The solution as I have now been
            educated, is mkarg() from the commands module.

            Thanks,


            David.

            Comment

            • David M. Wilson

              #7
              Re: Shell quoting as part of the standard library?

              Oren Tirosh <oren-py-l@hishome.net> wrote...
              [color=blue]
              > The best solution is often to bypass the shell (os.system) and execute
              > the external command you want directly with os.spawnv. In this case the
              > arguments are passed as a list of strings rather than a space-separated
              > string and therefore need no quoting.[/color]

              That would be a fine solution, except again it does not solve my
              problem. In the latest application of shell quoting, I needed to
              insert filenames into a shell excerpt provided by a user who is
              knowledgeable in shell.

              They could write, for instance:

              process_app %(input_filenam e) || something %(blah)
              [ -s %(output_filena me) ] && ...


              But it would be above them to express that in Python, so bypassing the
              shell is not an option. Thanks,


              David.

              Comment

              Working...