#!/usr/bin/python # Create the Setup.py file from the environment variables. TOKEN = '@' def CreateSetup(templateFileName, outFileName, lastCheckpoint = 0): ''' Creates a new setup file based on the environment variables. ''' import os if not os.path.exists(templateFileName): raise IOError, 11, "Input template file [%s] does not exist" % templateFileName # setup additional parameters tokens = os.environ.copy() expectedContent = ( 'P4D_DEPOTS', 'P4D_ROOT', 'P4D_REMOTEROOT', 'P4ENV', ) for x in expectedContent: if not tokens.has_key(x): tokens[x] = '' DEPOT_SETTINGS = '' REMOTEDEPOT_SETTINGS = '' for x in tokens['P4D_DEPOTS'].split(): DEPOT_SETTINGS = "%s\n '%s': '%s'," % ( DEPOT_SETTINGS, x, os.path.join(tokens['P4D_ROOT'], x)) REMOTEDEPOT_SETTINGS = "%s\n '%s': '%s'," % ( REMOTEDEPOT_SETTINGS, x, os.path.join(os.environ['P4D_REMOTEROOT'], x)) tokens['DEPOT_SETTINGS'] = DEPOT_SETTINGS tokens['REMOTEDEPOT_SETTINGS'] = REMOTEDEPOT_SETTINGS if not tokens.has_key('LAST_CHECKPOINT'): tokens['LAST_CHECKPOINT'] = str(lastCheckpoint) P4ENV_SETTINGS = '' for x in tokens['P4ENV'].split(): P4ENV_SETTINGS = "%s\n '%s': '%s'," % ( P4ENV_SETTINGS, x, os.environ[x]) tokens['P4ENV_SETTINGS'] = P4ENV_SETTINGS tf = open(templateFileName, 'r') of = open(outFileName, 'w+') for x in tf.readlines(): outl = '' currentToken = '' state = 0 for c in x: if state == 0: # normal text if c == TOKEN: state = 1 else: outl += c elif state == 1: # found first token if c == TOKEN: # escape the token outl += TOKEN else: currentToken = c state = 2 elif state == 2: # inside a token if c == TOKEN: state = 0 if tokens.has_key(currentToken): outl += tokens[currentToken] else: raise Exception, "Referenced token %s, but it wasn't found" % currentToken else: currentToken += c else: raise Exception, "Invalid parse state" if state == 1: # stand-alone token at the end. outl += TOKEN elif state == 2: # token + unescaped text outl += TOKEN + currentToken # else we're fine. of.write(outl) tf.close() of.close() if __name__ == '__main__': lastChangelist = 0 import sys if len(sys.argv) > 1 and len(sys.argv[1]) > 0: lastChangelist = int(sys.argv[1]) CreateSetup('Setup.py.template', 'Setup.py', lastChangelist)