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: |
Emma Vukelj | 6c73c7e | 2019-06-25 15:03:04 -0700 | [diff] [blame^] | 67 | # Copy log file to output directory because this is what we want to |
| 68 | # upload to gs |
Caroline Tice | d2c34b3 | 2018-04-04 15:08:55 -0700 | [diff] [blame] | 69 | shutil.copy2(f, self.warnings_dir) |
Caroline Tice | 9072391 | 2018-03-28 11:26:59 -0700 | [diff] [blame] | 70 | |
| 71 | def _CreateTarball(self): |
| 72 | target = os.path.join(self.options.out_dir, self.options.out_file) |
| 73 | cros_build_lib.CreateTarball(target, self.warnings_dir, sudo=True) |
| 74 | |
| 75 | def Perform(self): |
| 76 | """Generate the warnings files.""" |
| 77 | self._ParseLogFiles() |
| 78 | self._CreateTarball() |
| 79 | |
| 80 | |
| 81 | def FinishParsing(options): |
| 82 | """Run environment dependent checks on parsed args.""" |
| 83 | target = os.path.join(options.out_dir, options.out_file) |
| 84 | if os.path.exists(target): |
| 85 | cros_build_lib.Die('Output file %r already exists.' % target) |
| 86 | |
| 87 | if not os.path.isdir(options.out_dir): |
| 88 | cros_build_lib.Die( |
| 89 | 'Non-existent directory %r specified for --out-dir' % options.out_dir) |
| 90 | |
| 91 | |
| 92 | def main(argv): |
| 93 | options = ParseCommandLine(argv) |
| 94 | FinishParsing(options) |
| 95 | |
| 96 | cros_build_lib.AssertInsideChroot() |
| 97 | |
| 98 | with sudo.SudoKeepAlive(ttyless_sudo=False): |
| 99 | with osutils.TempDir(set_global=True, sudo_rm=True) as tempdir: |
| 100 | warnings_dir = os.path.join(tempdir, TIDY_WARNINGS) |
| 101 | os.mkdir(warnings_dir) |
| 102 | GenerateTidyWarnings(warnings_dir, options).Perform() |