Caroline Tice | 9072391 | 2018-03-28 11:26:59 -0700 | [diff] [blame] | 1 | # 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 Tice | 9072391 | 2018-03-28 11:26:59 -0700 | [diff] [blame] | 7 | import os |
Caroline Tice | d2c34b3 | 2018-04-04 15:08:55 -0700 | [diff] [blame] | 8 | import shutil |
Caroline Tice | 9072391 | 2018-03-28 11:26:59 -0700 | [diff] [blame] | 9 | |
| 10 | from chromite.lib import cros_build_lib |
| 11 | from chromite.lib import commandline |
| 12 | from chromite.lib import osutils |
| 13 | from chromite.lib import sudo |
| 14 | |
Mike Frysinger | 6a2b0f2 | 2020-02-20 13:34:07 -0500 | [diff] [blame] | 15 | |
Caroline Tice | 9072391 | 2018-03-28 11:26:59 -0700 | [diff] [blame] | 16 | DEFAULT_NAME = 'clang_tidy_warnings.tar.xz' |
| 17 | TIDY_WARNINGS = 'clang_tidy_warnings' |
George Burgess IV | 703ae7b | 2019-05-07 18:18:53 -0700 | [diff] [blame] | 18 | PARSING_SCRIPT = ('/mnt/host/source/src/third_party/toolchain-utils/' |
George Burgess IV | a3fa528 | 2019-05-30 00:36:44 -0700 | [diff] [blame] | 19 | 'clang_tidy/clang_tidy_parse_build_log.py') |
Caroline Tice | 9072391 | 2018-03-28 11:26:59 -0700 | [diff] [blame] | 20 | WORKING_DIR = '/usr/bin' |
| 21 | |
| 22 | |
| 23 | def 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 Tice | d2c34b3 | 2018-04-04 15:08:55 -0700 | [diff] [blame] | 28 | parser.add_argument('--logs-dir', required=True, |
| 29 | help=('The directory containg the logs files to ' |
| 30 | 'be parsed.')) |
Caroline Tice | 9072391 | 2018-03-28 11:26:59 -0700 | [diff] [blame] | 31 | 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 | |
| 41 | class 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 Tice | d2c34b3 | 2018-04-04 15:08:55 -0700 | [diff] [blame] | 54 | 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 Tice | 9072391 | 2018-03-28 11:26:59 -0700 | [diff] [blame] | 62 | def _ParseLogFiles(self): |
Caroline Tice | d2c34b3 | 2018-04-04 15:08:55 -0700 | [diff] [blame] | 63 | log_files = self._FindLogFiles(self.options.logs_dir) |
Caroline Tice | 9072391 | 2018-03-28 11:26:59 -0700 | [diff] [blame] | 64 | for f in log_files: |
Emma Vukelj | 6c73c7e | 2019-06-25 15:03:04 -0700 | [diff] [blame] | 65 | # Copy log file to output directory because this is what we want to |
| 66 | # upload to gs |
Caroline Tice | d2c34b3 | 2018-04-04 15:08:55 -0700 | [diff] [blame] | 67 | shutil.copy2(f, self.warnings_dir) |
Caroline Tice | 9072391 | 2018-03-28 11:26:59 -0700 | [diff] [blame] | 68 | |
| 69 | def _CreateTarball(self): |
Eliot Courtney | 93f7621 | 2020-10-22 11:16:33 +0900 | [diff] [blame] | 70 | 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 Tice | 9072391 | 2018-03-28 11:26:59 -0700 | [diff] [blame] | 72 | |
| 73 | def Perform(self): |
| 74 | """Generate the warnings files.""" |
| 75 | self._ParseLogFiles() |
| 76 | self._CreateTarball() |
| 77 | |
| 78 | |
| 79 | def 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 | |
| 90 | def 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() |