Skip to content

Commit

Permalink
py: Make makeversionhdr.py extract version from docs/conf.py if no git.
Browse files Browse the repository at this point in the history
Addresses issue micropython#1285.
  • Loading branch information
dpgeorge committed May 25, 2015
1 parent 3c4b5d4 commit 1a97f67
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion py/makeversionhdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
import datetime
import subprocess

def make_version_header(filename):
def get_version_info_from_git():
# Note: git describe doesn't work if no tag is available
try:
git_tag = subprocess.check_output(["git", "describe", "--dirty", "--always"], universal_newlines=True).strip()
except subprocess.CalledProcessError:
git_tag = ""
except OSError:
return None
try:
git_hash = subprocess.check_output(["git", "rev-parse", "--short", "HEAD"], stderr=subprocess.STDOUT, universal_newlines=True).strip()
except subprocess.CalledProcessError:
git_hash = "unknown"
except OSError:
return None

try:
# Check if there are any modified files.
Expand All @@ -25,6 +29,8 @@ def make_version_header(filename):
subprocess.check_call(["git", "diff-index", "--cached", "--quiet", "HEAD", "--"], stderr=subprocess.STDOUT)
except subprocess.CalledProcessError:
git_hash += "-dirty"
except OSError:
return None

# Try to extract MicroPython version from git tag
if git_tag.startswith("v"):
Expand All @@ -34,6 +40,28 @@ def make_version_header(filename):
else:
ver = ["0", "0", "1"]

return git_tag, git_hash, ver

def get_version_info_from_docs_conf():
with open("%s/docs/conf.py" % sys.argv[0].rsplit("/", 2)[0]) as f:
for line in f:
if line.startswith("release = '"):
ver = line.strip()[10:].strip("'")
git_tag = "v" + ver
ver = ver.split(".")
if len(ver) == 2:
ver.append("0")
return git_tag, "<no hash>", ver
return None

def make_version_header(filename):
# Get version info using git, with fallback to docs/conf.py
info = get_version_info_from_git()
if info is None:
info = get_version_info_from_docs_conf()

git_tag, git_hash, ver = info

# Generate the file with the git and version info
file_data = """\
// This file was generated by py/makeversionhdr.py
Expand Down

0 comments on commit 1a97f67

Please sign in to comment.