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 |
Mike Frysinger | 6a2b0f2 | 2020-02-20 13:34:07 -0500 | [diff] [blame] | 12 | import sys |
Caroline Tice | 9072391 | 2018-03-28 11:26:59 -0700 | [diff] [blame] | 13 | |
| 14 | from chromite.lib import cros_build_lib |
| 15 | from chromite.lib import commandline |
| 16 | from chromite.lib import osutils |
| 17 | from chromite.lib import sudo |
| 18 | |
Mike Frysinger | 6a2b0f2 | 2020-02-20 13:34:07 -0500 | [diff] [blame] | 19 | |
| 20 | assert sys.version_info >= (3, 6), 'This module requires Python 3.6+' |
| 21 | |
| 22 | |
Caroline Tice | 9072391 | 2018-03-28 11:26:59 -0700 | [diff] [blame] | 23 | DEFAULT_NAME = 'clang_tidy_warnings.tar.xz' |
| 24 | TIDY_WARNINGS = 'clang_tidy_warnings' |
George Burgess IV | 703ae7b | 2019-05-07 18:18:53 -0700 | [diff] [blame] | 25 | PARSING_SCRIPT = ('/mnt/host/source/src/third_party/toolchain-utils/' |
George Burgess IV | a3fa528 | 2019-05-30 00:36:44 -0700 | [diff] [blame] | 26 | 'clang_tidy/clang_tidy_parse_build_log.py') |
Caroline Tice | 9072391 | 2018-03-28 11:26:59 -0700 | [diff] [blame] | 27 | WORKING_DIR = '/usr/bin' |
| 28 | |
| 29 | |
| 30 | def ParseCommandLine(argv): |
| 31 | """Parse args, and run environment-independent checks.""" |
| 32 | parser = commandline.ArgumentParser(description=__doc__) |
| 33 | parser.add_argument('--board', required=True, |
| 34 | help=('The board to generate the sysroot for.')) |
Caroline Tice | d2c34b3 | 2018-04-04 15:08:55 -0700 | [diff] [blame] | 35 | parser.add_argument('--logs-dir', required=True, |
| 36 | help=('The directory containg the logs files to ' |
| 37 | 'be parsed.')) |
Caroline Tice | 9072391 | 2018-03-28 11:26:59 -0700 | [diff] [blame] | 38 | parser.add_argument('--out-dir', type='path', required=True, |
| 39 | help='Directory to place the generated tarball.') |
| 40 | parser.add_argument('--out-file', default=DEFAULT_NAME, |
| 41 | help='The name to give to the tarball. ' |
| 42 | 'Defaults to %(default)s.') |
| 43 | options = parser.parse_args(argv) |
| 44 | |
| 45 | return options |
| 46 | |
| 47 | |
| 48 | class GenerateTidyWarnings(object): |
| 49 | """Wrapper for generation functionality.""" |
| 50 | |
| 51 | def __init__(self, warnings_dir, options): |
| 52 | """Initialize |
| 53 | |
| 54 | Args: |
| 55 | warnings_dir: Path to warnings directory. |
| 56 | options: Parsed options. |
| 57 | """ |
| 58 | self.warnings_dir = warnings_dir |
| 59 | self.options = options |
| 60 | |
Caroline Tice | d2c34b3 | 2018-04-04 15:08:55 -0700 | [diff] [blame] | 61 | def _FindLogFiles(self, logs_dir): |
| 62 | files = [] |
| 63 | filelist = os.listdir(logs_dir) |
| 64 | for f in filelist: |
| 65 | logfile = os.path.join(logs_dir, f) |
| 66 | files.append(logfile) |
| 67 | return files |
| 68 | |
Caroline Tice | 9072391 | 2018-03-28 11:26:59 -0700 | [diff] [blame] | 69 | def _ParseLogFiles(self): |
Caroline Tice | d2c34b3 | 2018-04-04 15:08:55 -0700 | [diff] [blame] | 70 | log_files = self._FindLogFiles(self.options.logs_dir) |
Caroline Tice | 9072391 | 2018-03-28 11:26:59 -0700 | [diff] [blame] | 71 | for f in log_files: |
Emma Vukelj | 6c73c7e | 2019-06-25 15:03:04 -0700 | [diff] [blame] | 72 | # Copy log file to output directory because this is what we want to |
| 73 | # upload to gs |
Caroline Tice | d2c34b3 | 2018-04-04 15:08:55 -0700 | [diff] [blame] | 74 | shutil.copy2(f, self.warnings_dir) |
Caroline Tice | 9072391 | 2018-03-28 11:26:59 -0700 | [diff] [blame] | 75 | |
| 76 | def _CreateTarball(self): |
| 77 | target = os.path.join(self.options.out_dir, self.options.out_file) |
| 78 | cros_build_lib.CreateTarball(target, self.warnings_dir, sudo=True) |
| 79 | |
| 80 | def Perform(self): |
| 81 | """Generate the warnings files.""" |
| 82 | self._ParseLogFiles() |
| 83 | self._CreateTarball() |
| 84 | |
| 85 | |
| 86 | def FinishParsing(options): |
| 87 | """Run environment dependent checks on parsed args.""" |
| 88 | target = os.path.join(options.out_dir, options.out_file) |
| 89 | if os.path.exists(target): |
| 90 | cros_build_lib.Die('Output file %r already exists.' % target) |
| 91 | |
| 92 | if not os.path.isdir(options.out_dir): |
| 93 | cros_build_lib.Die( |
| 94 | 'Non-existent directory %r specified for --out-dir' % options.out_dir) |
| 95 | |
| 96 | |
| 97 | def main(argv): |
| 98 | options = ParseCommandLine(argv) |
| 99 | FinishParsing(options) |
| 100 | |
| 101 | cros_build_lib.AssertInsideChroot() |
| 102 | |
| 103 | with sudo.SudoKeepAlive(ttyless_sudo=False): |
| 104 | with osutils.TempDir(set_global=True, sudo_rm=True) as tempdir: |
| 105 | warnings_dir = os.path.join(tempdir, TIDY_WARNINGS) |
| 106 | os.mkdir(warnings_dir) |
| 107 | GenerateTidyWarnings(warnings_dir, options).Perform() |