Skip to content

Commit

Permalink
Merge pull request #61 from melver/master
Browse files Browse the repository at this point in the history
Provide Version fix when project is copied to foreign Git repository
  • Loading branch information
James A. Bednar committed Jun 11, 2014
2 parents e0cc628 + 066eb64 commit 786378d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
3 changes: 2 additions & 1 deletion param/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
descendents, ParameterizedFunction, ParamOverrides

from param.version import Version
__version__ = Version(release=(1,2,0), fpath=__file__, commit="$Format:%h$")
__version__ = Version(release=(1,2,0), fpath=__file__,
commit="$Format:%h$", reponame="param")

#: Top-level object to allow messaging not tied to a particular
#: Parameterized object, as in 'param.main.warning("Invalid option")'.
Expand Down
41 changes: 28 additions & 13 deletions param/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@

import os, subprocess

def run_cmd(args, cwd=None):
proc = subprocess.Popen(args, stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=cwd)
output, error = (str(s.decode()).strip() for s in proc.communicate())

if proc.returncode != 0:
raise Exception(proc.returncode, error)
return output

class Version(object):
"""
A simple approach to Python package versioning that supports PyPI
Expand Down Expand Up @@ -68,11 +78,11 @@ class Version(object):
__init__.py export-subst
"""

def __init__(self, release=None, fpath=None, commit=None):
def __init__(self, release=None, fpath=None, commit=None, reponame=None):
"""
release: Release tuple (corresponding to the current git tag)
fpath: Set to __file__ to access version control information
describe: Set to "$Format:%h$" (double quotes) for git archive
reponame: Used to verify VCS repository name.
"""
self.fpath = fpath
self._expected_commit = commit
Expand All @@ -82,6 +92,7 @@ def __init__(self, release=None, fpath=None, commit=None):
self._commit_count = 0
self._release = None
self._dirty = False
self.reponame = reponame

@property
def release(self):
Expand Down Expand Up @@ -128,17 +139,21 @@ def fetch(self):


def git_fetch(self, cmd='git'):
cmd = [cmd, 'describe', '--long', '--match', 'v*.*', '--dirty']
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=os.path.dirname(self.fpath))
output, error = (str(s.decode()).strip() for s in proc.communicate())

if error=='fatal: No names found, cannot describe anything.':
raise Exception("Cannot find any git version tags of format v*.*")

# If there is any other error, return (release value still useful)
if proc.returncode != 0: return self
try:
if self.reponame is not None:
# Verify this is the correct repository
output = run_cmd([cmd, 'remote', '-v'],
cwd=os.path.dirname(self.fpath))
if '/' + self.reponame + '.git' not in output:
return self

output = run_cmd([cmd, 'describe', '--long', '--match', 'v*.*', '--dirty'],
cwd=os.path.dirname(self.fpath))
except Exception as e:
if e.args[1] == 'fatal: No names found, cannot describe anything.':
raise Exception("Cannot find any git version tags of format v*.*")
# If there is any other error, return (release value still useful)
return self

split = output[1:].split('-')
self._release = tuple(int(el) for el in split[0].split('.'))
Expand Down

1 comment on commit 786378d

@jbednar
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

Please sign in to comment.