• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python2
2#
3# Copyright 2019 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
16import warnings
17import zipfile
18
19SEED_CORPUS_LIMIT_MB = 100
20
21
22def main():
23  parser = argparse.ArgumentParser(description="Generate fuzzer config.")
24  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)
28
29  args = parser.parse_args()
30  corpus_files = []
31  seed_corpus_path = args.output
32
33  for directory in args.corpus_directories:
34    if not os.path.exists(directory):
35      raise Exception('The given seed_corpus directory (%s) does not exist.' %
36                      directory)
37    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)
41
42  with zipfile.ZipFile(seed_corpus_path, 'w') as z:
43    # 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)
50
51  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)
55
56
57if __name__ == '__main__':
58  main()
59