-
Notifications
You must be signed in to change notification settings - Fork 20
/
P4.py
executable file
·1180 lines (949 loc) · 38.1 KB
/
P4.py
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
#!/usr/local/bin/python
from __future__ import print_function
"""A Python version of the Perforce "p4" client.
This uses the Python type P4API.P4Adapter, which is a wrapper for the
Perforce ClientApi object.
$Id: //depot/main/p4-python/P4.py#114 $
#*******************************************************************************
# Copyright (c) 2007-2010, 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 CONTRIBUTORS "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.
#*******************************************************************************
Copyright 2007-2010 Perforce Software, Inc. All rights reserved
License:
See accompanying LICENSE.txt including for redistribution permission.
"""
import sys, datetime, time
import re
import shutil
from contextlib import contextmanager
import uuid, tempfile
import os, os.path, platform
import subprocess
import threading
# P4Exception - some sort of error occurred
class P4Exception(Exception):
"""Exception thrown by P4 in case of Perforce errors or warnings"""
def __init__(self, value):
super().__init__(value)
if isinstance(value, (list, tuple)) and len(value) > 2:
self.value = value[0]
self.warnings = value[2]
if len(value[1]) > 0 or len(value[2]) > 0:
self.errors = value[1]
else:
self.errors = [re.sub(r'\[.*?\] ', '', str(self.value).split("\n")[0])]
else:
self.value = value
self.errors =self.warnings = None
def __str__(self):
if self.errors:
if isinstance(self.errors, (list, tuple)):
return str(self.errors[0])
else:
return str(self.errors)
elif self.warnings:
if isinstance(self.warnings, (list, tuple)):
return str(self.warnings[0])
else:
return str(self.warnings)
elif self.errors is None and self.warnings is None:
return str(self.value)
else:
if isinstance(self.value, (list, tuple)):
return re.sub(r'\[.*?\] ', '', str(self.value).split("\n")[0])
else:
return str(self.value)
def __repr__(self):
return f"{self.__class__.__name__}({str(self)!r})"
def __reduce__(self):
if hasattr(self, 'errors'):
return (self.__class__, ((self.value, self.errors, self.warnings),))
return (self.__class__, (self.value,))
class Spec(dict):
"""Subclass of dict, representing the fields of a spec definition.
Attributes can be accessed either with the conventional dict format,
spec['attribute'] or with shorthand spec._attribute.
Instances of this class will preventing any unknown keys.
"""
def __init__(self, fieldmap=None):
self.__dict__['_Spec__fields'] = fieldmap
def permitted_fields(self):
return self.__fields
def __setitem__(self, key, value):
if not isinstance(value, str) and not isinstance(value, list):
raise P4Exception("Illegal value of type %s, must be string or list" % value.__class__)
if key in self or self.__fields == None:
dict.__setitem__(self, key, value)
elif str(key).lower() in self.__fields:
dict.__setitem__(self, self.__fields[key.lower()], value)
else:
raise P4Exception("Illegal field '%s'" % str(key))
def __getattr__(self, attr):
key = str(attr).lower()
if key[0] != '_':
raise AttributeError(attr)
key = key[1:]
if key in self:
return self[key]
elif key in self.__fields:
return self[self.__fields[key]]
def __setattr__(self, attr, value):
if attr == 'comment':
self.__dict__[attr] = value
else:
key = str(attr).lower()
if key[0] != '_':
raise AttributeError(attr)
key = key[1:]
self[key] = value
#
# P4Integration objects hold details about the integrations that have
# been performed on a particular revision. Used primarily with the
# P4Revision class
#
class Integration:
def __init__( self, how, file, srev, erev ):
self.how = how
self.file = file
self.srev = srev
self.erev = erev
def __repr__(self):
return "Integration (how = %s file = %s srev = %s erev = %s)" \
% (self.how, self.file, self.srev, self.erev)
#
# Each P4Revision object holds details about a particular revision
# of a file. It may also contain the history of any integrations
# to/from the file
#
class Revision:
def __init__( self, depotFile ):
self.depotFile = depotFile
self.integrations = []
self.rev = None
self.change = None
self.action = None
self.type = None
self.time = None
self.user = None
self.client = None
self.desc = None
self.digest = None
self.fileSize = None
def integration( self, how, file, srev, erev ):
rec = Integration( how, file, srev, erev )
self.integrations.append( rec )
return rec
# iterator over the collection calling a provided function
# Python's poor man version of the Ruby block
def each_integration(self):
for i in self.integrations:
yield i
def __repr__(self):
return "Revision (depotFile = %s rev = %s change = %s action = %s type = %s time = %s user = %s client = %s)" % \
(self.depotFile, self.rev, self.change, self.action, self.type, self.time, self.user, self.client)
#
# Each DepotFile entry contains details about one depot file.
#
class DepotFile:
def __init__( self, name ):
self.depotFile = name
self.revisions = []
def new_revision(self):
r = Revision( self.depotFile )
self.revisions.append( r )
return r
def each_revision(self):
for r in self.revisions:
yield r
def str_revision(self, rev, revFormat, changeFormat):
result = "... #{rev:<{rf}} change {change:{cf}} {action:9} on {date} by {user}@{client} ({type}) '{desc}'". \
format(rev = rev.rev,
rf = revFormat,
change = rev.change,
cf = changeFormat,
action = rev.action,
date = rev.time,
user = rev.user,
client = rev.client,
type = rev.type,
desc = rev.desc)
return result
def str_integration(self, integ):
result = "... ... {how} {file}#{srev},{erev}". \
format(how = integ.how, file = integ.file, srev = integ.srev, erev = integ.erev)
return result
def __str__(self):
result = "{}".format(self.depotFile)
revFormat = len(str(self.revisions[0].rev))
changeFormat = len(str(self.revisions[0].change))
for rev in self.revisions:
result += "\n{}".format(self.str_revision(rev, revFormat, changeFormat))
for integ in rev.integrations:
result += "\n{}".format(self.str_integration(integ))
return result
def __repr__(self):
return "DepotFile (depotFile = %s, %s revisions)" % ( self.depotFile, len( self.revisions ) )
#
# Resolver class used in p4.run_resolve()
#
# The default simply checks that p4.input is set to sensible value
# This class is meant to be subclassed for a custom resolver and
# Resolver.resolve() overriden
#
class Resolver:
def __init__(self):
pass
def resolve(self, mergeInfo):
if mergeInfo.merge_hint == "e":
print("Standard resolver encountered merge conflict, skipping resolve")
return "s"
else:
return mergeInfo.merge_hint
def actionResolve(self, mergeInfo):
return mergeInfo.merge_hint
#
# OutputHandler base class
#
# Extend this class if you want to use the handler interface
#
class OutputHandler:
REPORT = 0
HANDLED = 1
CANCEL = 2
def __init__(self):
pass
def outputText(self, s):
return OutputHandler.REPORT
def outputBinary(self, b):
return OutputHandler.REPORT
def outputStat(self, h):
return OutputHandler.REPORT
def outputInfo(self, i):
return OutputHandler.REPORT
def outputMessage(self, e):
return OutputHandler.REPORT
class ReportHandler( OutputHandler ):
def __init__(self):
OutputHandler.__init__(self)
def outputText(self, s):
print( "text: ", s)
return OutputHandler.HANDLED
def outputBinary(self, b):
print( "binary: ", b)
return OutputHandler.HANDLED
def outputStat(self, h):
print( "stat:", h)
return OutputHandler.HANDLED
def outputInfo(self, i):
print( "info: ", i)
return OutputHandler.HANDLED
def outputMessage(self, e):
print( "error:", e)
return OutputHandler.HANDLED
class Progress:
TYPE_SENDFILE = 1
TYPE_RECEIVEFILE = 2
TYPE_TRANSFER = 3
TYPE_COMPUTATION = 4
UNIT_PERCENT = 1
UNIT_FILES = 2
UNIT_KBYTES = 3
UNIT_MBYTES = 4
def __init__(self):
pass
def init(self, type):
self.type = type
def setDescription( self, description, units ):
self.description = description
self.units = units
def setTotal( self, total ):
self.total = total
def update( self, position ):
self.position = position
def done( self, fail ):
pass
class TextProgress(Progress):
TYPES = [ "Unknown", "Submit", "Sync", "Clone" ]
UNITS = [ "Unknown", "Percent", "Files", "KBytes", "MBytes" ]
def __init__(self):
Progress.__init__(self)
def init(self, type):
Progress.init(self, type)
print( "Progress.init with '%s'" % self.TYPES[type] )
def setDescription(self, description, units):
Progress.setDescription(self, description, units)
print( "Progress.setDescription with '%s' and units '%s'" % (description, self.UNITS[units]) )
def setTotal( self, total ):
Progress.setTotal(self, total)
print( "Progress.setTotal with '%s' " % total )
def update( self, position ):
Progress.update(self, position )
print( "Progress.update with '%s'" % position )
def done( self, fail ):
Progress.done(self, fail)
print( "Progress.done with '%s"'' % fail )
def processFilelog(h):
if "depotFile" in h:
df = DepotFile( h[ "depotFile" ] )
for n, rev in enumerate( h[ "rev" ]):
# Create a new revision of this file ready for populating
r = df.new_revision()
# Populate the base attributes of each revision
r.rev = int( rev )
r.change = int( h[ "change" ][ n ] )
r.action = h[ "action" ][ n ]
r.type = h[ "type" ][ n ]
r.time = datetime.datetime.fromtimestamp( int( h[ "time" ][ n ]), tz=None )
r.user = h[ "user" ][ n ]
r.client = h[ "client" ][ n ]
r.desc = h[ "desc" ][ n ]
if "digest" in h and n < len(h[ "digest" ]):
r.digest = h[ "digest" ][ n ]
if "fileSize" in h and n < len(h[ "fileSize" ]):
r.fileSize = h[ "fileSize" ][ n ]
# Now if there are any integration records for this revision,
# add them in too
if (not "how" in h) or (n >= len(h["how"]) or h["how"][n] == None):
continue
else:
number_sign = '#'
if type(h["depotFile"]) == bytes:
number_sign = b'#'
for m, how in enumerate( h[ "how" ][ n ] ):
file = h[ "file" ][ n ][ m ]
srev = h[ "srev" ][ n ][ m ].lstrip(number_sign)
erev = h[ "erev" ][ n ][ m ].lstrip(number_sign)
if srev == "none" or srev == b"none":
srev = 0
else:
srev = int( srev )
if erev == "none" or erev == b"none":
erev = 0
else:
erev = int( erev )
r.integration( how, file, srev, erev )
return df
else:
raise Exception("Not a filelog object: " + h)
class FilelogOutputHandler(OutputHandler):
def __init__(self):
OutputHandler.__init__(self)
def outputStat(self, h):
df = processFilelog(h)
return self.outputFilelog(df)
def outputFilelog(self, f):
return OutputHandler.REPORT
# This is where the C/C++ shared library is loaded
# It has to be in this place because the library needs to access
# the classes defined above. Accessing classes defined below this
# entry would cause an endless loop
import P4API
class P4(P4API.P4Adapter):
"""Use this class to communicate with a Perforce server
Instances of P4 will use the environment settings (including P4CONFIG)
to determine the connection parameters such as P4CLIENT and P4PORT.
This attributes can also be set separately before connecting.
To run any Perforce commands, users of this class first need to run
the connect() method.
It is good practice to disconnect() after the program is complete.
"""
# Constants useful for exception_level
# RAISE_ALL: Errors and Warnings are raised as exceptions (default)
# RAISE_ERROR: Only Errors are raised as exceptions
# RAISE_NONE: No exceptions are raised, instead False is returned
RAISE_ALL = 2
RAISE_ERROR = 1
RAISE_ERRORS = 1
RAISE_NONE = 0
# Named values for generic error codes returned by
# P4API.Message.generic
EV_NONE = 0 # misc
# The fault of the user
EV_USAGE = 0x01 # request not consistent with dox
EV_UNKNOWN = 0x02 # using unknown entity
EV_CONTEXT = 0x03 # using entity in wrong context
EV_ILLEGAL = 0x04 # trying to do something you can't
EV_NOTYET = 0x05 # something must be corrected first
EV_PROTECT = 0x06 # protections prevented operation
# No fault at all
EV_EMPTY = 0x11 # action returned empty results
# not the fault of the user
EV_FAULT = 0x21 # inexplicable program fault
EV_CLIENT = 0x22 # client side program errors
EV_ADMIN = 0x23 # server administrative action required
EV_CONFIG = 0x24 # client configuration inadequate
EV_UPGRADE = 0x25 # client or server too old to interact
EV_COMM = 0x26 # communications error
EV_TOOBIG = 0x27 # not even Perforce can handle this much
# Named values for error severities returned by
# P4API.Message.severity
E_EMPTY = 0 # nothing yet
E_INFO = 1 # something good happened
E_WARN = 2 # something not good happened
E_FAILED = 3 # user did something wrong
E_FATAL = 4 # system broken -- nothing can continue
# mappings for __iterate
# list-of-specs => ( name-of-one-spec, field-name-in-list-of-specs )
specfields = {
'clients' : ('client', 'client'),
'labels' : ('label', 'label'),
'branches' : ('branch', 'branch'),
'changes' : ('change', 'change'),
'streams' : ('stream', 'Stream'),
'jobs' : ('job', 'Job'),
'users' : ('user', 'User'),
'groups' : ('group', 'group'),
'depots' : ('depot', 'name'),
'servers' : ('server', 'Name')
}
def __init__(self, *args, **kwlist):
P4API.P4Adapter.__init__(self, *args, **kwlist)
def __del__(self):
if self.debug > 3:
print("P4.__del__()", file=sys.stderr)
def __getattr__(self, name):
if name.startswith("run_"):
cmd = name[len("run_"):]
return lambda *args, **kargs: self.run(cmd, *args, **kargs)
elif name.startswith("delete_"):
cmd = name[len("delete_"):]
return lambda *args, **kargs: self.run(cmd, "-d", *args, **kargs)
elif name.startswith("fetch_"):
cmd = name[len("fetch_"):]
return lambda *args, **kargs: self.__fetch(cmd, *args, **kargs)
elif name.startswith("save_"):
cmd = name[len("save_"):]
return lambda *args, **kargs: self.__save(cmd, *args, **kargs)
elif name.startswith("parse_"):
cmd = name[len("parse_"):]
return lambda *args, **kargs: self.__parse_spec(cmd, *args, **kargs)
elif name.startswith("format_"):
cmd = name[len("format_"):]
return lambda *args, **kargs: self.__format_spec(cmd, *args, **kargs)
elif name.startswith("iterate_"):
cmd = name[len("iterate_"):]
return lambda *args, **kargs: self.__iterate(cmd, *args, **kargs)
else:
raise AttributeError(name)
def __save(self, cmd, *args, **kargs):
self.input = args[0]
return self.run(cmd, "-i", args[1:], **kargs)
def __parse_spec(self, cmd, *args, **kargs):
form = args[0]
comments = "\n".join( [ x for x in form.split('\n') if x.startswith('#') ] ) + "\n"
spec = self.parse_spec(cmd, *args, **kargs)
spec.__dict__['comment'] = comments
return spec
def __format_spec(self, cmd, *args, **kargs):
spec = args[0]
form = self.format_spec(cmd, *args, **kargs)
if 'comment' in spec.__dict__:
form = spec.__dict__['comment'] + "\n" + form
return form
def __fetch(self, cmd, *args, **kargs):
result = self.run(cmd, "-o", *args, **kargs)
for r in result:
if isinstance(r, tuple) or isinstance(r,dict):
return r
return result[0]
def __iterate(self, cmd, *args, **kargs):
if cmd in self.specfields:
specs = self.run(cmd, *args, **kargs)
spec = self.specfields[cmd][0]
field = self.specfields[cmd][1]
# Return a generators (Python iterator object)
# On iteration, this will retrieve one spec at a time
return ( self.run(spec, '-o', x[field])[0] for x in specs )
else:
raise Exception('Unknown spec list command: %s', cmd)
def __repr__(self):
state = "disconnected"
if self.connected():
state = "connected"
return "P4 [%s@%s %s] %s" % \
(self.user, self.client, self.port, state)
def identify(cls):
return P4API.identify()
identify = classmethod(identify)
def log_messages(self):
for message in self.messages:
if message.severity == 3:
self.logger.error(message)
elif message.severity == 2:
self.logger.warning(message)
elif message.severity == 1:
self.logger.info(message)
def run(self, *args, **kargs):
"Generic run method"
context = {}
resultLogging = True
if "resultLogging" in kargs:
resultLogging= False
del kargs["resultLogging"]
for (k,v) in list(kargs.items()):
context[k] = getattr(self, k)
setattr(self, k, v)
flatArgs = self.__flatten(args)
if self.logger:
self.logger.info("p4 " + " ".join(str(x) for x in flatArgs))
# if encoding is set, translate to Bytes
if hasattr(self,"encoding") and self.encoding and not self.encoding == 'raw':
result = []
for s in flatArgs:
if isinstance(s, str):
result.append( s.encode(self.encoding) )
else:
result.append(s)
flatArgs = result
try:
result = P4API.P4Adapter.run(self, *flatArgs)
except P4Exception as e:
if self.logger:
self.log_messages()
for (k,v) in list(context.items()):
setattr( self, k, v)
raise e
if self.logger:
self.log_messages()
if resultLogging and self.logger:
self.logger.debug(result)
for (k,v) in list(context.items()):
setattr( self, k, v)
return result
def run_submit(self, *args, **kargs):
"Simplified submit - if any arguments is a dict, assume it to be the changeform"
nargs = list(args)
form = None
for n, arg in enumerate(nargs):
if isinstance( arg, dict):
self.input = arg
nargs.pop(n)
nargs.append("-i")
break
return self.run("submit", *nargs, **kargs)
def run_shelve(self, *args, **kargs):
"Simplified shelve - if any arguments is a dict, assume it to be the changeform"
nargs = list(args)
form = None
for n, arg in enumerate(nargs):
if isinstance( arg, dict):
self.input = arg
nargs.pop(n)
nargs.append("-i")
break
return self.run("shelve", *nargs, **kargs)
def delete_shelve(self, *args, **kargs):
"Simplified deletion of shelves - if no -c is passed in, add it to the args"
nargs = list(args)
if '-c' not in nargs:
nargs = ['-c'] + nargs # prepend -c if it is not there
nargs = ['-d'] + nargs
return self.run("shelve", *nargs, **kargs)
def run_login(self, *args, **kargs):
"Simple interface to make login easier"
if "password" in kargs:
password = kargs["password"]
self.input = password
del kargs["password"]
else:
self.input = self.password
return self.run("login", *args, **kargs)
def run_password( self, oldpass, newpass, *args, **kargs ):
"Simple interface to allow setting of the password"
if( oldpass and len(oldpass) > 0 ):
self.input = [ oldpass, newpass, newpass ]
else:
self.input = [ newpass, newpass ]
try:
return self.run( "password" , *args, **kargs)
except P4Exception as e:
if self.errors and self.errors[0] == "Passwords don't match.":
raise P4Exception("Password invalid.")
#
# run_filelog: convert "p4 filelog" responses into objects with useful
# methods
#
# Requires tagged output to be of any real use. If tagged output it not
# enabled then you just get the raw data back
#
def run_filelog( self, *args, **kargs ):
kargs["resultLogging"] = False
raw = self.run( 'filelog', args, **kargs )
if (not self.tagged or not raw):
# untagged mode returns simple strings, which breaks the code below
# raw could be None if a handler is used
return raw
result = []
for h in raw:
df = None
if isinstance( h, dict ):
df = processFilelog( h )
else:
df = h
result.append( df )
# if logger is in the kargs, P4.run has changed its context but already reset it
# so we need to pull out the logger again for this special case
# run_filelog has its own result debug output because of the post-processing step
logger = self.logger
if "logger" in kargs:
logger = kargs["logger"]
if logger:
output = "\n\n".join([ str(x) for x in result ])
logger.debug(output)
return result
def run_print(self, *args, **kargs):
kargs["resultLogging"] = False
raw = self.run('print', args, **kargs)
logger = self.logger
if "logger" in kargs:
logger = kargs["logger"]
result = []
if raw:
debugResult = []
for line in raw:
if isinstance(line, dict):
result.append(line)
if logger:
debugResult.append(line)
result.append("")
else:
# to support encoding for Python 3, we have to do a little dance
# we cannot add bytes to the str "", but we expect that all lines
# are either str or bytes. So if we encounter bytes, we replace the content
try:
result[-1] += line
except TypeError:
if type(line) == bytes and type(result[-1]) == str and result[-1] == "":
result[-1] = line
else:
raise
if logger:
logger.debug(debugResult)
return result
else:
return []
def run_resolve(self, *args, **kargs):
if self.resolver:
myResolver = self.resolver
else:
myResolver = Resolver()
if "resolver" in kargs:
myResolver = kargs["resolver"]
savedResolver = self.resolver
self.resolver = myResolver
result = self.run("resolve", args)
self.resolver = savedResolver
return result
def run_tickets(self, *args):
fname = self.ticket_file
with open(fname) as f:
tickets_raw = f.readlines()
pattern = re.compile(r'([^=]*)=(.*):([^:]*)\n')
tickets = [ pattern.match(x).groups() for x in tickets_raw ]
keys = [ "Host", "User", "Ticket" ]
result = [ dict(zip(keys, x)) for x in tickets ]
return result
def run_init(self, *args, **kargs):
raise Exception("Please run P4.init() instead")
def run_clone(self, *args, **kargs):
raise Exception("Please run P4.clone) instead")
def __flatten(self, args):
result = []
if isinstance(args, tuple) or isinstance(args, list):
for i in args:
result.extend(self.__flatten(i))
else:
result.append(args)
return tuple(result)
def __enter__( self ):
return self
def __exit__( self, exc_type, exc_val, exc_tb ):
if self.connected():
self.disconnect()
return False
def connect( self ):
P4API.P4Adapter.connect( self )
return self
def is_ignored( self, path ):
return P4API.P4Adapter.is_ignored( self, os.path.abspath(path) )
@contextmanager
def while_tagged( self, t ):
old = self.tagged
self.tagged = t
try:
yield
finally:
self.tagged = old
@contextmanager
def at_exception_level( self, e ):
old = self.exception_level
self.exception_level = e
try:
yield
finally:
self.exception_level = old
@contextmanager
def using_handler( self, c ):
old = self.handler
self.handler = c
try:
yield
finally:
self.handler = old
@contextmanager
def saved_context( self , **kargs):
"""Saves the context of this p4 object and restores it again at the end of the block"""
saved_context = {}
for attr in self.__members__:
saved_context[attr] = getattr(self, attr)
for (k,v) in list(kargs.items()):
setattr( self, k, v)
try:
yield
finally:
# now restore the context again. Ignore AttributeError exception
# Exception is expected because some attributes only have getters, no setters
for (k,v) in list(saved_context.items()):
if k not in ("port", "track"):
try:
setattr( self, k, v )
except AttributeError:
pass # expected for server_level and p4config_file
@contextmanager
def temp_client( self, prefix, template ):
"""Creates a temporary workspace with a temporary root.
To be used with the "with" statement. Will clean up the temporary root and client
workspace after the block has finished.
The prefix is prepended to the workspace name and should be used in conjunction with
the SpecMap of a spec depot to avoid creating entries there.
"""
name = "{prefix}_{id}".format(prefix=prefix, id=str(uuid.uuid1()))
ws = self.fetch_client('-t', template, name)
try:
root = tempfile.mkdtemp(prefix=prefix)
ws._root = root
self.save_client(ws)
oldName = self.client
self.client = name
oldCwd = self.cwd
self.cwd = root
yield ws
self.cwd = oldCwd
self.client = oldName
finally:
ws._options = ws._options.replace(' locked ',' unlocked ')
self.save_client(ws)
self.delete_client(name)
shutil.rmtree(root)
class Map(P4API.P4Map):
def __init__(self, *args):
P4API.P4Map.__init__(self, *args)
if len(args) > 0:
self.insert( *args )
LEFT2RIGHT = True
RIGHT2LEFT = False
def __str__( self ):
result = ""
for a in self.as_array():
result += a + "\n"
return result
def is_empty(self):
"""Returns True if this map has no entries yet, otherwise False"""
return self.count() == 0
def includes(self, *args):
return self.translate(*args) != None
def reverse(self):
return Map(P4API.P4Map.reverse(self).as_array())
def insert(self, *args):
"""Insert an argument to the map. The argument can be:
A String,
Either of the form "[+-]//lhs/... //rhs/..." or "[+-]//lhs/..."
for label style maps.
A List:
This is a list of strings of one of the single string formats
described above.
A pair of Strings:
P4.Map.insert(lhs, rhs)
"""
if len(args) == 1 :
arg = args[0]
if isinstance( arg, str ):
P4API.P4Map.insert( self, arg )
elif isinstance( arg, list ):
for s in arg:
P4API.P4Map.insert( self, s )
else: # expecting 2 args in this case: left, right
left = args[0].strip()
right = args[1].strip()
P4API.P4Map.insert(self, left, right )
def init(*args, **kargs):
keywords = ("user", "client", "directory", "port", "casesensitive", "unicode")
new_kargs = dict((x,kargs[x]) for x in kargs if x in keywords)
result = P4API.dvcs_init(*args, **new_kargs)
return __dvcs_post_process(result, *args, **kargs)
def clone(*args, **kargs):
keywords = ("user", "client", "directory", "depth", "verbose", "port", "remote", "file", "noarchive", "progress")
new_kargs = dict((x,kargs[x]) for x in kargs if x in keywords)
result = P4API.dvcs_clone(*args, **new_kargs)
return __dvcs_post_process(result, *args, **kargs)