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 parser.add_argument('output_dir') 22 return parser.parse_args() 23 24 25def copy_files(input_dir, output_dir): 26 for filename in os.listdir(input_dir): 27 if ((filename.endswith('.go') and not filename.endswith('_test.go')) or 28 filename == 'build.py'): 29 shutil.copy( 30 os.path.join(input_dir, filename), os.path.join(output_dir, filename)) 31 32 33def read_change_id(input_dir): 34 last_commit_msg = subprocess.check_output( 35 ['git', '-C', input_dir, 'log', '-1', '--pretty=%B'], encoding='utf-8') 36 # Use last found change id to support reverts as well. 37 change_ids = re.findall(r'Change-Id: (\w+)', last_commit_msg) 38 if not change_ids: 39 sys.exit("Couldn't find Change-Id in last commit message.") 40 return change_ids[-1] 41 42 43def write_readme(input_dir, output_dir, change_id): 44 with open( 45 os.path.join(input_dir, 'bundle.README'), 'r', encoding='utf-8') as r: 46 with open(os.path.join(output_dir, 'README'), 'w', encoding='utf-8') as w: 47 content = r.read() 48 w.write(content.format(change_id=change_id)) 49 50 51def write_version(output_dir, change_id): 52 with open(os.path.join(output_dir, 'VERSION'), 'w', encoding='utf-8') as w: 53 w.write(change_id) 54 55 56def main(): 57 args = parse_args() 58 input_dir = os.path.dirname(__file__) 59 change_id = read_change_id(input_dir) 60 shutil.rmtree(args.output_dir, ignore_errors=True) 61 os.makedirs(args.output_dir) 62 copy_files(input_dir, args.output_dir) 63 write_readme(input_dir, args.output_dir, change_id) 64 write_version(args.output_dir, change_id) 65 66 67if __name__ == '__main__': 68 main() 69