Debian Codegraph @

cross-project-tests/debuginfo-tests/dexter/dex/utils/Version.py

raw
1 # DExTer : Debugging Experience Tester
2 # ~~~~~~ ~ ~~ ~ ~~
3 #
4 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 # See https://llvm.org/LICENSE.txt for license information.
6 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 """DExTer version output."""
8
9 import os
10 from subprocess import CalledProcessError, check_output, STDOUT
11 import sys
12 from urllib.parse import urlparse, urlunparse
13
14 from dex import __version__
15
16
17 def sanitize_repo_url(repo):
18 parsed = urlparse(repo)
19 # No username present, repo URL is fine.
20 if parsed.username is None:
21 return repo
22 # Otherwise, strip the login details from the URL by reconstructing the netloc from just `<hostname>(:<port>)?`.
23 sanitized_netloc = parsed.hostname
24 if parsed.port:
25 sanitized_netloc = f"{sanitized_netloc}:{parsed.port}"
26 return urlunparse(parsed._replace(netloc=sanitized_netloc))
27
28
29 def _git_version():
30 dir_ = os.path.dirname(__file__)
31 try:
32 branch = (
33 check_output(
34 ["git", "rev-parse", "--abbrev-ref", "HEAD"], stderr=STDOUT, cwd=dir_
35 )
36 .rstrip()
37 .decode("utf-8")
38 )
39 hash_ = (
40 check_output(["git", "rev-parse", "HEAD"], stderr=STDOUT, cwd=dir_)
41 .rstrip()
42 .decode("utf-8")
43 )
44 repo = sanitize_repo_url(
45 check_output(
46 ["git", "remote", "get-url", "origin"], stderr=STDOUT, cwd=dir_
47 )
48 .rstrip()
49 .decode("utf-8")
50 )
51 return "[{} {}] ({})".format(branch, hash_, repo)
52 except (OSError, CalledProcessError):
53 pass
54 return None
55
56
57 def version(name):
58 lines = []
59 lines.append(" ".join([s for s in [name, __version__, _git_version()] if s]))
60 lines.append(" using Python {}".format(sys.version))
61 return "\n".join(lines)