# p4.py -- Python interface to Perforce # Gareth Rees, Ravenbrook Limited, 2000-09-04 # $Id: //info.ravenbrook.com/project/p4dti/branch/2000-09-05/interface-debugging/python-perforce-interface/p4.py#1 $ import marshal, os, win32pipe # The path to the Python marshalling Perforce client executable. p4_executable = 'c:/p4/project/p4dti/import/2000-07-31/p4beta16490/p4beta16490.exe' # run(id, arguments, input) runs the p4 client with the given arguments. # The arguments should be a Perforce command and its arguments, like # "jobs -o //foo/...". Options should generally include -i and/or -o to # avoid forms being put up interactively. If input is supplied, then it # should be a list of dictionaries. These dictionaries are sent one by # one to the process. The results are read into a list and returned. # Since the output of the process is read to EOF and the input is # closed, there should be no process left hanging about. The id # parameter is an string used as part of the name of a temporary file. def run(id, port, user, client, arguments, input = None): temp_filename = None if input: if (os.environ.has_key['TMPDIR']): temp_dir = os.environ['TMPDIR'] elif (os.environ.has_key['TMP']): temp_dir = os.environ['TMP'] elif (os.environ.has_key['TEMP']): temp_dir = os.environ['TEMP'] else: temp_dir = "c:/temp" temp_filename = '%s/p4dti_%s_data' % (temp_dir, replicator_id) temp_file = open(temp_filename, 'w') marshal.dump(input[0], temp_file) temp_file.close() command = "%s -G -p %s -u %s -c %s %s < %s" \ % (p4_executable, port, user, client, arguments, temp_filename) else: command = "%s -G -p %s -u %s -c %s" \ % (p4_executable, port, user, client, arguments) stream = win32pipe.popen(command, 'r') results = [] while 1==1: try: results = results + [marshal.load(stream)] except EOFError: if (temp_filename): os.remove(temp_filename) return results