#!/usr/local/bin/python """A Python version of the Perforce "p4" client. This uses the Python type P4Client, which is a wrapper for the Perforce ClientApi object. It includes the "ClientUser" class, which is a Python version of the default ClientUser from Perforce. Copyright 1999, Mike Meyer. All rights reserved. Modified by Robert Cowham, 2004. See the comment in the source for redistribution permission. """ # License: # This file and any derivatives or translations of it may be freely # copied and redistributed so long as: # 1) This license and copyright notice are not changed. # 2) Any fixes or enhancements are reported back to either the # author (mwm@phone.net). # and any of: # a) The source is redistributed with it. # b) The source is compiled unmodified, and instructions for finding # the original source are included. # c) The source is made available by some other means. import sys, os, string import P4Client class P4ClientHandler: """Class to wrap P4Client.""" p4attributes = ["Cwd", "Charset", "Port", "Client", "User", "Password", "Host", "Language", "Os"] def __init__(self, **options): "Create the P4Client object I'm wrapping." self.p4 = P4Client.P4Client(self) def __getattr__(self, name): if name in self.p4attributes: return getattr(self.p4, name) else: return self.__dict__[name] def __setattr__(self, name, value): if name in self.p4attributes: setattr(self.p4, name, value) else: self.__dict__[name] = value def Connect(self): return self.p4.Init() def Dropped(self): "Check to see if the P4Client connection has been dropped." return self.p4.Dropped() def Final(self): "Close the connection to the P4Client." return self.p4.Final() def Run(self, command, args=None): "Run the given command, parsing output with the named parser." self.data = [] if args == None: args = [] self.p4.SetArgv(args) self.p4.Run(command) return self.data def OutputInfo(self, data, level): "Parse the given data, adding it to self data." for line in string.split(data, '\n'): self.data.append(line) def OutputText(self, text): "Adds text lines to the output data." for line in string.split(text, '\n'): self.data.append(line) def OutputStat(self, dict): "Process dictionary of file status info" text = "otherOpen" test = len(text) for key, value in dict.items(): if len(key) > test and key[:test] == text: self.OutputInfo("%s %s" % (key, value), '2') else: self.OutputInfo("%s %s" % (key, value), '1') def HandleError(self, text, severity = None): "Raise an error from P4." raise P4Client.error(text) class P4: """Class to provide nice interface to P4.""" def __init__(self): self.client = P4ClientHandler() def connect(self): return self.client.Connect() def __getattr__(self, name): if name <> "client": return getattr(self.client, name) else: return self.__dict__[name] def __setattr__(self, name, value): "Allow user to set things like 'port' etc." if name <> "client": setattr(self.client, name, value) else: self.__dict__[name] = value def run(self, command, args): return self.client.Run(command, args) if __name__ == "__main__": p4 = P4() p4.Port = "1666" p4.User = "robert" p4.Client = "bruno_ws" p4.connect() ret = p4.run(sys.argv[1], sys.argv[2:]) for line in ret: print line print "Host: ", p4.Host print "Os: ", p4.Os