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)
| 153 | #--------------------------------------------------------------------------- |
| 154 | |
| 155 | def git_prebuild(pkg_dir, build_cmd=build_py): |
| 156 | """Return extended build or sdist command class for recording commit |
| 157 | |
| 158 | records git commit in IPython.utils._sysinfo.commit |
| 159 | |
| 160 | for use in IPython.utils.sysinfo.sys_info() calls after installation. |
| 161 | """ |
| 162 | |
| 163 | class MyBuildPy(build_cmd): |
| 164 | ''' Subclass to write commit data into installation tree ''' |
| 165 | def run(self): |
| 166 | # loose as `.dev` is suppose to be invalid |
| 167 | print("check version number") |
| 168 | loose_pep440re = re.compile(r'^(\d+)\.(\d+)\.(\d+((a|b|rc)\d+)?)(\.post\d+)?(\.dev\d*)?$') |
| 169 | if not loose_pep440re.match(version): |
| 170 | raise ValueError("Version number '%s' is not valid (should match [N!]N(.N)*[{a|b|rc}N][.postN][.devN])" % version) |
| 171 | |
| 172 | |
| 173 | build_cmd.run(self) |
| 174 | # this one will only fire for build commands |
| 175 | if hasattr(self, 'build_lib'): |
| 176 | self._record_commit(self.build_lib) |
| 177 | |
| 178 | def make_release_tree(self, base_dir, files): |
| 179 | # this one will fire for sdist |
| 180 | build_cmd.make_release_tree(self, base_dir, files) |
| 181 | self._record_commit(base_dir) |
| 182 | |
| 183 | def _record_commit(self, base_dir): |
| 184 | import subprocess |
| 185 | proc = subprocess.Popen('git rev-parse --short HEAD', |
| 186 | stdout=subprocess.PIPE, |
| 187 | stderr=subprocess.PIPE, |
| 188 | shell=True) |
| 189 | repo_commit, _ = proc.communicate() |
| 190 | repo_commit = repo_commit.strip().decode("ascii") |
| 191 | |
| 192 | out_pth = Path(base_dir) / pkg_dir / "utils" / "_sysinfo.py" |
| 193 | if out_pth.is_file() and not repo_commit: |
| 194 | # nothing to write, don't clobber |
| 195 | return |
| 196 | |
| 197 | print(f"writing git commit '{repo_commit}' to {out_pth}") |
| 198 | |
| 199 | # remove to avoid overwriting original via hard link |
| 200 | try: |
| 201 | out_pth.unlink() |
| 202 | except FileNotFoundError: |
| 203 | pass |
| 204 | with out_pth.open("w", encoding="utf-8") as out_file: |
| 205 | out_file.writelines( |
| 206 | [ |
| 207 | "# GENERATED BY setup.py\n", |
| 208 | f'commit = "{repo_commit}"\n', |
| 209 | ] |
| 210 | ) |
| 211 | |
| 212 | return MyBuildPy |
no outgoing calls
no test coverage detected
searching dependent graphs…