1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright 2019 The Chromium OS Authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7"""Build script that copies the go sources to a build destination.""" 8 9from __future__ import print_function 10 11import argparse 12import os.path 13import re 14import shutil 15import subprocess 16import sys 17 18 19def parse_args(): 20 parser = argparse.ArgumentParser() 21 default_output_dir = os.path.normpath( 22 os.path.join( 23 os.path.dirname(os.path.realpath(__file__)), 24 '../../chromiumos-overlay/sys-devel/llvm/files/compiler_wrapper')) 25 parser.add_argument( 26 '--output_dir', 27 default=default_output_dir, 28 help='Output directory to place bundled files (default: %(default)s)') 29 parser.add_argument( 30 '--create', 31 action='store_true', 32 help='Create output_dir if it does not already exist') 33 return parser.parse_args() 34 35 36def copy_files(input_dir, output_dir): 37 for filename in os.listdir(input_dir): 38 if ((filename.endswith('.go') and not filename.endswith('_test.go')) or 39 filename in ('build.py', 'go.mod')): 40 shutil.copy( 41 os.path.join(input_dir, filename), os.path.join(output_dir, filename)) 42 43 44def read_change_id(input_dir): 45 last_commit_msg = subprocess.check_output( 46 ['git', '-C', input_dir, 'log', '-1', '--pretty=%B'], encoding='utf-8') 47 # Use last found change id to support reverts as well. 48 change_ids = re.findall(r'Change-Id: (\w+)', last_commit_msg) 49 if not change_ids: 50 sys.exit("Couldn't find Change-Id in last commit message.") 51 return change_ids[-1] 52 53 54def write_readme(input_dir, output_dir, change_id): 55 with open( 56 os.path.join(input_dir, 'bundle.README'), 'r', encoding='utf-8') as r: 57 with open(os.path.join(output_dir, 'README'), 'w', encoding='utf-8') as w: 58 content = r.read() 59 w.write(content.format(change_id=change_id)) 60 61 62def write_version(output_dir, change_id): 63 with open(os.path.join(output_dir, 'VERSION'), 'w', encoding='utf-8') as w: 64 w.write(change_id) 65 66 67def main(): 68 args = parse_args() 69 input_dir = os.path.dirname(__file__) 70 change_id = read_change_id(input_dir) 71 if not args.create: 72 assert os.path.exists( 73 args.output_dir 74 ), f'Specified output directory ({args.output_dir}) does not exist' 75 shutil.rmtree(args.output_dir, ignore_errors=True) 76 os.makedirs(args.output_dir) 77 copy_files(input_dir, args.output_dir) 78 write_readme(input_dir, args.output_dir, change_id) 79 write_version(args.output_dir, change_id) 80 81 82if __name__ == '__main__': 83 main() 84