fake_cipd.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env python
  2. # Copyright (c) 2018 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. import argparse
  6. import os
  7. import re
  8. import shutil
  9. import sys
  10. CIPD_SUBDIR_RE = '@Subdir (.*)'
  11. def parse_cipd(root, contents):
  12. tree = {}
  13. current_subdir = None
  14. for line in contents:
  15. line = line.strip()
  16. match = re.match(CIPD_SUBDIR_RE, line)
  17. if match:
  18. current_subdir = os.path.join(root, *match.group(1).split('/'))
  19. elif line and current_subdir:
  20. tree.setdefault(current_subdir, []).append(line)
  21. return tree
  22. def main():
  23. assert sys.argv[1] == 'ensure'
  24. parser = argparse.ArgumentParser()
  25. parser.add_argument('-ensure-file')
  26. parser.add_argument('-root')
  27. args, _ = parser.parse_known_args()
  28. with open(args.ensure_file) as f:
  29. new_content = parse_cipd(args.root, f.readlines())
  30. # Install new packages
  31. for path, packages in new_content.items():
  32. if not os.path.exists(path):
  33. os.makedirs(path)
  34. with open(os.path.join(path, '_cipd'), 'w') as f:
  35. f.write('\n'.join(packages))
  36. # Save the ensure file that we got
  37. shutil.copy(args.ensure_file, os.path.join(args.root, '_cipd'))
  38. return 0
  39. if __name__ == '__main__':
  40. sys.exit(main())