/*******************************************************************************
Copyright (c) 1997-2004, Perforce Software, Inc. All rights reserved.
Portions Copyright (c) 1999, Mike Meyer. All rights reserved.
Portions Copyright (c) 2004-2007, Robert Cowham. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTR
IBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
$Id: //depot/r07.3/p4-python/PythonClientAPI.cpp#1 $
*******************************************************************************/
#include <Python.h>
#include "undefdups.h"
#include <clientapi.h>
#include <i18napi.h>
#include <strtable.h>
#include <enviro.h>
#include <hostenv.h>
#include <spec.h>
#include <debug.h>
#include "P4Result.h"
#include "SpecMgr.h"
#include "PythonClientUser.h"
#include "PythonClientAPI.h"
#include "P4PythonDebug.h"
#include <iostream>
#define M_TAGGED 0x01
#define M_PARSE_FORMS 0x02
#define IS_TAGGED(x) (x & M_TAGGED )
#define IS_PARSE_FORMS(x) (x & M_PARSE_FORMS )
using namespace std;
PythonClientAPI::PythonClientAPI(PyObject * pyErr) : ui(&specMgr)
{
connected = false;
debug = 0;
server2 = 0;
depth = 0;
exceptionLevel = 2;
maxResults = 0;
maxScanRows = 0;
maxLockTime = 0;
prog = "unnamed p4-python script";
p4Error = pyErr;
apiLevel = atoi( P4Tag::l_client );
// Enable form parsing, and set tagged mode on by default
mode = M_PARSE_FORMS + M_TAGGED ;
client.SetProtocol( "specstring", "" );
//
// Load the current ticket file. Start with the default, and then
// override it if P4TICKETS is set.
//
HostEnv henv;
Enviro enviro;
const char *t;
henv.GetTicketFile( ticketFile );
if( (t = enviro.Get( "P4TICKETS" )) ) {
ticketFile = t;
}
}
PythonClientAPI::~PythonClientAPI()
{
if( connected ) {
Error e;
client.Final( &e );
// Ignore errors
}
}
PythonClientAPI::intattribute_t PythonClientAPI::intattributes[] = {
{ "tagged", &PythonClientAPI::SetTagged, &PythonClientAPI::GetTagged },
{ "api_level", &PythonClientAPI::SetApiLevel, &PythonClientAPI::GetApiLevel },
{ "maxresults", &PythonClientAPI::SetMaxResults, &PythonClientAPI::GetMaxResults },
{ "maxscanrows", &PythonClientAPI::SetMaxScanRows, &PythonClientAPI::GetMaxScanRows },
{ "maxlocktime", &PythonClientAPI::SetMaxLockTime, &PythonClientAPI::GetMaxLockTime },
{ "exception_level", &PythonClientAPI::SetExceptionLevel, &PythonClientAPI::GetExceptionLevel },
{ "debug", &PythonClientAPI::SetDebug, &PythonClientAPI::GetDebug },
{ "server_level", NULL, &PythonClientAPI::GetServerLevel },
{ NULL, NULL, NULL }, // guard
};
PythonClientAPI::strattribute_t PythonClientAPI::strattributes[] = {
{ "charset", &PythonClientAPI::SetCharset, &PythonClientAPI::GetCharset },
{ "client", &PythonClientAPI::SetClient, &PythonClientAPI::GetClient },
{ "p4config_file", NULL, &PythonClientAPI::GetConfig },
{ "cwd", &PythonClientAPI::SetCwd, &PythonClientAPI::GetCwd },
{ "host", &PythonClientAPI::SetHost, &PythonClientAPI::GetHost },
{ "language", &PythonClientAPI::SetLanguage, &PythonClientAPI::GetLanguage },
{ "port", &PythonClientAPI::SetPort, &PythonClientAPI::GetPort },
{ "prog", &PythonClientAPI::SetProg, &PythonClientAPI::GetProg },
{ "ticket_file", &PythonClientAPI::SetTicketFile, &PythonClientAPI::GetTicketFile },
{ "password", &PythonClientAPI::SetPassword, &PythonClientAPI::GetPassword },
{ "user", &PythonClientAPI::SetUser, &PythonClientAPI::GetUser },
{ "version", &PythonClientAPI::SetVersion, &PythonClientAPI::GetVersion },
{ NULL, NULL, NULL }, // guard
};
PythonClientAPI::objattribute_t PythonClientAPI::objattributes[] = {
{ "input", &PythonClientAPI::SetInput, &PythonClientAPI::GetInput },
{ "errors", NULL, &PythonClientAPI::GetErrors },
{ "warnings", NULL, &PythonClientAPI::GetWarnings },
{ "__members__", NULL, &PythonClientAPI::GetMembers },
{ NULL, NULL, NULL }, // guard
};
PythonClientAPI::intattribute_t * PythonClientAPI::GetInt(const char * forAttr)
{
PythonClientAPI::intattribute_t * ptr = PythonClientAPI::intattributes;
while (ptr->attribute != NULL) {
if( !strcmp(forAttr, ptr->attribute) ) {
return ptr;
}
ptr++;
}
return NULL;
}
PythonClientAPI::strattribute_t * PythonClientAPI::GetStr(const char * forAttr)
{
PythonClientAPI::strattribute_t * ptr = PythonClientAPI::strattributes;
while (ptr->attribute != NULL) {
if( !strcmp(forAttr, ptr->attribute) ) {
return ptr;
}
ptr++;
}
return NULL;
}
PythonClientAPI::objattribute_t * PythonClientAPI::GetObj(const char * forAttr)
{
PythonClientAPI::objattribute_t * ptr = PythonClientAPI::objattributes;
while (ptr->attribute != NULL) {
if( !strcmp(forAttr, ptr->attribute) ) {
return ptr;
}
ptr++;
}
return NULL;
}
PythonClientAPI::intsetter PythonClientAPI::GetIntSetter(const char * forAttr)
{
PythonClientAPI::intattribute_t * ptr = PythonClientAPI::GetInt(forAttr);
if (ptr) {
return ptr->setter;
}
return NULL;
}
PythonClientAPI::intgetter PythonClientAPI::GetIntGetter(const char * forAttr)
{
PythonClientAPI::intattribute_t * ptr = PythonClientAPI::GetInt(forAttr);
if (ptr) {
return ptr->getter;
}
return NULL;
}
PythonClientAPI::strsetter PythonClientAPI::GetStrSetter(const char * forAttr)
{
PythonClientAPI::strattribute_t * ptr = PythonClientAPI::GetStr(forAttr);
if (ptr) {
return ptr->setter;
}
return NULL;
}
PythonClientAPI::strgetter PythonClientAPI::GetStrGetter(const char * forAttr)
{
PythonClientAPI::strattribute_t * ptr = PythonClientAPI::GetStr(forAttr);
if (ptr) {
return ptr->getter;
}
return NULL;
}
PythonClientAPI::objsetter PythonClientAPI::GetObjSetter(const char * forAttr)
{
PythonClientAPI::objattribute_t * ptr = PythonClientAPI::GetObj(forAttr);
if (ptr) {
return ptr->setter;
}
return NULL;
}
PythonClientAPI::objgetter PythonClientAPI::GetObjGetter(const char * forAttr)
{
PythonClientAPI::objattribute_t * ptr = PythonClientAPI::GetObj(forAttr);
if (ptr) {
return ptr->getter;
}
return NULL;
}
// Returns an array of string pointers with the attributes defined
// in P4API, such as "client" or "port"
// Ownership of returned list is passed to caller!
// Free it, keep it or suffer memory leak!
const char ** PythonClientAPI::GetAttributes()
{
size_t intAttrCount = 0;
size_t strAttrCount = 0;
size_t objAttrCount = 0;
for (PythonClientAPI::intattribute_t * pi = PythonClientAPI::intattributes;
pi->attribute != NULL; pi++)
{
intAttrCount++;
}
for (PythonClientAPI::strattribute_t * ps = PythonClientAPI::strattributes;
ps->attribute != NULL; ps++)
{
strAttrCount++;
}
for (PythonClientAPI::objattribute_t * po = PythonClientAPI::objattributes;
po->attribute != NULL; po++)
{
objAttrCount++;
}
size_t total = intAttrCount + strAttrCount + + objAttrCount + 1;
const char ** result = (const char **) malloc(total * sizeof(const char *));
const char **ptr = result;
for (PythonClientAPI::intattribute_t * pi = PythonClientAPI::intattributes;
pi->attribute != NULL; pi++)
{
*ptr = pi->attribute; ptr++;
}
for (PythonClientAPI::strattribute_t * ps = PythonClientAPI::strattributes;
ps->attribute != NULL; ps++)
{
*ptr = ps->attribute; ptr++;
}
for (PythonClientAPI::objattribute_t * po = PythonClientAPI::objattributes;
po->attribute != NULL; po++)
{
*ptr = po->attribute; ptr++;
}
*ptr = NULL;
return result;
}
int PythonClientAPI::SetTagged( int enable )
{
if( enable )
mode |= M_TAGGED;
else
mode &= ~M_TAGGED;
return 0;
}
int PythonClientAPI::GetTagged()
{
return mode & M_TAGGED;
}
int PythonClientAPI::SetCharset( const char *c )
{
if( P4PYDBG_COMMANDS )
cerr << "[P4] Setting charset: " << c << endl;
CharSetApi::CharSet cs = CharSetApi::Lookup( c );
if( cs < 0 )
{
if( exceptionLevel )
{
StrBuf m;
m = "Unknown or unsupported charset: ";
m.Append( c );
Except( "P4.charset", m.Text() );
return -1;
}
return -1;
}
charset = c;
client.SetTrans( cs, cs, cs, cs );
return 0;
}
int PythonClientAPI::SetTicketFile( const char *p )
{
client.SetTicketFile( p );
ticketFile = p;
return 0;
}
int PythonClientAPI::SetDebug( int d )
{
debug = d;
ui.SetDebug( d );
specMgr.SetDebug( d );
if( P4PYDBG_RPC )
p4debug.SetLevel( "rpc=5" );
else
p4debug.SetLevel( "rpc=0" );
return 0;
}
int PythonClientAPI::SetApiLevel( int level )
{
StrBuf b;
b << level;
apiLevel = level;
client.SetProtocol( "api", b.Text() );
return 0;
}
PyObject * PythonClientAPI::GetMembers() {
PyObject * memberList = PyList_New(0); // empty list
const char ** members = PythonClientAPI::GetAttributes();
for (int i = 0; members[i] != NULL; ++i) {
const char * member = members[i];
PyList_Append(memberList, PyString_FromString(member));
}
return memberList;
}
PyObject * PythonClientAPI::Connect()
{
if ( P4PYDBG_COMMANDS )
cerr << "[P4] Connecting to Perforce" << endl;
if ( connected )
{
(void) PyErr_WarnEx( PyExc_UserWarning,
"P4.connect() - Perforce client already connected!", 1 );
Py_RETURN_NONE;
}
Error e;
client.Init( &e );
if ( e.Test() && exceptionLevel ) {
Except( "P4.connect()", &e );
return NULL;
}
if ( e.Test() )
Py_RETURN_FALSE;
connected = true;
Py_RETURN_NONE;
}
PyObject * PythonClientAPI::Connected()
{
if ( connected && !client.Dropped() ) {
Py_RETURN_TRUE;
}
Py_RETURN_FALSE;
}
PyObject * PythonClientAPI::Disconnect()
{
if ( P4PYDBG_COMMANDS )
cerr << "[P4] Disconnect" << endl;
if ( ! connected )
{
(void) PyErr_WarnEx( PyExc_UserWarning,
"P4.disconnect() - Not connected!", 1 );
Py_RETURN_NONE;
}
Error e;
client.Final( &e );
// Clear the specdef cache.
specMgr.Reset();
connected = false;
Py_RETURN_NONE;
}
PyObject * PythonClientAPI::Run( const char *cmd, int argc, char * const *argv )
{
// Save the entire command string for our error messages. Makes it
// easy to see where a script has gone wrong.
StrBuf cmdString;
cmdString << "\"p4 " << cmd;
for( int i = 0; i < argc; i++ )
cmdString << " " << argv[ i ];
cmdString << "\"";
if ( P4PYDBG_COMMANDS )
cerr << "[P4] Executing " << cmdString.Text() << endl;
if ( depth )
{
(void) PyErr_WarnEx( PyExc_UserWarning,
"P4.run() - Can't execute nested Perforce commands.", 1 );
Py_RETURN_FALSE;
}
if ( ! connected && exceptionLevel ) {
Except( "P4.run()", "not connected." );
return NULL;
}
if ( ! connected )
Py_RETURN_FALSE;
// Clear out any results from the previous command
ui.Reset();
// Tell the UI which command we're running.
ui.SetCommand( cmd );
depth++;
RunCmd( cmd, &ui, argc, argv );
depth--;
P4Result &results = ui.GetResults();
if ( results.ErrorCount() && exceptionLevel ) {
Except( "P4#run", "Errors during command execution", cmdString.Text() );
return NULL;
}
if ( results.WarningCount() && exceptionLevel > 1 ) {
Except( "P4#run", "Warnings during command execution",cmdString.Text());
return NULL;
}
return results.GetOutput();
}
int PythonClientAPI::SetInput( PyObject * input )
{
if ( P4PYDBG_COMMANDS )
cerr << "[P4] Received input for next command" << endl;
if ( ! ui.SetInput( input ) )
{
if ( exceptionLevel ) {
Except( "P4#input", "Error parsing supplied data." );
return -1;
}
else {
return -1;
}
}
return 0;
}
PyObject * PythonClientAPI::GetInput()
{
return ui.GetInput();
}
//
// Parses a string supplied by the user into a dict. To do this we need
// the specstring from the server. We try to cache those as we see them,
// but the user may not have executed any commands to allow us to cache
// them so we may have to fetch the spec first.
//
PyObject * PythonClientAPI::ParseSpec( const char * type, const char *form )
{
if ( !specMgr.HaveSpecDef( type ) )
{
if( exceptionLevel )
{
StrBuf m;
m = "No spec definition for ";
m.Append( type );
m.Append( " objects." );
Except( "P4.parse_spec()", m.Text() );
return NULL;
}
else
{
Py_RETURN_FALSE;
}
}
// Got a specdef so now we can attempt to parse it.
Error e;
PyObject * v = specMgr.StringToSpec( type, form, &e );
if ( e.Test() )
{
if( exceptionLevel ) {
Except( "P4.parse_spec()", &e );
return NULL;
}
else {
Py_RETURN_FALSE;
}
}
return v;
}
//
// Converts a dict supplied by the user into a string using the specstring
// from the server. We may have to fetch the specstring first.
//
PyObject * PythonClientAPI::FormatSpec( const char *type, PyObject * dict )
{
if ( !specMgr.HaveSpecDef( type ) )
{
if( exceptionLevel )
{
StrBuf m;
m = "No spec definition for ";
m.Append( type );
m.Append( " objects." );
Except( "P4.format_spec()", m.Text() );
return NULL;
}
else
{
Py_RETURN_FALSE;
}
}
// Got a specdef so now we can attempt to convert.
StrBuf buf;
Error e;
specMgr.SpecToString( type, dict, buf, &e );
if( !e.Test() ) {
return PyString_FromString( buf.Text() );
}
if( exceptionLevel )
{
StrBuf m;
m = "Error converting hash to a string.";
if( e.Test() ) e.Fmt( m, EF_PLAIN );
Except( "P4.format_spec()", m.Text() );
return NULL;
}
Py_RETURN_NONE;
}
//
// Returns a dict whose keys contain the names of the fields in a spec of the
// specified type. Not yet exposed to Python clients, but may be in future.
//
PyObject * PythonClientAPI::SpecFields( const char * type )
{
if ( !specMgr.HaveSpecDef( type ) )
{
if( exceptionLevel )
{
StrBuf m;
m = "No spec definition for ";
m.Append( type );
m.Append( " objects." );
Except( "P4.spec_fields()", m.Text() );
return NULL;
}
else
{
Py_RETURN_FALSE;
}
}
return specMgr.SpecFields( type );
}
void PythonClientAPI::Except( const char *func, const char *msg )
{
StrBuf m;
StrBuf errors;
StrBuf warnings;
bool terminate = false;
m << "[" << func << "] " << msg;
// Now append any errors and warnings to the text
ui.GetResults().FmtErrors( errors );
ui.GetResults().FmtWarnings( warnings );
if( errors.Length() )
{
m << "\n" << errors;
terminate= true;
}
if( exceptionLevel > 1 && warnings.Length() )
{
m << "\n" << warnings;
terminate = true;
}
if( terminate )
m << "\n\n";
PyErr_SetString(p4Error, m.Text() );
}
void PythonClientAPI::Except( const char *func, Error *e )
{
StrBuf m;
e->Fmt( &m );
Except( func, m.Text() );
}
void PythonClientAPI::Except( const char *func, const char *msg, const char *cmd )
{
StrBuf m;
m << msg;
m << "( " << cmd << " )";
Except( func, m.Text() );
}
//
// RunCmd is a private function to work around an obscure protocol
// bug in 2000.[12] servers. Running a "p4 -Ztag client -o" messes up the
// protocol so if they're running this command then we disconnect and
// reconnect to refresh it. For efficiency, we only do this if the
// server2 protocol is either 9 or 10 as other versions aren't affected.
//
void PythonClientAPI::RunCmd(const char *cmd, ClientUser *ui, int argc, char * const *argv)
{
// #if P4APIVER_ID >= 513026
// ClientApi::SetProg() was introduced in 2004.2
client.SetProg( &prog );
// #endif
// #if P4APIVER_ID >= 513282
// ClientApi::SetVersion() was introduced in 2005.2
if( version.Length() )
client.SetVersion( &version );
// #endif
if( mode & M_TAGGED )
client.SetVar( "tag" );
// If maxresults or maxscanrows is set, enforce them now
if( maxResults ) client.SetVar( "maxResults", maxResults );
if( maxScanRows ) client.SetVar( "maxScanRows", maxScanRows );
if( maxLockTime ) client.SetVar( "maxLockTime", maxLockTime );
client.SetArgv( argc, argv );
client.Run( cmd, ui );
// Have to request server2 protocol *after* a command has been run. I
// don't know why, but that's the way it is.
if ( ! server2 )
{
StrPtr *pv = client.GetProtocol( "server2" );
if ( pv )
server2 = pv->Atoi();
}
if ( IS_TAGGED(mode) && StrRef( cmd ) == "client" &&
server2 >= 9 && server2 <= 10 )
{
if ( argc && ( StrRef( argv[ 0 ] ) == "-o" ) )
{
if ( P4PYDBG_COMMANDS )
cout << "Resetting client to avoid 2000.[12] protocol bug" << endl;
Error e;
client.Final( &e );
client.Init( &e );
// Pass any errors down to the UI, so they'll get picked up.
if ( e.Test() )
ui->HandleError( &e );
}
}
}