aboutsummaryrefslogtreecommitdiff
path: root/compiler_wrapper/bundle.py
blob: 6df821469f289144677cb4c131b5bb513f36ddda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2019 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Build script that copies the go sources to a build destination."""

from __future__ import print_function

import argparse
import os.path
import re
import shutil
import subprocess
import sys


def parse_args():
  parser = argparse.ArgumentParser()
  default_output_dir = os.path.normpath(
      os.path.join(
          os.path.dirname(os.path.realpath(__file__)),
          '../../chromiumos-overlay/sys-devel/llvm/files/compiler_wrapper'))
  parser.add_argument(
      '--output_dir',
      default=default_output_dir,
      help='Output directory to place bundled files (default: %(default)s)')
  parser.add_argument(
      '--create',
      action='store_true',
      help='Create output_dir if it does not already exist')
  return parser.parse_args()


def copy_files(input_dir, output_dir):
  for filename in os.listdir(input_dir):
    if ((filename.endswith('.go') and not filename.endswith('_test.go')) or
        filename in ('build.py', 'go.mod')):
      shutil.copy(
          os.path.join(input_dir, filename), os.path.join(output_dir, filename))


def read_change_id(input_dir):
  last_commit_msg = subprocess.check_output(
      ['git', '-C', input_dir, 'log', '-1', '--pretty=%B'], encoding='utf-8')
  # Use last found change id to support reverts as well.
  change_ids = re.findall(r'Change-Id: (\w+)', last_commit_msg)
  if not change_ids:
    sys.exit("Couldn't find Change-Id in last commit message.")
  return change_ids[-1]


def write_readme(input_dir, output_dir, change_id):
  with open(
      os.path.join(input_dir, 'bundle.README'), 'r', encoding='utf-8') as r:
    with open(os.path.join(output_dir, 'README'), 'w', encoding='utf-8') as w:
      content = r.read()
      w.write(content.format(change_id=change_id))


def write_version(output_dir, change_id):
  with open(os.path.join(output_dir, 'VERSION'), 'w', encoding='utf-8') as w:
    w.write(change_id)


def main():
  args = parse_args()
  input_dir = os.path.dirname(__file__)
  change_id = read_change_id(input_dir)
  if not args.create:
    assert os.path.exists(
        args.output_dir
    ), f'Specified output directory ({args.output_dir}) does not exist'
  shutil.rmtree(args.output_dir, ignore_errors=True)
  os.makedirs(args.output_dir)
  copy_files(input_dir, args.output_dir)
  write_readme(input_dir, args.output_dir, change_id)
  write_version(args.output_dir, change_id)


if __name__ == '__main__':
  main()