2018-03-09 13:25:11 -05:00
|
|
|
"""Various utilities that are handy."""
|
2010-02-04 10:21:36 -05:00
|
|
|
|
2012-04-11 10:57:25 -04:00
|
|
|
import codecs
|
2010-05-14 13:38:26 -04:00
|
|
|
import os
|
2012-02-16 21:49:02 -05:00
|
|
|
import os.path
|
2024-10-10 10:59:18 -07:00
|
|
|
import re
|
2012-02-16 21:49:02 -05:00
|
|
|
import subprocess
|
|
|
|
|
import sys
|
2012-05-10 14:46:08 -04:00
|
|
|
|
2010-02-04 10:21:36 -05:00
|
|
|
|
2018-03-27 14:30:46 -04:00
|
|
|
def get_git_branch():
|
|
|
|
|
"""Return the git branch version."""
|
2018-03-09 13:25:11 -05:00
|
|
|
if not os.path.exists(".git") or not os.path.isdir(".git"):
|
2010-07-19 15:37:19 -04:00
|
|
|
return None
|
2010-08-26 14:33:41 -04:00
|
|
|
|
2018-03-09 13:25:11 -05:00
|
|
|
version = open(".git/HEAD", "r").read().strip()
|
|
|
|
|
if not version.startswith("ref: "):
|
2010-07-19 15:37:19 -04:00
|
|
|
return version
|
2018-03-09 13:25:11 -05:00
|
|
|
version = version.split("/")
|
2018-03-26 11:25:04 -04:00
|
|
|
version = version[len(version) - 1]
|
2010-07-19 15:37:19 -04:00
|
|
|
return version
|
|
|
|
|
|
2018-03-09 13:25:11 -05:00
|
|
|
|
2018-03-27 14:30:46 -04:00
|
|
|
def get_git_branch_string(prefix="", postfix=""):
|
|
|
|
|
"""Return the git branch name."""
|
|
|
|
|
tt = re.compile(r"[/\\]").split(os.getcwd())
|
|
|
|
|
if len(tt) > 2 and tt[len(tt) - 1] == "mongo":
|
|
|
|
|
par = tt[len(tt) - 2]
|
|
|
|
|
mt = re.compile(r".*_([vV]\d+\.\d+)$").match(par)
|
|
|
|
|
if mt is not None:
|
|
|
|
|
return prefix + mt.group(1).lower() + postfix
|
2010-07-19 15:37:19 -04:00
|
|
|
if par.find("Nightly") > 0:
|
|
|
|
|
return ""
|
|
|
|
|
|
2018-03-27 14:30:46 -04:00
|
|
|
branch = get_git_branch()
|
|
|
|
|
if branch is None or branch == "master":
|
2010-07-19 15:37:19 -04:00
|
|
|
return ""
|
2018-03-27 14:30:46 -04:00
|
|
|
return prefix + branch + postfix
|
2010-07-19 15:37:19 -04:00
|
|
|
|
2018-03-09 13:25:11 -05:00
|
|
|
|
2018-03-27 14:30:46 -04:00
|
|
|
def get_git_version():
|
|
|
|
|
"""Return the git version."""
|
2018-03-09 13:25:11 -05:00
|
|
|
if not os.path.exists(".git") or not os.path.isdir(".git"):
|
2010-07-19 15:37:19 -04:00
|
|
|
return "nogitversion"
|
|
|
|
|
|
2018-03-09 13:25:11 -05:00
|
|
|
version = open(".git/HEAD", "r").read().strip()
|
|
|
|
|
if not version.startswith("ref: "):
|
2010-07-19 15:37:19 -04:00
|
|
|
return version
|
|
|
|
|
version = version[5:]
|
2018-03-27 14:30:46 -04:00
|
|
|
git_ver = ".git/" + version
|
|
|
|
|
if not os.path.exists(git_ver):
|
2010-07-19 15:37:19 -04:00
|
|
|
return version
|
2018-03-27 14:30:46 -04:00
|
|
|
return open(git_ver, "r").read().strip()
|
2018-03-09 13:25:11 -05:00
|
|
|
|
2010-07-19 15:37:19 -04:00
|
|
|
|
2018-03-27 14:30:46 -04:00
|
|
|
def get_git_describe():
|
2019-05-07 13:29:58 -04:00
|
|
|
"""Return 'git describe --abbrev=7'."""
|
2015-05-21 14:15:43 -04:00
|
|
|
with open(os.devnull, "r+") as devnull:
|
2024-05-16 18:00:17 -04:00
|
|
|
proc = subprocess.Popen(
|
|
|
|
|
"git describe --abbrev=7",
|
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
|
stderr=devnull,
|
|
|
|
|
stdin=devnull,
|
|
|
|
|
shell=True,
|
|
|
|
|
)
|
|
|
|
|
return proc.communicate()[0].strip().decode("utf-8")
|
2015-05-21 14:15:43 -04:00
|
|
|
|
2018-03-09 13:25:11 -05:00
|
|
|
|
|
|
|
|
def execsys(args):
|
2018-03-27 14:30:46 -04:00
|
|
|
"""Execute a subprocess of 'args'."""
|
2018-03-09 13:25:11 -05:00
|
|
|
if isinstance(args, str):
|
2018-03-27 14:30:46 -04:00
|
|
|
rc = re.compile(r"\s+")
|
|
|
|
|
args = rc.split(args)
|
|
|
|
|
proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
|
res = proc.communicate()
|
|
|
|
|
return res
|
2010-02-04 10:21:36 -05:00
|
|
|
|
2010-05-14 23:37:29 -04:00
|
|
|
|
2012-02-16 21:49:02 -05:00
|
|
|
def which(executable):
|
2018-03-27 14:30:46 -04:00
|
|
|
"""Return full path of 'executable'."""
|
2018-03-09 13:25:11 -05:00
|
|
|
if sys.platform == "win32":
|
|
|
|
|
paths = os.environ.get("Path", "").split(";")
|
2012-02-16 21:49:02 -05:00
|
|
|
else:
|
2018-03-09 13:25:11 -05:00
|
|
|
paths = os.environ.get("PATH", "").split(":")
|
2012-02-16 21:49:02 -05:00
|
|
|
|
|
|
|
|
for path in paths:
|
|
|
|
|
path = os.path.expandvars(path)
|
|
|
|
|
path = os.path.expanduser(path)
|
|
|
|
|
path = os.path.abspath(path)
|
|
|
|
|
executable_path = os.path.join(path, executable)
|
|
|
|
|
if os.path.exists(executable_path):
|
|
|
|
|
return executable_path
|
|
|
|
|
|
|
|
|
|
return executable
|
|
|
|
|
|
2012-05-10 14:46:08 -04:00
|
|
|
|
2012-04-11 10:57:25 -04:00
|
|
|
def replace_with_repr(unicode_error):
|
2018-03-27 14:30:46 -04:00
|
|
|
"""Codec error handler replacement."""
|
|
|
|
|
# Unicode is a pain, some strings cannot be unicode()'d
|
|
|
|
|
# but we want to just preserve the bytes in a human-readable
|
|
|
|
|
# fashion. This codec error handler will substitute the
|
|
|
|
|
# repr() of the offending bytes into the decoded string
|
|
|
|
|
# at the position they occurred
|
2024-05-16 18:00:17 -04:00
|
|
|
offender = unicode_error.object[unicode_error.start : unicode_error.end]
|
2019-02-19 10:50:57 -05:00
|
|
|
return (str(repr(offender).strip("'").strip('"')), unicode_error.end)
|
2012-04-11 10:57:25 -04:00
|
|
|
|
2018-03-26 11:25:04 -04:00
|
|
|
|
2018-03-09 13:25:11 -05:00
|
|
|
codecs.register_error("repr", replace_with_repr)
|