Skip to content

Commit

Permalink
fixed 'setup.py build' from a git-archive tree
Browse files Browse the repository at this point in the history
I think that's the full matrix of possibilities.
  • Loading branch information
warner committed Nov 16, 2011
1 parent 3d7ab19 commit 0b8a0da
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 13 deletions.
4 changes: 4 additions & 0 deletions NOTES
Expand Up @@ -73,3 +73,7 @@
- _version.py will need to import that
*** setup.py build

* current problems
** DONE running 'setup.py build' from a git-archive tree
- need version_from_expanded_variable() in versioneer.py too, not just
_version.py
4 changes: 4 additions & 0 deletions setup.py
Expand Up @@ -14,14 +14,18 @@ def get(fn): return open(fn, "r").read()
for line in open("src/%s/parse.py" % vcs, "r").readlines():
if line.startswith("#### START"):
f.write("LONG_VERSION_PY = '''\n")
elif line.startswith("#### SUBPROCESS_HELPER"):
f.write(unquote(get("src/subprocess-helper.py")))
elif line.startswith("#### VERSION_FROM_CHECKOUT"):
f.write(unquote(get("src/%s/from-checkout.py" % vcs)))
elif line.startswith("#### VERSION_FROM_VARIABLE"):
f.write(unquote(get("src/%s/from-variable.py" % vcs)))
elif line.startswith("#### END"):
f.write("'''\n")
else:
f.write(line)
f.write(get("src/%s/from-checkout.py" % vcs))
f.write(get("src/%s/from-variable.py" % vcs))
f.write(get("src/%s/setup.py" % vcs))
f.write(get("src/trailer.py"))
f.close()
Expand Down
1 change: 1 addition & 0 deletions src/git/from-checkout.py
Expand Up @@ -15,3 +15,4 @@ def version_from_vcs(tag_prefix, verbose=False):
(stdout, tag_prefix)
return None
return stdout[len(tag_prefix):]

11 changes: 11 additions & 0 deletions src/git/from-variable.py
@@ -0,0 +1,11 @@
def version_from_expanded_variable(s, tag_prefix):
s = s.strip()
if "$Format" in s: # unexpanded
return version_from_vcs(tag_prefix)
refs = set([r.strip() for r in s.strip("()").split(",")])
refs.discard("HEAD") ; refs.discard("master")
for r in reversed(sorted(refs)):
if r.startswith(tag_prefix):
return r[len(tag_prefix):]
return "unknown"

17 changes: 4 additions & 13 deletions src/git/parse.py
Expand Up @@ -8,19 +8,10 @@
# this string will be replaced by git during git-archive
verstr = "%(DOLLAR)sFormat:%%d%(DOLLAR)s"

#### SUBPROCESS_HELPER
#### VERSION_FROM_CHECKOUT

def parse(s):
tag_prefix = "%(TAG_PREFIX)s"
if "%(DOLLAR)sFormat" in s: # unexpanded
return version_from_vcs(tag_prefix)
refs = set([r.strip() for r in s.strip("()").split(",")])
refs.discard("HEAD") ; refs.discard("master")
for r in reversed(sorted(refs)):
if r.startswith(tag_prefix):
return r[len(tag_prefix):]
return "unknown"

__version__ = parse(verstr.strip())
#### VERSION_FROM_VARIABLE
tag_prefix = "%(TAG_PREFIX)s"
__version__ = version_from_expanded_variable(verstr.strip(), tag_prefix)
#### END

21 changes: 21 additions & 0 deletions src/trailer.py
Expand Up @@ -33,6 +33,20 @@ def write_to_version_file(filename, ver):
f.close()
print "set %s to '%s'" % (filename, ver)

def get_expanded_variable(versionfile_source):
# the code embedded in _version.py can just fetch the value of this
# variable. We don't want to import _version.py, so we do it with a
# regexp instead
try:
for line in open(versionfile_source,"r").readlines():
if line.strip().startswith("verstr ="):
mo = re.search(r'=\s*"(.*)"', line)
if mo:
return mo.group(1)
except EnvironmentError:
pass
return None

def get_best_version(versionfile, tag_prefix, parentdir_prefix,
default=None, verbose=False):
# extract version from first of: 'git describe', _version.py, parentdir.
Expand All @@ -45,6 +59,13 @@ def get_best_version(versionfile, tag_prefix, parentdir_prefix,
if verbose: print "got version from git"
return ver

verstr = get_expanded_variable(versionfile_source)
if verstr:
ver = version_from_expanded_variable(verstr, tag_prefix)
if ver is not None:
if verbose: print "got version from expanded variable"
return ver

ver = version_from_file(versionfile)
if ver is not None:
if verbose: print "got version from file %s" % versionfile
Expand Down

0 comments on commit 0b8a0da

Please sign in to comment.