Return extended build or sdist command class for recording commit records git commit in IPython.utils._sysinfo.commit for use in IPython.utils.sysinfo.sys_info() calls after installation.
(pkg_dir, build_cmd=build_py)
| 350 | |
| 351 | |
| 352 | def git_prebuild(pkg_dir, build_cmd=build_py): |
| 353 | """Return extended build or sdist command class for recording commit |
| 354 | |
| 355 | records git commit in IPython.utils._sysinfo.commit |
| 356 | |
| 357 | for use in IPython.utils.sysinfo.sys_info() calls after installation. |
| 358 | """ |
| 359 | |
| 360 | class MyBuildPy(build_cmd): |
| 361 | ''' Subclass to write commit data into installation tree ''' |
| 362 | def run(self): |
| 363 | # loose as `.dev` is suppose to be invalid |
| 364 | print("check version number") |
| 365 | loose_pep440re = re.compile(r'^(\d+)\.(\d+)\.(\d+((a|b|rc)\d+)?)(\.post\d+)?(\.dev\d*)?$') |
| 366 | if not loose_pep440re.match(version): |
| 367 | raise ValueError("Version number '%s' is not valid (should match [N!]N(.N)*[{a|b|rc}N][.postN][.devN])" % version) |
| 368 | |
| 369 | |
| 370 | build_cmd.run(self) |
| 371 | # this one will only fire for build commands |
| 372 | if hasattr(self, 'build_lib'): |
| 373 | self._record_commit(self.build_lib) |
| 374 | |
| 375 | def make_release_tree(self, base_dir, files): |
| 376 | # this one will fire for sdist |
| 377 | build_cmd.make_release_tree(self, base_dir, files) |
| 378 | self._record_commit(base_dir) |
| 379 | |
| 380 | def _record_commit(self, base_dir): |
| 381 | import subprocess |
| 382 | proc = subprocess.Popen('git rev-parse --short HEAD', |
| 383 | stdout=subprocess.PIPE, |
| 384 | stderr=subprocess.PIPE, |
| 385 | shell=True) |
| 386 | repo_commit, _ = proc.communicate() |
| 387 | repo_commit = repo_commit.strip().decode("ascii") |
| 388 | |
| 389 | out_pth = pjoin(base_dir, pkg_dir, 'utils', '_sysinfo.py') |
| 390 | if os.path.isfile(out_pth) and not repo_commit: |
| 391 | # nothing to write, don't clobber |
| 392 | return |
| 393 | |
| 394 | print("writing git commit '%s' to %s" % (repo_commit, out_pth)) |
| 395 | |
| 396 | # remove to avoid overwriting original via hard link |
| 397 | try: |
| 398 | os.remove(out_pth) |
| 399 | except (IOError, OSError): |
| 400 | pass |
| 401 | with open(out_pth, 'w') as out_file: |
| 402 | out_file.writelines([ |
| 403 | '# GENERATED BY setup.py\n', |
| 404 | 'commit = u"%s"\n' % repo_commit, |
| 405 | ]) |
| 406 | return MyBuildPy |
| 407 |