|
11 | 11 | import shlex |
12 | 12 | import shutil |
13 | 13 | import signal |
| 14 | +import sys |
14 | 15 | import subprocess |
15 | 16 | import tempfile |
16 | 17 | from urllib.parse import urlparse |
17 | 18 |
|
| 19 | +import reframe |
18 | 20 | from reframe.core.exceptions import (ReframeError, SpawnedProcessError, |
19 | 21 | SpawnedProcessTimeout) |
20 | 22 |
|
@@ -68,7 +70,6 @@ def run_command_async(cmd, |
68 | 70 | shell=False, |
69 | 71 | log=True, |
70 | 72 | **popen_args): |
71 | | - # Import logger here to avoid unnecessary circular dependencies |
72 | 73 | if log: |
73 | 74 | from reframe.core.logging import getlogger |
74 | 75 | getlogger().debug('executing OS command: ' + cmd) |
@@ -315,6 +316,53 @@ def git_repo_exists(url, timeout=5): |
315 | 316 | return True |
316 | 317 |
|
317 | 318 |
|
| 319 | +def git_repo_hash(branch='HEAD', short=True, wd=None): |
| 320 | + '''Return the SHA1 hash of a git repository. |
| 321 | +
|
| 322 | + :arg branch: The branch to look at. |
| 323 | + :arg short: Return a short hash. This always corresponds to the first 8 |
| 324 | + characters of the long hash. We don't rely on Git for the short hash, |
| 325 | + since depending on the version it might return either 7 or 8 |
| 326 | + characters. |
| 327 | + :arg wd: Change to this directory before retrieving the hash. If ``None``, |
| 328 | + ReFrame's install prefix will be used. |
| 329 | + :returns: The repository has or ``None`` if the hash could not be |
| 330 | + retrieved. |
| 331 | +
|
| 332 | + ''' |
| 333 | + try: |
| 334 | + wd = wd or reframe.INSTALL_PREFIX |
| 335 | + with change_dir(wd): |
| 336 | + # Do not log this command, since we need to call this function |
| 337 | + # from the logger |
| 338 | + completed = run_command('git rev-parse %s' % branch, |
| 339 | + check=True, log=False) |
| 340 | + |
| 341 | + except SpawnedProcessError: |
| 342 | + return None |
| 343 | + |
| 344 | + hash = completed.stdout.strip() |
| 345 | + if hash: |
| 346 | + return hash[:8] if short else hash |
| 347 | + else: |
| 348 | + return None |
| 349 | + |
| 350 | + |
| 351 | +def reframe_version(): |
| 352 | + '''Return ReFrame version. |
| 353 | +
|
| 354 | + If ReFrame's installation contains the repository metadata, the |
| 355 | + repository's hash will be appended to the actual version. |
| 356 | +
|
| 357 | + ''' |
| 358 | + version = reframe.VERSION |
| 359 | + repo_hash = git_repo_hash() |
| 360 | + if repo_hash: |
| 361 | + return '%s (rev: %s)' % (version, repo_hash) |
| 362 | + else: |
| 363 | + return version |
| 364 | + |
| 365 | + |
318 | 366 | def expandvars(path): |
319 | 367 | '''Expand environment variables in ``path`` and |
320 | 368 | perform any command substitution |
|
0 commit comments