1#!/usr/bin/env python
2#
3# Copyright 2019 Google LLC
4#
5# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
7
8
9"""Check the DEPS file for correctness."""
10
11
12import os
13import re
14import subprocess
15import sys
16
17import utils
18
19
20INFRA_BOTS_DIR = os.path.dirname(os.path.realpath(__file__))
21SKIA_DIR = os.path.abspath(os.path.join(INFRA_BOTS_DIR, os.pardir, os.pardir))
22
23
24def main():
25  """Load the DEPS file and verify that all entries are valid."""
26  # Find gclient.py and run that instead of simply "gclient", which calls into
27  # update_depot_tools.
28  gclient = subprocess.check_output([utils.WHICH, utils.GCLIENT])
29  gclient_py = os.path.join(os.path.dirname(gclient), 'gclient.py')
30  python = sys.executable or 'python'
31
32  # Obtain the DEPS mapping.
33  output = subprocess.check_output(
34      [python, gclient_py, 'revinfo'], cwd=SKIA_DIR)
35
36  # Check each entry.
37  errs = []
38  for e in output.rstrip().splitlines():
39    split = e.split(': ')
40    if len(split) != 2:
41      errs.append(
42          'Failed to parse `gclient revinfo` output; invalid format: %s' % e)
43    if split[0] == 'skia':
44      continue
45    split = split[1].split('@')
46    if len(split) != 2:
47      errs.append(
48          'Failed to parse `gclient revinfo` output; invalid format: %s' % e)
49    repo = split[0]
50    rev = split[1]
51    if 'chrome-infra-packages' in repo:
52      continue
53    if not 'googlesource.com' in repo:
54      errs.append(
55          'DEPS must be hosted on googlesource.com; %s is not allowed. '
56          'See http://go/new-skia-git-mirror' % repo)
57    if not re.match(r'^[a-z0-9]{40}$', rev):
58      errs.append('%s: "%s" does not look like a commit hash.' % (repo, rev))
59  if errs:
60    print >> sys.stderr, 'Found problems in DEPS:'
61    for err in errs:
62      print >> sys.stderr, err
63    sys.exit(1)
64
65
66if __name__ == '__main__':
67  main()
68