Running Python scripts from BASH

Discussion in 'Python' started by Ishpeck, Feb 27, 2007.

  1. Ishpeck

    Ishpeck Guest

    I'm using Python to automate testing software for my company. I
    wanted the computers in my testing lab to automatically fetch the
    latest version of the python scripts from a CVS repository and then
    ask a local database server which of the scripts to run.

    I built the following:

    #!/bin/bash
    # Batcher will run the specified scripts.

    cvs update

    while true
    do
    # This part makes sure that
    # every hour or so, we get the latest
    # snapshot of the suite from CVS.
    if [ $(date +%M) = 0 ]; then
    cvs update
    sleep 360
    fi
    # Then we grab the name of
    # a randomly-selected script
    i=$(python randomRun.py)
    # If the return-value of randomRun.py
    #is empty, we don't run it.
    if ["$i"=""]; then
    echo Not running anything
    sleep 3600
    # If randomRun doesn't return
    # empty, we run the script that it prints.
    else
    python "$i";
    sleep 2
    fi
    done


    --------- END BASH FILE --------

    For debugging purposes, you can just build "randomRun.py" to do the
    following:

    print "randomRun.py"

    It's silly but works.

    Whenever I run this script, Python decides that it doesn't like the
    way BASH feeds it the name of the script. I get the following
    message:

    ': [Errno 22] Invalid argumentopen file 'foo.py

    I dunno. Maybe this is a better question for a BASH-related group.
    How do I get around this?
     
    Ishpeck, Feb 27, 2007
    #1
    1. Advertising

  2. Ishpeck

    Ishpeck Guest

    It may be worth noting that I'm running Cygwin on WindowsXP
    professional.
     
    Ishpeck, Feb 27, 2007
    #2
    1. Advertising

  3. Ishpeck

    Ben Finney Guest

    "Ishpeck" <> writes:

    > I'm using Python to automate testing software for my company. I
    > wanted the computers in my testing lab to automatically fetch the
    > latest version of the python scripts from a CVS repository and then
    > ask a local database server which of the scripts to run.


    You may be interested in experimenting with the Buildbot for this
    purpose.

    <URL:http://buildbot.sourceforge.net/>

    --
    \ "Madness is rare in individuals, but in groups, parties, |
    `\ nations and ages it is the rule." -- Friedrich Nietzsche |
    _o__) |
    Ben Finney
     
    Ben Finney, Feb 27, 2007
    #3
  4. Ishpeck

    James Stroud Guest

    Ishpeck wrote:
    > I'm using Python to automate testing software for my company. I
    > wanted the computers in my testing lab to automatically fetch the
    > latest version of the python scripts from a CVS repository and then
    > ask a local database server which of the scripts to run.
    >
    > I built the following:
    >
    > #!/bin/bash
    > # Batcher will run the specified scripts.
    >
    > cvs update
    >
    > while true
    > do
    > # This part makes sure that
    > # every hour or so, we get the latest
    > # snapshot of the suite from CVS.
    > if [ $(date +%M) = 0 ]; then
    > cvs update
    > sleep 360
    > fi
    > # Then we grab the name of
    > # a randomly-selected script
    > i=$(python randomRun.py)
    > # If the return-value of randomRun.py
    > #is empty, we don't run it.
    > if ["$i"=""]; then
    > echo Not running anything
    > sleep 3600
    > # If randomRun doesn't return
    > # empty, we run the script that it prints.
    > else
    > python "$i";
    > sleep 2
    > fi
    > done
    >
    >
    > --------- END BASH FILE --------
    >
    > For debugging purposes, you can just build "randomRun.py" to do the
    > following:
    >
    > print "randomRun.py"
    >
    > It's silly but works.
    >
    > Whenever I run this script, Python decides that it doesn't like the
    > way BASH feeds it the name of the script. I get the following
    > message:
    >
    > ': [Errno 22] Invalid argumentopen file 'foo.py
    >
    > I dunno. Maybe this is a better question for a BASH-related group.
    > How do I get around this?
    >


    Perhaps code the whole thing in python and not bash/python. Don't send a
    boy in to do a man's job.

    All you need to remember is

    import os
    [....]
    os.system('whatever command here')

    James
     
    James Stroud, Feb 27, 2007
    #4
  5. Ishpeck

    Ross Ridge Guest

    Ishpeck <> wrote:
    >': [Errno 22] Invalid argumentopen file 'foo.py


    The problem is that Python is using the standard Windows CRLF line
    endings, while Cygwin bash expects Unix LF-only line endings. Your script
    ends up trying run the script "foo.y\r" instead of "foo.y", and since
    CR is isn't allowed in Windows filename you get the "Invalid argument"
    error when Python tries to open the file.

    >Maybe this is a better question for a BASH-related group.


    The Cygwin list probably would've been the best.

    >How do I get around this?


    Don't mix Cygwin tools and native Windows tools. Either use the Cygwin
    version of Python or don't use Cygwin bash.

    Ross Ridge
     
    Ross Ridge, Feb 28, 2007
    #5
  6. "Ishpeck" <> wrote:

    8<--------------- a bash problem ---------------------

    If it were Python, the advice would have been to use the
    print statement to figure out what the content of the
    variables were.

    As it is Bash, you may have to stoop to something like
    echo to see what is in $i...

    hth - Hendrik
     
    Hendrik van Rooyen, Feb 28, 2007
    #6
  7. Ishpeck

    ina Guest

    On Feb 27, 11:16 pm, "Hendrik van Rooyen" <>
    wrote:
    > "Ishpeck" <> wrote:
    >
    > 8<--------------- a bash problem ---------------------
    >
    > If it were Python, the advice would have been to use the
    > print statement to figure out what the content of the
    > variables were.
    >
    > As it is Bash, you may have to stoop to something like
    > echo to see what is in $i...
    >
    > hth - Hendrik


    When you print the string in bash it looks correct. It isn't until you
    try to execute it you have the problem.

    os.system is the solution to this problem anyway.
     
    ina, Feb 28, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William Park

    (patch for Bash) GTK+2 + Bash

    William Park, Jul 11, 2005, in forum: XML
    Replies:
    3
    Views:
    2,331
    William Park
    Jul 14, 2005
  2. Jan Danielsson
    Replies:
    8
    Views:
    666
    Mike Meyer
    Jul 22, 2005
  3. Replies:
    7
    Views:
    617
    Fredrik Lundh
    Jan 8, 2006
  4. davidj411
    Replies:
    0
    Views:
    578
    davidj411
    Jun 27, 2008
  5. Greg Hauptmann
    Replies:
    13
    Views:
    236
    ruud grosmann
    Jul 14, 2008
Loading...

Share This Page