1#!/usr/bin/env python
2
3# Copyright 2020 Google Inc.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17################################################################################
18"""Script for filter Pigweed's CIPD dependencies for OSS Fuzz conflicts."""
19
20import argparse
21import json
22import os
23import sys
24
25
26def main():
27  """Main entry point of the script."""
28  parser = argparse.ArgumentParser()
29  parser.add_argument('--root', default=os.path.join(os.getcwd(), 'pigweed'))
30  parser.add_argument(
31      '--json', default='pw_env_setup/py/pw_env_setup/cipd_setup/pigweed.json')
32  parser.add_argument('--excludes', default='clang', nargs='*')
33  args = parser.parse_args()
34
35  # Load args.json
36  prebuilts = []
37  try:
38    with open(args.json, 'r') as json_file:
39      prebuilts = json.load(json_file)
40  except Exception:
41    print('Encountered error attempting to load ' + args.json)
42    raise
43
44  # Filter out args.excludes
45  for exclude in args.excludes:
46    prebuilts[:] = [p for p in prebuilts if exclude not in str(p['path'])]
47
48  # Rename original CIPD JSON file
49  try:
50    os.rename(args.json, args.json + '.orig')
51  except Exception:
52    print('Encountered error attempting to rename ' + args.json)
53    raise
54
55  # Save new CIPD JSON file
56  try:
57    with open(args.json, 'w') as json_file:
58      json.dump(prebuilts, json_file, indent=2)
59  except Exception:
60    print('Encountered error attempting to write ' + args.json)
61    raise
62
63  return 0
64
65
66if __name__ == '__main__':
67  sys.exit(main())
68