roll_webgl_conformance.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. #!/usr/bin/env python3
  2. # Copyright 2015 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. from __future__ import print_function
  6. import argparse
  7. import collections
  8. import logging
  9. import os
  10. import re
  11. import subprocess
  12. import sys
  13. import time
  14. extra_trybots = [
  15. {
  16. "mastername": "luci.chromium.try",
  17. "buildernames": ["win_optional_gpu_tests_rel"]
  18. },
  19. {
  20. "mastername": "luci.chromium.try",
  21. "buildernames": ["mac_optional_gpu_tests_rel"]
  22. },
  23. {
  24. "mastername": "luci.chromium.try",
  25. "buildernames": ["linux_optional_gpu_tests_rel"]
  26. },
  27. {
  28. "mastername": "luci.chromium.try",
  29. "buildernames": ["android_optional_gpu_tests_rel"]
  30. },
  31. ]
  32. SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
  33. SRC_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir))
  34. sys.path.insert(0, os.path.join(SRC_DIR, 'build'))
  35. import find_depot_tools
  36. find_depot_tools.add_depot_tools_to_path()
  37. CHROMIUM_GIT_URL = 'https://chromium.googlesource.com/chromium/src.git'
  38. CL_ISSUE_RE = re.compile('^Issue number: ([0-9]+) \((.*)\)$')
  39. REVIEW_URL_RE = re.compile('^https?://(.*)/(.*)')
  40. ROLL_BRANCH_NAME = 'special_webgl_roll_branch'
  41. TRYJOB_STATUS_SLEEP_SECONDS = 30
  42. # Use a shell for subcommands on Windows to get a PATH search.
  43. IS_WIN = sys.platform.startswith('win')
  44. WEBGL_PATH = os.path.join('third_party', 'webgl', 'src')
  45. WEBGL_REVISION_TEXT_FILE = os.path.join(
  46. 'content', 'test', 'gpu', 'gpu_tests', 'webgl_conformance_revision.txt')
  47. CommitInfo = collections.namedtuple('CommitInfo', ['git_commit',
  48. 'git_repo_url'])
  49. CLInfo = collections.namedtuple('CLInfo', ['issue', 'url', 'review_server'])
  50. def _VarLookup(local_scope):
  51. return lambda var_name: local_scope['vars'][var_name]
  52. def _PosixPath(path):
  53. """Convert a possibly-Windows path to a posix-style path."""
  54. (_, path) = os.path.splitdrive(path)
  55. return path.replace(os.sep, '/')
  56. def _ParseGitCommitHash(description):
  57. for line in description.splitlines():
  58. if line.startswith('commit '):
  59. return line.split()[1]
  60. logging.error('Failed to parse git commit id from:\n%s\n', description)
  61. sys.exit(-1)
  62. return None
  63. def _ParseDepsFile(filename):
  64. logging.debug('Parsing deps file %s', filename)
  65. with open(filename, 'rb') as f:
  66. deps_content = f.read()
  67. return _ParseDepsDict(deps_content)
  68. def _ParseDepsDict(deps_content):
  69. local_scope = {}
  70. global_scope = {
  71. 'Str': lambda arg: str(arg),
  72. 'Var': _VarLookup(local_scope),
  73. 'deps_os': {},
  74. }
  75. exec(deps_content, global_scope, local_scope)
  76. return local_scope
  77. def _GenerateCLDescriptionCommand(webgl_current, webgl_new, bugs):
  78. def GetChangeString(current_hash, new_hash):
  79. return '%s..%s' % (current_hash[0:7], new_hash[0:7]);
  80. def GetChangeLogURL(git_repo_url, change_string):
  81. return '%s/+log/%s' % (git_repo_url, change_string)
  82. def GetBugString(bugs):
  83. bug_str = 'Bug: '
  84. for bug in bugs:
  85. bug_str += str(bug) + ','
  86. return bug_str.rstrip(',')
  87. change_str = GetChangeString(webgl_current.git_commit,
  88. webgl_new.git_commit)
  89. changelog_url = GetChangeLogURL(webgl_current.git_repo_url,
  90. change_str)
  91. if webgl_current.git_commit == webgl_new.git_commit:
  92. print('WARNING: WebGL repository is unchanged; proceeding with no-op roll')
  93. def GetExtraTrybotString():
  94. s = ''
  95. for t in extra_trybots:
  96. if s:
  97. s += ';'
  98. s += t['mastername'] + ':' + ','.join(t['buildernames'])
  99. return s
  100. return ('Roll WebGL %s\n\n'
  101. '%s\n\n'
  102. '%s\n'
  103. 'Cq-Include-Trybots: %s\n') % (
  104. change_str,
  105. changelog_url,
  106. GetBugString(bugs),
  107. GetExtraTrybotString())
  108. class AutoRoller(object):
  109. def __init__(self, chromium_src):
  110. self._chromium_src = chromium_src
  111. def _RunCommand(self, command, working_dir=None, ignore_exit_code=False,
  112. extra_env=None):
  113. """Runs a command and returns the stdout from that command.
  114. If the command fails (exit code != 0), the function will exit the process.
  115. """
  116. working_dir = working_dir or self._chromium_src
  117. logging.debug('cmd: %s cwd: %s', ' '.join(command), working_dir)
  118. env = os.environ.copy()
  119. if extra_env:
  120. logging.debug('extra env: %s', extra_env)
  121. env.update(extra_env)
  122. p = subprocess.Popen(command, stdout=subprocess.PIPE,
  123. stderr=subprocess.PIPE, shell=IS_WIN, env=env,
  124. cwd=working_dir, universal_newlines=True)
  125. output = p.stdout.read()
  126. p.wait()
  127. p.stdout.close()
  128. p.stderr.close()
  129. if not ignore_exit_code and p.returncode != 0:
  130. logging.error('Command failed: %s\n%s', str(command), output)
  131. sys.exit(p.returncode)
  132. return output
  133. def _GetCommitInfo(self, path_below_src, git_hash=None, git_repo_url=None):
  134. working_dir = os.path.join(self._chromium_src, path_below_src)
  135. self._RunCommand(['git', 'fetch', 'origin'], working_dir=working_dir)
  136. revision_range = git_hash or 'origin/main'
  137. ret = self._RunCommand(
  138. ['git', '--no-pager', 'log', revision_range,
  139. '--no-abbrev-commit', '--pretty=full', '-1'],
  140. working_dir=working_dir)
  141. parsed_hash = _ParseGitCommitHash(ret)
  142. logging.debug('parsed Git commit hash: %s', parsed_hash)
  143. return CommitInfo(parsed_hash, git_repo_url)
  144. def _GetDepsCommitInfo(self, deps_dict, path_below_src):
  145. logging.debug('Getting deps commit info for %s', path_below_src)
  146. entry = deps_dict['deps'][_PosixPath('src/%s' % path_below_src)]
  147. at_index = entry.find('@')
  148. git_repo_url = entry[:at_index]
  149. git_hash = entry[at_index + 1:]
  150. return self._GetCommitInfo(path_below_src, git_hash, git_repo_url)
  151. def _GetCLInfo(self):
  152. cl_output = self._RunCommand(['git', 'cl', 'issue'])
  153. m = CL_ISSUE_RE.match(cl_output.strip())
  154. if not m:
  155. logging.error('Cannot find any CL info. Output was:\n%s', cl_output)
  156. sys.exit(-1)
  157. issue_number = int(m.group(1))
  158. url = m.group(2)
  159. # Parse the codereview host from the URL.
  160. m = REVIEW_URL_RE.match(url)
  161. if not m:
  162. logging.error('Cannot parse codereview host from URL: %s', url)
  163. sys.exit(-1)
  164. review_server = m.group(1)
  165. return CLInfo(issue_number, url, review_server)
  166. def _GetCurrentBranchName(self):
  167. return self._RunCommand(
  168. ['git', 'rev-parse', '--abbrev-ref', 'HEAD']).splitlines()[0]
  169. def _IsTreeClean(self):
  170. lines = self._RunCommand(
  171. ['git', 'status', '--porcelain', '-uno']).splitlines()
  172. if len(lines) == 0:
  173. return True
  174. logging.debug('Dirty/unversioned files:\n%s', '\n'.join(lines))
  175. return False
  176. def _GetBugList(self, path_below_src, webgl_current, webgl_new):
  177. # TODO(kbr): this isn't useful, at least not yet, when run against
  178. # the WebGL Github repository.
  179. working_dir = os.path.join(self._chromium_src, path_below_src)
  180. lines = self._RunCommand(
  181. ['git','log',
  182. '%s..%s' % (webgl_current.git_commit, webgl_new.git_commit)],
  183. working_dir=working_dir).split('\n')
  184. bugs = set()
  185. for line in lines:
  186. line = line.strip()
  187. bug_prefix = 'BUG='
  188. if line.startswith(bug_prefix):
  189. bugs_strings = line[len(bug_prefix):].split(',')
  190. for bug_string in bugs_strings:
  191. try:
  192. bugs.add(int(bug_string))
  193. except:
  194. # skip this, it may be a project specific bug such as
  195. # "angleproject:X" or an ill-formed BUG= message
  196. pass
  197. return bugs
  198. def _UpdateReadmeFile(self, readme_path, new_revision):
  199. readme = open(os.path.join(self._chromium_src, readme_path), 'r+')
  200. txt = readme.read()
  201. m = re.sub(re.compile('.*^Revision\: ([0-9]*).*', re.MULTILINE),
  202. ('Revision: %s' % new_revision), txt)
  203. readme.seek(0)
  204. readme.write(m)
  205. readme.truncate()
  206. def PrepareRoll(self, ignore_checks, run_tryjobs):
  207. # TODO(kjellander): use os.path.normcase, os.path.join etc for all paths for
  208. # cross platform compatibility.
  209. if not ignore_checks:
  210. if self._GetCurrentBranchName() != 'main':
  211. logging.error('Please checkout the main branch.')
  212. return -1
  213. if not self._IsTreeClean():
  214. logging.error('Please make sure you don\'t have any modified files.')
  215. return -1
  216. # Always clean up any previous roll.
  217. self.Abort()
  218. logging.debug('Pulling latest changes')
  219. if not ignore_checks:
  220. self._RunCommand(['git', 'pull'])
  221. self._RunCommand(['git', 'checkout', '-b', ROLL_BRANCH_NAME])
  222. # Modify Chromium's DEPS file.
  223. # Parse current hashes.
  224. deps_filename = os.path.join(self._chromium_src, 'DEPS')
  225. deps = _ParseDepsFile(deps_filename)
  226. webgl_current = self._GetDepsCommitInfo(deps, WEBGL_PATH)
  227. # Find ToT revisions.
  228. webgl_latest = self._GetCommitInfo(WEBGL_PATH)
  229. if IS_WIN:
  230. # Make sure the roll script doesn't use windows line endings
  231. self._RunCommand(['git', 'config', 'core.autocrlf', 'true'])
  232. self._UpdateDep(deps_filename, WEBGL_PATH, webgl_latest)
  233. self._UpdateWebGLRevTextFile(WEBGL_REVISION_TEXT_FILE, webgl_latest)
  234. if self._IsTreeClean():
  235. logging.debug('Tree is clean - no changes detected.')
  236. self._DeleteRollBranch()
  237. else:
  238. bugs = self._GetBugList(WEBGL_PATH, webgl_current, webgl_latest)
  239. description = _GenerateCLDescriptionCommand(
  240. webgl_current, webgl_latest, bugs)
  241. logging.debug('Committing changes locally.')
  242. self._RunCommand(['git', 'add', '--update', '.'])
  243. self._RunCommand(['git', 'commit', '-m', description])
  244. logging.debug('Uploading changes...')
  245. self._RunCommand(['git', 'cl', 'upload'],
  246. extra_env={'EDITOR': 'true'})
  247. if run_tryjobs:
  248. # Kick off tryjobs.
  249. base_try_cmd = ['git', 'cl', 'try']
  250. self._RunCommand(base_try_cmd)
  251. cl_info = self._GetCLInfo()
  252. print('Issue: %d URL: %s' % (cl_info.issue, cl_info.url))
  253. # Checkout main again.
  254. self._RunCommand(['git', 'checkout', 'main'])
  255. print('Roll branch left as ' + ROLL_BRANCH_NAME)
  256. return 0
  257. def _UpdateDep(self, deps_filename, dep_relative_to_src, commit_info):
  258. dep_name = _PosixPath(os.path.join('src', dep_relative_to_src))
  259. dep_revision = '%s@%s' % (dep_name, commit_info.git_commit)
  260. self._RunCommand(
  261. ['gclient', 'setdep', '-r', dep_revision],
  262. working_dir=os.path.dirname(deps_filename))
  263. def _UpdateWebGLRevTextFile(self, txt_filename, commit_info):
  264. # Rolling the WebGL conformance tests must cause at least all of
  265. # the WebGL tests to run. There are already exclusions in
  266. # trybot_analyze_config.json which force all tests to run if
  267. # changes under src/content/test/gpu are made. (This rule
  268. # typically only takes effect on the GPU bots.) To make sure this
  269. # happens all the time, update an autogenerated text file in this
  270. # directory.
  271. with open(txt_filename, 'w') as fh:
  272. print('# AUTOGENERATED FILE - DO NOT EDIT', file=fh)
  273. print('# SEE roll_webgl_conformance.py', file=fh)
  274. print('Current webgl revision %s' % commit_info.git_commit, file=fh)
  275. def _DeleteRollBranch(self):
  276. self._RunCommand(['git', 'checkout', 'main'])
  277. self._RunCommand(['git', 'branch', '-D', ROLL_BRANCH_NAME])
  278. logging.debug('Deleted the local roll branch (%s)', ROLL_BRANCH_NAME)
  279. def _GetBranches(self):
  280. """Returns a tuple of active,branches.
  281. The 'active' is the name of the currently active branch and 'branches' is a
  282. list of all branches.
  283. """
  284. lines = self._RunCommand(['git', 'branch']).split('\n')
  285. branches = []
  286. active = ''
  287. for l in lines:
  288. if '*' in l:
  289. # The assumption is that the first char will always be the '*'.
  290. active = l[1:].strip()
  291. branches.append(active)
  292. else:
  293. b = l.strip()
  294. if b:
  295. branches.append(b)
  296. return (active, branches)
  297. def Abort(self):
  298. active_branch, branches = self._GetBranches()
  299. if active_branch == ROLL_BRANCH_NAME:
  300. active_branch = 'main'
  301. if ROLL_BRANCH_NAME in branches:
  302. print('Aborting pending roll.')
  303. self._RunCommand(['git', 'checkout', ROLL_BRANCH_NAME])
  304. # Ignore an error here in case an issue wasn't created for some reason.
  305. self._RunCommand(['git', 'cl', 'set_close'], ignore_exit_code=True)
  306. self._RunCommand(['git', 'checkout', active_branch])
  307. self._RunCommand(['git', 'branch', '-D', ROLL_BRANCH_NAME])
  308. return 0
  309. def main():
  310. parser = argparse.ArgumentParser(
  311. description='Auto-generates a CL containing a WebGL conformance roll.')
  312. parser.add_argument('--abort',
  313. help=('Aborts a previously prepared roll. '
  314. 'Closes any associated issues and deletes the roll branches'),
  315. action='store_true')
  316. parser.add_argument(
  317. '--ignore-checks',
  318. action='store_true',
  319. default=False,
  320. help=('Skips checks for being on the main branch, dirty workspaces and '
  321. 'the updating of the checkout. Will still delete and create local '
  322. 'Git branches.'))
  323. parser.add_argument('--run-tryjobs', action='store_true', default=False,
  324. help=('Start the dry-run tryjobs for the newly generated CL. Use this '
  325. 'when you have no need to make changes to the WebGL conformance '
  326. 'test expectations in the same CL and want to avoid.'))
  327. parser.add_argument('-v', '--verbose', action='store_true', default=False,
  328. help='Be extra verbose in printing of log messages.')
  329. args = parser.parse_args()
  330. if args.verbose:
  331. logging.basicConfig(level=logging.DEBUG)
  332. else:
  333. logging.basicConfig(level=logging.ERROR)
  334. autoroller = AutoRoller(SRC_DIR)
  335. if args.abort:
  336. return autoroller.Abort()
  337. else:
  338. return autoroller.PrepareRoll(args.ignore_checks, args.run_tryjobs)
  339. if __name__ == '__main__':
  340. sys.exit(main())