

def record_commit_info(pkg_dir, build_cmd=build_py):
    " Return extended build command class for recording commit\n\n    The extended command tries to run git to find the current commit, getting\n    the empty string if it fails.  It then writes the commit hash into a file\n    in the `pkg_dir` path, named ``.git_commit_info.ini``.\n\n    In due course this information can be used by the package after it is\n    installed, to tell you what commit it was installed from if known.\n\n    To make use of this system, you need a package with a .git_commit_info.ini\n    file - e.g. ``myproject/.git_commit_info.ini`` - that might well look like\n    this::\n\n        # This is an ini file that may contain information about the code state\n        [commit hash]\n        # The line below may contain a valid hash if it has been substituted\n        # during 'git archive'\n        archive_subst_hash=$Format:%h$\n        # This line may be modified by the install process\n        install_hash=\n\n    The .git_commit_info file above is also designed to be used with git\n    substitution - so you probably also want a ``.gitattributes`` file in the\n    root directory of your working tree that contains something like this::\n\n       myproject/.git_commit_info.ini export-subst\n\n    That will cause the ``.git_commit_info.ini`` file to get filled in by ``git\n    archive`` - useful in case someone makes such an archive - for example with\n    via the github 'download source' button.\n\n    Although all the above will work as is, you might consider having something\n    like a ``get_info()`` function in your package to display the commit\n    information at the terminal.  See the ``pkg_info.py`` module in the nipy\n    package for an example.\n    "

    class MyBuildPy(build_cmd):
        ' Subclass to write commit data into installation tree '

        def run(self):
            build_py.run(self)
            import subprocess
            proc = subprocess.Popen('git rev-parse --short HEAD', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
            (repo_commit, _) = proc.communicate()
            cfg_parser = ConfigParser()
            cfg_parser.read(pjoin(pkg_dir, '.git_commit_info.ini'))
            cfg_parser.set('commit hash', 'install_hash', repo_commit.decode('ascii'))
            out_pth = pjoin(self.build_lib, pkg_dir, '.git_commit_info.ini')
            out_file = open(out_pth, 'wt')
            cfg_parser.write(out_file)
            out_file.close()
    return MyBuildPy

dataset/ETHPy150Open ipython/ipython-py3k/setupbase.py record_commit_info/original