blob: a467bd7ac657f8d14d4a88f20b64d080f05ab787 [file] [log] [blame]
Caroline Tice90723912018-03-28 11:26:59 -07001# Copyright (c) 2012 The Chromium OS 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"""Generates a clang-tidy tarball for the clang-tidy builder."""
6
Caroline Tice90723912018-03-28 11:26:59 -07007import os
Caroline Ticed2c34b32018-04-04 15:08:55 -07008import shutil
Caroline Tice90723912018-03-28 11:26:59 -07009
10from chromite.lib import cros_build_lib
11from chromite.lib import commandline
12from chromite.lib import osutils
13from chromite.lib import sudo
14
Mike Frysinger6a2b0f22020-02-20 13:34:07 -050015
Caroline Tice90723912018-03-28 11:26:59 -070016DEFAULT_NAME = 'clang_tidy_warnings.tar.xz'
17TIDY_WARNINGS = 'clang_tidy_warnings'
George Burgess IV703ae7b2019-05-07 18:18:53 -070018PARSING_SCRIPT = ('/mnt/host/source/src/third_party/toolchain-utils/'
George Burgess IVa3fa5282019-05-30 00:36:44 -070019 'clang_tidy/clang_tidy_parse_build_log.py')
Caroline Tice90723912018-03-28 11:26:59 -070020WORKING_DIR = '/usr/bin'
21
22
23def ParseCommandLine(argv):
24 """Parse args, and run environment-independent checks."""
25 parser = commandline.ArgumentParser(description=__doc__)
26 parser.add_argument('--board', required=True,
27 help=('The board to generate the sysroot for.'))
Caroline Ticed2c34b32018-04-04 15:08:55 -070028 parser.add_argument('--logs-dir', required=True,
29 help=('The directory containg the logs files to '
30 'be parsed.'))
Caroline Tice90723912018-03-28 11:26:59 -070031 parser.add_argument('--out-dir', type='path', required=True,
32 help='Directory to place the generated tarball.')
33 parser.add_argument('--out-file', default=DEFAULT_NAME,
34 help='The name to give to the tarball. '
35 'Defaults to %(default)s.')
36 options = parser.parse_args(argv)
37
38 return options
39
40
41class GenerateTidyWarnings(object):
42 """Wrapper for generation functionality."""
43
44 def __init__(self, warnings_dir, options):
45 """Initialize
46
47 Args:
48 warnings_dir: Path to warnings directory.
49 options: Parsed options.
50 """
51 self.warnings_dir = warnings_dir
52 self.options = options
53
Caroline Ticed2c34b32018-04-04 15:08:55 -070054 def _FindLogFiles(self, logs_dir):
55 files = []
56 filelist = os.listdir(logs_dir)
57 for f in filelist:
58 logfile = os.path.join(logs_dir, f)
59 files.append(logfile)
60 return files
61
Caroline Tice90723912018-03-28 11:26:59 -070062 def _ParseLogFiles(self):
Caroline Ticed2c34b32018-04-04 15:08:55 -070063 log_files = self._FindLogFiles(self.options.logs_dir)
Caroline Tice90723912018-03-28 11:26:59 -070064 for f in log_files:
Emma Vukelj6c73c7e2019-06-25 15:03:04 -070065 # Copy log file to output directory because this is what we want to
66 # upload to gs
Caroline Ticed2c34b32018-04-04 15:08:55 -070067 shutil.copy2(f, self.warnings_dir)
Caroline Tice90723912018-03-28 11:26:59 -070068
69 def _CreateTarball(self):
Eliot Courtney93f76212020-10-22 11:16:33 +090070 tarball_path = os.path.join(self.options.out_dir, self.options.out_file)
71 cros_build_lib.CreateTarball(tarball_path, self.warnings_dir, sudo=True)
Caroline Tice90723912018-03-28 11:26:59 -070072
73 def Perform(self):
74 """Generate the warnings files."""
75 self._ParseLogFiles()
76 self._CreateTarball()
77
78
79def FinishParsing(options):
80 """Run environment dependent checks on parsed args."""
81 target = os.path.join(options.out_dir, options.out_file)
82 if os.path.exists(target):
83 cros_build_lib.Die('Output file %r already exists.' % target)
84
85 if not os.path.isdir(options.out_dir):
86 cros_build_lib.Die(
87 'Non-existent directory %r specified for --out-dir' % options.out_dir)
88
89
90def main(argv):
91 options = ParseCommandLine(argv)
92 FinishParsing(options)
93
94 cros_build_lib.AssertInsideChroot()
95
96 with sudo.SudoKeepAlive(ttyless_sudo=False):
97 with osutils.TempDir(set_global=True, sudo_rm=True) as tempdir:
98 warnings_dir = os.path.join(tempdir, TIDY_WARNINGS)
99 os.mkdir(warnings_dir)
100 GenerateTidyWarnings(warnings_dir, options).Perform()