Caroline Tice | 9072391 | 2018-03-28 11:26:59 -0700 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright (c) 2012 The Chromium OS 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 | """Generates a clang-tidy tarball for the clang-tidy builder.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | import os |
Caroline Tice | d2c34b3 | 2018-04-04 15:08:55 -0700 | [diff] [blame] | 11 | import shutil |
Caroline Tice | 9072391 | 2018-03-28 11:26:59 -0700 | [diff] [blame] | 12 | |
| 13 | from chromite.lib import cros_build_lib |
| 14 | from chromite.lib import commandline |
| 15 | from chromite.lib import osutils |
| 16 | from chromite.lib import sudo |
| 17 | |
| 18 | DEFAULT_NAME = 'clang_tidy_warnings.tar.xz' |
| 19 | TIDY_WARNINGS = 'clang_tidy_warnings' |
George Burgess IV | 703ae7b | 2019-05-07 18:18:53 -0700 | [diff] [blame] | 20 | PARSING_SCRIPT = ('/mnt/host/source/src/third_party/toolchain-utils/' |
George Burgess IV | a3fa528 | 2019-05-30 00:36:44 -0700 | [diff] [blame] | 21 | 'clang_tidy/clang_tidy_parse_build_log.py') |
Caroline Tice | 9072391 | 2018-03-28 11:26:59 -0700 | [diff] [blame] | 22 | WORKING_DIR = '/usr/bin' |
| 23 | |
| 24 | |
| 25 | def ParseCommandLine(argv): |
| 26 | """Parse args, and run environment-independent checks.""" |
| 27 | parser = commandline.ArgumentParser(description=__doc__) |
| 28 | parser.add_argument('--board', required=True, |
| 29 | help=('The board to generate the sysroot for.')) |
Caroline Tice | d2c34b3 | 2018-04-04 15:08:55 -0700 | [diff] [blame] | 30 | parser.add_argument('--logs-dir', required=True, |
| 31 | help=('The directory containg the logs files to ' |
| 32 | 'be parsed.')) |
Caroline Tice | 9072391 | 2018-03-28 11:26:59 -0700 | [diff] [blame] | 33 | parser.add_argument('--out-dir', type='path', required=True, |
| 34 | help='Directory to place the generated tarball.') |
| 35 | parser.add_argument('--out-file', default=DEFAULT_NAME, |
| 36 | help='The name to give to the tarball. ' |
| 37 | 'Defaults to %(default)s.') |
| 38 | options = parser.parse_args(argv) |
| 39 | |
| 40 | return options |
| 41 | |
| 42 | |
| 43 | class GenerateTidyWarnings(object): |
| 44 | """Wrapper for generation functionality.""" |
| 45 | |
| 46 | def __init__(self, warnings_dir, options): |
| 47 | """Initialize |
| 48 | |
| 49 | Args: |
| 50 | warnings_dir: Path to warnings directory. |
| 51 | options: Parsed options. |
| 52 | """ |
| 53 | self.warnings_dir = warnings_dir |
| 54 | self.options = options |
| 55 | |
Caroline Tice | d2c34b3 | 2018-04-04 15:08:55 -0700 | [diff] [blame] | 56 | def _FindLogFiles(self, logs_dir): |
| 57 | files = [] |
| 58 | filelist = os.listdir(logs_dir) |
| 59 | for f in filelist: |
| 60 | logfile = os.path.join(logs_dir, f) |
| 61 | files.append(logfile) |
| 62 | return files |
| 63 | |
Caroline Tice | 9072391 | 2018-03-28 11:26:59 -0700 | [diff] [blame] | 64 | def _ParseLogFiles(self): |
Caroline Tice | d2c34b3 | 2018-04-04 15:08:55 -0700 | [diff] [blame] | 65 | log_files = self._FindLogFiles(self.options.logs_dir) |
Caroline Tice | 9072391 | 2018-03-28 11:26:59 -0700 | [diff] [blame] | 66 | for f in log_files: |
| 67 | cmd = [PARSING_SCRIPT, '--log_file', f, '--output_dir', self.warnings_dir] |
| 68 | cros_build_lib.RunCommand(cmd, cwd=WORKING_DIR, enter_chroot=True) |
Caroline Tice | d2c34b3 | 2018-04-04 15:08:55 -0700 | [diff] [blame] | 69 | # Copy log file to output directory. We want the log files in the |
| 70 | # tarball, for now, in case we ever need to re-process them. (A |
| 71 | # possibility, since the final dashboard details have not been worked out |
| 72 | # yet.) |
| 73 | shutil.copy2(f, self.warnings_dir) |
Caroline Tice | 9072391 | 2018-03-28 11:26:59 -0700 | [diff] [blame] | 74 | |
| 75 | def _CreateTarball(self): |
| 76 | target = os.path.join(self.options.out_dir, self.options.out_file) |
| 77 | cros_build_lib.CreateTarball(target, self.warnings_dir, sudo=True) |
| 78 | |
| 79 | def Perform(self): |
| 80 | """Generate the warnings files.""" |
| 81 | self._ParseLogFiles() |
| 82 | self._CreateTarball() |
| 83 | |
| 84 | |
| 85 | def FinishParsing(options): |
| 86 | """Run environment dependent checks on parsed args.""" |
| 87 | target = os.path.join(options.out_dir, options.out_file) |
| 88 | if os.path.exists(target): |
| 89 | cros_build_lib.Die('Output file %r already exists.' % target) |
| 90 | |
| 91 | if not os.path.isdir(options.out_dir): |
| 92 | cros_build_lib.Die( |
| 93 | 'Non-existent directory %r specified for --out-dir' % options.out_dir) |
| 94 | |
| 95 | |
| 96 | def main(argv): |
| 97 | options = ParseCommandLine(argv) |
| 98 | FinishParsing(options) |
| 99 | |
| 100 | cros_build_lib.AssertInsideChroot() |
| 101 | |
| 102 | with sudo.SudoKeepAlive(ttyless_sudo=False): |
| 103 | with osutils.TempDir(set_global=True, sudo_rm=True) as tempdir: |
| 104 | warnings_dir = os.path.join(tempdir, TIDY_WARNINGS) |
| 105 | os.mkdir(warnings_dir) |
| 106 | GenerateTidyWarnings(warnings_dir, options).Perform() |