blob: 1ce39bdd608e6b5f33b98a4d0c14a5f2b9c4e437 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2012 The ChromiumOS Authors
Caroline Tice90723912018-03-28 11:26:59 -07002# 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
Caroline Tice90723912018-03-28 11:26:59 -070010from chromite.lib import commandline
Mike Frysinger807d8282022-04-28 22:45:17 -040011from chromite.lib import cros_build_lib
Caroline Tice90723912018-03-28 11:26:59 -070012from chromite.lib import osutils
13from chromite.lib import sudo
14
Mike Frysinger6a2b0f22020-02-20 13:34:07 -050015
Alex Klein1699fab2022-09-08 08:46:06 -060016DEFAULT_NAME = "clang_tidy_warnings.tar.xz"
17TIDY_WARNINGS = "clang_tidy_warnings"
18PARSING_SCRIPT = (
19 "/mnt/host/source/src/third_party/toolchain-utils/"
20 "clang_tidy/clang_tidy_parse_build_log.py"
21)
22WORKING_DIR = "/usr/bin"
Caroline Tice90723912018-03-28 11:26:59 -070023
24
25def ParseCommandLine(argv):
Alex Klein1699fab2022-09-08 08:46:06 -060026 """Parse args, and run environment-independent checks."""
27 parser = commandline.ArgumentParser(description=__doc__)
28 parser.add_argument(
29 "--board",
30 required=True,
31 help=("The board to generate the sysroot for."),
32 )
33 parser.add_argument(
34 "--logs-dir",
35 required=True,
36 help=("The directory containg the logs files to " "be parsed."),
37 )
38 parser.add_argument(
39 "--out-dir",
40 type="path",
41 required=True,
42 help="Directory to place the generated tarball.",
43 )
44 parser.add_argument(
45 "--out-file",
46 default=DEFAULT_NAME,
47 help="The name to give to the tarball. " "Defaults to %(default)s.",
48 )
49 options = parser.parse_args(argv)
Caroline Tice90723912018-03-28 11:26:59 -070050
Alex Klein1699fab2022-09-08 08:46:06 -060051 return options
Caroline Tice90723912018-03-28 11:26:59 -070052
53
Alex Klein074f94f2023-06-22 10:32:06 -060054class GenerateTidyWarnings:
Alex Klein1699fab2022-09-08 08:46:06 -060055 """Wrapper for generation functionality."""
Caroline Tice90723912018-03-28 11:26:59 -070056
Alex Klein1699fab2022-09-08 08:46:06 -060057 def __init__(self, warnings_dir, options):
58 """Initialize
Caroline Tice90723912018-03-28 11:26:59 -070059
Alex Klein1699fab2022-09-08 08:46:06 -060060 Args:
Alex Klein345222d2023-01-20 17:33:41 -070061 warnings_dir: Path to warnings directory.
62 options: Parsed options.
Alex Klein1699fab2022-09-08 08:46:06 -060063 """
64 self.warnings_dir = warnings_dir
65 self.options = options
Caroline Tice90723912018-03-28 11:26:59 -070066
Alex Klein1699fab2022-09-08 08:46:06 -060067 def _FindLogFiles(self, logs_dir):
68 files = []
69 filelist = os.listdir(logs_dir)
70 for f in filelist:
71 logfile = os.path.join(logs_dir, f)
72 files.append(logfile)
73 return files
Caroline Ticed2c34b32018-04-04 15:08:55 -070074
Alex Klein1699fab2022-09-08 08:46:06 -060075 def _ParseLogFiles(self):
76 log_files = self._FindLogFiles(self.options.logs_dir)
77 for f in log_files:
78 # Copy log file to output directory because this is what we want to
79 # upload to gs
80 shutil.copy2(f, self.warnings_dir)
Caroline Tice90723912018-03-28 11:26:59 -070081
Alex Klein1699fab2022-09-08 08:46:06 -060082 def _CreateTarball(self):
83 tarball_path = os.path.join(self.options.out_dir, self.options.out_file)
84 cros_build_lib.CreateTarball(tarball_path, self.warnings_dir, sudo=True)
Caroline Tice90723912018-03-28 11:26:59 -070085
Alex Klein1699fab2022-09-08 08:46:06 -060086 def Perform(self):
87 """Generate the warnings files."""
88 self._ParseLogFiles()
89 self._CreateTarball()
Caroline Tice90723912018-03-28 11:26:59 -070090
91
92def FinishParsing(options):
Alex Klein1699fab2022-09-08 08:46:06 -060093 """Run environment dependent checks on parsed args."""
94 target = os.path.join(options.out_dir, options.out_file)
95 if os.path.exists(target):
Alex Kleindf8ee502022-10-18 09:48:15 -060096 cros_build_lib.Die("Output file %r already exists.", target)
Caroline Tice90723912018-03-28 11:26:59 -070097
Alex Klein1699fab2022-09-08 08:46:06 -060098 if not os.path.isdir(options.out_dir):
99 cros_build_lib.Die(
100 "Non-existent directory %r specified for --out-dir"
101 % options.out_dir
102 )
Caroline Tice90723912018-03-28 11:26:59 -0700103
104
105def main(argv):
Alex Klein1699fab2022-09-08 08:46:06 -0600106 options = ParseCommandLine(argv)
107 FinishParsing(options)
Caroline Tice90723912018-03-28 11:26:59 -0700108
Alex Klein1699fab2022-09-08 08:46:06 -0600109 cros_build_lib.AssertInsideChroot()
Caroline Tice90723912018-03-28 11:26:59 -0700110
Alex Klein1699fab2022-09-08 08:46:06 -0600111 with sudo.SudoKeepAlive(ttyless_sudo=False):
112 with osutils.TempDir(set_global=True, sudo_rm=True) as tempdir:
113 warnings_dir = os.path.join(tempdir, TIDY_WARNINGS)
114 os.mkdir(warnings_dir)
115 GenerateTidyWarnings(warnings_dir, options).Perform()