|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import subprocess |
| 4 | +from ConfigParser import ConfigParser |
| 5 | + |
| 6 | +COMMIT_INFO_FNAME = 'COMMIT_INFO.txt' |
| 7 | + |
| 8 | +def pkg_commit_hash(pkg_path): |
| 9 | + ''' Get short form of commit hash given directory `pkg_path` |
| 10 | +
|
| 11 | + There should be a file called 'COMMIT_INFO.txt' in `pkg_path`. This is a |
| 12 | + file in INI file format, with at least one section: ``commit hash``, and two |
| 13 | + variables ``archive_subst_hash`` and ``install_hash``. The first has a |
| 14 | + substitution pattern in it which may have been filled by the execution of |
| 15 | + ``git archive`` if this is an archive generated that way. The second is |
| 16 | + filled in by the installation, if the installation is from a git archive. |
| 17 | +
|
| 18 | + We get the commit hash from (in order of preference): |
| 19 | +
|
| 20 | + * A substituted value in ``archive_subst_hash`` |
| 21 | + * A written commit hash value in ``install_hash` |
| 22 | + * git's output, if we are in a git repository |
| 23 | +
|
| 24 | + If all these fail, we return a not-found placeholder tuple |
| 25 | +
|
| 26 | + Parameters |
| 27 | + ---------- |
| 28 | + pkg_path : str |
| 29 | + directory containing package |
| 30 | +
|
| 31 | + Returns |
| 32 | + ------- |
| 33 | + hash_from : str |
| 34 | + Where we got the hash from - description |
| 35 | + hash_str : str |
| 36 | + short form of hash |
| 37 | + ''' |
| 38 | + # Try and get commit from written commit text file |
| 39 | + pth = os.path.join(pkg_path, COMMIT_INFO_FNAME) |
| 40 | + if not os.path.isfile(pth): |
| 41 | + raise IOError('Missing commit info file %s' % pth) |
| 42 | + cfg_parser = ConfigParser() |
| 43 | + cfg_parser.read(pth) |
| 44 | + archive_subst = cfg_parser.get('commit hash', 'archive_subst_hash') |
| 45 | + if not archive_subst.startswith('$Format'): # it has been substituted |
| 46 | + return 'archive substitution', archive_subst |
| 47 | + install_subst = cfg_parser.get('commit hash', 'install_hash') |
| 48 | + if install_subst != '': |
| 49 | + return 'installation', install_subst |
| 50 | + # maybe we are in a repository |
| 51 | + proc = subprocess.Popen('git rev-parse --short HEAD', |
| 52 | + stdout=subprocess.PIPE, |
| 53 | + stderr=subprocess.PIPE, |
| 54 | + cwd=pkg_path, shell=True) |
| 55 | + repo_commit, _ = proc.communicate() |
| 56 | + if repo_commit: |
| 57 | + return 'repository', repo_commit.strip() |
| 58 | + return '(none found)', '<not found>' |
| 59 | + |
| 60 | + |
| 61 | +def get_pkg_info(pkg_path): |
| 62 | + ''' Return dict describing the context of this package |
| 63 | +
|
| 64 | + Parameters |
| 65 | + ---------- |
| 66 | + pkg_path : str |
| 67 | + path containing __init__.py for package |
| 68 | +
|
| 69 | + Returns |
| 70 | + ------- |
| 71 | + context : dict |
| 72 | + with named parameters of interest |
| 73 | + ''' |
| 74 | + src, hsh = pkg_commit_hash(pkg_path) |
| 75 | + import networkx |
| 76 | + import nibabel |
| 77 | + import nipype |
| 78 | + import numpy |
| 79 | + import scipy |
| 80 | + import traits |
| 81 | + return dict( |
| 82 | + pkg_path=pkg_path, |
| 83 | + commit_source=src, |
| 84 | + commit_hash=hsh, |
| 85 | + sys_version=sys.version, |
| 86 | + sys_executable=sys.executable, |
| 87 | + sys_platform=sys.platform, |
| 88 | + nipype_version=nipype.__version__, |
| 89 | + numpy_version=numpy.__version__, |
| 90 | + scipy_version=scipy.__version__, |
| 91 | + networkx_version=networkx.__version__, |
| 92 | + nibabel_version=nibabel.__version__, |
| 93 | + traits_version=traits.__version__) |
0 commit comments