Skip to content

Commit 0399e17

Browse files
sokcevicGLUCI CQ
authored and
LUCI CQ
committed
Add a basic depot_tools version information
depot_tools has no versioning. It's hard to know if reported issues were caused due to outdated depot_tools or actual unresolved bug. This CL adds basic information about depot_tools version and it's included in presubmit failure. [email protected], [email protected] Change-Id: If8577c0826063693a7278a57a0cce629d4b1325f Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3541061 Reviewed-by: Aravind Vasudevan <[email protected]> Reviewed-by: Gavin Mak <[email protected]> Commit-Queue: Josip Sokcevic <[email protected]>
1 parent fd03219 commit 0399e17

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

git_cl.py

+5
Original file line numberDiff line numberDiff line change
@@ -5471,6 +5471,11 @@ def CMDlol(parser, args):
54715471
return 0
54725472

54735473

5474+
def CMDversion(parser, args):
5475+
import utils
5476+
print(utils.depot_tools_version())
5477+
5478+
54745479
class OptionParser(optparse.OptionParser):
54755480
"""Creates the option parse and add --verbose support."""
54765481

presubmit_support.py

+3
Original file line numberDiff line numberDiff line change
@@ -1988,8 +1988,11 @@ def main(argv=None):
19881988
options.json_output,
19891989
options.use_python3)
19901990
except PresubmitFailure as e:
1991+
import utils
19911992
print(e, file=sys.stderr)
19921993
print('Maybe your depot_tools is out of date?', file=sys.stderr)
1994+
print('depot_tools version: %s' % utils.depot_tools_version(),
1995+
file=sys.stderr)
19931996
return 2
19941997

19951998

tests/utils_test.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env vpython3
2+
# Copyright 2022 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+
6+
import logging
7+
import os
8+
import sys
9+
import unittest
10+
11+
if sys.version_info.major == 2:
12+
import mock
13+
else:
14+
from unittest import mock
15+
16+
DEPOT_TOOLS_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17+
sys.path.insert(0, DEPOT_TOOLS_ROOT)
18+
19+
from testing_support import coverage_utils
20+
import utils
21+
22+
23+
class GitCacheTest(unittest.TestCase):
24+
def setUp(self):
25+
pass
26+
27+
@mock.patch('subprocess.check_output', lambda x, **kwargs: b'foo')
28+
def testVersionWithGit(self):
29+
version = utils.depot_tools_version()
30+
self.assertEqual(version, 'git-foo')
31+
32+
@mock.patch('subprocess.check_output')
33+
@mock.patch('os.path.getmtime', lambda x: 42)
34+
def testVersionWithNoGit(self, mock_subprocess):
35+
mock_subprocess.side_effect = Exception
36+
version = utils.depot_tools_version()
37+
self.assertEqual(version, 'recipes.cfg-42')
38+
39+
@mock.patch('subprocess.check_output')
40+
@mock.patch('os.path.getmtime')
41+
def testVersionWithNoGit(self, mock_subprocess, mock_getmtime):
42+
mock_subprocess.side_effect = Exception
43+
mock_getmtime.side_effect = Exception
44+
version = utils.depot_tools_version()
45+
self.assertEqual(version, 'unknown')
46+
47+
48+
if __name__ == '__main__':
49+
logging.basicConfig(
50+
level=logging.DEBUG if '-v' in sys.argv else logging.ERROR)
51+
sys.exit(
52+
coverage_utils.covered_main(
53+
(os.path.join(DEPOT_TOOLS_ROOT, 'git_cache.py')),
54+
required_percentage=0))

utils.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2022 The Chromium Authors. All rights reserved.
2+
# Use of this source code is governed by a BSD-style license that can be
3+
# found in the LICENSE file.
4+
5+
import os
6+
import subprocess
7+
8+
9+
def depot_tools_version():
10+
depot_tools_root = os.path.dirname(os.path.abspath(__file__))
11+
try:
12+
commit_hash = subprocess.check_output(['git', 'rev-parse', 'HEAD'],
13+
cwd=depot_tools_root).decode(
14+
'utf-8', 'ignore')
15+
return 'git-%s' % commit_hash
16+
except Exception:
17+
pass
18+
19+
# git check failed, let's check last modification of frequently checked file
20+
try:
21+
mtime = os.path.getmtime(
22+
os.path.join(depot_tools_root, 'infra', 'config', 'recipes.cfg'))
23+
return 'recipes.cfg-%d' % (mtime)
24+
except Exception:
25+
return 'unknown'

0 commit comments

Comments
 (0)