stamp_grit_sources.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # Copyright 2014 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. # This script enumerates the files in the given directory, writing an empty
  5. # stamp file and a .d file listing the inputs required to make the stamp. This
  6. # allows us to dynamically depend on the grit sources without enumerating the
  7. # grit directory for every invocation of grit (which is what adding the source
  8. # files to every .grd file's .d file would entail) or shelling out to grit
  9. # synchronously during GN execution to get the list (which would be slow).
  10. #
  11. # Usage:
  12. # stamp_grit_sources.py <directory> <stamp-file> <.d-file>
  13. from __future__ import print_function
  14. import os
  15. import sys
  16. def GritSourceFiles(grit_root_dir):
  17. files = []
  18. for root, _, filenames in os.walk(grit_root_dir):
  19. grit_src = [os.path.join(root, f) for f in filenames
  20. if f.endswith('.py') and not f.endswith('_unittest.py')]
  21. files.extend(grit_src)
  22. files = [f.replace('\\', '/') for f in files]
  23. return sorted(files)
  24. def WriteDepFile(dep_file, stamp_file, source_files):
  25. with open(dep_file, "w") as f:
  26. f.write(stamp_file)
  27. f.write(": ")
  28. f.write(' '.join(source_files))
  29. def WriteStampFile(stamp_file):
  30. with open(stamp_file, "w"):
  31. pass
  32. def main(argv):
  33. if len(argv) != 4:
  34. print("Error: expecting 3 args.")
  35. return 1
  36. grit_root_dir = sys.argv[1]
  37. stamp_file = sys.argv[2]
  38. dep_file = sys.argv[3]
  39. WriteStampFile(stamp_file)
  40. WriteDepFile(dep_file, stamp_file, GritSourceFiles(grit_root_dir))
  41. return 0
  42. if __name__ == '__main__':
  43. sys.exit(main(sys.argv))