77import datetime
88import subprocess
99
10- def make_version_header ( filename ):
10+ def get_version_info_from_git ( ):
1111 # Note: git describe doesn't work if no tag is available
1212 try :
1313 git_tag = subprocess .check_output (["git" , "describe" , "--dirty" , "--always" ], universal_newlines = True ).strip ()
1414 except subprocess .CalledProcessError :
1515 git_tag = ""
16+ except OSError :
17+ return None
1618 try :
1719 git_hash = subprocess .check_output (["git" , "rev-parse" , "--short" , "HEAD" ], stderr = subprocess .STDOUT , universal_newlines = True ).strip ()
1820 except subprocess .CalledProcessError :
1921 git_hash = "unknown"
22+ except OSError :
23+ return None
2024
2125 try :
2226 # Check if there are any modified files.
@@ -25,6 +29,8 @@ def make_version_header(filename):
2529 subprocess .check_call (["git" , "diff-index" , "--cached" , "--quiet" , "HEAD" , "--" ], stderr = subprocess .STDOUT )
2630 except subprocess .CalledProcessError :
2731 git_hash += "-dirty"
32+ except OSError :
33+ return None
2834
2935 # Try to extract MicroPython version from git tag
3036 if git_tag .startswith ("v" ):
@@ -34,6 +40,28 @@ def make_version_header(filename):
3440 else :
3541 ver = ["0" , "0" , "1" ]
3642
43+ return git_tag , git_hash , ver
44+
45+ def get_version_info_from_docs_conf ():
46+ with open ("%s/docs/conf.py" % sys .argv [0 ].rsplit ("/" , 2 )[0 ]) as f :
47+ for line in f :
48+ if line .startswith ("release = '" ):
49+ ver = line .strip ()[10 :].strip ("'" )
50+ git_tag = "v" + ver
51+ ver = ver .split ("." )
52+ if len (ver ) == 2 :
53+ ver .append ("0" )
54+ return git_tag , "<no hash>" , ver
55+ return None
56+
57+ def make_version_header (filename ):
58+ # Get version info using git, with fallback to docs/conf.py
59+ info = get_version_info_from_git ()
60+ if info is None :
61+ info = get_version_info_from_docs_conf ()
62+
63+ git_tag , git_hash , ver = info
64+
3765 # Generate the file with the git and version info
3866 file_data = """\
3967 // This file was generated by py/makeversionhdr.py
0 commit comments