#!/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
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#13 | 6134 | Robert Cowham |
1.1 - Added raise error on dropped connection - thanks to Ryan Blazecka - Added Solaris option to setup.py (and tidied it a little) |
||
#12 | 6044 | Robert Cowham | Change filetypes | ||
#11 | 6043 | Robert Cowham | Changed license (with Mike's agreement) to make it more easily distributable and also maintainable by Perforce | ||
#10 | 5755 | Robert Cowham |
Added set_protocol() and api() methods. Updated index.html and new Windows dist. |
||
#9 | 5754 | Robert Cowham |
Added new attributes (and tests): GetConfig SetProg SetVersion SetTicketFile |
||
#8 | 5745 | Robert Cowham |
- Added option to read api ver P4.p4_api_ver - Added tests for different P4 API builds Use macro stringification to get P4 API VER. |
||
#7 | 5726 | Robert Cowham |
Handle binary output (e.g. print), together with associated test |
||
#6 | 5057 | Robert Cowham |
- Added P4Error class and catch all errors - shouldn't see P4Client.error any more! - Raise the error when appropriate. - Added translate() function to allow working with Internationalised servers. - Fix problem with diff2 and diff |
||
#5 | 4770 | Robert Cowham |
Changed example to show better usage. Raise AttributeError if appropriate. |
||
#4 | 4766 | Robert Cowham |
Rather better documentation (and license and changelog). Reorganised dirs. Added .zip file and windows binary installer. |
||
#3 | 4759 | Robert Cowham | Added login processing. | ||
#2 | 4756 | Robert Cowham |
Revised substantially. Placeholder for docs. |
||
#1 | 4755 | Robert Cowham |
Rename //guest/robert_cowham/perforce/API/python/P4Client/... To //guest/robert_cowham/perforce/API/python/main/... |
||
//guest/robert_cowham/perforce/API/python/P4Client/p4.py | |||||
#3 | 4618 | Robert Cowham |
Note that the contents of p4.py is now in p4cmd.py as I want to re-use the name p4.py for the more general module case. Started converting package to be more like p4ruby and p4perl in the way it works. |
||
#2 | 4592 | Robert Cowham |
Untabbed. Added setup.py - works for ActiveState Python 2.3! |
||
#1 | 4591 | Robert Cowham | Branch for local mods. | ||
//public/perforce/api/python/P4Client/p4.py | |||||
#1 | 157 | Laura Wingerd |
Publish Mike Meyer's Python api interface. (And delete the stuff I branched into the wrong path.) |
||
//guest/mike_meyer/python/P4Client/p4.py | |||||
#2 | 135 | Mike Meyer |
Changed the P4Clientmodule::OutputInfo to swap the argument order. This allows the same routine to method OutputInfo and OutputText if the level argument is made optional. |
||
#1 | 129 | Mike Meyer | Initial public version. |