blob: ea466e840159fd96eacc11fd84000b400e82c83e [file] [log] [blame]
Caroline Tice90723912018-03-28 11:26:59 -07001# -*- 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
8from __future__ import print_function
9
10import os
Caroline Ticed2c34b32018-04-04 15:08:55 -070011import shutil
Caroline Tice90723912018-03-28 11:26:59 -070012
13from chromite.lib import cros_build_lib
14from chromite.lib import commandline
15from chromite.lib import osutils
16from chromite.lib import sudo
17
18DEFAULT_NAME = 'clang_tidy_warnings.tar.xz'
19TIDY_WARNINGS = 'clang_tidy_warnings'
George Burgess IV703ae7b2019-05-07 18:18:53 -070020PARSING_SCRIPT = ('/mnt/host/source/src/third_party/toolchain-utils/'
George Burgess IVa3fa5282019-05-30 00:36:44 -070021 'clang_tidy/clang_tidy_parse_build_log.py')
Caroline Tice90723912018-03-28 11:26:59 -070022WORKING_DIR = '/usr/bin'
23
24
25def 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 Ticed2c34b32018-04-04 15:08:55 -070030 parser.add_argument('--logs-dir', required=True,
31 help=('The directory containg the logs files to '
32 'be parsed.'))
Caroline Tice90723912018-03-28 11:26:59 -070033 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
43class 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 Ticed2c34b32018-04-04 15:08:55 -070056 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 Tice90723912018-03-28 11:26:59 -070064 def _ParseLogFiles(self):
Caroline Ticed2c34b32018-04-04 15:08:55 -070065 log_files = self._FindLogFiles(self.options.logs_dir)
Caroline Tice90723912018-03-28 11:26:59 -070066 for f in log_files:
Emma Vukelj6c73c7e2019-06-25 15:03:04 -070067 # Copy log file to output directory because this is what we want to
68 # upload to gs
Caroline Ticed2c34b32018-04-04 15:08:55 -070069 shutil.copy2(f, self.warnings_dir)
Caroline Tice90723912018-03-28 11:26:59 -070070
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
81def 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
92def 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()