version_gen.py 824 B

123456789101112131415161718192021222324252627282930
  1. #!/usr/bin/env python3
  2. import sys, os, subprocess
  3. def generate(infile, outfile, fallback):
  4. workdir = os.path.split(infile)[0]
  5. if workdir == '':
  6. workdir = '.'
  7. try:
  8. version = subprocess.check_output(['git', 'describe'], cwd=workdir).decode().strip()
  9. except (subprocess.CalledProcessError, OSError, UnicodeDecodeError):
  10. version = fallback
  11. with open(infile) as f:
  12. newdata = f.read().replace('@VERSION@', version)
  13. try:
  14. with open(outfile) as f:
  15. olddata = f.read()
  16. if olddata == newdata:
  17. return
  18. except OSError:
  19. pass
  20. with open(outfile, 'w') as f:
  21. f.write(newdata)
  22. if __name__ == '__main__':
  23. infile = sys.argv[1]
  24. outfile = sys.argv[2]
  25. fallback = sys.argv[3]
  26. generate(infile, outfile, fallback)