#!/usr/bin/python

#Generate a batchfile batch.py. Axel Brink 2007-10-29.

batchfile = "batch.py"
editor = "gedit"

import os
if os.path.exists(batchfile):
    print "Generates a batch.py. But remove the old %s first." % batchfile
else:
    file = open(batchfile, 'w')
    file.write(
"""#!/usr/bin/python
#Batch job script generated by Axel Brink's batchjob.py.
#Modify this for your needs.

import os, glob

def command(text):
    print "Command:", text
    err = 0
    #err = os.system(text)
    if err:
        print 'Error %i occurred during execution of "%s."' % (err, text)
    return err

pattern = "*"
for filename in glob.glob(pattern):
    #print "Processing %s..." % filename
    dirname, basename = os.path.split(filename)
    fileid, ext = os.path.splitext(basename)
    err = command("echo %s %s" % (filename, fileid))
    if err:
        print "Break."
        break
""")
    file.close()
    os.system('chmod +x ' + batchfile)
    print "%s generated." % batchfile
    print "Run %s to start the batch job." % batchfile
    print "Starting editor..."
    command = "%s %s &" % (editor, batchfile)
    print command
    os.system(command)
