blob: dddc18ffaf28a3a021be2dd9739fac5a3c5e69e4 [file] [log] [blame]
aizatsky198e3022016-03-08 21:33:461#!/usr/bin/python2
2#
3# Copyright 2016 The Chromium 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"""Archive corpus file into zip and generate .d depfile.
8
9Invoked by GN from fuzzer_test.gni.
10"""
11
12from __future__ import print_function
13import argparse
14import os
15import sys
mmoroz0aa34792016-12-02 10:07:5916import warnings
aizatsky198e3022016-03-08 21:33:4617import zipfile
18
Abhishek Arya7f66e072019-04-12 18:46:3519SEED_CORPUS_LIMIT_MB = 100
20
aizatsky198e3022016-03-08 21:33:4621
22def main():
23 parser = argparse.ArgumentParser(description="Generate fuzzer config.")
mmoroz902ef432017-02-07 17:03:3724 parser.add_argument('corpus_directories', metavar='corpus_dir', type=str,
25 nargs='+')
26 parser.add_argument('--output', metavar='output_archive_name.zip',
27 required=True)
aizatsky198e3022016-03-08 21:33:4628
Jonathan Metzman950be742019-04-12 22:54:2629 args = parser.parse_args()
aizatsky198e3022016-03-08 21:33:4630 corpus_files = []
Abhishek Arya7f66e072019-04-12 18:46:3531 seed_corpus_path = args.output
mmoroz146a2e82016-04-28 10:32:3532
mmoroz902ef432017-02-07 17:03:3733 for directory in args.corpus_directories:
Max Morozebe225802017-08-08 06:52:3334 if not os.path.exists(directory):
35 raise Exception('The given seed_corpus directory (%s) does not exist.' %
36 directory)
mmoroz902ef432017-02-07 17:03:3737 for (dirpath, _, filenames) in os.walk(directory):
38 for filename in filenames:
39 full_filename = os.path.join(dirpath, filename)
40 corpus_files.append(full_filename)
aizatsky198e3022016-03-08 21:33:4641
Abhishek Arya7f66e072019-04-12 18:46:3542 with zipfile.ZipFile(seed_corpus_path, 'w') as z:
mmoroz0aa34792016-12-02 10:07:5943 # Turn warnings into errors to interrupt the build: crbug.com/653920.
44 with warnings.catch_warnings():
45 warnings.simplefilter("error")
46 for i, corpus_file in enumerate(corpus_files):
47 # To avoid duplication of filenames inside the archive, use numbers.
48 arcname = '%016d' % i
49 z.write(corpus_file, arcname)
aizatsky198e3022016-03-08 21:33:4650
Abhishek Arya7f66e072019-04-12 18:46:3551 if os.path.getsize(seed_corpus_path) > SEED_CORPUS_LIMIT_MB * 1024 * 1024:
52 print('Seed corpus %s exceeds maximum allowed size (%d MB).' %
53 (seed_corpus_path, SEED_CORPUS_LIMIT_MB))
54 sys.exit(-1)
aizatsky198e3022016-03-08 21:33:4655
56if __name__ == '__main__':
57 main()