PageRenderTime 90ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/codereview/codereview.py

http://github.com/ssrl/go
Python | 3281 lines | 3124 code | 45 blank | 112 comment | 91 complexity | 2be25a8626dc6bfc70744de23c0b58ff MD5 | raw file
Possible License(s): BSD-3-Clause
  1. # coding=utf-8
  2. # (The line above is necessary so that I can use 世界 in the
  3. # *comment* below without Python getting all bent out of shape.)
  4. # Copyright 2007-2009 Google Inc.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. '''Mercurial interface to codereview.appspot.com.
  18. To configure, set the following options in
  19. your repository's .hg/hgrc file.
  20. [extensions]
  21. codereview = path/to/codereview.py
  22. [codereview]
  23. server = codereview.appspot.com
  24. The server should be running Rietveld; see http://code.google.com/p/rietveld/.
  25. In addition to the new commands, this extension introduces
  26. the file pattern syntax @nnnnnn, where nnnnnn is a change list
  27. number, to mean the files included in that change list, which
  28. must be associated with the current client.
  29. For example, if change 123456 contains the files x.go and y.go,
  30. "hg diff @123456" is equivalent to"hg diff x.go y.go".
  31. '''
  32. from mercurial import cmdutil, commands, hg, util, error, match
  33. from mercurial.node import nullrev, hex, nullid, short
  34. import os, re, time
  35. import stat
  36. import subprocess
  37. import threading
  38. from HTMLParser import HTMLParser
  39. # The standard 'json' package is new in Python 2.6.
  40. # Before that it was an external package named simplejson.
  41. try:
  42. # Standard location in 2.6 and beyond.
  43. import json
  44. except Exception, e:
  45. try:
  46. # Conventional name for earlier package.
  47. import simplejson as json
  48. except:
  49. try:
  50. # Was also bundled with django, which is commonly installed.
  51. from django.utils import simplejson as json
  52. except:
  53. # We give up.
  54. raise e
  55. try:
  56. hgversion = util.version()
  57. except:
  58. from mercurial.version import version as v
  59. hgversion = v.get_version()
  60. try:
  61. from mercurial.discovery import findcommonincoming
  62. except:
  63. def findcommonincoming(repo, remote):
  64. return repo.findcommonincoming(remote)
  65. oldMessage = """
  66. The code review extension requires Mercurial 1.3 or newer.
  67. To install a new Mercurial,
  68. sudo easy_install mercurial
  69. works on most systems.
  70. """
  71. linuxMessage = """
  72. You may need to clear your current Mercurial installation by running:
  73. sudo apt-get remove mercurial mercurial-common
  74. sudo rm -rf /etc/mercurial
  75. """
  76. if hgversion < '1.3':
  77. msg = oldMessage
  78. if os.access("/etc/mercurial", 0):
  79. msg += linuxMessage
  80. raise util.Abort(msg)
  81. def promptyesno(ui, msg):
  82. # Arguments to ui.prompt changed between 1.3 and 1.3.1.
  83. # Even so, some 1.3.1 distributions seem to have the old prompt!?!?
  84. # What a terrible way to maintain software.
  85. try:
  86. return ui.promptchoice(msg, ["&yes", "&no"], 0) == 0
  87. except AttributeError:
  88. return ui.prompt(msg, ["&yes", "&no"], "y") != "n"
  89. # To experiment with Mercurial in the python interpreter:
  90. # >>> repo = hg.repository(ui.ui(), path = ".")
  91. #######################################################################
  92. # Normally I would split this into multiple files, but it simplifies
  93. # import path headaches to keep it all in one file. Sorry.
  94. import sys
  95. if __name__ == "__main__":
  96. print >>sys.stderr, "This is a Mercurial extension and should not be invoked directly."
  97. sys.exit(2)
  98. server = "codereview.appspot.com"
  99. server_url_base = None
  100. defaultcc = None
  101. contributors = {}
  102. missing_codereview = None
  103. real_rollback = None
  104. releaseBranch = None
  105. #######################################################################
  106. # RE: UNICODE STRING HANDLING
  107. #
  108. # Python distinguishes between the str (string of bytes)
  109. # and unicode (string of code points) types. Most operations
  110. # work on either one just fine, but some (like regexp matching)
  111. # require unicode, and others (like write) require str.
  112. #
  113. # As befits the language, Python hides the distinction between
  114. # unicode and str by converting between them silently, but
  115. # *only* if all the bytes/code points involved are 7-bit ASCII.
  116. # This means that if you're not careful, your program works
  117. # fine on "hello, world" and fails on "hello, 世界". And of course,
  118. # the obvious way to be careful - use static types - is unavailable.
  119. # So the only way is trial and error to find where to put explicit
  120. # conversions.
  121. #
  122. # Because more functions do implicit conversion to str (string of bytes)
  123. # than do implicit conversion to unicode (string of code points),
  124. # the convention in this module is to represent all text as str,
  125. # converting to unicode only when calling a unicode-only function
  126. # and then converting back to str as soon as possible.
  127. def typecheck(s, t):
  128. if type(s) != t:
  129. raise util.Abort("type check failed: %s has type %s != %s" % (repr(s), type(s), t))
  130. # If we have to pass unicode instead of str, ustr does that conversion clearly.
  131. def ustr(s):
  132. typecheck(s, str)
  133. return s.decode("utf-8")
  134. # Even with those, Mercurial still sometimes turns unicode into str
  135. # and then tries to use it as ascii. Change Mercurial's default.
  136. def set_mercurial_encoding_to_utf8():
  137. from mercurial import encoding
  138. encoding.encoding = 'utf-8'
  139. set_mercurial_encoding_to_utf8()
  140. # Even with those we still run into problems.
  141. # I tried to do things by the book but could not convince
  142. # Mercurial to let me check in a change with UTF-8 in the
  143. # CL description or author field, no matter how many conversions
  144. # between str and unicode I inserted and despite changing the
  145. # default encoding. I'm tired of this game, so set the default
  146. # encoding for all of Python to 'utf-8', not 'ascii'.
  147. def default_to_utf8():
  148. import sys
  149. reload(sys) # site.py deleted setdefaultencoding; get it back
  150. sys.setdefaultencoding('utf-8')
  151. default_to_utf8()
  152. #######################################################################
  153. # Change list parsing.
  154. #
  155. # Change lists are stored in .hg/codereview/cl.nnnnnn
  156. # where nnnnnn is the number assigned by the code review server.
  157. # Most data about a change list is stored on the code review server
  158. # too: the description, reviewer, and cc list are all stored there.
  159. # The only thing in the cl.nnnnnn file is the list of relevant files.
  160. # Also, the existence of the cl.nnnnnn file marks this repository
  161. # as the one where the change list lives.
  162. emptydiff = """Index: ~rietveld~placeholder~
  163. ===================================================================
  164. diff --git a/~rietveld~placeholder~ b/~rietveld~placeholder~
  165. new file mode 100644
  166. """
  167. class CL(object):
  168. def __init__(self, name):
  169. typecheck(name, str)
  170. self.name = name
  171. self.desc = ''
  172. self.files = []
  173. self.reviewer = []
  174. self.cc = []
  175. self.url = ''
  176. self.local = False
  177. self.web = False
  178. self.copied_from = None # None means current user
  179. self.mailed = False
  180. self.private = False
  181. def DiskText(self):
  182. cl = self
  183. s = ""
  184. if cl.copied_from:
  185. s += "Author: " + cl.copied_from + "\n\n"
  186. if cl.private:
  187. s += "Private: " + str(self.private) + "\n"
  188. s += "Mailed: " + str(self.mailed) + "\n"
  189. s += "Description:\n"
  190. s += Indent(cl.desc, "\t")
  191. s += "Files:\n"
  192. for f in cl.files:
  193. s += "\t" + f + "\n"
  194. typecheck(s, str)
  195. return s
  196. def EditorText(self):
  197. cl = self
  198. s = _change_prolog
  199. s += "\n"
  200. if cl.copied_from:
  201. s += "Author: " + cl.copied_from + "\n"
  202. if cl.url != '':
  203. s += 'URL: ' + cl.url + ' # cannot edit\n\n'
  204. if cl.private:
  205. s += "Private: True\n"
  206. s += "Reviewer: " + JoinComma(cl.reviewer) + "\n"
  207. s += "CC: " + JoinComma(cl.cc) + "\n"
  208. s += "\n"
  209. s += "Description:\n"
  210. if cl.desc == '':
  211. s += "\t<enter description here>\n"
  212. else:
  213. s += Indent(cl.desc, "\t")
  214. s += "\n"
  215. if cl.local or cl.name == "new":
  216. s += "Files:\n"
  217. for f in cl.files:
  218. s += "\t" + f + "\n"
  219. s += "\n"
  220. typecheck(s, str)
  221. return s
  222. def PendingText(self):
  223. cl = self
  224. s = cl.name + ":" + "\n"
  225. s += Indent(cl.desc, "\t")
  226. s += "\n"
  227. if cl.copied_from:
  228. s += "\tAuthor: " + cl.copied_from + "\n"
  229. s += "\tReviewer: " + JoinComma(cl.reviewer) + "\n"
  230. s += "\tCC: " + JoinComma(cl.cc) + "\n"
  231. s += "\tFiles:\n"
  232. for f in cl.files:
  233. s += "\t\t" + f + "\n"
  234. typecheck(s, str)
  235. return s
  236. def Flush(self, ui, repo):
  237. if self.name == "new":
  238. self.Upload(ui, repo, gofmt_just_warn=True, creating=True)
  239. dir = CodeReviewDir(ui, repo)
  240. path = dir + '/cl.' + self.name
  241. f = open(path+'!', "w")
  242. f.write(self.DiskText())
  243. f.close()
  244. if sys.platform == "win32" and os.path.isfile(path):
  245. os.remove(path)
  246. os.rename(path+'!', path)
  247. if self.web and not self.copied_from:
  248. EditDesc(self.name, desc=self.desc,
  249. reviewers=JoinComma(self.reviewer), cc=JoinComma(self.cc),
  250. private=self.private)
  251. def Delete(self, ui, repo):
  252. dir = CodeReviewDir(ui, repo)
  253. os.unlink(dir + "/cl." + self.name)
  254. def Subject(self):
  255. s = line1(self.desc)
  256. if len(s) > 60:
  257. s = s[0:55] + "..."
  258. if self.name != "new":
  259. s = "code review %s: %s" % (self.name, s)
  260. typecheck(s, str)
  261. return s
  262. def Upload(self, ui, repo, send_mail=False, gofmt=True, gofmt_just_warn=False, creating=False, quiet=False):
  263. if not self.files and not creating:
  264. ui.warn("no files in change list\n")
  265. if ui.configbool("codereview", "force_gofmt", True) and gofmt:
  266. CheckFormat(ui, repo, self.files, just_warn=gofmt_just_warn)
  267. set_status("uploading CL metadata + diffs")
  268. os.chdir(repo.root)
  269. form_fields = [
  270. ("content_upload", "1"),
  271. ("reviewers", JoinComma(self.reviewer)),
  272. ("cc", JoinComma(self.cc)),
  273. ("description", self.desc),
  274. ("base_hashes", ""),
  275. ]
  276. if self.name != "new":
  277. form_fields.append(("issue", self.name))
  278. vcs = None
  279. # We do not include files when creating the issue,
  280. # because we want the patch sets to record the repository
  281. # and base revision they are diffs against. We use the patch
  282. # set message for that purpose, but there is no message with
  283. # the first patch set. Instead the message gets used as the
  284. # new CL's overall subject. So omit the diffs when creating
  285. # and then we'll run an immediate upload.
  286. # This has the effect that every CL begins with an empty "Patch set 1".
  287. if self.files and not creating:
  288. vcs = MercurialVCS(upload_options, ui, repo)
  289. data = vcs.GenerateDiff(self.files)
  290. files = vcs.GetBaseFiles(data)
  291. if len(data) > MAX_UPLOAD_SIZE:
  292. uploaded_diff_file = []
  293. form_fields.append(("separate_patches", "1"))
  294. else:
  295. uploaded_diff_file = [("data", "data.diff", data)]
  296. else:
  297. uploaded_diff_file = [("data", "data.diff", emptydiff)]
  298. if vcs and self.name != "new":
  299. form_fields.append(("subject", "diff -r " + vcs.base_rev + " " + getremote(ui, repo, {}).path))
  300. else:
  301. # First upload sets the subject for the CL itself.
  302. form_fields.append(("subject", self.Subject()))
  303. ctype, body = EncodeMultipartFormData(form_fields, uploaded_diff_file)
  304. response_body = MySend("/upload", body, content_type=ctype)
  305. patchset = None
  306. msg = response_body
  307. lines = msg.splitlines()
  308. if len(lines) >= 2:
  309. msg = lines[0]
  310. patchset = lines[1].strip()
  311. patches = [x.split(" ", 1) for x in lines[2:]]
  312. if response_body.startswith("Issue updated.") and quiet:
  313. pass
  314. else:
  315. ui.status(msg + "\n")
  316. set_status("uploaded CL metadata + diffs")
  317. if not response_body.startswith("Issue created.") and not response_body.startswith("Issue updated."):
  318. raise util.Abort("failed to update issue: " + response_body)
  319. issue = msg[msg.rfind("/")+1:]
  320. self.name = issue
  321. if not self.url:
  322. self.url = server_url_base + self.name
  323. if not uploaded_diff_file:
  324. set_status("uploading patches")
  325. patches = UploadSeparatePatches(issue, rpc, patchset, data, upload_options)
  326. if vcs:
  327. set_status("uploading base files")
  328. vcs.UploadBaseFiles(issue, rpc, patches, patchset, upload_options, files)
  329. if send_mail:
  330. set_status("sending mail")
  331. MySend("/" + issue + "/mail", payload="")
  332. self.web = True
  333. set_status("flushing changes to disk")
  334. self.Flush(ui, repo)
  335. return
  336. def Mail(self, ui, repo):
  337. pmsg = "Hello " + JoinComma(self.reviewer)
  338. if self.cc:
  339. pmsg += " (cc: %s)" % (', '.join(self.cc),)
  340. pmsg += ",\n"
  341. pmsg += "\n"
  342. repourl = getremote(ui, repo, {}).path
  343. if not self.mailed:
  344. pmsg += "I'd like you to review this change to\n" + repourl + "\n"
  345. else:
  346. pmsg += "Please take another look.\n"
  347. typecheck(pmsg, str)
  348. PostMessage(ui, self.name, pmsg, subject=self.Subject())
  349. self.mailed = True
  350. self.Flush(ui, repo)
  351. def GoodCLName(name):
  352. typecheck(name, str)
  353. return re.match("^[0-9]+$", name)
  354. def ParseCL(text, name):
  355. typecheck(text, str)
  356. typecheck(name, str)
  357. sname = None
  358. lineno = 0
  359. sections = {
  360. 'Author': '',
  361. 'Description': '',
  362. 'Files': '',
  363. 'URL': '',
  364. 'Reviewer': '',
  365. 'CC': '',
  366. 'Mailed': '',
  367. 'Private': '',
  368. }
  369. for line in text.split('\n'):
  370. lineno += 1
  371. line = line.rstrip()
  372. if line != '' and line[0] == '#':
  373. continue
  374. if line == '' or line[0] == ' ' or line[0] == '\t':
  375. if sname == None and line != '':
  376. return None, lineno, 'text outside section'
  377. if sname != None:
  378. sections[sname] += line + '\n'
  379. continue
  380. p = line.find(':')
  381. if p >= 0:
  382. s, val = line[:p].strip(), line[p+1:].strip()
  383. if s in sections:
  384. sname = s
  385. if val != '':
  386. sections[sname] += val + '\n'
  387. continue
  388. return None, lineno, 'malformed section header'
  389. for k in sections:
  390. sections[k] = StripCommon(sections[k]).rstrip()
  391. cl = CL(name)
  392. if sections['Author']:
  393. cl.copied_from = sections['Author']
  394. cl.desc = sections['Description']
  395. for line in sections['Files'].split('\n'):
  396. i = line.find('#')
  397. if i >= 0:
  398. line = line[0:i].rstrip()
  399. line = line.strip()
  400. if line == '':
  401. continue
  402. cl.files.append(line)
  403. cl.reviewer = SplitCommaSpace(sections['Reviewer'])
  404. cl.cc = SplitCommaSpace(sections['CC'])
  405. cl.url = sections['URL']
  406. if sections['Mailed'] != 'False':
  407. # Odd default, but avoids spurious mailings when
  408. # reading old CLs that do not have a Mailed: line.
  409. # CLs created with this update will always have
  410. # Mailed: False on disk.
  411. cl.mailed = True
  412. if sections['Private'] in ('True', 'true', 'Yes', 'yes'):
  413. cl.private = True
  414. if cl.desc == '<enter description here>':
  415. cl.desc = ''
  416. return cl, 0, ''
  417. def SplitCommaSpace(s):
  418. typecheck(s, str)
  419. s = s.strip()
  420. if s == "":
  421. return []
  422. return re.split(", *", s)
  423. def CutDomain(s):
  424. typecheck(s, str)
  425. i = s.find('@')
  426. if i >= 0:
  427. s = s[0:i]
  428. return s
  429. def JoinComma(l):
  430. for s in l:
  431. typecheck(s, str)
  432. return ", ".join(l)
  433. def ExceptionDetail():
  434. s = str(sys.exc_info()[0])
  435. if s.startswith("<type '") and s.endswith("'>"):
  436. s = s[7:-2]
  437. elif s.startswith("<class '") and s.endswith("'>"):
  438. s = s[8:-2]
  439. arg = str(sys.exc_info()[1])
  440. if len(arg) > 0:
  441. s += ": " + arg
  442. return s
  443. def IsLocalCL(ui, repo, name):
  444. return GoodCLName(name) and os.access(CodeReviewDir(ui, repo) + "/cl." + name, 0)
  445. # Load CL from disk and/or the web.
  446. def LoadCL(ui, repo, name, web=True):
  447. typecheck(name, str)
  448. set_status("loading CL " + name)
  449. if not GoodCLName(name):
  450. return None, "invalid CL name"
  451. dir = CodeReviewDir(ui, repo)
  452. path = dir + "cl." + name
  453. if os.access(path, 0):
  454. ff = open(path)
  455. text = ff.read()
  456. ff.close()
  457. cl, lineno, err = ParseCL(text, name)
  458. if err != "":
  459. return None, "malformed CL data: "+err
  460. cl.local = True
  461. else:
  462. cl = CL(name)
  463. if web:
  464. set_status("getting issue metadata from web")
  465. d = JSONGet(ui, "/api/" + name + "?messages=true")
  466. set_status(None)
  467. if d is None:
  468. return None, "cannot load CL %s from server" % (name,)
  469. if 'owner_email' not in d or 'issue' not in d or str(d['issue']) != name:
  470. return None, "malformed response loading CL data from code review server"
  471. cl.dict = d
  472. cl.reviewer = d.get('reviewers', [])
  473. cl.cc = d.get('cc', [])
  474. if cl.local and cl.copied_from and cl.desc:
  475. # local copy of CL written by someone else
  476. # and we saved a description. use that one,
  477. # so that committers can edit the description
  478. # before doing hg submit.
  479. pass
  480. else:
  481. cl.desc = d.get('description', "")
  482. cl.url = server_url_base + name
  483. cl.web = True
  484. cl.private = d.get('private', False) != False
  485. set_status("loaded CL " + name)
  486. return cl, ''
  487. global_status = None
  488. def set_status(s):
  489. # print >>sys.stderr, "\t", time.asctime(), s
  490. global global_status
  491. global_status = s
  492. class StatusThread(threading.Thread):
  493. def __init__(self):
  494. threading.Thread.__init__(self)
  495. def run(self):
  496. # pause a reasonable amount of time before
  497. # starting to display status messages, so that
  498. # most hg commands won't ever see them.
  499. time.sleep(30)
  500. # now show status every 15 seconds
  501. while True:
  502. time.sleep(15 - time.time() % 15)
  503. s = global_status
  504. if s is None:
  505. continue
  506. if s == "":
  507. s = "(unknown status)"
  508. print >>sys.stderr, time.asctime(), s
  509. def start_status_thread():
  510. t = StatusThread()
  511. t.setDaemon(True) # allowed to exit if t is still running
  512. t.start()
  513. class LoadCLThread(threading.Thread):
  514. def __init__(self, ui, repo, dir, f, web):
  515. threading.Thread.__init__(self)
  516. self.ui = ui
  517. self.repo = repo
  518. self.dir = dir
  519. self.f = f
  520. self.web = web
  521. self.cl = None
  522. def run(self):
  523. cl, err = LoadCL(self.ui, self.repo, self.f[3:], web=self.web)
  524. if err != '':
  525. self.ui.warn("loading "+self.dir+self.f+": " + err + "\n")
  526. return
  527. self.cl = cl
  528. # Load all the CLs from this repository.
  529. def LoadAllCL(ui, repo, web=True):
  530. dir = CodeReviewDir(ui, repo)
  531. m = {}
  532. files = [f for f in os.listdir(dir) if f.startswith('cl.')]
  533. if not files:
  534. return m
  535. active = []
  536. first = True
  537. for f in files:
  538. t = LoadCLThread(ui, repo, dir, f, web)
  539. t.start()
  540. if web and first:
  541. # first request: wait in case it needs to authenticate
  542. # otherwise we get lots of user/password prompts
  543. # running in parallel.
  544. t.join()
  545. if t.cl:
  546. m[t.cl.name] = t.cl
  547. first = False
  548. else:
  549. active.append(t)
  550. for t in active:
  551. t.join()
  552. if t.cl:
  553. m[t.cl.name] = t.cl
  554. return m
  555. # Find repository root. On error, ui.warn and return None
  556. def RepoDir(ui, repo):
  557. url = repo.url();
  558. if not url.startswith('file:'):
  559. ui.warn("repository %s is not in local file system\n" % (url,))
  560. return None
  561. url = url[5:]
  562. if url.endswith('/'):
  563. url = url[:-1]
  564. typecheck(url, str)
  565. return url
  566. # Find (or make) code review directory. On error, ui.warn and return None
  567. def CodeReviewDir(ui, repo):
  568. dir = RepoDir(ui, repo)
  569. if dir == None:
  570. return None
  571. dir += '/.hg/codereview/'
  572. if not os.path.isdir(dir):
  573. try:
  574. os.mkdir(dir, 0700)
  575. except:
  576. ui.warn('cannot mkdir %s: %s\n' % (dir, ExceptionDetail()))
  577. return None
  578. typecheck(dir, str)
  579. return dir
  580. # Turn leading tabs into spaces, so that the common white space
  581. # prefix doesn't get confused when people's editors write out
  582. # some lines with spaces, some with tabs. Only a heuristic
  583. # (some editors don't use 8 spaces either) but a useful one.
  584. def TabsToSpaces(line):
  585. i = 0
  586. while i < len(line) and line[i] == '\t':
  587. i += 1
  588. return ' '*(8*i) + line[i:]
  589. # Strip maximal common leading white space prefix from text
  590. def StripCommon(text):
  591. typecheck(text, str)
  592. ws = None
  593. for line in text.split('\n'):
  594. line = line.rstrip()
  595. if line == '':
  596. continue
  597. line = TabsToSpaces(line)
  598. white = line[:len(line)-len(line.lstrip())]
  599. if ws == None:
  600. ws = white
  601. else:
  602. common = ''
  603. for i in range(min(len(white), len(ws))+1):
  604. if white[0:i] == ws[0:i]:
  605. common = white[0:i]
  606. ws = common
  607. if ws == '':
  608. break
  609. if ws == None:
  610. return text
  611. t = ''
  612. for line in text.split('\n'):
  613. line = line.rstrip()
  614. line = TabsToSpaces(line)
  615. if line.startswith(ws):
  616. line = line[len(ws):]
  617. if line == '' and t == '':
  618. continue
  619. t += line + '\n'
  620. while len(t) >= 2 and t[-2:] == '\n\n':
  621. t = t[:-1]
  622. typecheck(t, str)
  623. return t
  624. # Indent text with indent.
  625. def Indent(text, indent):
  626. typecheck(text, str)
  627. typecheck(indent, str)
  628. t = ''
  629. for line in text.split('\n'):
  630. t += indent + line + '\n'
  631. typecheck(t, str)
  632. return t
  633. # Return the first line of l
  634. def line1(text):
  635. typecheck(text, str)
  636. return text.split('\n')[0]
  637. _change_prolog = """# Change list.
  638. # Lines beginning with # are ignored.
  639. # Multi-line values should be indented.
  640. """
  641. #######################################################################
  642. # Mercurial helper functions
  643. # Get effective change nodes taking into account applied MQ patches
  644. def effective_revpair(repo):
  645. try:
  646. return cmdutil.revpair(repo, ['qparent'])
  647. except:
  648. return cmdutil.revpair(repo, None)
  649. # Return list of changed files in repository that match pats.
  650. # Warn about patterns that did not match.
  651. def matchpats(ui, repo, pats, opts):
  652. matcher = cmdutil.match(repo, pats, opts)
  653. node1, node2 = effective_revpair(repo)
  654. modified, added, removed, deleted, unknown, ignored, clean = repo.status(node1, node2, matcher, ignored=True, clean=True, unknown=True)
  655. return (modified, added, removed, deleted, unknown, ignored, clean)
  656. # Return list of changed files in repository that match pats.
  657. # The patterns came from the command line, so we warn
  658. # if they have no effect or cannot be understood.
  659. def ChangedFiles(ui, repo, pats, opts, taken=None):
  660. taken = taken or {}
  661. # Run each pattern separately so that we can warn about
  662. # patterns that didn't do anything useful.
  663. for p in pats:
  664. modified, added, removed, deleted, unknown, ignored, clean = matchpats(ui, repo, [p], opts)
  665. redo = False
  666. for f in unknown:
  667. promptadd(ui, repo, f)
  668. redo = True
  669. for f in deleted:
  670. promptremove(ui, repo, f)
  671. redo = True
  672. if redo:
  673. modified, added, removed, deleted, unknown, ignored, clean = matchpats(ui, repo, [p], opts)
  674. for f in modified + added + removed:
  675. if f in taken:
  676. ui.warn("warning: %s already in CL %s\n" % (f, taken[f].name))
  677. if not modified and not added and not removed:
  678. ui.warn("warning: %s did not match any modified files\n" % (p,))
  679. # Again, all at once (eliminates duplicates)
  680. modified, added, removed = matchpats(ui, repo, pats, opts)[:3]
  681. l = modified + added + removed
  682. l.sort()
  683. if taken:
  684. l = Sub(l, taken.keys())
  685. return l
  686. # Return list of changed files in repository that match pats and still exist.
  687. def ChangedExistingFiles(ui, repo, pats, opts):
  688. modified, added = matchpats(ui, repo, pats, opts)[:2]
  689. l = modified + added
  690. l.sort()
  691. return l
  692. # Return list of files claimed by existing CLs
  693. def Taken(ui, repo):
  694. all = LoadAllCL(ui, repo, web=False)
  695. taken = {}
  696. for _, cl in all.items():
  697. for f in cl.files:
  698. taken[f] = cl
  699. return taken
  700. # Return list of changed files that are not claimed by other CLs
  701. def DefaultFiles(ui, repo, pats, opts):
  702. return ChangedFiles(ui, repo, pats, opts, taken=Taken(ui, repo))
  703. def Sub(l1, l2):
  704. return [l for l in l1 if l not in l2]
  705. def Add(l1, l2):
  706. l = l1 + Sub(l2, l1)
  707. l.sort()
  708. return l
  709. def Intersect(l1, l2):
  710. return [l for l in l1 if l in l2]
  711. def getremote(ui, repo, opts):
  712. # save $http_proxy; creating the HTTP repo object will
  713. # delete it in an attempt to "help"
  714. proxy = os.environ.get('http_proxy')
  715. source = hg.parseurl(ui.expandpath("default"), None)[0]
  716. try:
  717. remoteui = hg.remoteui # hg 1.6
  718. except:
  719. remoteui = cmdutil.remoteui
  720. other = hg.repository(remoteui(repo, opts), source)
  721. if proxy is not None:
  722. os.environ['http_proxy'] = proxy
  723. return other
  724. def Incoming(ui, repo, opts):
  725. _, incoming, _ = findcommonincoming(repo, getremote(ui, repo, opts))
  726. return incoming
  727. desc_re = '^(.+: |(tag )?(release|weekly)\.|fix build|undo CL)'
  728. desc_msg = '''Your CL description appears not to use the standard form.
  729. The first line of your change description is conventionally a
  730. one-line summary of the change, prefixed by the primary affected package,
  731. and is used as the subject for code review mail; the rest of the description
  732. elaborates.
  733. Examples:
  734. encoding/rot13: new package
  735. math: add IsInf, IsNaN
  736. net: fix cname in LookupHost
  737. unicode: update to Unicode 5.0.2
  738. '''
  739. def promptremove(ui, repo, f):
  740. if promptyesno(ui, "hg remove %s (y/n)?" % (f,)):
  741. if commands.remove(ui, repo, 'path:'+f) != 0:
  742. ui.warn("error removing %s" % (f,))
  743. def promptadd(ui, repo, f):
  744. if promptyesno(ui, "hg add %s (y/n)?" % (f,)):
  745. if commands.add(ui, repo, 'path:'+f) != 0:
  746. ui.warn("error adding %s" % (f,))
  747. def EditCL(ui, repo, cl):
  748. set_status(None) # do not show status
  749. s = cl.EditorText()
  750. while True:
  751. s = ui.edit(s, ui.username())
  752. clx, line, err = ParseCL(s, cl.name)
  753. if err != '':
  754. if not promptyesno(ui, "error parsing change list: line %d: %s\nre-edit (y/n)?" % (line, err)):
  755. return "change list not modified"
  756. continue
  757. # Check description.
  758. if clx.desc == '':
  759. if promptyesno(ui, "change list should have a description\nre-edit (y/n)?"):
  760. continue
  761. elif re.search('<enter reason for undo>', clx.desc):
  762. if promptyesno(ui, "change list description omits reason for undo\nre-edit (y/n)?"):
  763. continue
  764. elif not re.match(desc_re, clx.desc.split('\n')[0]):
  765. if promptyesno(ui, desc_msg + "re-edit (y/n)?"):
  766. continue
  767. # Check file list for files that need to be hg added or hg removed
  768. # or simply aren't understood.
  769. pats = ['path:'+f for f in clx.files]
  770. modified, added, removed, deleted, unknown, ignored, clean = matchpats(ui, repo, pats, {})
  771. files = []
  772. for f in clx.files:
  773. if f in modified or f in added or f in removed:
  774. files.append(f)
  775. continue
  776. if f in deleted:
  777. promptremove(ui, repo, f)
  778. files.append(f)
  779. continue
  780. if f in unknown:
  781. promptadd(ui, repo, f)
  782. files.append(f)
  783. continue
  784. if f in ignored:
  785. ui.warn("error: %s is excluded by .hgignore; omitting\n" % (f,))
  786. continue
  787. if f in clean:
  788. ui.warn("warning: %s is listed in the CL but unchanged\n" % (f,))
  789. files.append(f)
  790. continue
  791. p = repo.root + '/' + f
  792. if os.path.isfile(p):
  793. ui.warn("warning: %s is a file but not known to hg\n" % (f,))
  794. files.append(f)
  795. continue
  796. if os.path.isdir(p):
  797. ui.warn("error: %s is a directory, not a file; omitting\n" % (f,))
  798. continue
  799. ui.warn("error: %s does not exist; omitting\n" % (f,))
  800. clx.files = files
  801. cl.desc = clx.desc
  802. cl.reviewer = clx.reviewer
  803. cl.cc = clx.cc
  804. cl.files = clx.files
  805. cl.private = clx.private
  806. break
  807. return ""
  808. # For use by submit, etc. (NOT by change)
  809. # Get change list number or list of files from command line.
  810. # If files are given, make a new change list.
  811. def CommandLineCL(ui, repo, pats, opts, defaultcc=None):
  812. if len(pats) > 0 and GoodCLName(pats[0]):
  813. if len(pats) != 1:
  814. return None, "cannot specify change number and file names"
  815. if opts.get('message'):
  816. return None, "cannot use -m with existing CL"
  817. cl, err = LoadCL(ui, repo, pats[0], web=True)
  818. if err != "":
  819. return None, err
  820. else:
  821. cl = CL("new")
  822. cl.local = True
  823. cl.files = ChangedFiles(ui, repo, pats, opts, taken=Taken(ui, repo))
  824. if not cl.files:
  825. return None, "no files changed"
  826. if opts.get('reviewer'):
  827. cl.reviewer = Add(cl.reviewer, SplitCommaSpace(opts.get('reviewer')))
  828. if opts.get('cc'):
  829. cl.cc = Add(cl.cc, SplitCommaSpace(opts.get('cc')))
  830. if defaultcc:
  831. cl.cc = Add(cl.cc, defaultcc)
  832. if cl.name == "new":
  833. if opts.get('message'):
  834. cl.desc = opts.get('message')
  835. else:
  836. err = EditCL(ui, repo, cl)
  837. if err != '':
  838. return None, err
  839. return cl, ""
  840. # reposetup replaces cmdutil.match with this wrapper,
  841. # which expands the syntax @clnumber to mean the files
  842. # in that CL.
  843. original_match = None
  844. def ReplacementForCmdutilMatch(repo, pats=None, opts=None, globbed=False, default='relpath'):
  845. taken = []
  846. files = []
  847. pats = pats or []
  848. opts = opts or {}
  849. for p in pats:
  850. if p.startswith('@'):
  851. taken.append(p)
  852. clname = p[1:]
  853. if not GoodCLName(clname):
  854. raise util.Abort("invalid CL name " + clname)
  855. cl, err = LoadCL(repo.ui, repo, clname, web=False)
  856. if err != '':
  857. raise util.Abort("loading CL " + clname + ": " + err)
  858. if not cl.files:
  859. raise util.Abort("no files in CL " + clname)
  860. files = Add(files, cl.files)
  861. pats = Sub(pats, taken) + ['path:'+f for f in files]
  862. return original_match(repo, pats=pats, opts=opts, globbed=globbed, default=default)
  863. def RelativePath(path, cwd):
  864. n = len(cwd)
  865. if path.startswith(cwd) and path[n] == '/':
  866. return path[n+1:]
  867. return path
  868. def CheckFormat(ui, repo, files, just_warn=False):
  869. set_status("running gofmt")
  870. CheckGofmt(ui, repo, files, just_warn)
  871. CheckTabfmt(ui, repo, files, just_warn)
  872. # Check that gofmt run on the list of files does not change them
  873. def CheckGofmt(ui, repo, files, just_warn):
  874. files = [f for f in files if (f.startswith('src/') or f.startswith('test/bench/')) and f.endswith('.go')]
  875. if not files:
  876. return
  877. cwd = os.getcwd()
  878. files = [RelativePath(repo.root + '/' + f, cwd) for f in files]
  879. files = [f for f in files if os.access(f, 0)]
  880. if not files:
  881. return
  882. try:
  883. cmd = subprocess.Popen(["gofmt", "-l"] + files, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=sys.platform != "win32")
  884. cmd.stdin.close()
  885. except:
  886. raise util.Abort("gofmt: " + ExceptionDetail())
  887. data = cmd.stdout.read()
  888. errors = cmd.stderr.read()
  889. cmd.wait()
  890. set_status("done with gofmt")
  891. if len(errors) > 0:
  892. ui.warn("gofmt errors:\n" + errors.rstrip() + "\n")
  893. return
  894. if len(data) > 0:
  895. msg = "gofmt needs to format these files (run hg gofmt):\n" + Indent(data, "\t").rstrip()
  896. if just_warn:
  897. ui.warn("warning: " + msg + "\n")
  898. else:
  899. raise util.Abort(msg)
  900. return
  901. # Check that *.[chys] files indent using tabs.
  902. def CheckTabfmt(ui, repo, files, just_warn):
  903. files = [f for f in files if f.startswith('src/') and re.search(r"\.[chys]$", f)]
  904. if not files:
  905. return
  906. cwd = os.getcwd()
  907. files = [RelativePath(repo.root + '/' + f, cwd) for f in files]
  908. files = [f for f in files if os.access(f, 0)]
  909. badfiles = []
  910. for f in files:
  911. try:
  912. for line in open(f, 'r'):
  913. # Four leading spaces is enough to complain about,
  914. # except that some Plan 9 code uses four spaces as the label indent,
  915. # so allow that.
  916. if line.startswith(' ') and not re.match(' [A-Za-z0-9_]+:', line):
  917. badfiles.append(f)
  918. break
  919. except:
  920. # ignore cannot open file, etc.
  921. pass
  922. if len(badfiles) > 0:
  923. msg = "these files use spaces for indentation (use tabs instead):\n\t" + "\n\t".join(badfiles)
  924. if just_warn:
  925. ui.warn("warning: " + msg + "\n")
  926. else:
  927. raise util.Abort(msg)
  928. return
  929. #######################################################################
  930. # Mercurial commands
  931. # every command must take a ui and and repo as arguments.
  932. # opts is a dict where you can find other command line flags
  933. #
  934. # Other parameters are taken in order from items on the command line that
  935. # don't start with a dash. If no default value is given in the parameter list,
  936. # they are required.
  937. #
  938. def change(ui, repo, *pats, **opts):
  939. """create, edit or delete a change list
  940. Create, edit or delete a change list.
  941. A change list is a group of files to be reviewed and submitted together,
  942. plus a textual description of the change.
  943. Change lists are referred to by simple alphanumeric names.
  944. Changes must be reviewed before they can be submitted.
  945. In the absence of options, the change command opens the
  946. change list for editing in the default editor.
  947. Deleting a change with the -d or -D flag does not affect
  948. the contents of the files listed in that change. To revert
  949. the files listed in a change, use
  950. hg revert @123456
  951. before running hg change -d 123456.
  952. """
  953. if missing_codereview:
  954. return missing_codereview
  955. dirty = {}
  956. if len(pats) > 0 and GoodCLName(pats[0]):
  957. name = pats[0]
  958. if len(pats) != 1:
  959. return "cannot specify CL name and file patterns"
  960. pats = pats[1:]
  961. cl, err = LoadCL(ui, repo, name, web=True)
  962. if err != '':
  963. return err
  964. if not cl.local and (opts["stdin"] or not opts["stdout"]):
  965. return "cannot change non-local CL " + name
  966. else:
  967. if repo[None].branch() != "default":
  968. return "cannot run hg change outside default branch"
  969. name = "new"
  970. cl = CL("new")
  971. dirty[cl] = True
  972. files = ChangedFiles(ui, repo, pats, opts, taken=Taken(ui, repo))
  973. if opts["delete"] or opts["deletelocal"]:
  974. if opts["delete"] and opts["deletelocal"]:
  975. return "cannot use -d and -D together"
  976. flag = "-d"
  977. if opts["deletelocal"]:
  978. flag = "-D"
  979. if name == "new":
  980. return "cannot use "+flag+" with file patterns"
  981. if opts["stdin"] or opts["stdout"]:
  982. return "cannot use "+flag+" with -i or -o"
  983. if not cl.local:
  984. return "cannot change non-local CL " + name
  985. if opts["delete"]:
  986. if cl.copied_from:
  987. return "original author must delete CL; hg change -D will remove locally"
  988. PostMessage(ui, cl.name, "*** Abandoned ***", send_mail=cl.mailed)
  989. EditDesc(cl.name, closed=True, private=cl.private)
  990. cl.Delete(ui, repo)
  991. return
  992. if opts["stdin"]:
  993. s = sys.stdin.read()
  994. clx, line, err = ParseCL(s, name)
  995. if err != '':
  996. return "error parsing change list: line %d: %s" % (line, err)
  997. if clx.desc is not None:
  998. cl.desc = clx.desc;
  999. dirty[cl] = True
  1000. if clx.reviewer is not None:
  1001. cl.reviewer = clx.reviewer
  1002. dirty[cl] = True
  1003. if clx.cc is not None:
  1004. cl.cc = clx.cc
  1005. dirty[cl] = True
  1006. if clx.files is not None:
  1007. cl.files = clx.files
  1008. dirty[cl] = True
  1009. if clx.private != cl.private:
  1010. cl.private = clx.private
  1011. dirty[cl] = True
  1012. if not opts["stdin"] and not opts["stdout"]:
  1013. if name == "new":
  1014. cl.files = files
  1015. err = EditCL(ui, repo, cl)
  1016. if err != "":
  1017. return err
  1018. dirty[cl] = True
  1019. for d, _ in dirty.items():
  1020. name = d.name
  1021. d.Flush(ui, repo)
  1022. if name == "new":
  1023. d.Upload(ui, repo, quiet=True)
  1024. if opts["stdout"]:
  1025. ui.write(cl.EditorText())
  1026. elif opts["pending"]:
  1027. ui.write(cl.PendingText())
  1028. elif name == "new":
  1029. if ui.quiet:
  1030. ui.write(cl.name)
  1031. else:
  1032. ui.write("CL created: " + cl.url + "\n")
  1033. return
  1034. def code_login(ui, repo, **opts):
  1035. """log in to code review server
  1036. Logs in to the code review server, saving a cookie in
  1037. a file in your home directory.
  1038. """
  1039. if missing_codereview:
  1040. return missing_codereview
  1041. MySend(None)
  1042. def clpatch(ui, repo, clname, **opts):
  1043. """import a patch from the code review server
  1044. Imports a patch from the code review server into the local client.
  1045. If the local client has already modified any of the files that the
  1046. patch modifies, this command will refuse to apply the patch.
  1047. Submitting an imported patch will keep the original author's
  1048. name as the Author: line but add your own name to a Committer: line.
  1049. """
  1050. if repo[None].branch() != "default":
  1051. return "cannot run hg clpatch outside default branch"
  1052. return clpatch_or_undo(ui, repo, clname, opts, mode="clpatch")
  1053. def undo(ui, repo, clname, **opts):
  1054. """undo the effect of a CL
  1055. Creates a new CL that undoes an earlier CL.
  1056. After creating the CL, opens the CL text for editing so that
  1057. you can add the reason for the undo to the description.
  1058. """
  1059. if repo[None].branch() != "default":
  1060. return "cannot run hg undo outside default branch"
  1061. return clpatch_or_undo(ui, repo, clname, opts, mode="undo")
  1062. def release_apply(ui, repo, clname, **opts):
  1063. """apply a CL to the release branch
  1064. Creates a new CL copying a previously committed change
  1065. from the main branch to the release branch.
  1066. The current client must either be clean or already be in
  1067. the release branch.
  1068. The release branch must be created by starting with a
  1069. clean client, disabling the code review plugin, and running:
  1070. hg update weekly.YYYY-MM-DD
  1071. hg branch release-branch.rNN
  1072. hg commit -m 'create release-branch.rNN'
  1073. hg push --new-branch
  1074. Then re-enable the code review plugin.
  1075. People can test the release branch by running
  1076. hg update release-branch.rNN
  1077. in a clean client. To return to the normal tree,
  1078. hg update default
  1079. Move changes since the weekly into the release branch
  1080. using hg release-apply followed by the usual code review
  1081. process and hg submit.
  1082. When it comes time to tag the release, record the
  1083. final long-form tag of the release-branch.rNN
  1084. in the *default* branch's .hgtags file. That is, run
  1085. hg update default
  1086. and then edit .hgtags as you would for a weekly.
  1087. """
  1088. c = repo[None]
  1089. if not releaseBranch:
  1090. return "no active release branches"
  1091. if c.branch() != releaseBranch:
  1092. if c.modified() or c.added() or c.removed():
  1093. raise util.Abort("uncommitted local changes - cannot switch branches")
  1094. err = hg.clean(repo, releaseBranch)
  1095. if err:
  1096. return err
  1097. try:
  1098. err = clpatch_or_undo(ui, repo, clname, opts, mode="backport")
  1099. if err:
  1100. raise util.Abort(err)
  1101. except Exception, e:
  1102. hg.clean(repo, "default")
  1103. raise e
  1104. return None
  1105. def rev2clname(rev):
  1106. # Extract CL name from revision description.
  1107. # The last line in the description that is a codereview URL is the real one.
  1108. # Earlier lines might be part of the user-written description.
  1109. all = re.findall('(?m)^http://codereview.appspot.com/([0-9]+)$', rev.description())
  1110. if len(all) > 0:
  1111. return all[-1]
  1112. return ""
  1113. undoHeader = """undo CL %s / %s
  1114. <enter reason for undo>
  1115. ««« original CL description
  1116. """
  1117. undoFooter = """
  1118. »»»
  1119. """
  1120. backportHeader = """[%s] %s
  1121. ««« CL %s / %s
  1122. """
  1123. backportFooter = """
  1124. »»»
  1125. """
  1126. # Implementation of clpatch/undo.
  1127. def clpatch_or_undo(ui, repo, clname, opts, mode):
  1128. if missing_codereview:
  1129. return missing_codereview
  1130. if mode == "undo" or mode == "backport":
  1131. if hgversion < '1.4':
  1132. # Don't have cmdutil.match (see implementation of sync command).
  1133. return "hg is too old to run hg %s - update to 1.4 or newer" % mode
  1134. # Find revision in Mercurial repository.
  1135. # Assume CL number is 7+ decimal digits.
  1136. # Otherwise is either change log sequence number (fewer decimal digits),
  1137. # hexadecimal hash, or tag name.
  1138. # Mercurial will fall over long before the change log
  1139. # sequence numbers get to be 7 digits long.
  1140. if re.match('^[0-9]{7,}$', clname):
  1141. found = False
  1142. matchfn = cmdutil.match(repo, [], {'rev': None})
  1143. def prep(ctx, fns):
  1144. pass
  1145. for ctx in cmdutil.walkchangerevs(repo, matchfn, {'rev': None}, prep):
  1146. rev = repo[ctx.rev()]
  1147. # Last line with a code review URL is the actual review URL.
  1148. # Earlier ones might be part of the CL description.
  1149. n = rev2clname(rev)
  1150. if n == clname:
  1151. found = True
  1152. break
  1153. if not found:
  1154. return "cannot find CL %s in local repository" % clname
  1155. else:
  1156. rev = repo[clname]
  1157. if not rev:
  1158. return "unknown revision %s" % clname
  1159. clname = rev2clname(rev)
  1160. if clname == "":
  1161. return "cannot find CL name in revision description"
  1162. # Create fresh CL and start with patch that would reverse the change.
  1163. vers = short(rev.node())
  1164. cl = CL("new")
  1165. desc = str(rev.description())
  1166. if mode == "undo":
  1167. cl.desc = (undoHeader % (clname, vers)) + desc + undoFooter
  1168. else:
  1169. cl.desc = (backportHeader % (releaseBranch, line1(desc), clname, vers)) + desc + undoFooter
  1170. v1 = vers
  1171. v0 = short(rev.parents()[0].node())
  1172. if mode == "undo":
  1173. arg = v1 + ":" + v0
  1174. else:
  1175. vers = v0
  1176. arg = v0 + ":" + v1
  1177. patch = RunShell(["hg", "diff", "--git", "-r", arg])
  1178. else: # clpatch
  1179. cl, vers, patch, err = DownloadCL(ui, repo, clname)
  1180. if err != "":
  1181. return err
  1182. if patch == emptydiff:
  1183. return "codereview issue %s has no diff" % clname
  1184. # find current hg version (hg identify)
  1185. ctx = repo[None]
  1186. parents = ctx.parents()
  1187. id = '+'.join([short(p.node()) for p in parents])
  1188. # if version does not match the patch version,
  1189. # try to update the patch line numbers.
  1190. if vers != "" and id != vers:
  1191. # "vers in repo" gives the wrong answer
  1192. # on some versions of Mercurial. Instead, do the actual
  1193. # lookup and catch the exception.
  1194. try:
  1195. repo[vers].description()
  1196. except:
  1197. return "local repository is out of date; sync to get %s" % (vers)
  1198. patch1, err = portPatch(repo, patch, vers, id)
  1199. if err != "":
  1200. if not opts["ignore_hgpatch_failure"]:
  1201. return "codereview issue %s is out of date: %s (%s->%s)" % (clname, err, vers, id)
  1202. else:
  1203. patch = patch1
  1204. argv = ["hgpatch"]
  1205. if opts["no_incoming"] or mode == "backport":
  1206. argv += ["--checksync=false"]
  1207. try:
  1208. cmd = subprocess.Popen(argv, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=None, close_fds=sys.platform != "win32")
  1209. except:
  1210. return "hgpatch: " + ExceptionDetail()
  1211. out, err = cmd.communicate(patch)
  1212. if cmd.returncode != 0 and not opts["ignore_hgpatch_failure"]:
  1213. return "hgpatch failed"
  1214. cl.local = True
  1215. cl.files = out.strip().split()
  1216. if not cl.files and not opts["ignore_hgpatch_failure"]:
  1217. return "codereview issue %s has no changed files" % clname
  1218. files = ChangedFiles(ui, repo, [], opts)
  1219. extra = Sub(cl.files, files)
  1220. if extra:
  1221. ui.warn("warning: these files were listed in the patch but not changed:\n\t" + "\n\t".join(extra) + "\n")
  1222. cl.Flush(ui, repo)
  1223. if mode == "undo":
  1224. err = EditCL(ui, repo, cl)
  1225. if err != "":
  1226. return "CL created, but error editing: " + err
  1227. cl.Flush(ui, repo)
  1228. else:
  1229. ui.write(cl.PendingText() + "\n")
  1230. # portPatch rewrites patch from being a patch against
  1231. # oldver to being a patch against newver.
  1232. def portPatch(repo, patch, oldver, newver):
  1233. lines = patch.splitlines(True) # True = keep \n
  1234. delta = None
  1235. for i in range(len(lines)):
  1236. line = lines[i]
  1237. if line.startswith('--- a/'):
  1238. file = line[6:-1]
  1239. delta = fileDeltas(repo, file, oldver, newver)
  1240. if not delta or not line.startswith('@@ '):
  1241. continue
  1242. # @@ -x,y +z,w @@ means the patch chunk replaces
  1243. # the original file's line numbers x up to x+y with the
  1244. # line numbers z up to z+w in the new file.
  1245. # Find the delta from x in the original to the same
  1246. # line in the current version and add that delta to both
  1247. # x and z.
  1248. m = re.match('@@ -([0-9]+),([0-9]+) \+([0-9]+),([0-9]+) @@', line)
  1249. if not m:
  1250. return None, "error parsing patch line numbers"
  1251. n1, len1, n2, len2 = int(m.group(1)), int(m.group(2)), int(m.group(3)), int(m.group(4))
  1252. d, err = lineDelta(delta, n1, len1)
  1253. if err != "":
  1254. return "", err
  1255. n1 += d
  1256. n2 += d
  1257. lines[i] = "@@ -%d,%d +%d,%d @@\n" % (n1, len1, n2, len2)
  1258. newpatch = ''.join(lines)
  1259. return newpatch, ""
  1260. # fileDelta returns the line number deltas for the given file's
  1261. # changes from oldver to newver.
  1262. # The deltas are a list of (n, len, newdelta) triples that say
  1263. # lines [n, n+len) were modified, and after that range the
  1264. # line numbers are +newdelta from what they were before.
  1265. def fileDeltas(repo, file, oldver, newver):
  1266. cmd = ["hg", "diff", "--git", "-r", oldver + ":" + newver, "path:" + file]
  1267. data = RunShell(cmd, silent_ok=True)
  1268. deltas = []
  1269. for line in data.splitlines():
  1270. m = re.match('@@ -([0-9]+),([0-9]+) \+([0-9]+),([0-9]+) @@', line)
  1271. if not m:
  1272. continue
  1273. n1, len1, n2, len2 = int(m.group(1)), int(m.group(2)), int(m.group(3)), int(m.group(4))
  1274. deltas.append((n1, len1, n2+len2-(n1+len1)))
  1275. return deltas
  1276. # lineDelta finds the appropriate line number delta to apply to the lines [n, n+len).
  1277. # It returns an error if those lines were rewritten by the patch.
  1278. def lineDelta(deltas, n, len):
  1279. d = 0
  1280. for (old, oldlen, newdelta) in deltas:
  1281. if old >= n+len:
  1282. break
  1283. if old+len > n:
  1284. return 0, "patch and recent changes conflict"
  1285. d = newdelta
  1286. return d, ""
  1287. def download(ui, repo, clname, **opts):
  1288. """download a change from the code review server
  1289. Download prints a description of the given change list
  1290. followed by its diff, downloaded from the code review server.
  1291. """
  1292. if missing_codereview:
  1293. return missing_codereview
  1294. cl, vers, patch, err = DownloadCL(ui, repo, clname)
  1295. if err != "":
  1296. return err
  1297. ui.write(cl.EditorText() + "\n")
  1298. ui.write(patch + "\n")
  1299. return
  1300. def file(ui, repo, clname, pat, *pats, **opts):
  1301. """assign files to or remove files from a change list
  1302. Assign files to or (with -d) remove files from a change list.
  1303. The -d option only removes files from the change list.
  1304. It does not edit them or remove them from the repository.
  1305. """
  1306. if missing_codereview:
  1307. return missing_codereview
  1308. pats = tuple([pat] + list(pats))
  1309. if not GoodCLName(clname):
  1310. return "invalid CL name " + clname
  1311. dirty = {}
  1312. cl, err = LoadCL(ui, repo, clname, web=False)
  1313. if err != '':
  1314. return err
  1315. if not cl.local:
  1316. return "cannot change non-local CL " + clname
  1317. files = ChangedFiles(ui, repo, pats, opts)
  1318. if opts["delete"]:
  1319. oldfiles = Intersect(files, cl.files)
  1320. if oldfiles:
  1321. if not ui.quiet:
  1322. ui.status("# Removing files from CL. To undo:\n")
  1323. ui.status("# cd %s\n" % (repo.root))
  1324. for f in oldfiles:
  1325. ui.status("# hg file %s %s\n" % (cl.name, f))
  1326. cl.files = Sub(cl.files, oldfiles)
  1327. cl.Flush(ui, repo)
  1328. else:
  1329. ui.status("no such files in CL")
  1330. return
  1331. if not files:
  1332. return "no such modified files"
  1333. files = Sub(files, cl.files)
  1334. taken = Taken(ui, repo)
  1335. warned = False
  1336. for f in files:
  1337. if f in taken:
  1338. if not warned and not ui.quiet:
  1339. ui.status("# Taking files from other CLs. To undo:\n")
  1340. ui.status("# cd %s\n" % (repo.root))
  1341. warned = True
  1342. ocl = taken[f]
  1343. if not ui.quiet:
  1344. ui.status("# hg file %s %s\n" % (ocl.name, f))
  1345. if ocl not in dirty:
  1346. ocl.files = Sub(ocl.files, files)
  1347. dirty[ocl] = True
  1348. cl.files = Add(cl.files, files)
  1349. dirty[cl] = True
  1350. for d, _ in dirty.items():
  1351. d.Flush(ui, repo)
  1352. return
  1353. def gofmt(ui, repo, *pats, **opts):
  1354. """apply gofmt to modified files
  1355. Applies gofmt to the modified files in the repository that match
  1356. the given patterns.
  1357. """
  1358. if missing_codereview:
  1359. return missing_codereview
  1360. files = ChangedExistingFiles(ui, repo, pats, opts)
  1361. files = [f for f in files if f.endswith(".go")]
  1362. if not files:
  1363. return "no modified go files"
  1364. cwd = os.getcwd()
  1365. files = [RelativePath(repo.root + '/' + f, cwd) for f in files]
  1366. try:
  1367. cmd = ["gofmt", "-l"]
  1368. if not opts["list"]:
  1369. cmd += ["-w"]
  1370. if os.spawnvp(os.P_WAIT, "gofmt", cmd + files) != 0:
  1371. raise util.Abort("gofmt did not exit cleanly")
  1372. except error.Abort, e:
  1373. raise
  1374. except:
  1375. raise util.Abort("gofmt: " + ExceptionDetail())
  1376. return
  1377. def mail(ui, repo, *pats, **opts):
  1378. """mail a change for review
  1379. Uploads a patch to the code review server and then sends mail
  1380. to the reviewer and CC list asking for a review.
  1381. """
  1382. if missing_codereview:
  1383. return missing_codereview
  1384. cl, err = CommandLineCL(ui, repo, pats, opts, defaultcc=defaultcc)
  1385. if err != "":
  1386. return err
  1387. cl.Upload(ui, repo, gofmt_just_warn=True)
  1388. if not cl.reviewer:
  1389. # If no reviewer is listed, assign the review to defaultcc.
  1390. # This makes sure that it appears in the
  1391. # codereview.appspot.com/user/defaultcc
  1392. # page, so that it doesn't get dropped on the floor.
  1393. if not defaultcc:
  1394. return "no reviewers listed in CL"
  1395. cl.cc = Sub(cl.cc, defaultcc)
  1396. cl.reviewer = defaultcc
  1397. cl.Flush(ui, repo)
  1398. if cl.files == []:
  1399. return "no changed files, not sending mail"
  1400. cl.Mail(ui, repo)
  1401. def pending(ui, repo, *pats, **opts):
  1402. """show pending changes
  1403. Lists pending changes followed by a list of unassigned but modified files.
  1404. """
  1405. if missing_codereview:
  1406. return missing_codereview
  1407. m = LoadAllCL(ui, repo, web=True)
  1408. names = m.keys()
  1409. names.sort()
  1410. for name in names:
  1411. cl = m[name]
  1412. ui.write(cl.PendingText() + "\n")
  1413. files = DefaultFiles(ui, repo, [], opts)
  1414. if len(files) > 0:
  1415. s = "Changed files not in any CL:\n"
  1416. for f in files:
  1417. s += "\t" + f + "\n"
  1418. ui.write(s)
  1419. def reposetup(ui, repo):
  1420. global original_match
  1421. if original_match is None:
  1422. start_status_thread()
  1423. original_match = cmdutil.match
  1424. cmdutil.match = ReplacementForCmdutilMatch
  1425. RietveldSetup(ui, repo)
  1426. def CheckContributor(ui, repo, user=None):
  1427. set_status("checking CONTRIBUTORS file")
  1428. user, userline = FindContributor(ui, repo, user, warn=False)
  1429. if not userline:
  1430. raise util.Abort("cannot find %s in CONTRIBUTORS" % (user,))
  1431. return userline
  1432. def FindContributor(ui, repo, user=None, warn=True):
  1433. if not user:
  1434. user = ui.config("ui", "username")
  1435. if not user:
  1436. raise util.Abort("[ui] username is not configured in .hgrc")
  1437. user = user.lower()
  1438. m = re.match(r".*<(.*)>", user)
  1439. if m:
  1440. user = m.group(1)
  1441. if user not in contributors:
  1442. if warn:
  1443. ui.warn("warning: cannot find %s in CONTRIBUTORS\n" % (user,))
  1444. return user, None
  1445. user, email = contributors[user]
  1446. return email, "%s <%s>" % (user, email)
  1447. def submit(ui, repo, *pats, **opts):
  1448. """submit change to remote repository
  1449. Submits change to remote repository.
  1450. Bails out if the local repository is not in sync with the remote one.
  1451. """
  1452. if missing_codereview:
  1453. return missing_codereview
  1454. # We already called this on startup but sometimes Mercurial forgets.
  1455. set_mercurial_encoding_to_utf8()
  1456. repo.ui.quiet = True
  1457. if not opts["no_incoming"] and Incoming(ui, repo, opts):
  1458. return "local repository out of date; must sync before submit"
  1459. cl, err = CommandLineCL(ui, repo, pats, opts, defaultcc=defaultcc)
  1460. if err != "":
  1461. return err
  1462. user = None
  1463. if cl.copied_from:
  1464. user = cl.copied_from
  1465. userline = CheckContributor(ui, repo, user)
  1466. typecheck(userline, str)
  1467. about = ""
  1468. if cl.reviewer:
  1469. about += "R=" + JoinComma([CutDomain(s) for s in cl.reviewer]) + "\n"
  1470. if opts.get('tbr'):
  1471. tbr = SplitCommaSpace(opts.get('tbr'))
  1472. cl.reviewer = Add(cl.reviewer, tbr)
  1473. about += "TBR=" + JoinComma([CutDomain(s) for s in tbr]) + "\n"
  1474. if cl.cc:
  1475. about += "CC=" + JoinComma([CutDomain(s) for s in cl.cc]) + "\n"
  1476. if not cl.reviewer:
  1477. return "no reviewers listed in CL"
  1478. if not cl.local:
  1479. return "cannot submit non-local CL"
  1480. # upload, to sync current patch and also get change number if CL is new.
  1481. if not cl.copied_from:
  1482. cl.Upload(ui, repo, gofmt_just_warn=True)
  1483. # check gofmt for real; allowed upload to warn in order to save CL.
  1484. cl.Flush(ui, repo)
  1485. CheckFormat(ui, repo, cl.files)
  1486. about += "%s%s\n" % (server_url_base, cl.name)
  1487. if cl.copied_from:
  1488. about += "\nCommitter: " + CheckContributor(ui, repo, None) + "\n"
  1489. typecheck(about, str)
  1490. if not cl.mailed and not cl.copied_from: # in case this is TBR
  1491. cl.Mail(ui, repo)
  1492. # submit changes locally
  1493. date = opts.get('date')
  1494. if date:
  1495. opts['date'] = util.parsedate(date)
  1496. typecheck(opts['date'], str)
  1497. opts['message'] = cl.desc.rstrip() + "\n\n" + about
  1498. typecheck(opts['message'], str)
  1499. if opts['dryrun']:
  1500. print "NOT SUBMITTING:"
  1501. print "User: ", userline
  1502. print "Message:"
  1503. print Indent(opts['message'], "\t")
  1504. print "Files:"
  1505. print Indent('\n'.join(cl.files), "\t")
  1506. return "dry run; not submitted"
  1507. m = match.exact(repo.root, repo.getcwd(), cl.files)
  1508. node = repo.commit(ustr(opts['message']), ustr(userline), opts.get('date'), m)
  1509. if not node:
  1510. return "nothing changed"
  1511. # push to remote; if it fails for any reason, roll back
  1512. try:
  1513. log = repo.changelog
  1514. rev = log.rev(node)
  1515. parents = log.parentrevs(rev)
  1516. if (rev-1 not in parents and
  1517. (parents == (nullrev, nullrev) or
  1518. len(log.heads(log.node(parents[0]))) > 1 and
  1519. (parents[1] == nullrev or len(log.heads(log.node(parents[1]))) > 1))):
  1520. # created new head
  1521. raise util.Abort("local repository out of date; must sync before submit")
  1522. # push changes to remote.
  1523. # if it works, we're committed.
  1524. # if not, roll back
  1525. other = getremote(ui, repo, opts)
  1526. r = repo.push(other, False, None)
  1527. if r == 0:
  1528. raise util.Abort("local repository out of date; must sync before submit")
  1529. except:
  1530. real_rollback()
  1531. raise
  1532. # we're committed. upload final patch, close review, add commit message
  1533. changeURL = short(node)
  1534. url = other.url()
  1535. m = re.match("^https?://([^@/]+@)?([^.]+)\.googlecode\.com/hg/?", url)
  1536. if m:
  1537. changeURL = "http://code.google.com/p/%s/source/detail?r=%s" % (m.group(2), changeURL)
  1538. else:
  1539. print >>sys.stderr, "URL: ", url
  1540. pmsg = "*** Submitted as " + changeURL + " ***\n\n" + opts['message']
  1541. # When posting, move reviewers to CC line,
  1542. # so that the issue stops showing up in their "My Issues" page.
  1543. PostMessage(ui, cl.name, pmsg, reviewers="", cc=JoinComma(cl.reviewer+cl.cc))
  1544. if not cl.copied_from:
  1545. EditDesc(cl.name, closed=True, private=cl.private)
  1546. cl.Delete(ui, repo)
  1547. c = repo[None]
  1548. if c.branch() == releaseBranch and not c.modified() and not c.added() and not c.removed():
  1549. ui.write("switching from %s to default branch.\n" % releaseBranch)
  1550. err = hg.clean(repo, "default")
  1551. if err:
  1552. return err
  1553. return None
  1554. def sync(ui, repo, **opts):
  1555. """synchronize with remote repository
  1556. Incorporates recent changes from the remote repository
  1557. into the local repository.
  1558. """
  1559. if missing_codereview:
  1560. return missing_codereview
  1561. if not opts["local"]:
  1562. ui.status = sync_note
  1563. ui.note = sync_note
  1564. other = getremote(ui, repo, opts)
  1565. modheads = repo.pull(other)
  1566. err = commands.postincoming(ui, repo, modheads, True, "tip")
  1567. if err:
  1568. return err
  1569. commands.update(ui, repo, rev="default")
  1570. sync_changes(ui, repo)
  1571. def sync_note(msg):
  1572. # we run sync (pull -u) in verbose mode to get the
  1573. # list of files being updated, but that drags along
  1574. # a bunch of messages we don't care about.
  1575. # omit them.
  1576. if msg == 'resolving manifests\n':
  1577. return
  1578. if msg == 'searching for changes\n':
  1579. return
  1580. if msg == "couldn't find merge tool hgmerge\n":
  1581. return
  1582. sys.stdout.write(msg)
  1583. def sync_changes(ui, repo):
  1584. # Look through recent change log descriptions to find
  1585. # potential references to http://.*/our-CL-number.
  1586. # Double-check them by looking at the Rietveld log.
  1587. def Rev(rev):
  1588. desc = repo[rev].description().strip()
  1589. for clname in re.findall('(?m)^http://(?:[^\n]+)/([0-9]+)$', desc):
  1590. if IsLocalCL(ui, repo, clname) and IsRietveldSubmitted(ui, clname, repo[rev].hex()):
  1591. ui.warn("CL %s submitted as %s; closing\n" % (clname, repo[rev]))
  1592. cl, err = LoadCL(ui, repo, clname, web=False)
  1593. if err != "":
  1594. ui.warn("loading CL %s: %s\n" % (clname, err))
  1595. continue
  1596. if not cl.copied_from:
  1597. EditDesc(cl.name, closed=True, private=cl.private)
  1598. cl.Delete(ui, repo)
  1599. if hgversion < '1.4':
  1600. get = util.cachefunc(lambda r: repo[r].changeset())
  1601. changeiter, matchfn = cmdutil.walkchangerevs(ui, repo, [], get, {'rev': None})
  1602. n = 0
  1603. for st, rev, fns in changeiter:
  1604. if st != 'iter':
  1605. continue
  1606. n += 1
  1607. if n > 100:
  1608. break
  1609. Rev(rev)
  1610. else:
  1611. matchfn = cmdutil.match(repo, [], {'rev': None})
  1612. def prep(ctx, fns):
  1613. pass
  1614. for ctx in cmdutil.walkchangerevs(repo, matchfn, {'rev': None}, prep):
  1615. Rev(ctx.rev())
  1616. # Remove files that are not modified from the CLs in which they appear.
  1617. all = LoadAllCL(ui, repo, web=False)
  1618. changed = ChangedFiles(ui, repo, [], {})
  1619. for _, cl in all.items():
  1620. extra = Sub(cl.files, changed)
  1621. if extra:
  1622. ui.warn("Removing unmodified files from CL %s:\n" % (cl.name,))
  1623. for f in extra:
  1624. ui.warn("\t%s\n" % (f,))
  1625. cl.files = Sub(cl.files, extra)
  1626. cl.Flush(ui, repo)
  1627. if not cl.files:
  1628. if not cl.copied_from:
  1629. ui.warn("CL %s has no files; delete (abandon) with hg change -d %s\n" % (cl.name, cl.name))
  1630. else:
  1631. ui.warn("CL %s has no files; delete locally with hg change -D %s\n" % (cl.name, cl.name))
  1632. return
  1633. def upload(ui, repo, name, **opts):
  1634. """upload diffs to the code review server
  1635. Uploads the current modifications for a given change to the server.
  1636. """
  1637. if missing_codereview:
  1638. return missing_codereview
  1639. repo.ui.quiet = True
  1640. cl, err = LoadCL(ui, repo, name, web=True)
  1641. if err != "":
  1642. return err
  1643. if not cl.local:
  1644. return "cannot upload non-local change"
  1645. cl.Upload(ui, repo)
  1646. print "%s%s\n" % (server_url_base, cl.name)
  1647. return
  1648. review_opts = [
  1649. ('r', 'reviewer', '', 'add reviewer'),
  1650. ('', 'cc', '', 'add cc'),
  1651. ('', 'tbr', '', 'add future reviewer'),
  1652. ('m', 'message', '', 'change description (for new change)'),
  1653. ]
  1654. cmdtable = {
  1655. # The ^ means to show this command in the help text that
  1656. # is printed when running hg with no arguments.
  1657. "^change": (
  1658. change,
  1659. [
  1660. ('d', 'delete', None, 'delete existing change list'),
  1661. ('D', 'deletelocal', None, 'delete locally, but do not change CL on server'),
  1662. ('i', 'stdin', None, 'read change list from standard input'),
  1663. ('o', 'stdout', None, 'print change list to standard output'),
  1664. ('p', 'pending', None, 'print pending summary to standard output'),
  1665. ],
  1666. "[-d | -D] [-i] [-o] change# or FILE ..."
  1667. ),
  1668. "^clpatch": (
  1669. clpatch,
  1670. [
  1671. ('', 'ignore_hgpatch_failure', None, 'create CL metadata even if hgpatch fails'),
  1672. ('', 'no_incoming', None, 'disable check for incoming changes'),
  1673. ],
  1674. "change#"
  1675. ),
  1676. # Would prefer to call this codereview-login, but then
  1677. # hg help codereview prints the help for this command
  1678. # instead of the help for the extension.
  1679. "code-login": (
  1680. code_login,
  1681. [],
  1682. "",
  1683. ),
  1684. "^download": (
  1685. download,
  1686. [],
  1687. "change#"
  1688. ),
  1689. "^file": (
  1690. file,
  1691. [
  1692. ('d', 'delete', None, 'delete files from change list (but not repository)'),
  1693. ],
  1694. "[-d] change# FILE ..."
  1695. ),
  1696. "^gofmt": (
  1697. gofmt,
  1698. [
  1699. ('l', 'list', None, 'list files that would change, but do not edit them'),
  1700. ],
  1701. "FILE ..."
  1702. ),
  1703. "^pending|p": (
  1704. pending,
  1705. [],
  1706. "[FILE ...]"
  1707. ),
  1708. "^mail": (
  1709. mail,
  1710. review_opts + [
  1711. ] + commands.walkopts,
  1712. "[-r reviewer] [--cc cc] [change# | file ...]"
  1713. ),
  1714. "^release-apply": (
  1715. release_apply,
  1716. [
  1717. ('', 'ignore_hgpatch_failure', None, 'create CL metadata even if hgpatch fails'),
  1718. ('', 'no_incoming', None, 'disable check for incoming changes'),
  1719. ],
  1720. "change#"
  1721. ),
  1722. # TODO: release-start, release-tag, weekly-tag
  1723. "^submit": (
  1724. submit,
  1725. review_opts + [
  1726. ('', 'no_incoming', None, 'disable initial incoming check (for testing)'),
  1727. ('n', 'dryrun', None, 'make change only locally (for testing)'),
  1728. ] + commands.walkopts + commands.commitopts + commands.commitopts2,
  1729. "[-r reviewer] [--cc cc] [change# | file ...]"
  1730. ),
  1731. "^sync": (
  1732. sync,
  1733. [
  1734. ('', 'local', None, 'do not pull changes from remote repository')
  1735. ],
  1736. "[--local]",
  1737. ),
  1738. "^undo": (
  1739. undo,
  1740. [
  1741. ('', 'ignore_hgpatch_failure', None, 'create CL metadata even if hgpatch fails'),
  1742. ('', 'no_incoming', None, 'disable check for incoming changes'),
  1743. ],
  1744. "change#"
  1745. ),
  1746. "^upload": (
  1747. upload,
  1748. [],
  1749. "change#"
  1750. ),
  1751. }
  1752. #######################################################################
  1753. # Wrappers around upload.py for interacting with Rietveld
  1754. # HTML form parser
  1755. class FormParser(HTMLParser):
  1756. def __init__(self):
  1757. self.map = {}
  1758. self.curtag = None
  1759. self.curdata = None
  1760. HTMLParser.__init__(self)
  1761. def handle_starttag(self, tag, attrs):
  1762. if tag == "input":
  1763. key = None
  1764. value = ''
  1765. for a in attrs:
  1766. if a[0] == 'name':
  1767. key = a[1]
  1768. if a[0] == 'value':
  1769. value = a[1]
  1770. if key is not None:
  1771. self.map[key] = value
  1772. if tag == "textarea":
  1773. key = None
  1774. for a in attrs:
  1775. if a[0] == 'name':
  1776. key = a[1]
  1777. if key is not None:
  1778. self.curtag = key
  1779. self.curdata = ''
  1780. def handle_endtag(self, tag):
  1781. if tag == "textarea" and self.curtag is not None:
  1782. self.map[self.curtag] = self.curdata
  1783. self.curtag = None
  1784. self.curdata = None
  1785. def handle_charref(self, name):
  1786. self.handle_data(unichr(int(name)))
  1787. def handle_entityref(self, name):
  1788. import htmlentitydefs
  1789. if name in htmlentitydefs.entitydefs:
  1790. self.handle_data(htmlentitydefs.entitydefs[name])
  1791. else:
  1792. self.handle_data("&" + name + ";")
  1793. def handle_data(self, data):
  1794. if self.curdata is not None:
  1795. self.curdata += data
  1796. def JSONGet(ui, path):
  1797. try:
  1798. data = MySend(path, force_auth=False)
  1799. typecheck(data, str)
  1800. d = fix_json(json.loads(data))
  1801. except:
  1802. ui.warn("JSONGet %s: %s\n" % (path, ExceptionDetail()))
  1803. return None
  1804. return d
  1805. # Clean up json parser output to match our expectations:
  1806. # * all strings are UTF-8-encoded str, not unicode.
  1807. # * missing fields are missing, not None,
  1808. # so that d.get("foo", defaultvalue) works.
  1809. def fix_json(x):
  1810. if type(x) in [str, int, float, bool, type(None)]:
  1811. pass
  1812. elif type(x) is unicode:
  1813. x = x.encode("utf-8")
  1814. elif type(x) is list:
  1815. for i in range(len(x)):
  1816. x[i] = fix_json(x[i])
  1817. elif type(x) is dict:
  1818. todel = []
  1819. for k in x:
  1820. if x[k] is None:
  1821. todel.append(k)
  1822. else:
  1823. x[k] = fix_json(x[k])
  1824. for k in todel:
  1825. del x[k]
  1826. else:
  1827. raise util.Abort("unknown type " + str(type(x)) + " in fix_json")
  1828. if type(x) is str:
  1829. x = x.replace('\r\n', '\n')
  1830. return x
  1831. def IsRietveldSubmitted(ui, clname, hex):
  1832. dict = JSONGet(ui, "/api/" + clname + "?messages=true")
  1833. if dict is None:
  1834. return False
  1835. for msg in dict.get("messages", []):
  1836. text = msg.get("text", "")
  1837. m = re.match('\*\*\* Submitted as [^*]*?([0-9a-f]+) \*\*\*', text)
  1838. if m is not None and len(m.group(1)) >= 8 and hex.startswith(m.group(1)):
  1839. return True
  1840. return False
  1841. def IsRietveldMailed(cl):
  1842. for msg in cl.dict.get("messages", []):
  1843. if msg.get("text", "").find("I'd like you to review this change") >= 0:
  1844. return True
  1845. return False
  1846. def DownloadCL(ui, repo, clname):
  1847. set_status("downloading CL " + clname)
  1848. cl, err = LoadCL(ui, repo, clname, web=True)
  1849. if err != "":
  1850. return None, None, None, "error loading CL %s: %s" % (clname, err)
  1851. # Find most recent diff
  1852. diffs = cl.dict.get("patchsets", [])
  1853. if not diffs:
  1854. return None, None, None, "CL has no patch sets"
  1855. patchid = diffs[-1]
  1856. patchset = JSONGet(ui, "/api/" + clname + "/" + str(patchid))
  1857. if patchset is None:
  1858. return None, None, None, "error loading CL patchset %s/%d" % (clname, patchid)
  1859. if patchset.get("patchset", 0) != patchid:
  1860. return None, None, None, "malformed patchset information"
  1861. vers = ""
  1862. msg = patchset.get("message", "").split()
  1863. if len(msg) >= 3 and msg[0] == "diff" and msg[1] == "-r":
  1864. vers = msg[2]
  1865. diff = "/download/issue" + clname + "_" + str(patchid) + ".diff"
  1866. diffdata = MySend(diff, force_auth=False)
  1867. # Print warning if email is not in CONTRIBUTORS file.
  1868. email = cl.dict.get("owner_email", "")
  1869. if not email:
  1870. return None, None, None, "cannot find owner for %s" % (clname)
  1871. him = FindContributor(ui, repo, email)
  1872. me = FindContributor(ui, repo, None)
  1873. if him == me:
  1874. cl.mailed = IsRietveldMailed(cl)
  1875. else:
  1876. cl.copied_from = email
  1877. return cl, vers, diffdata, ""
  1878. def MySend(request_path, payload=None,
  1879. content_type="application/octet-stream",
  1880. timeout=None, force_auth=True,
  1881. **kwargs):
  1882. """Run MySend1 maybe twice, because Rietveld is unreliable."""
  1883. try:
  1884. return MySend1(request_path, payload, content_type, timeout, force_auth, **kwargs)
  1885. except Exception, e:
  1886. if type(e) != urllib2.HTTPError or e.code != 500: # only retry on HTTP 500 error
  1887. raise
  1888. print >>sys.stderr, "Loading "+request_path+": "+ExceptionDetail()+"; trying again in 2 seconds."
  1889. time.sleep(2)
  1890. return MySend1(request_path, payload, content_type, timeout, force_auth, **kwargs)
  1891. # Like upload.py Send but only authenticates when the
  1892. # redirect is to www.google.com/accounts. This keeps
  1893. # unnecessary redirects from happening during testing.
  1894. def MySend1(request_path, payload=None,
  1895. content_type="application/octet-stream",
  1896. timeout=None, force_auth=True,
  1897. **kwargs):
  1898. """Sends an RPC and returns the response.
  1899. Args:
  1900. request_path: The path to send the request to, eg /api/appversion/create.
  1901. payload: The body of the request, or None to send an empty request.
  1902. content_type: The Content-Type header to use.
  1903. timeout: timeout in seconds; default None i.e. no timeout.
  1904. (Note: for large requests on OS X, the timeout doesn't work right.)
  1905. kwargs: Any keyword arguments are converted into query string parameters.
  1906. Returns:
  1907. The response body, as a string.
  1908. """
  1909. # TODO: Don't require authentication. Let the server say
  1910. # whether it is necessary.
  1911. global rpc
  1912. if rpc == None:
  1913. rpc = GetRpcServer(upload_options)
  1914. self = rpc
  1915. if not self.authenticated and force_auth:
  1916. self._Authenticate()
  1917. if request_path is None:
  1918. return
  1919. old_timeout = socket.getdefaulttimeout()
  1920. socket.setdefaulttimeout(timeout)
  1921. try:
  1922. tries = 0
  1923. while True:
  1924. tries += 1
  1925. args = dict(kwargs)
  1926. url = "http://%s%s" % (self.host, request_path)
  1927. if args:
  1928. url += "?" + urllib.urlencode(args)
  1929. req = self._CreateRequest(url=url, data=payload)
  1930. req.add_header("Content-Type", content_type)
  1931. try:
  1932. f = self.opener.open(req)
  1933. response = f.read()
  1934. f.close()
  1935. # Translate \r\n into \n, because Rietveld doesn't.
  1936. response = response.replace('\r\n', '\n')
  1937. # who knows what urllib will give us
  1938. if type(response) == unicode:
  1939. response = response.encode("utf-8")
  1940. typecheck(response, str)
  1941. return response
  1942. except urllib2.HTTPError, e:
  1943. if tries > 3:
  1944. raise
  1945. elif e.code == 401:
  1946. self._Authenticate()
  1947. elif e.code == 302:
  1948. loc = e.info()["location"]
  1949. if not loc.startswith('https://www.google.com/a') or loc.find('/ServiceLogin') < 0:
  1950. return ''
  1951. self._Authenticate()
  1952. else:
  1953. raise
  1954. finally:
  1955. socket.setdefaulttimeout(old_timeout)
  1956. def GetForm(url):
  1957. f = FormParser()
  1958. f.feed(ustr(MySend(url))) # f.feed wants unicode
  1959. f.close()
  1960. # convert back to utf-8 to restore sanity
  1961. m = {}
  1962. for k,v in f.map.items():
  1963. m[k.encode("utf-8")] = v.replace("\r\n", "\n").encode("utf-8")
  1964. return m
  1965. def EditDesc(issue, subject=None, desc=None, reviewers=None, cc=None, closed=False, private=False):
  1966. set_status("uploading change to description")
  1967. form_fields = GetForm("/" + issue + "/edit")
  1968. if subject is not None:
  1969. form_fields['subject'] = subject
  1970. if desc is not None:
  1971. form_fields['description'] = desc
  1972. if reviewers is not None:
  1973. form_fields['reviewers'] = reviewers
  1974. if cc is not None:
  1975. form_fields['cc'] = cc
  1976. if closed:
  1977. form_fields['closed'] = "checked"
  1978. if private:
  1979. form_fields['private'] = "checked"
  1980. ctype, body = EncodeMultipartFormData(form_fields.items(), [])
  1981. response = MySend("/" + issue + "/edit", body, content_type=ctype)
  1982. if response != "":
  1983. print >>sys.stderr, "Error editing description:\n" + "Sent form: \n", form_fields, "\n", response
  1984. sys.exit(2)
  1985. def PostMessage(ui, issue, message, reviewers=None, cc=None, send_mail=True, subject=None):
  1986. set_status("uploading message")
  1987. form_fields = GetForm("/" + issue + "/publish")
  1988. if reviewers is not None:
  1989. form_fields['reviewers'] = reviewers
  1990. if cc is not None:
  1991. form_fields['cc'] = cc
  1992. if send_mail:
  1993. form_fields['send_mail'] = "checked"
  1994. else:
  1995. del form_fields['send_mail']
  1996. if subject is not None:
  1997. form_fields['subject'] = subject
  1998. form_fields['message'] = message
  1999. form_fields['message_only'] = '1' # Don't include draft comments
  2000. if reviewers is not None or cc is not None:
  2001. form_fields['message_only'] = '' # Must set '' in order to override cc/reviewer
  2002. ctype = "applications/x-www-form-urlencoded"
  2003. body = urllib.urlencode(form_fields)
  2004. response = MySend("/" + issue + "/publish", body, content_type=ctype)
  2005. if response != "":
  2006. print response
  2007. sys.exit(2)
  2008. class opt(object):
  2009. pass
  2010. def nocommit(*pats, **opts):
  2011. """(disabled when using this extension)"""
  2012. raise util.Abort("codereview extension enabled; use mail, upload, or submit instead of commit")
  2013. def nobackout(*pats, **opts):
  2014. """(disabled when using this extension)"""
  2015. raise util.Abort("codereview extension enabled; use undo instead of backout")
  2016. def norollback(*pats, **opts):
  2017. """(disabled when using this extension)"""
  2018. raise util.Abort("codereview extension enabled; use undo instead of rollback")
  2019. def RietveldSetup(ui, repo):
  2020. global defaultcc, upload_options, rpc, server, server_url_base, force_google_account, verbosity, contributors
  2021. global missing_codereview
  2022. repo_config_path = ''
  2023. # Read repository-specific options from lib/codereview/codereview.cfg
  2024. try:
  2025. repo_config_path = repo.root + '/lib/codereview/codereview.cfg'
  2026. f = open(repo_config_path)
  2027. for line in f:
  2028. if line.startswith('defaultcc: '):
  2029. defaultcc = SplitCommaSpace(line[10:])
  2030. except:
  2031. # If there are no options, chances are good this is not
  2032. # a code review repository; stop now before we foul
  2033. # things up even worse. Might also be that repo doesn't
  2034. # even have a root. See issue 959.
  2035. if repo_config_path == '':
  2036. missing_codereview = 'codereview disabled: repository has no root'
  2037. else:
  2038. missing_codereview = 'codereview disabled: cannot open ' + repo_config_path
  2039. return
  2040. # Should only modify repository with hg submit.
  2041. # Disable the built-in Mercurial commands that might
  2042. # trip things up.
  2043. cmdutil.commit = nocommit
  2044. global real_rollback
  2045. real_rollback = repo.rollback
  2046. repo.rollback = norollback
  2047. # would install nobackout if we could; oh well
  2048. try:
  2049. f = open(repo.root + '/CONTRIBUTORS', 'r')
  2050. except:
  2051. raise util.Abort("cannot open %s: %s" % (repo.root+'/CONTRIBUTORS', ExceptionDetail()))
  2052. for line in f:
  2053. # CONTRIBUTORS is a list of lines like:
  2054. # Person <email>
  2055. # Person <email> <alt-email>
  2056. # The first email address is the one used in commit logs.
  2057. if line.startswith('#'):
  2058. continue
  2059. m = re.match(r"([^<>]+\S)\s+(<[^<>\s]+>)((\s+<[^<>\s]+>)*)\s*$", line)
  2060. if m:
  2061. name = m.group(1)
  2062. email = m.group(2)[1:-1]
  2063. contributors[email.lower()] = (name, email)
  2064. for extra in m.group(3).split():
  2065. contributors[extra[1:-1].lower()] = (name, email)
  2066. if not ui.verbose:
  2067. verbosity = 0
  2068. # Config options.
  2069. x = ui.config("codereview", "server")
  2070. if x is not None:
  2071. server = x
  2072. # TODO(rsc): Take from ui.username?
  2073. email = None
  2074. x = ui.config("codereview", "email")
  2075. if x is not None:
  2076. email = x
  2077. server_url_base = "http://" + server + "/"
  2078. testing = ui.config("codereview", "testing")
  2079. force_google_account = ui.configbool("codereview", "force_google_account", False)
  2080. upload_options = opt()
  2081. upload_options.email = email
  2082. upload_options.host = None
  2083. upload_options.verbose = 0
  2084. upload_options.description = None
  2085. upload_options.description_file = None
  2086. upload_options.reviewers = None
  2087. upload_options.cc = None
  2088. upload_options.message = None
  2089. upload_options.issue = None
  2090. upload_options.download_base = False
  2091. upload_options.revision = None
  2092. upload_options.send_mail = False
  2093. upload_options.vcs = None
  2094. upload_options.server = server
  2095. upload_options.save_cookies = True
  2096. if testing:
  2097. upload_options.save_cookies = False
  2098. upload_options.email = "test@example.com"
  2099. rpc = None
  2100. global releaseBranch
  2101. tags = repo.branchtags().keys()
  2102. if 'release-branch.r100' in tags:
  2103. # NOTE(rsc): This tags.sort is going to get the wrong
  2104. # answer when comparing release-branch.r99 with
  2105. # release-branch.r100. If we do ten releases a year
  2106. # that gives us 4 years before we have to worry about this.
  2107. raise util.Abort('tags.sort needs to be fixed for release-branch.r100')
  2108. tags.sort()
  2109. for t in tags:
  2110. if t.startswith('release-branch.'):
  2111. releaseBranch = t
  2112. #######################################################################
  2113. # http://codereview.appspot.com/static/upload.py, heavily edited.
  2114. #!/usr/bin/env python
  2115. #
  2116. # Copyright 2007 Google Inc.
  2117. #
  2118. # Licensed under the Apache License, Version 2.0 (the "License");
  2119. # you may not use this file except in compliance with the License.
  2120. # You may obtain a copy of the License at
  2121. #
  2122. # http://www.apache.org/licenses/LICENSE-2.0
  2123. #
  2124. # Unless required by applicable law or agreed to in writing, software
  2125. # distributed under the License is distributed on an "AS IS" BASIS,
  2126. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  2127. # See the License for the specific language governing permissions and
  2128. # limitations under the License.
  2129. """Tool for uploading diffs from a version control system to the codereview app.
  2130. Usage summary: upload.py [options] [-- diff_options]
  2131. Diff options are passed to the diff command of the underlying system.
  2132. Supported version control systems:
  2133. Git
  2134. Mercurial
  2135. Subversion
  2136. It is important for Git/Mercurial users to specify a tree/node/branch to diff
  2137. against by using the '--rev' option.
  2138. """
  2139. # This code is derived from appcfg.py in the App Engine SDK (open source),
  2140. # and from ASPN recipe #146306.
  2141. import cookielib
  2142. import getpass
  2143. import logging
  2144. import mimetypes
  2145. import optparse
  2146. import os
  2147. import re
  2148. import socket
  2149. import subprocess
  2150. import sys
  2151. import urllib
  2152. import urllib2
  2153. import urlparse
  2154. # The md5 module was deprecated in Python 2.5.
  2155. try:
  2156. from hashlib import md5
  2157. except ImportError:
  2158. from md5 import md5
  2159. try:
  2160. import readline
  2161. except ImportError:
  2162. pass
  2163. # The logging verbosity:
  2164. # 0: Errors only.
  2165. # 1: Status messages.
  2166. # 2: Info logs.
  2167. # 3: Debug logs.
  2168. verbosity = 1
  2169. # Max size of patch or base file.
  2170. MAX_UPLOAD_SIZE = 900 * 1024
  2171. # whitelist for non-binary filetypes which do not start with "text/"
  2172. # .mm (Objective-C) shows up as application/x-freemind on my Linux box.
  2173. TEXT_MIMETYPES = [
  2174. 'application/javascript',
  2175. 'application/x-javascript',
  2176. 'application/x-freemind'
  2177. ]
  2178. def GetEmail(prompt):
  2179. """Prompts the user for their email address and returns it.
  2180. The last used email address is saved to a file and offered up as a suggestion
  2181. to the user. If the user presses enter without typing in anything the last
  2182. used email address is used. If the user enters a new address, it is saved
  2183. for next time we prompt.
  2184. """
  2185. last_email_file_name = os.path.expanduser("~/.last_codereview_email_address")
  2186. last_email = ""
  2187. if os.path.exists(last_email_file_name):
  2188. try:
  2189. last_email_file = open(last_email_file_name, "r")
  2190. last_email = last_email_file.readline().strip("\n")
  2191. last_email_file.close()
  2192. prompt += " [%s]" % last_email
  2193. except IOError, e:
  2194. pass
  2195. email = raw_input(prompt + ": ").strip()
  2196. if email:
  2197. try:
  2198. last_email_file = open(last_email_file_name, "w")
  2199. last_email_file.write(email)
  2200. last_email_file.close()
  2201. except IOError, e:
  2202. pass
  2203. else:
  2204. email = last_email
  2205. return email
  2206. def StatusUpdate(msg):
  2207. """Print a status message to stdout.
  2208. If 'verbosity' is greater than 0, print the message.
  2209. Args:
  2210. msg: The string to print.
  2211. """
  2212. if verbosity > 0:
  2213. print msg
  2214. def ErrorExit(msg):
  2215. """Print an error message to stderr and exit."""
  2216. print >>sys.stderr, msg
  2217. sys.exit(1)
  2218. class ClientLoginError(urllib2.HTTPError):
  2219. """Raised to indicate there was an error authenticating with ClientLogin."""
  2220. def __init__(self, url, code, msg, headers, args):
  2221. urllib2.HTTPError.__init__(self, url, code, msg, headers, None)
  2222. self.args = args
  2223. self.reason = args["Error"]
  2224. class AbstractRpcServer(object):
  2225. """Provides a common interface for a simple RPC server."""
  2226. def __init__(self, host, auth_function, host_override=None, extra_headers={}, save_cookies=False):
  2227. """Creates a new HttpRpcServer.
  2228. Args:
  2229. host: The host to send requests to.
  2230. auth_function: A function that takes no arguments and returns an
  2231. (email, password) tuple when called. Will be called if authentication
  2232. is required.
  2233. host_override: The host header to send to the server (defaults to host).
  2234. extra_headers: A dict of extra headers to append to every request.
  2235. save_cookies: If True, save the authentication cookies to local disk.
  2236. If False, use an in-memory cookiejar instead. Subclasses must
  2237. implement this functionality. Defaults to False.
  2238. """
  2239. self.host = host
  2240. self.host_override = host_override
  2241. self.auth_function = auth_function
  2242. self.authenticated = False
  2243. self.extra_headers = extra_headers
  2244. self.save_cookies = save_cookies
  2245. self.opener = self._GetOpener()
  2246. if self.host_override:
  2247. logging.info("Server: %s; Host: %s", self.host, self.host_override)
  2248. else:
  2249. logging.info("Server: %s", self.host)
  2250. def _GetOpener(self):
  2251. """Returns an OpenerDirector for making HTTP requests.
  2252. Returns:
  2253. A urllib2.OpenerDirector object.
  2254. """
  2255. raise NotImplementedError()
  2256. def _CreateRequest(self, url, data=None):
  2257. """Creates a new urllib request."""
  2258. logging.debug("Creating request for: '%s' with payload:\n%s", url, data)
  2259. req = urllib2.Request(url, data=data)
  2260. if self.host_override:
  2261. req.add_header("Host", self.host_override)
  2262. for key, value in self.extra_headers.iteritems():
  2263. req.add_header(key, value)
  2264. return req
  2265. def _GetAuthToken(self, email, password):
  2266. """Uses ClientLogin to authenticate the user, returning an auth token.
  2267. Args:
  2268. email: The user's email address
  2269. password: The user's password
  2270. Raises:
  2271. ClientLoginError: If there was an error authenticating with ClientLogin.
  2272. HTTPError: If there was some other form of HTTP error.
  2273. Returns:
  2274. The authentication token returned by ClientLogin.
  2275. """
  2276. account_type = "GOOGLE"
  2277. if self.host.endswith(".google.com") and not force_google_account:
  2278. # Needed for use inside Google.
  2279. account_type = "HOSTED"
  2280. req = self._CreateRequest(
  2281. url="https://www.google.com/accounts/ClientLogin",
  2282. data=urllib.urlencode({
  2283. "Email": email,
  2284. "Passwd": password,
  2285. "service": "ah",
  2286. "source": "rietveld-codereview-upload",
  2287. "accountType": account_type,
  2288. }),
  2289. )
  2290. try:
  2291. response = self.opener.open(req)
  2292. response_body = response.read()
  2293. response_dict = dict(x.split("=") for x in response_body.split("\n") if x)
  2294. return response_dict["Auth"]
  2295. except urllib2.HTTPError, e:
  2296. if e.code == 403:
  2297. body = e.read()
  2298. response_dict = dict(x.split("=", 1) for x in body.split("\n") if x)
  2299. raise ClientLoginError(req.get_full_url(), e.code, e.msg, e.headers, response_dict)
  2300. else:
  2301. raise
  2302. def _GetAuthCookie(self, auth_token):
  2303. """Fetches authentication cookies for an authentication token.
  2304. Args:
  2305. auth_token: The authentication token returned by ClientLogin.
  2306. Raises:
  2307. HTTPError: If there was an error fetching the authentication cookies.
  2308. """
  2309. # This is a dummy value to allow us to identify when we're successful.
  2310. continue_location = "http://localhost/"
  2311. args = {"continue": continue_location, "auth": auth_token}
  2312. req = self._CreateRequest("http://%s/_ah/login?%s" % (self.host, urllib.urlencode(args)))
  2313. try:
  2314. response = self.opener.open(req)
  2315. except urllib2.HTTPError, e:
  2316. response = e
  2317. if (response.code != 302 or
  2318. response.info()["location"] != continue_location):
  2319. raise urllib2.HTTPError(req.get_full_url(), response.code, response.msg, response.headers, response.fp)
  2320. self.authenticated = True
  2321. def _Authenticate(self):
  2322. """Authenticates the user.
  2323. The authentication process works as follows:
  2324. 1) We get a username and password from the user
  2325. 2) We use ClientLogin to obtain an AUTH token for the user
  2326. (see http://code.google.com/apis/accounts/AuthForInstalledApps.html).
  2327. 3) We pass the auth token to /_ah/login on the server to obtain an
  2328. authentication cookie. If login was successful, it tries to redirect
  2329. us to the URL we provided.
  2330. If we attempt to access the upload API without first obtaining an
  2331. authentication cookie, it returns a 401 response (or a 302) and
  2332. directs us to authenticate ourselves with ClientLogin.
  2333. """
  2334. for i in range(3):
  2335. credentials = self.auth_function()
  2336. try:
  2337. auth_token = self._GetAuthToken(credentials[0], credentials[1])
  2338. except ClientLoginError, e:
  2339. if e.reason == "BadAuthentication":
  2340. print >>sys.stderr, "Invalid username or password."
  2341. continue
  2342. if e.reason == "CaptchaRequired":
  2343. print >>sys.stderr, (
  2344. "Please go to\n"
  2345. "https://www.google.com/accounts/DisplayUnlockCaptcha\n"
  2346. "and verify you are a human. Then try again.")
  2347. break
  2348. if e.reason == "NotVerified":
  2349. print >>sys.stderr, "Account not verified."
  2350. break
  2351. if e.reason == "TermsNotAgreed":
  2352. print >>sys.stderr, "User has not agreed to TOS."
  2353. break
  2354. if e.reason == "AccountDeleted":
  2355. print >>sys.stderr, "The user account has been deleted."
  2356. break
  2357. if e.reason == "AccountDisabled":
  2358. print >>sys.stderr, "The user account has been disabled."
  2359. break
  2360. if e.reason == "ServiceDisabled":
  2361. print >>sys.stderr, "The user's access to the service has been disabled."
  2362. break
  2363. if e.reason == "ServiceUnavailable":
  2364. print >>sys.stderr, "The service is not available; try again later."
  2365. break
  2366. raise
  2367. self._GetAuthCookie(auth_token)
  2368. return
  2369. def Send(self, request_path, payload=None,
  2370. content_type="application/octet-stream",
  2371. timeout=None,
  2372. **kwargs):
  2373. """Sends an RPC and returns the response.
  2374. Args:
  2375. request_path: The path to send the request to, eg /api/appversion/create.
  2376. payload: The body of the request, or None to send an empty request.
  2377. content_type: The Content-Type header to use.
  2378. timeout: timeout in seconds; default None i.e. no timeout.
  2379. (Note: for large requests on OS X, the timeout doesn't work right.)
  2380. kwargs: Any keyword arguments are converted into query string parameters.
  2381. Returns:
  2382. The response body, as a string.
  2383. """
  2384. # TODO: Don't require authentication. Let the server say
  2385. # whether it is necessary.
  2386. if not self.authenticated:
  2387. self._Authenticate()
  2388. old_timeout = socket.getdefaulttimeout()
  2389. socket.setdefaulttimeout(timeout)
  2390. try:
  2391. tries = 0
  2392. while True:
  2393. tries += 1
  2394. args = dict(kwargs)
  2395. url = "http://%s%s" % (self.host, request_path)
  2396. if args:
  2397. url += "?" + urllib.urlencode(args)
  2398. req = self._CreateRequest(url=url, data=payload)
  2399. req.add_header("Content-Type", content_type)
  2400. try:
  2401. f = self.opener.open(req)
  2402. response = f.read()
  2403. f.close()
  2404. return response
  2405. except urllib2.HTTPError, e:
  2406. if tries > 3:
  2407. raise
  2408. elif e.code == 401 or e.code == 302:
  2409. self._Authenticate()
  2410. else:
  2411. raise
  2412. finally:
  2413. socket.setdefaulttimeout(old_timeout)
  2414. class HttpRpcServer(AbstractRpcServer):
  2415. """Provides a simplified RPC-style interface for HTTP requests."""
  2416. def _Authenticate(self):
  2417. """Save the cookie jar after authentication."""
  2418. super(HttpRpcServer, self)._Authenticate()
  2419. if self.save_cookies:
  2420. StatusUpdate("Saving authentication cookies to %s" % self.cookie_file)
  2421. self.cookie_jar.save()
  2422. def _GetOpener(self):
  2423. """Returns an OpenerDirector that supports cookies and ignores redirects.
  2424. Returns:
  2425. A urllib2.OpenerDirector object.
  2426. """
  2427. opener = urllib2.OpenerDirector()
  2428. opener.add_handler(urllib2.ProxyHandler())
  2429. opener.add_handler(urllib2.UnknownHandler())
  2430. opener.add_handler(urllib2.HTTPHandler())
  2431. opener.add_handler(urllib2.HTTPDefaultErrorHandler())
  2432. opener.add_handler(urllib2.HTTPSHandler())
  2433. opener.add_handler(urllib2.HTTPErrorProcessor())
  2434. if self.save_cookies:
  2435. self.cookie_file = os.path.expanduser("~/.codereview_upload_cookies_" + server)
  2436. self.cookie_jar = cookielib.MozillaCookieJar(self.cookie_file)
  2437. if os.path.exists(self.cookie_file):
  2438. try:
  2439. self.cookie_jar.load()
  2440. self.authenticated = True
  2441. StatusUpdate("Loaded authentication cookies from %s" % self.cookie_file)
  2442. except (cookielib.LoadError, IOError):
  2443. # Failed to load cookies - just ignore them.
  2444. pass
  2445. else:
  2446. # Create an empty cookie file with mode 600
  2447. fd = os.open(self.cookie_file, os.O_CREAT, 0600)
  2448. os.close(fd)
  2449. # Always chmod the cookie file
  2450. os.chmod(self.cookie_file, 0600)
  2451. else:
  2452. # Don't save cookies across runs of update.py.
  2453. self.cookie_jar = cookielib.CookieJar()
  2454. opener.add_handler(urllib2.HTTPCookieProcessor(self.cookie_jar))
  2455. return opener
  2456. def GetRpcServer(options):
  2457. """Returns an instance of an AbstractRpcServer.
  2458. Returns:
  2459. A new AbstractRpcServer, on which RPC calls can be made.
  2460. """
  2461. rpc_server_class = HttpRpcServer
  2462. def GetUserCredentials():
  2463. """Prompts the user for a username and password."""
  2464. # Disable status prints so they don't obscure the password prompt.
  2465. global global_status
  2466. st = global_status
  2467. global_status = None
  2468. email = options.email
  2469. if email is None:
  2470. email = GetEmail("Email (login for uploading to %s)" % options.server)
  2471. password = getpass.getpass("Password for %s: " % email)
  2472. # Put status back.
  2473. global_status = st
  2474. return (email, password)
  2475. # If this is the dev_appserver, use fake authentication.
  2476. host = (options.host or options.server).lower()
  2477. if host == "localhost" or host.startswith("localhost:"):
  2478. email = options.email
  2479. if email is None:
  2480. email = "test@example.com"
  2481. logging.info("Using debug user %s. Override with --email" % email)
  2482. server = rpc_server_class(
  2483. options.server,
  2484. lambda: (email, "password"),
  2485. host_override=options.host,
  2486. extra_headers={"Cookie": 'dev_appserver_login="%s:False"' % email},
  2487. save_cookies=options.save_cookies)
  2488. # Don't try to talk to ClientLogin.
  2489. server.authenticated = True
  2490. return server
  2491. return rpc_server_class(options.server, GetUserCredentials,
  2492. host_override=options.host, save_cookies=options.save_cookies)
  2493. def EncodeMultipartFormData(fields, files):
  2494. """Encode form fields for multipart/form-data.
  2495. Args:
  2496. fields: A sequence of (name, value) elements for regular form fields.
  2497. files: A sequence of (name, filename, value) elements for data to be
  2498. uploaded as files.
  2499. Returns:
  2500. (content_type, body) ready for httplib.HTTP instance.
  2501. Source:
  2502. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146306
  2503. """
  2504. BOUNDARY = '-M-A-G-I-C---B-O-U-N-D-A-R-Y-'
  2505. CRLF = '\r\n'
  2506. lines = []
  2507. for (key, value) in fields:
  2508. typecheck(key, str)
  2509. typecheck(value, str)
  2510. lines.append('--' + BOUNDARY)
  2511. lines.append('Content-Disposition: form-data; name="%s"' % key)
  2512. lines.append('')
  2513. lines.append(value)
  2514. for (key, filename, value) in files:
  2515. typecheck(key, str)
  2516. typecheck(filename, str)
  2517. typecheck(value, str)
  2518. lines.append('--' + BOUNDARY)
  2519. lines.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
  2520. lines.append('Content-Type: %s' % GetContentType(filename))
  2521. lines.append('')
  2522. lines.append(value)
  2523. lines.append('--' + BOUNDARY + '--')
  2524. lines.append('')
  2525. body = CRLF.join(lines)
  2526. content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
  2527. return content_type, body
  2528. def GetContentType(filename):
  2529. """Helper to guess the content-type from the filename."""
  2530. return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
  2531. # Use a shell for subcommands on Windows to get a PATH search.
  2532. use_shell = sys.platform.startswith("win")
  2533. def RunShellWithReturnCode(command, print_output=False,
  2534. universal_newlines=True, env=os.environ):
  2535. """Executes a command and returns the output from stdout and the return code.
  2536. Args:
  2537. command: Command to execute.
  2538. print_output: If True, the output is printed to stdout.
  2539. If False, both stdout and stderr are ignored.
  2540. universal_newlines: Use universal_newlines flag (default: True).
  2541. Returns:
  2542. Tuple (output, return code)
  2543. """
  2544. logging.info("Running %s", command)
  2545. p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
  2546. shell=use_shell, universal_newlines=universal_newlines, env=env)
  2547. if print_output:
  2548. output_array = []
  2549. while True:
  2550. line = p.stdout.readline()
  2551. if not line:
  2552. break
  2553. print line.strip("\n")
  2554. output_array.append(line)
  2555. output = "".join(output_array)
  2556. else:
  2557. output = p.stdout.read()
  2558. p.wait()
  2559. errout = p.stderr.read()
  2560. if print_output and errout:
  2561. print >>sys.stderr, errout
  2562. p.stdout.close()
  2563. p.stderr.close()
  2564. return output, p.returncode
  2565. def RunShell(command, silent_ok=False, universal_newlines=True,
  2566. print_output=False, env=os.environ):
  2567. data, retcode = RunShellWithReturnCode(command, print_output, universal_newlines, env)
  2568. if retcode:
  2569. ErrorExit("Got error status from %s:\n%s" % (command, data))
  2570. if not silent_ok and not data:
  2571. ErrorExit("No output from %s" % command)
  2572. return data
  2573. class VersionControlSystem(object):
  2574. """Abstract base class providing an interface to the VCS."""
  2575. def __init__(self, options):
  2576. """Constructor.
  2577. Args:
  2578. options: Command line options.
  2579. """
  2580. self.options = options
  2581. def GenerateDiff(self, args):
  2582. """Return the current diff as a string.
  2583. Args:
  2584. args: Extra arguments to pass to the diff command.
  2585. """
  2586. raise NotImplementedError(
  2587. "abstract method -- subclass %s must override" % self.__class__)
  2588. def GetUnknownFiles(self):
  2589. """Return a list of files unknown to the VCS."""
  2590. raise NotImplementedError(
  2591. "abstract method -- subclass %s must override" % self.__class__)
  2592. def CheckForUnknownFiles(self):
  2593. """Show an "are you sure?" prompt if there are unknown files."""
  2594. unknown_files = self.GetUnknownFiles()
  2595. if unknown_files:
  2596. print "The following files are not added to version control:"
  2597. for line in unknown_files:
  2598. print line
  2599. prompt = "Are you sure to continue?(y/N) "
  2600. answer = raw_input(prompt).strip()
  2601. if answer != "y":
  2602. ErrorExit("User aborted")
  2603. def GetBaseFile(self, filename):
  2604. """Get the content of the upstream version of a file.
  2605. Returns:
  2606. A tuple (base_content, new_content, is_binary, status)
  2607. base_content: The contents of the base file.
  2608. new_content: For text files, this is empty. For binary files, this is
  2609. the contents of the new file, since the diff output won't contain
  2610. information to reconstruct the current file.
  2611. is_binary: True iff the file is binary.
  2612. status: The status of the file.
  2613. """
  2614. raise NotImplementedError(
  2615. "abstract method -- subclass %s must override" % self.__class__)
  2616. def GetBaseFiles(self, diff):
  2617. """Helper that calls GetBase file for each file in the patch.
  2618. Returns:
  2619. A dictionary that maps from filename to GetBaseFile's tuple. Filenames
  2620. are retrieved based on lines that start with "Index:" or
  2621. "Property changes on:".
  2622. """
  2623. files = {}
  2624. for line in diff.splitlines(True):
  2625. if line.startswith('Index:') or line.startswith('Property changes on:'):
  2626. unused, filename = line.split(':', 1)
  2627. # On Windows if a file has property changes its filename uses '\'
  2628. # instead of '/'.
  2629. filename = filename.strip().replace('\\', '/')
  2630. files[filename] = self.GetBaseFile(filename)
  2631. return files
  2632. def UploadBaseFiles(self, issue, rpc_server, patch_list, patchset, options,
  2633. files):
  2634. """Uploads the base files (and if necessary, the current ones as well)."""
  2635. def UploadFile(filename, file_id, content, is_binary, status, is_base):
  2636. """Uploads a file to the server."""
  2637. set_status("uploading " + filename)
  2638. file_too_large = False
  2639. if is_base:
  2640. type = "base"
  2641. else:
  2642. type = "current"
  2643. if len(content) > MAX_UPLOAD_SIZE:
  2644. print ("Not uploading the %s file for %s because it's too large." %
  2645. (type, filename))
  2646. file_too_large = True
  2647. content = ""
  2648. checksum = md5(content).hexdigest()
  2649. if options.verbose > 0 and not file_too_large:
  2650. print "Uploading %s file for %s" % (type, filename)
  2651. url = "/%d/upload_content/%d/%d" % (int(issue), int(patchset), file_id)
  2652. form_fields = [
  2653. ("filename", filename),
  2654. ("status", status),
  2655. ("checksum", checksum),
  2656. ("is_binary", str(is_binary)),
  2657. ("is_current", str(not is_base)),
  2658. ]
  2659. if file_too_large:
  2660. form_fields.append(("file_too_large", "1"))
  2661. if options.email:
  2662. form_fields.append(("user", options.email))
  2663. ctype, body = EncodeMultipartFormData(form_fields, [("data", filename, content)])
  2664. response_body = rpc_server.Send(url, body, content_type=ctype)
  2665. if not response_body.startswith("OK"):
  2666. StatusUpdate(" --> %s" % response_body)
  2667. sys.exit(1)
  2668. # Don't want to spawn too many threads, nor do we want to
  2669. # hit Rietveld too hard, or it will start serving 500 errors.
  2670. # When 8 works, it's no better than 4, and sometimes 8 is
  2671. # too many for Rietveld to handle.
  2672. MAX_PARALLEL_UPLOADS = 4
  2673. sema = threading.BoundedSemaphore(MAX_PARALLEL_UPLOADS)
  2674. upload_threads = []
  2675. finished_upload_threads = []
  2676. class UploadFileThread(threading.Thread):
  2677. def __init__(self, args):
  2678. threading.Thread.__init__(self)
  2679. self.args = args
  2680. def run(self):
  2681. UploadFile(*self.args)
  2682. finished_upload_threads.append(self)
  2683. sema.release()
  2684. def StartUploadFile(*args):
  2685. sema.acquire()
  2686. while len(finished_upload_threads) > 0:
  2687. t = finished_upload_threads.pop()
  2688. upload_threads.remove(t)
  2689. t.join()
  2690. t = UploadFileThread(args)
  2691. upload_threads.append(t)
  2692. t.start()
  2693. def WaitForUploads():
  2694. for t in upload_threads:
  2695. t.join()
  2696. patches = dict()
  2697. [patches.setdefault(v, k) for k, v in patch_list]
  2698. for filename in patches.keys():
  2699. base_content, new_content, is_binary, status = files[filename]
  2700. file_id_str = patches.get(filename)
  2701. if file_id_str.find("nobase") != -1:
  2702. base_content = None
  2703. file_id_str = file_id_str[file_id_str.rfind("_") + 1:]
  2704. file_id = int(file_id_str)
  2705. if base_content != None:
  2706. StartUploadFile(filename, file_id, base_content, is_binary, status, True)
  2707. if new_content != None:
  2708. StartUploadFile(filename, file_id, new_content, is_binary, status, False)
  2709. WaitForUploads()
  2710. def IsImage(self, filename):
  2711. """Returns true if the filename has an image extension."""
  2712. mimetype = mimetypes.guess_type(filename)[0]
  2713. if not mimetype:
  2714. return False
  2715. return mimetype.startswith("image/")
  2716. def IsBinary(self, filename):
  2717. """Returns true if the guessed mimetyped isnt't in text group."""
  2718. mimetype = mimetypes.guess_type(filename)[0]
  2719. if not mimetype:
  2720. return False # e.g. README, "real" binaries usually have an extension
  2721. # special case for text files which don't start with text/
  2722. if mimetype in TEXT_MIMETYPES:
  2723. return False
  2724. return not mimetype.startswith("text/")
  2725. class FakeMercurialUI(object):
  2726. def __init__(self):
  2727. self.quiet = True
  2728. self.output = ''
  2729. def write(self, *args, **opts):
  2730. self.output += ' '.join(args)
  2731. use_hg_shell = False # set to True to shell out to hg always; slower
  2732. class MercurialVCS(VersionControlSystem):
  2733. """Implementation of the VersionControlSystem interface for Mercurial."""
  2734. def __init__(self, options, ui, repo):
  2735. super(MercurialVCS, self).__init__(options)
  2736. self.ui = ui
  2737. self.repo = repo
  2738. # Absolute path to repository (we can be in a subdir)
  2739. self.repo_dir = os.path.normpath(repo.root)
  2740. # Compute the subdir
  2741. cwd = os.path.normpath(os.getcwd())
  2742. assert cwd.startswith(self.repo_dir)
  2743. self.subdir = cwd[len(self.repo_dir):].lstrip(r"\/")
  2744. if self.options.revision:
  2745. self.base_rev = self.options.revision
  2746. else:
  2747. mqparent, err = RunShellWithReturnCode(['hg', 'log', '--rev', 'qparent', '--template={node}'])
  2748. if not err and mqparent != "":
  2749. self.base_rev = mqparent
  2750. else:
  2751. self.base_rev = RunShell(["hg", "parents", "-q"]).split(':')[1].strip()
  2752. def _GetRelPath(self, filename):
  2753. """Get relative path of a file according to the current directory,
  2754. given its logical path in the repo."""
  2755. assert filename.startswith(self.subdir), (filename, self.subdir)
  2756. return filename[len(self.subdir):].lstrip(r"\/")
  2757. def GenerateDiff(self, extra_args):
  2758. # If no file specified, restrict to the current subdir
  2759. extra_args = extra_args or ["."]
  2760. cmd = ["hg", "diff", "--git", "-r", self.base_rev] + extra_args
  2761. data = RunShell(cmd, silent_ok=True)
  2762. svndiff = []
  2763. filecount = 0
  2764. for line in data.splitlines():
  2765. m = re.match("diff --git a/(\S+) b/(\S+)", line)
  2766. if m:
  2767. # Modify line to make it look like as it comes from svn diff.
  2768. # With this modification no changes on the server side are required
  2769. # to make upload.py work with Mercurial repos.
  2770. # NOTE: for proper handling of moved/copied files, we have to use
  2771. # the second filename.
  2772. filename = m.group(2)
  2773. svndiff.append("Index: %s" % filename)
  2774. svndiff.append("=" * 67)
  2775. filecount += 1
  2776. logging.info(line)
  2777. else:
  2778. svndiff.append(line)
  2779. if not filecount:
  2780. ErrorExit("No valid patches found in output from hg diff")
  2781. return "\n".join(svndiff) + "\n"
  2782. def GetUnknownFiles(self):
  2783. """Return a list of files unknown to the VCS."""
  2784. args = []
  2785. status = RunShell(["hg", "status", "--rev", self.base_rev, "-u", "."],
  2786. silent_ok=True)
  2787. unknown_files = []
  2788. for line in status.splitlines():
  2789. st, fn = line.split(" ", 1)
  2790. if st == "?":
  2791. unknown_files.append(fn)
  2792. return unknown_files
  2793. def GetBaseFile(self, filename):
  2794. set_status("inspecting " + filename)
  2795. # "hg status" and "hg cat" both take a path relative to the current subdir
  2796. # rather than to the repo root, but "hg diff" has given us the full path
  2797. # to the repo root.
  2798. base_content = ""
  2799. new_content = None
  2800. is_binary = False
  2801. oldrelpath = relpath = self._GetRelPath(filename)
  2802. # "hg status -C" returns two lines for moved/copied files, one otherwise
  2803. if use_hg_shell:
  2804. out = RunShell(["hg", "status", "-C", "--rev", self.base_rev, relpath])
  2805. else:
  2806. fui = FakeMercurialUI()
  2807. ret = commands.status(fui, self.repo, *[relpath], **{'rev': [self.base_rev], 'copies': True})
  2808. if ret:
  2809. raise util.Abort(ret)
  2810. out = fui.output
  2811. out = out.splitlines()
  2812. # HACK: strip error message about missing file/directory if it isn't in
  2813. # the working copy
  2814. if out[0].startswith('%s: ' % relpath):
  2815. out = out[1:]
  2816. status, what = out[0].split(' ', 1)
  2817. if len(out) > 1 and status == "A" and what == relpath:
  2818. oldrelpath = out[1].strip()
  2819. status = "M"
  2820. if ":" in self.base_rev:
  2821. base_rev = self.base_rev.split(":", 1)[0]
  2822. else:
  2823. base_rev = self.base_rev
  2824. if status != "A":
  2825. if use_hg_shell:
  2826. base_content = RunShell(["hg", "cat", "-r", base_rev, oldrelpath], silent_ok=True)
  2827. else:
  2828. base_content = str(self.repo[base_rev][oldrelpath].data())
  2829. is_binary = "\0" in base_content # Mercurial's heuristic
  2830. if status != "R":
  2831. new_content = open(relpath, "rb").read()
  2832. is_binary = is_binary or "\0" in new_content
  2833. if is_binary and base_content and use_hg_shell:
  2834. # Fetch again without converting newlines
  2835. base_content = RunShell(["hg", "cat", "-r", base_rev, oldrelpath],
  2836. silent_ok=True, universal_newlines=False)
  2837. if not is_binary or not self.IsImage(relpath):
  2838. new_content = None
  2839. return base_content, new_content, is_binary, status
  2840. # NOTE: The SplitPatch function is duplicated in engine.py, keep them in sync.
  2841. def SplitPatch(data):
  2842. """Splits a patch into separate pieces for each file.
  2843. Args:
  2844. data: A string containing the output of svn diff.
  2845. Returns:
  2846. A list of 2-tuple (filename, text) where text is the svn diff output
  2847. pertaining to filename.
  2848. """
  2849. patches = []
  2850. filename = None
  2851. diff = []
  2852. for line in data.splitlines(True):
  2853. new_filename = None
  2854. if line.startswith('Index:'):
  2855. unused, new_filename = line.split(':', 1)
  2856. new_filename = new_filename.strip()
  2857. elif line.startswith('Property changes on:'):
  2858. unused, temp_filename = line.split(':', 1)
  2859. # When a file is modified, paths use '/' between directories, however
  2860. # when a property is modified '\' is used on Windows. Make them the same
  2861. # otherwise the file shows up twice.
  2862. temp_filename = temp_filename.strip().replace('\\', '/')
  2863. if temp_filename != filename:
  2864. # File has property changes but no modifications, create a new diff.
  2865. new_filename = temp_filename
  2866. if new_filename:
  2867. if filename and diff:
  2868. patches.append((filename, ''.join(diff)))
  2869. filename = new_filename
  2870. diff = [line]
  2871. continue
  2872. if diff is not None:
  2873. diff.append(line)
  2874. if filename and diff:
  2875. patches.append((filename, ''.join(diff)))
  2876. return patches
  2877. def UploadSeparatePatches(issue, rpc_server, patchset, data, options):
  2878. """Uploads a separate patch for each file in the diff output.
  2879. Returns a list of [patch_key, filename] for each file.
  2880. """
  2881. patches = SplitPatch(data)
  2882. rv = []
  2883. for patch in patches:
  2884. set_status("uploading patch for " + patch[0])
  2885. if len(patch[1]) > MAX_UPLOAD_SIZE:
  2886. print ("Not uploading the patch for " + patch[0] +
  2887. " because the file is too large.")
  2888. continue
  2889. form_fields = [("filename", patch[0])]
  2890. if not options.download_base:
  2891. form_fields.append(("content_upload", "1"))
  2892. files = [("data", "data.diff", patch[1])]
  2893. ctype, body = EncodeMultipartFormData(form_fields, files)
  2894. url = "/%d/upload_patch/%d" % (int(issue), int(patchset))
  2895. print "Uploading patch for " + patch[0]
  2896. response_body = rpc_server.Send(url, body, content_type=ctype)
  2897. lines = response_body.splitlines()
  2898. if not lines or lines[0] != "OK":
  2899. StatusUpdate(" --> %s" % response_body)
  2900. sys.exit(1)
  2901. rv.append([lines[1], patch[0]])
  2902. return rv