1 #!/usr/bin/env python 2 # Copyright 2014 The Chromium 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 """Creates IPC fuzzer packages suitable for uploading to ClusterFuzz. Stores 7 the packages into chrome build directory. See fuzzer_list below for the list of 8 fuzzers. 9 """ 10 11 import argparse 12 import distutils.archive_util 13 import os 14 import shutil 15 import sys 16 import tempfile 17 18 class CFPackageBuilder: 19 def __init__(self): 20 self.fuzzer_list = [ 21 'ipc_fuzzer_mut', 22 'ipc_fuzzer_gen', 23 ] 24 25 def parse_args(self): 26 desc = 'Builder of IPC fuzzer packages for ClusterFuzz' 27 parser = argparse.ArgumentParser(description=desc) 28 parser.add_argument('--out-dir', dest='out_dir', default='out', 29 help='output directory under src/ directory') 30 parser.add_argument('--build-type', dest='build_type', default='Release', 31 help='Debug vs. Release build') 32 self.args = parser.parse_args() 33 34 def get_paths(self): 35 script_path = os.path.realpath(__file__) 36 self.mutate_dir = os.path.dirname(script_path) 37 src_dir = os.path.join(self.mutate_dir, os.pardir, os.pardir, os.pardir) 38 src_dir = os.path.abspath(src_dir) 39 out_dir = os.path.join(src_dir, self.args.out_dir) 40 self.build_dir = os.path.join(out_dir, self.args.build_type) 41 42 def enter_tmp_workdir(self): 43 self.old_cwd = os.getcwd() 44 self.work_dir = tempfile.mkdtemp() 45 os.chdir(self.work_dir) 46 47 def rm_tmp_workdir(self): 48 os.chdir(self.old_cwd) 49 shutil.rmtree(self.work_dir) 50 51 def build_package(self, fuzzer): 52 os.makedirs(fuzzer) 53 fuzzer_src_path = os.path.join(self.mutate_dir, fuzzer + '.py') 54 fuzzer_dst_path = os.path.join(fuzzer, 'run.py') 55 shutil.copyfile(fuzzer_src_path, fuzzer_dst_path) 56 distutils.archive_util.make_zipfile(fuzzer, fuzzer) 57 package_name = fuzzer + '.zip' 58 shutil.copy(package_name, self.build_dir) 59 final_package_path = os.path.join(self.build_dir, package_name) 60 print 'Built %s' % final_package_path 61 62 def main(self): 63 self.parse_args() 64 self.get_paths() 65 self.enter_tmp_workdir() 66 for fuzzer in self.fuzzer_list: 67 self.build_package(fuzzer) 68 self.rm_tmp_workdir() 69 return 0 70 71 if __name__ == "__main__": 72 builder = CFPackageBuilder() 73 sys.exit(builder.main()) 74