goptions
version.py
Go to the documentation of this file.
1 #!/usr/bin/python3
2 
3 import subprocess
4 from datetime import datetime
5 
7  try:
8  # Run the git command to get the latest tag
9  tag = subprocess.check_output(['git', 'describe', '--tags', '--abbrev=0', '--always']).strip().decode('utf-8')
10  # Run another git command to get the date of the tag
11  date = subprocess.check_output(['git', 'log', '-1', '--format=%ai', tag]).strip().decode('utf-8')
12  return tag, datetime.strptime(date, '%Y-%m-%d %H:%M:%S %z').strftime('%Y-%m-%d')
13  except subprocess.CalledProcessError:
14  print("Error: Unable to get git version.")
15  return None, None
16 
17 
18 def generate_header_file(version, release_date):
19  header_content = f"""\
20 #ifndef VERSION_H
21 #define VERSION_H
22 
23 const char* gversion = "{version}";
24 const char* grelease_date = "{release_date}";
25 const char* greference = "Nucl. Instrum. Meth. A, Volume 959, 163422 (2020)";
26 const char* gweb = "https://gemc.github.io/home/";
27 const char* gauthor = "Maurizio Ungaro, ungaro@jlab.org";
28 
29 #endif // VERSION_H
30 """
31  with open('gversion.h', 'w') as header_file:
32  header_file.write(header_content)
33 
34 
35 def main():
36  version, release_date = get_git_version()
37  if version and release_date:
38  generate_header_file(version, release_date)
39  print("Version header file 'gversion.h' generated successfully.")
40  else:
41  print("Failed to generate version header file.")
42 
43 
44 if __name__ == "__main__":
45  main()
def generate_header_file(version, release_date)
Definition: version.py:18
def get_git_version()
Definition: version.py:6
def main()
Definition: version.py:35