vcstagger.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Copyright 2015-2016 The Meson development team
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. # Unless required by applicable law or agreed to in writing, software
  7. # distributed under the License is distributed on an "AS IS" BASIS,
  8. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. # See the License for the specific language governing permissions and
  10. # limitations under the License.
  11. import sys, os, subprocess, re
  12. def config_vcs_tag(infile, outfile, fallback, source_dir, replace_string, regex_selector, cmd):
  13. try:
  14. output = subprocess.check_output(cmd, cwd=source_dir)
  15. new_string = re.search(regex_selector, output.decode()).group(1).strip()
  16. except Exception:
  17. new_string = fallback
  18. with open(infile) as f:
  19. new_data = f.read().replace(replace_string, new_string)
  20. if os.path.exists(outfile):
  21. with open(outfile) as f:
  22. needs_update = (f.read() != new_data)
  23. else:
  24. needs_update = True
  25. if needs_update:
  26. with open(outfile, 'w') as f:
  27. f.write(new_data)
  28. def run(args):
  29. infile, outfile, fallback, source_dir, replace_string, regex_selector = args[0:6]
  30. command = args[6:]
  31. config_vcs_tag(infile, outfile, fallback, source_dir, replace_string, regex_selector, command)
  32. return 0
  33. if __name__ == '__main__':
  34. sys.exit(run(sys.argv[1:]))