Insert git hash into python file during installation

I want to embed a git hash in the version number of a python module if this module is installed from the git repository using ./setup.py install . How should I do it?

My thought was to define a function in setup.py , to insert a hash and organize it when she copied the module into its build/lib/ directory, but before he installed it in his final destination. Is there a way to connect to the build process at this point?

Change I know how to get the hash of the current version from the command line. I ask how to get such a command to run at the right time during assembly / installation.

+4
source share
1 answer

Another, possibly easier way, to do this using gitpython , as in dd/setup.py :

 from pkg_resources import parse_version # part of `setuptools` def git_version(version): """Return version with local version identifier.""" import git repo = git.Repo('.git') repo.git.status() # assert versions are increasing latest_tag = repo.git.describe( match='v[0-9]*', tags=True, abbrev=0) assert parse_version(latest_tag) <= parse_version(version), ( latest_tag, version) sha = repo.head.commit.hexsha if repo.is_dirty(): return '{v}.dev0+{sha}.dirty'.format( v=version, sha=sha) # commit is clean # is it release of `version` ? try: tag = repo.git.describe( match='v[0-9]*', exact_match=True, tags=True, dirty=True) except git.GitCommandError: return '{v}.dev0+{sha}'.format( v=version, sha=sha) assert tag == 'v' + version, (tag, version) return version 

cf also discussion at https://github.com/tulip-control/tulip-control/pull/145

+2
source

Source: https://habr.com/ru/post/1492964/


All Articles