1 # -*- test-case-name: buildbot.test.test_p4poller -*-
3 # Many thanks to Dave Peticolas for contributing this module
8 from twisted
.python
import log
, failure
9 from twisted
.internet
import defer
, reactor
10 from twisted
.internet
.utils
import getProcessOutput
11 from twisted
.internet
.task
import LoopingCall
13 from buildbot
import util
14 from buildbot
.changes
import base
, changes
16 def get_simple_split(branchfile
):
17 """Splits the branchfile argument and assuming branch is
18 the first path component in branchfile, will return
19 branch and file else None."""
21 index
= branchfile
.find('/')
22 if index
== -1: return None, None
23 branch
, file = branchfile
.split('/', 1)
26 class P4Source(base
.ChangeSource
, util
.ComparableMixin
):
27 """This source will poll a perforce repository for changes and submit
28 them to the change master."""
30 compare_attrs
= ["p4port", "p4user", "p4passwd", "p4base",
31 "p4bin", "pollinterval", "histmax"]
33 changes_line_re
= re
.compile(
34 r
"Change (?P<num>\d+) on \S+ by \S+@\S+ '.+'$")
35 describe_header_re
= re
.compile(
36 r
"Change \d+ by (?P<who>\S+)@\S+ on (?P<when>.+)$")
37 file_re
= re
.compile(r
"^\.\.\. (?P<path>[^#]+)#\d+ \w+$")
38 datefmt
= '%Y/%m/%d %H:%M:%S'
40 parent
= None # filled in when we're added
46 def __init__(self
, p4port
=None, p4user
=None, p4passwd
=None,
47 p4base
='//', p4bin
='p4',
48 split_file
=lambda branchfile
: (None, branchfile
),
49 pollinterval
=60 * 10, histmax
=100):
52 @param p4port: p4 port definition (host:portno)
54 @param p4user: p4 user
55 @type p4passwd: string
56 @param p4passwd: p4 passwd
58 @param p4base: p4 file specification to limit a poll to
59 without the trailing '...' (i.e., //)
61 @param p4bin: path to p4 binary, defaults to just 'p4'
62 @type split_file: func
63 $param split_file: splits a filename into branch and filename.
64 @type pollinterval: int
65 @param pollinterval: interval in seconds between polls
67 @param histmax: maximum number of changes to look back through
72 self
.p4passwd
= p4passwd
75 self
.split_file
= split_file
76 self
.pollinterval
= pollinterval
77 self
.histmax
= histmax
79 def startService(self
):
80 self
.loop
= LoopingCall(self
.checkp4
)
81 base
.ChangeSource
.startService(self
)
83 # Don't start the loop just yet because the reactor isn't running.
84 # Give it a chance to go and install our SIGCHLD handler before
86 reactor
.callLater(0, self
.loop
.start
, self
.pollinterval
)
88 def stopService(self
):
90 return base
.ChangeSource
.stopService(self
)
93 return "p4source %s %s" % (self
.p4port
, self
.p4base
)
96 # Our return value is only used for unit testing.
98 log
.msg("Skipping checkp4 because last one has not finished")
99 return defer
.succeed(None)
102 d
= self
._get
_changes
()
103 d
.addCallback(self
._process
_changes
)
104 d
.addBoth(self
._finished
)
107 def _finished(self
, res
):
111 # Again, the return value is only for unit testing.
112 # If there's a failure, log it so it isn't lost.
113 if isinstance(res
, failure
.Failure
):
114 log
.msg('P4 poll failed: %s' % res
)
117 def _get_changes(self
):
120 args
.extend(['-p', self
.p4port
])
122 args
.extend(['-u', self
.p4user
])
124 args
.extend(['-P', self
.p4passwd
])
125 args
.extend(['changes', '-m', str(self
.histmax
), self
.p4base
+ '...'])
127 return getProcessOutput(self
.p4bin
, args
, env
)
129 def _process_changes(self
, result
):
130 last_change
= self
.last_change
132 for line
in result
.split('\n'):
134 if not line
: continue
135 m
= self
.changes_line_re
.match(line
)
136 assert m
, "Unexpected 'p4 changes' output: %r" % result
138 if last_change
is None:
139 log
.msg('P4Poller: starting at change %s' % num
)
140 self
.last_change
= num
142 if last_change
== num
:
144 changelists
.append(num
)
145 changelists
.reverse() # oldest first
147 # Retrieve each sequentially.
148 d
= defer
.succeed(None)
149 for c
in changelists
:
150 d
.addCallback(self
._get
_describe
, c
)
151 d
.addCallback(self
._process
_describe
, c
)
154 def _get_describe(self
, dummy
, num
):
157 args
.extend(['-p', self
.p4port
])
159 args
.extend(['-u', self
.p4user
])
161 args
.extend(['-P', self
.p4passwd
])
162 args
.extend(['describe', '-s', num
])
164 d
= getProcessOutput(self
.p4bin
, args
, env
)
167 def _process_describe(self
, result
, num
):
168 lines
= result
.split('\n')
169 m
= self
.describe_header_re
.match(lines
[0])
170 assert m
, "Unexpected 'p4 describe -s' result: %r" % result
172 when
= time
.mktime(time
.strptime(m
.group('when'), self
.datefmt
))
174 while not lines
[0].startswith('Affected files'):
175 comments
+= lines
.pop(0) + '\n'
176 lines
.pop(0) # affected files
178 branch_files
= {} # dict for branch mapped to file(s)
180 line
= lines
.pop(0).strip()
181 if not line
: continue
182 m
= self
.file_re
.match(line
)
183 assert m
, "Invalid file line: %r" % line
184 path
= m
.group('path')
185 if path
.startswith(self
.p4base
):
186 branch
, file = self
.split_file(path
[len(self
.p4base
):])
187 if (branch
== None and file == None): continue
188 if branch_files
.has_key(branch
):
189 branch_files
[branch
].append(file)
191 branch_files
[branch
] = [file]
193 for branch
in branch_files
:
194 c
= changes
.Change(who
=who
,
195 files
=branch_files
[branch
],
200 self
.parent
.addChange(c
)
202 self
.last_change
= num