-
Notifications
You must be signed in to change notification settings - Fork 20
/
P4API.cpp
1500 lines (1269 loc) · 45.7 KB
/
P4API.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Python wrapper for the Perforce ClientApi object.
*
* Copyright (c) 2007-2015, 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/main/p4-python/P4API.cpp#64 $
*
* Build instructions:
* Use Distutils - see accompanying setup.py
*
* python setup.py install
*
*/
#include <Python.h>
#include <bytesobject.h>
#include <structmember.h>
#include "undefdups.h"
#include "python2to3.h"
#include <serverhelperapi.h>
#include <spec.h>
#include <ident.h>
#include <mapapi.h>
#include <clientprog.h>
#include "P4PythonDebug.h"
#include "SpecMgr.h"
#include "P4Result.h"
#include "PythonClientUser.h"
#include "PythonClientAPI.h"
#include "PythonMergeData.h"
#include "PythonActionMergeData.h"
#include "P4MapMaker.h"
#include "PythonMessage.h"
#include "PythonTypes.h"
#include "debug.h"
#include "PythonKeepAlive.h"
// #include <alloca.h>
#include <iostream>
#include <cstring>
#include <sstream>
#include <vector>
#include <memory>
using namespace std;
static Ident ident = {
IdentMagic "P4PYTHON" "/" ID_OS "/" ID_REL "/" ID_PATCH " (" ID_API " API)", ID_Y "/" ID_M "/" ID_D
};
// ===================
// ==== P4Adapter ====
// ===================
PyObject * P4Error;
PyObject * P4OutputHandler;
PyObject * P4Progress;
/*
* P4Adapter destructor
*/
static void
P4Adapter_dealloc(P4Adapter *self)
{
delete self->clientAPI;
Py_TYPE(self)->tp_free((PyObject*)self);
}
/*
* P4Adapter constructor.
*/
static PyObject *
P4Adapter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
P4Adapter *self = (P4Adapter *) type->tp_alloc(type, 0);
if (self != NULL) {
self->clientAPI = new PythonClientAPI();
}
return (PyObject *) self;
}
/*
* P4Adapter initializer.
*/
static int
P4Adapter_init(P4Adapter *self, PyObject *args, PyObject *kwds)
{
if (kwds != NULL && PyDict_Check(kwds)) {
Py_ssize_t pos = 0;
PyObject *key, *value;
while (PyDict_Next(kwds, &pos, &key, &value)) {
const char * name = GetPythonString(key);
if (PyInt_Check(value)) {
PythonClientAPI::intsetter isetter = self->clientAPI->GetIntSetter(name);
if (isetter) {
int result = (self->clientAPI->*isetter)(PyInt_AS_LONG(value));
if (result)
return result;
}
else {
ostringstream os;
os << "No integer keyword with name " << name;
PyErr_SetString(PyExc_AttributeError, os.str().c_str());
return -1;
}
}
else
if (IsString(value)) {
PythonClientAPI::strsetter ssetter = self->clientAPI->GetStrSetter(name);
if (ssetter) {
int result = (self->clientAPI->*ssetter)(GetPythonString(value));
if (result)
return result;
}
else {
ostringstream os;
os << "No string keyword with name " << name;
PyErr_SetString(PyExc_AttributeError, os.str().c_str());
return -1;
}
}
}
}
return 0;
}
static PyObject *
P4Adapter_repr(P4Adapter *self)
{
return CreatePythonString("P4Adapter");
}
// **************************************
// P4Adapter directly implemented methods
// **************************************
static PyObject * P4Adapter_connect(P4Adapter * self)
{
return self->clientAPI->Connect();
}
static PyObject * P4Adapter_connected(P4Adapter * self)
{
return self->clientAPI->Connected();
}
static PyObject * P4Adapter_disconnect(P4Adapter * self)
{
return self->clientAPI->Disconnect();
}
//
// Get a value from the environment following the Perforce conventions,
// including honouring P4CONFIG files etc.
//
static PyObject * P4Adapter_env(P4Adapter * self, PyObject * var)
{
if ( !var ) Py_RETURN_NONE;
const char *val = self->clientAPI->GetEnv( GetPythonString( var ) );
if( !val ) Py_RETURN_NONE;
return CreatePythonString( val );
}
static PyObject * P4Adapter_set_env(P4Adapter * self, PyObject *args)
{
const char * var;
const char * val = 0; // if not provided, will reset the registry value
if ( PyArg_ParseTuple(args, "s|s", &var, &val) ) {
return self->clientAPI->SetEnv( var, val );
}
return NULL;
}
static PyObject * P4Adapter_run(P4Adapter * self, PyObject * args)
{
PyObject * cmd = PyTuple_GetItem(args, 0);
if (cmd == NULL) {
return NULL;
}
// assume the args are flattened already
vector<const char *> argv;
for (Py_ssize_t i = 1; i < PyTuple_Size(args); ++i) {
PyObject * item = PyTuple_GET_ITEM(args, i);
#if PY_MAJOR_VERSION >= 3
// check encoding here?
if( ! PyUnicode_Check(item) && ! PyBytes_Check(item) ) { // preserve plain Unicode and Byte strings
item = PyObject_Str(item);
}
#else
if( ! PyBytes_Check(item) ) {
item = PyObject_Str(item);
}
#endif
argv.push_back(GetPythonString(item));
}
// this is a bit of a hack: it assumes the storage layout of the vector is continuous
// the other hack is that the API expects (char * const *), but this cannot be stored
// a std::vector<>, because it cannot exchange pointers
return self->clientAPI->Run(GetPythonString(cmd), (int)argv.size(),
(argv.size() > 0) ? (char * const *) &argv[0] : NULL );
}
static PyObject * P4API_identify(PyObject * self)
{
StrBuf s;
ident.GetMessage( &s );
return CreatePythonString( s.Text() );
}
// DVCS init and clone commands
// Requires 2015.2 API
static bool found_error(Error& e)
{
if( e.Test() ) {
StrBuf msg;
e.Fmt(&msg);
PyErr_SetString(PyExc_RuntimeError, msg.Text());
return true;
}
return false;
}
static ServerHelperApi * create_server(const char * user, const char * client, const char * directory, ClientUser * ui)
{
Error e;
ServerHelperApi * server = new ServerHelperApi(&e);
if( found_error(e) ) return NULL;
server->SetDvcsDir(directory, &e);
if( found_error(e) ) return NULL;
if( user )
server->SetUser( user );
if( client )
server->SetClient( client );
if( server->Exists(ui, &e)) {
StrBuf msg("Personal Server already exists at path '");
if( directory )
msg << directory << "'";
else
msg << ".'";
PyErr_SetString(PyExc_RuntimeError, msg.Text());
return NULL;
}
if( found_error(e) ) return NULL;
return server;
}
static bool copy_config(ServerHelperApi * personalServer, const char * port, ClientUser * ui)
{
Error e;
ServerHelperApi remoteServer(&e);
if( found_error(e) ) return false;
remoteServer.SetPort(port, &e);
if( found_error(e) ) return false;
personalServer->CopyConfiguration(&remoteServer, ui, &e);
if( found_error(e) ) return false;
return true;
}
static PyObject * P4API_dvcs_init(P4Adapter * self, PyObject * args, PyObject * keywds)
{
char * user = NULL;
char * client = NULL;
char * directory = (char *) ".";
char * port = NULL;
PyObject * casesensitive = NULL;
PyObject * unicode = NULL;
PythonDebug debug;
p4py::SpecMgr specMgr(&debug);
PythonClientUser ui(&debug, &specMgr);
Error e;
static const char *kwlist[] = { "user", "client", "directory",
"port", "casesensitive", "unicode", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds, "|zzzzO!O!", (char **) kwlist,
&user, &client, &directory,
&port,
&PyBool_Type, &casesensitive,
&PyBool_Type, &unicode))
return NULL;
auto_ptr<ServerHelperApi> personalServer( create_server(user, client, directory, &ui) );
if( personalServer.get() == NULL)
return NULL;
if( port ) {
if( !copy_config(personalServer.get(), port, &ui))
return NULL;
}
else if( casesensitive && unicode) {
StrBuf caseFlag = PyObject_IsTrue(casesensitive) ? "0" : "1";
personalServer->SetCaseFlag(&caseFlag, &e);
int isUnicode = PyObject_IsTrue(unicode);
personalServer->SetUnicode(isUnicode);
}
else { // default is to use "perforce:1666 if it can be reached
if( !copy_config(personalServer.get(), "perforce:1666", &ui))
return NULL;
}
// personalServer->SetQuiet(); // always set, we do not ever want to see string output here
personalServer->InitLocalServer(&ui, &e);
if( found_error(e) ) return NULL;
p4py::P4Result& results = ui.GetResults();
return results.GetOutput();
}
static PyObject * P4API_dvcs_clone(P4Adapter * self, PyObject * args, PyObject * keywds)
{
char * user = NULL;
char * client = NULL;
char * directory = NULL;
int depth = 0;
PyObject * verbose;
char * port = NULL;
char * remote = NULL;
char * file = NULL;
PyObject * archive = NULL;
PyObject * progress = NULL;
PythonDebug debug;
p4py::SpecMgr specMgr(&debug);
PythonClientUser ui(&debug, &specMgr);
Error e;
static const char *kwlist[] = { "user", "client", "directory",
"depth", "verbose", "port", "remote", "file",
"noarchive",
"progress", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds, "|zzziO!zzzO!O", (char **) kwlist,
&user, &client, &directory,
&depth,
&PyBool_Type, &verbose,
&port, &remote, &file,
&PyBool_Type, &archive,
&progress))
return NULL;
auto_ptr<ServerHelperApi> personalServer( create_server(user, client, directory, &ui) );
if( personalServer.get() == NULL)
return NULL;
if( port == NULL) {
PyErr_SetString(PyExc_RuntimeError, "Need to specify P4PORT to clone");
return NULL;
}
if( progress != NULL ) {
PyObject * result = ui.SetProgress(progress);
if (result == NULL) {
cout << "Setting progress failed" << endl;
return NULL;
}
}
ServerHelperApi remoteServer(&e);
if( found_error(e) ) return NULL;
remoteServer.SetPort(port, &e);
if( found_error(e) ) return NULL;
if( remote && file ) {
PyErr_SetString(PyExc_RuntimeError, "Only specify one of (remote | file)");
return NULL;
}
if( remote ) {
personalServer->PrepareToCloneRemote(&remoteServer, remote, &ui, &e);
if( found_error(e) ) return NULL;
}
else if ( file ) {
personalServer->PrepareToCloneFilepath(&remoteServer, file, &ui, &e);
if( found_error(e) ) return NULL;
}
else {
PyErr_SetString(PyExc_RuntimeError, "Need to specify one of (remote | file)");
return NULL;
}
personalServer->InitLocalServer( &ui, &e );
if( found_error(e) ) return NULL;
int noArchives = 0;
if (archive != NULL && PyObject_IsTrue(archive))
noArchives = 1;
personalServer->CloneFromRemote( depth, noArchives, (char *) 0, &ui, &e );
if( found_error(e) ) return NULL;
p4py::P4Result& results = ui.GetResults();
return results.GetOutput();
}
static PyObject * P4Adapter_formatSpec(P4Adapter * self, PyObject * args)
{
const char * type;
PyObject * dict;
if ( PyArg_ParseTuple(args, "sO", &type, &dict) ) {
if ( PyDict_Check(dict) ) {
return self->clientAPI->FormatSpec(type, dict);
}
else {
PyErr_SetString(PyExc_TypeError, "Second argument needs to be a dictionary");
return NULL;
}
}
return NULL;
}
static PyObject * P4Adapter_parseSpec(P4Adapter * self, PyObject * args)
{
const char * type;
const char * form;
if ( PyArg_ParseTuple(args, "ss", &type, &form) ) {
return self->clientAPI->ParseSpec(type, form);
}
return NULL;
}
static PyObject * P4Adapter_defineSpec(P4Adapter * self, PyObject *args)
{
const char * type;
const char * spec;
if ( PyArg_ParseTuple(args, "ss", &type, &spec) ) {
return self->clientAPI->DefineSpec(type, spec);
}
return NULL;
}
static PyObject * P4Adapter_protocol(P4Adapter * self, PyObject *args)
{
const char * var;
const char * val = 0;
if ( PyArg_ParseTuple(args, "s|s", &var, &val) ) {
if ( val ) {
return self->clientAPI->SetProtocol( var, val );
}
else {
return self->clientAPI->GetProtocol( var );
}
}
return NULL;
}
static PyObject * P4Adapter_isIgnored(P4Adapter * self, PyObject *args)
{
const char * var;
if ( PyArg_ParseTuple(args, "s", &var)) {
return self->clientAPI->IsIgnored(var);
}
return NULL;
}
static PyObject * P4Adapter_disableTmpCleanup(P4Adapter * self, PyObject *args)
{
return self->clientAPI->DisableTmpCleanup();
}
static PyObject * P4Adapter_setTunable(P4Adapter * self, PyObject *args)
{
const char *tunable;
const char *value;
if( PyArg_ParseTuple(args, "ss", &tunable, &value)) {
// check if tunable exists
int index = p4tunable.GetIndex(tunable);
if( index >= 0) {
// Get the old value
int oldValue = p4tunable.Get(index);
// set the new tunable
StrBuf setter(tunable);
setter << "=" << value;
p4tunable.Set(setter.Text());
// return the old tunable
return PyLong_FromLong(oldValue);
}
else {
StrBuf errorMsg("Unknown tunable '");
errorMsg << tunable << "'";
PyErr_SetString(PyExc_TypeError, errorMsg.Text());
return NULL;
}
}
return NULL;
}
static PyObject * P4Adapter_getTunable(P4Adapter * self, PyObject *args)
{
const char *tunable;
if( PyArg_ParseTuple(args, "s", &tunable)) {
// check if tunable exists
int index = p4tunable.GetIndex(tunable);
if( index >= 0) {
// Get the old value
int oldValue = p4tunable.Get(index);
return PyLong_FromLong(oldValue);
}
else {
StrBuf errorMsg("Unknown tunable '");
errorMsg << tunable << "'";
PyErr_SetString(PyExc_TypeError, errorMsg.Text());
return NULL;
}
}
return NULL;
}
// ==================
// ==== SetBreak ====
// ==================
static PyObject* P4Adapter_setBreak(P4Adapter* self, PyObject* args) {
PyObject* py_callable;
// Parse the arguments
if (!PyArg_ParseTuple(args, "O", &py_callable)) {
return NULL;
}
// Check if the parsed object is callable
if (!PyCallable_Check(py_callable)) {
PyErr_SetString(PyExc_TypeError, "parameter must be callable");
return NULL;
}
// Create an object pointer of PythonKeepAlive and pass py_callable
PythonKeepAlive* cb = new PythonKeepAlive(py_callable);
self->clientAPI->SetBreak(cb);
Py_RETURN_NONE;
}
#if PY_MAJOR_VERSION >= 3
static PyObject * P4Adapter_convert(P4Adapter * self, PyObject *args)
{
const char * charset;
PyObject * content;
if( PyArg_ParseTuple(args, "sO", &charset, &content)) {
return self->clientAPI->Convert(charset, content);
}
return NULL;
}
#endif
static PyMethodDef P4Adapter_methods[] = {
{"connect", (PyCFunction)P4Adapter_connect, METH_NOARGS,
"Connects to the Perforce Server"},
{"connected", (PyCFunction)P4Adapter_connected, METH_NOARGS,
"Checks whether we are (still) connected"},
{"disconnect", (PyCFunction)P4Adapter_disconnect, METH_NOARGS,
"Closes the connection to the Perforce Server"},
{"env", (PyCFunction)P4Adapter_env, METH_O,
"Get values from the Perforce environment"},
{"set_env", (PyCFunction)P4Adapter_set_env, METH_VARARGS,
"Set values in the registry (if available on the platform) for the Perforce environment"},
{"run", (PyCFunction)P4Adapter_run, METH_VARARGS,
"Runs a command"},
{"format_spec", (PyCFunction)P4Adapter_formatSpec, METH_VARARGS,
"Converts a dictionary-based form into a string"},
{"parse_spec", (PyCFunction)P4Adapter_parseSpec, METH_VARARGS,
"Converts a string form into a dictionary"},
{"define_spec", (PyCFunction)P4Adapter_defineSpec, METH_VARARGS,
"Sets the internal spec for parsing and formating"},
{"protocol", (PyCFunction)P4Adapter_protocol, METH_VARARGS,
"Sets a server protocol variable to the given value or gets the protocol level"},
{"disable_tmp_cleanup", (PyCFunction)P4Adapter_disableTmpCleanup, METH_VARARGS,
"Makes P4Python thread safe by disabling cleanup of temporary resources"},
{"is_ignored", (PyCFunction)P4Adapter_isIgnored, METH_VARARGS,
"Returns True if the specified file path will be ignored by the current ignore file"},
{"set_tunable", (PyCFunction)P4Adapter_setTunable, METH_VARARGS,
"Sets a tunable to the specified value"},
{"get_tunable", (PyCFunction)P4Adapter_getTunable, METH_VARARGS,
"Returns the value for this tunable or 0"},
{"setbreak", (PyCFunction)P4Adapter_setBreak, METH_VARARGS,
"Set the break callback"},
#if PY_MAJOR_VERSION >= 3
{"__convert", (PyCFunction)P4Adapter_convert, METH_VARARGS,
"Converts a Unicode string into a Perforce-converted String" },
#endif
{NULL} /* Sentinel */
};
static PyMemberDef P4Adapter_members[] = {
// {"first", T_OBJECT_EX, offsetof(Noddy, first), 0,
// "first name"},
{NULL} /* Sentinel */
};
static PyObject * P4Adapter_getattro(P4Adapter *self, PyObject * nameObject)
{
const char * name = GetPythonString(nameObject);
PythonClientAPI::intgetter igetter = self->clientAPI->GetIntGetter(name);
if (igetter) {
return PyInt_FromLong((self->clientAPI->*igetter)());
}
PythonClientAPI::strgetter sgetter = self->clientAPI->GetStrGetter(name);
if (sgetter) {
return CreatePythonString((self->clientAPI->*sgetter)());
}
PythonClientAPI::objgetter ogetter = self->clientAPI->GetObjGetter(name);
if (ogetter) {
return (self->clientAPI->*ogetter)();
}
return PyObject_GenericGetAttr((PyObject *) self, nameObject);
}
static int P4Adapter_setattro(P4Adapter *self, PyObject * nameObject, PyObject * value)
{
const char * name = GetPythonString(nameObject);
// Special case first:
// If there is a specific ObjectSetter for this name available use this one
PythonClientAPI::objsetter osetter = self->clientAPI->GetObjSetter(name);
if (osetter) {
return (self->clientAPI->*osetter)(value);
}
else
if (PyInt_Check(value)) {
PythonClientAPI::intsetter isetter = self->clientAPI->GetIntSetter(name);
if (isetter) {
return (self->clientAPI->*isetter)(PyInt_AS_LONG(value));
}
else {
ostringstream os;
os << "No integer attribute with name " << name;
PyErr_SetString(PyExc_AttributeError, os.str().c_str());
return -1;
}
}
else
if (IsString(value)) {
PythonClientAPI::strsetter ssetter = self->clientAPI->GetStrSetter(name);
if (ssetter) {
return (self->clientAPI->*ssetter)(GetPythonString(value));
}
else {
ostringstream os;
os << "No string attribute with name " << name;
PyErr_SetString(PyExc_AttributeError, os.str().c_str());
return -1;
}
}
// can only set int and string or certain object values -> bail out with exception
ostringstream os;
os << "Cannot set attribute : " << name << " with value " << GetPythonString(PyObject_Str(value));
PyErr_SetString(PyExc_AttributeError, os.str().c_str());
return -1;
}
/* PyObject object for the P4Adapter */
static PyTypeObject P4AdapterType = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"P4API.P4Adapter", /* name */
sizeof(P4Adapter), /* basicsize */
0, /* itemsize */
(destructor) P4Adapter_dealloc, /* dealloc */
0, /* print */
0, /* getattr */
0, /* setattr */
0, /* compare */
(reprfunc) P4Adapter_repr, /* repr */
0, /* number methods */
0, /* sequence methods */
0, /* mapping methods */
0, /* tp_hash */
0, /* tp_call*/
0, /* tp_str*/
(getattrofunc) P4Adapter_getattro, /* tp_getattro*/
(setattrofunc) P4Adapter_setattro, /* tp_setattro*/
0, /* tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags*/
"P4Adapter - base class for P4", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
P4Adapter_methods, /* tp_methods */
P4Adapter_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)P4Adapter_init, /* tp_init */
0, /* tp_alloc */
P4Adapter_new, /* tp_new */
};
// =====================
// ==== P4MergeData ====
// =====================
/*
* P4MergeData destructor
*/
static void P4MergeData_dealloc(P4MergeData *self)
{
delete self->mergeData;
Py_TYPE(self)->tp_free((PyObject*)self);
}
static PyObject * P4MergeData_repr(P4MergeData *self)
{
// TODO: add more output information to give full representation
return CreatePythonString(self->mergeData->GetString().Text());
}
// ****************************************
// P4MergeData directly implemented methods
// ****************************************
static PyObject * P4MergeData_run_merge(P4MergeData * self)
{
return self->mergeData->RunMergeTool();
}
static PyMethodDef P4MergeData_methods[] = {
{"run_merge", (PyCFunction)P4MergeData_run_merge, METH_NOARGS,
"Runs the merge tool with this data"},
{NULL} /* Sentinel */
};
static PyObject * P4MergeData_getattro(P4MergeData * self, PyObject * nameObject)
{
const char * name = GetPythonString(nameObject);
if( !strcmp( name, "your_name" ) ) {
return self->mergeData->GetYourName();
}
else if( !strcmp( name, "their_name" ) ) {
return self->mergeData->GetTheirName();
}
else if( !strcmp( name, "base_name" ) ) {
return self->mergeData->GetBaseName();
}
else if( !strcmp( name, "your_path" ) ) {
return self->mergeData->GetYourPath();
}
else if( !strcmp( name, "their_path" ) ) {
return self->mergeData->GetTheirPath();
}
else if( !strcmp( name, "base_path" ) ) {
return self->mergeData->GetBasePath();
}
else if( !strcmp( name, "result_path" ) ) {
return self->mergeData->GetResultPath();
}
else if( !strcmp( name, "merge_hint" ) ) {
return self->mergeData->GetMergeHint();
}
// no matching name found, falling back to default
return PyObject_GenericGetAttr((PyObject *) self, nameObject);
}
/* PyObject object for the P4MergeData */
PyTypeObject P4MergeDataType = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"P4API.P4MergeData", /* name */
sizeof(P4MergeData), /* basicsize */
0, /* itemsize */
(destructor) P4MergeData_dealloc, /* dealloc */
0, /* print */
0, /* getattr */
0, /* setattr */
0, /* compare */
(reprfunc) P4MergeData_repr, /* repr */
0, /* number methods */
0, /* sequence methods */
0, /* mapping methods */
0, /* tp_hash */
0, /* tp_call*/
0, /* tp_str*/
(getattrofunc) P4MergeData_getattro, /* tp_getattro*/
0, /* tp_setattro*/
0, /* tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /* tp_flags*/
"P4MergeData - contains merge information for resolve", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
P4MergeData_methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
};
// ===========================
// ==== P4ActionMergeData ====
// ===========================
/*
* P4ActionMergeData destructor
*/
static void P4ActionMergeData_dealloc(P4ActionMergeData *self)
{
delete self->mergeData;
Py_TYPE(self)->tp_free((PyObject*)self);
}
static PyObject * P4ActionMergeData_repr(P4ActionMergeData *self)
{
// TODO: add more output information to give full representation
return CreatePythonString(self->mergeData->GetString().Text());
}
// ****************************************
// P4MergeData directly implemented methods
// ****************************************
static PyObject * P4ActionMergeData_getattro(P4ActionMergeData * self, PyObject * nameObject)
{
const char * name = GetPythonString(nameObject);
if( !strcmp( name, "merge_action" ) ) {
return self->mergeData->GetMergeAction();
}
else if( !strcmp( name, "yours_action" ) ) {
return self->mergeData->GetYoursAction();
}
else if( !strcmp( name, "their_action" ) ) {
return self->mergeData->GetTheirAction();
}
else if( !strcmp( name, "type" ) ) {
return self->mergeData->GetType();
}
else if( !strcmp( name, "merge_hint" ) ) {
return self->mergeData->GetMergeHint();
}
else if( !strcmp( name, "info" ) ) {
return self->mergeData->GetMergeInfo();
}
// no matching name found, falling back to default
return PyObject_GenericGetAttr((PyObject *) self, nameObject);
}
/* PyObject object for the P4MergeData */
PyTypeObject P4ActionMergeDataType = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"P4API.P4ActionMergeData", /* name */
sizeof(P4ActionMergeData), /* basicsize */
0, /* itemsize */
(destructor) P4ActionMergeData_dealloc, /* dealloc */
0, /* print */
0, /* getattr */
0, /* setattr */
0, /* compare */
(reprfunc) P4ActionMergeData_repr, /* repr */
0, /* number methods */
0, /* sequence methods */
0, /* mapping methods */
0, /* tp_hash */
0, /* tp_call*/
0, /* tp_str*/
(getattrofunc) P4ActionMergeData_getattro, /* tp_getattro*/
0, /* tp_setattro*/
0, /* tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /* tp_flags*/
"P4ActionMergeData - contains action merge information for resolve", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
};
// ===============
// ==== P4Map ====
// ===============
extern PyTypeObject P4MapType; // forward
/*
* P4Map destructor
*/
static void
P4Map_dealloc(P4Map *self)