FAQ
List,

First I'm very new to Python. I usually do this kind of thing with shell
scripts, however, I'm trying to move to using python primarily so I can
learn the language. I'm attempting to write a script that will check a
server for various configuration settings and report and/or change based on
those findings. I know there are other tools that can do this, but for
various reasons I must script this. I have come up with two ways to
accomplish what I'm trying to do and I'm looking for the more elegant
solution -- or if there is another solution that I have not thought of. Any
input would be greatly appreciated and thanks for your time.

method 1:
=============
[master script]

def execute_script(cscript):
process = subprocess.Popen(cscript, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output_data = process.communicate()

if process.returncode == 0:
return "OK"
elif process.returncode == 90:
return "DEFER"
elif process.returncode == 80:
return "UPDATED"
else:
return "ERROR"

if __name__ == '__main__':

# Find all configuration scripts
print "Searching for config scripts"
cscripts = []
for config_script in os.listdir(BASE_DIR + "/config.d"):
cscripts.append(BASE_DIR + '/config.d/' + config_script)
print "Found %d configuration scripts" % (len(cscripts))

# Loop through scripts and execute
for config_script in cscripts:
action_title = "Checking %s" % (config_script[len(BASE_DIR +
'/config.d/'):])
print "%-75.75s [ %s ]" % (action_title,
execute_script(config_script))

[config script template]
#!/usr/bin/env python

def check():
# do some checking
return results

return check()



method 2:
============
[master script]
if __name__ == '__main__':

# Find all configuration scripts
print "Searching for config scripts"
cscripts = []
for config_script in os.listdir(BASE_DIR + "/config.d"):
cscripts.append(BASE_DIR + '/config.d/' + config_script)
print "Found %d configuration scripts" % (len(cscripts))

# Loop through scripts and execute
for config_script in cscripts:
import config_script
action_title = "Checking %s" % (config_script[len(BASE_DIR +
'/config.d/'):])
print "%-75.75s [ %s ]" % (action_title, config_script.check())


[config script template]
#!/usr/bin/env python

def check():
# do some stuff
return result # return str OK, DEFER, UPDATED or ERROR
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110620/f93c4d5f/attachment.html>

Search Discussions

  • Chris Angelico at Jun 20, 2011 at 4:26 am

    On Mon, Jun 20, 2011 at 2:13 PM, Stephen Bunn wrote:
    List,

    ? First I'm very new to Python. I usually do this kind of thing with shell
    scripts, however, I'm trying to move to using python primarily so I can
    learn the language.
    A worthy cause :)
    ...? I have come up with two ways to
    accomplish what I'm trying to do and I'm looking for the more elegant
    solution -- or if there is another solution that I have not thought of.? Any
    input would be greatly appreciated and thanks for your time.
    Your first method is language-independent. The underlying scripts
    merely need to be legal shell scripts and they will work. This is a
    measure of flexibility that may be of value.

    Your second method, on the other hand, avoids reinvoking Python and
    myriad processes, which may work out to be rather faster. Also, you
    could have the scripts communicate back to the main module more
    easily.

    I'd be inclined toward the second solution if I'm writing all the code
    myself, but very definitely the first if someone else might write one
    of the subscripts (especially so if this is going to be distributed
    widely) - spawning a new process means that the system's isolation of
    processes keeps your system safe. If you don't need that measure of
    protection, I would still surround the import and check() call with
    try/except and gracefully handle any errors.

    There's many ways these things can be done, but I think you've already
    hit on the two broad types (import the code, or use stdout/rc).

    Chris Angelico
  • Florencio Cano at Jun 20, 2011 at 6:21 am

    I'd be inclined toward the second solution if I'm writing all the code
    myself, but very definitely the first if someone else might write one
    of the subscripts (especially so if this is going to be distributed
    widely) - spawning a new process means that the system's isolation of
    processes keeps your system safe. If you don't need that measure of
    protection, I would still surround the import and check() call with
    try/except and gracefully handle any errors.
    I'm with Chris, if the config_scripts are going to be implemented in
    Python always, the second solution is better for me as the operative
    system is less involved. But if you would like to have scripts with
    different nature, the first one is best. What about to implement the
    two alternatives depending on the config_script type?
  • Stephen Bunn at Jun 20, 2011 at 6:51 am

    On Mon, Jun 20, 2011 at 1:26 PM, Chris Angelico wrote:

    I'd be inclined toward the second solution if I'm writing all the code
    myself
    On Mon, Jun 20, 2011 at 3:21 PM, Florencio Cano wrote:
    I'm with Chris, if the config_scripts are going to be implemented in
    Python always, the second solution is better for me as the operative
    system is less involved.
    Thanks for the replies. I would like to use the second method because I plan
    to implement everything in python. Unfortunately I have not been able to
    work out how to get the imports to work.

    import config_script obviously doesn't work and __import__(config_script)
    works from the python interpreter but fails in the script (ImportError:
    Import by filename is not supported.)
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20110620/1c3818e2/attachment.html>
  • Florencio Cano at Jun 20, 2011 at 2:19 pm

    Unfortunately I have not been able to
    work out how to get the imports to work.

    import config_script obviously doesn't work and __import__(config_script)
    works from the python interpreter but fails in the script (ImportError:
    Import by filename is not supported.)
    You can use this:

    exec("import " + module_name)
  • Stephen Bunn at Jun 21, 2011 at 12:34 am

    On Mon, Jun 20, 2011 at 11:19 PM, Florencio Cano wrote:
    import config_script obviously doesn't work and __import__(config_script)
    works from the python interpreter but fails in the script (ImportError:
    Import by filename is not supported.)
    You can use this:

    exec("import " + module_name)

    Thanks for the hint. I will give that a try.
    On Tue, Jun 21, 2011 at 2:48 AM, Terry Reedy wrote:


    The main problem of your second method is that you repeat the check
    function in each script. That will be a problem if you ever want to modify
    it. I might put is into a separate file and import into each script.
    repeating the check function in each script is what I think I want. The
    check function is what would scan the system and make sure that compliance
    is met. I'm slightly confused about how that would become an issue later and
    what you mean about importing each of them ????

    import config_script obviously doesn't work and
    __import__(config_script) works from the python interpreter but fails in
    the script (ImportError: Import by filename is not supported.)
    Look at builtin function __import__(filename).
    Terry Jan Reedy

    I looked into __import__(filename) but that was not working for me, based
    upon the error in my previous message. I will give Florencio Cano's solution
    of using exec a try. If there are any other ideas out there I'm all ears :)

    Thanks everybody for the replies and guidance.
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20110621/72d00dda/attachment.html>
  • Terry Reedy at Jun 20, 2011 at 5:48 pm

    On 6/20/2011 2:51 AM, Stephen Bunn wrote:

    Thanks for the replies. I would like to use the second method because I
    plan to implement everything in python.
    The main problem of your second method is that you repeat the check
    function in each script. That will be a problem if you ever want to
    modify it. I might put is into a separate file and import into each script.

    Unfortunately I have not been
    able to work out how to get the imports to work.

    import config_script obviously doesn't work and
    __import__(config_script) works from the python interpreter but fails in
    the script (ImportError: Import by filename is not supported.)
    Look at builtin function __import__(filename).


    --
    Terry Jan Reedy

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJun 20, '11 at 4:13a
activeJun 21, '11 at 12:34a
posts7
users4
websitepython.org

People

Translate

site design / logo © 2018 Grokbase