Make-alike Python module

François Pinard pinard at iro.umontreal.ca
Sat Aug 12 16:04:20 EDT 2000


Hi, people.

For years, I've been growing Makefiles for synchronising my projects
between machines, rebuilding Web sites, and doing various other things;
and resorted to parallel Make builds to save my time, as I do these things
on many remote machines at once.  The output of parallel processes get all
mangled at times, to the point of becoming hard to decipher and sort out.
Also, there are also limits on what one can cleanly do with Makefiles.

So, for some while, I have not been satisfied, and decided to try rewriting
part of all this in Python.  It went surprisingly well!  At the hearth,
I wrote a simple Make-alike Python module for my needs, later used by some
other Python programs.  It is fairly straightforward and simple to use, so
let me share it with you.  I quickly looked around this morning, and most
Make-alike projects for Python are far more ambitious than my little thing.

I'm providing two attachments.  First is an example of usage, in the form of
a small program meant to execute a single command on many hosts.  Second is
the `make.py' file itself, which is rather small.  The documentation about
how to use it appears as a doc string in the module.  Comments welcome! :-)

-------------- next part --------------
#!/usr/bin/env python
# Copyright ? 2000 Progiciels Bourbeau-Pinard inc.
# Fran?ois Pinard <pinard at iro.umontreal.ca>, 2000.

"""\
Execute a given command on many hosts, using SSH.

Usage: partout [OPTION]... HOST_LIST COMMAND [ARGUMENT]...

  -j N      do not run more than N commands at once (default is no bound))

HOST_LIST is a comma-separated list of host names.
"""

import getopt, os, re, string, sys
import make

def main(*arguments):
    if not arguments:
        sys.stdout.write(__doc__)
        sys.exit(0)
    # Decode options.
    agents = 0
    options, arguments = getopt.getopt(arguments, 'j:')
    for option, value in options:
        if option == '-j':
            agents = int(value)
    assert len(arguments) >= 2, "Program needs more arguments."
    hosts = string.split(arguments[0], ',')
    quoted = "'%s'" % re.sub(r"([\'])", r"\\\1", string.join(arguments[1:]))
    # Launch execution.
    maker = make.Maker(logall=1, agents=agents)
    for host in hosts:
        Host(host, maker, None, host, quoted)
    apply(maker.run, hosts)

class Host(make.Rule):
    def action(self, host, quoted):
        self.do('ssh 2>&1 %s %s' % (host, quoted))

if __name__ == '__main__':
    apply(main, sys.argv[1:])
-------------- next part --------------
A non-text attachment was scrubbed...
Name: make.py.gz
Type: application/octet-stream
Size: 3240 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20000812/62039ab1/attachment.obj>
-------------- next part --------------

-- 
Fran?ois Pinard   http://www.iro.umontreal.ca/~pinard


More information about the Python-list mailing list