version_gen.py 892 B

12345678910111213141516171819202122232425262728293031323334
  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. version = fallback
  8. try:
  9. p = subprocess.Popen(['git', 'describe'], cwd=workdir, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  10. (stdo, _) = p.communicate()
  11. if p.returncode == 0:
  12. version = stdo.decode().strip()
  13. except:
  14. pass
  15. with open(infile) as f:
  16. newdata = f.read().replace('@VERSION@', version)
  17. try:
  18. with open(outfile) as f:
  19. olddata = f.read()
  20. if olddata == newdata:
  21. return
  22. except:
  23. pass
  24. with open(outfile, 'w') as f:
  25. f.write(newdata)
  26. if __name__ == '__main__':
  27. infile = sys.argv[1]
  28. outfile = sys.argv[2]
  29. fallback = sys.argv[3]
  30. generate(infile, outfile, fallback)