Coverage for python/lsst/sconsUtils/vcs/hg.py: 10%
34 statements
« prev ^ index » next coverage.py v7.17.5, created at 2026-11-26 06:33 +0000
« prev ^ index » next coverage.py v7.17.5, created at 2026-11-26 06:33 +0000
1# This file is part of sconsUtils.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
22"""A simple python interface to hg (Mercurial), using `os.popen`
24Based on the svn interface.
26If ever we want to do anything clever, we should use one of
27the supported svn/python packages
28"""
30import os
31import re
33from .. import state, utils
36def guessVersionName():
37 """Guess a version name.
39 Returns
40 -------
41 name : `str`
42 Descriptive name of the repository version state.
43 """
44 version = "unknown"
45 if not os.path.exists(".hg"):
46 state.log.warn(f"Cannot guess version without .hg directory; will be set to '{version}'.")
47 return version
49 idents = utils.runExternal("hg id", fatal=True)
50 ident = re.split(r"\s+", idents)
51 if len(ident) == 0:
52 raise RuntimeError("Unable to determine hg version")
54 if re.search(r"\+", ident[0]):
55 raise RuntimeError("Error with hg version: uncommitted changes")
57 if len(ident) == 1:
58 # Somehow, this is all we've got...
59 return ident[0]
61 # Prefer tag name to branch name; branch names get printed in parens
62 index = 1
63 while ident[index].startswith("(") and ident[index].endswith(")") and len(ident) > index + 1:
64 index += 1
66 # Prefer hash to "tip"
67 if ident[index] == "tip":
68 return ident[0]
70 return ident[index]
73def guessFingerprint():
74 """Guess a unique fingerprint.
76 Returns
77 -------
78 fingerprint : `str`
79 SHA1 of current repository state.
80 """
81 fingerprint = "0x0"
82 if not os.path.exists(".hg"):
83 state.log.warn(f"Cannot guess fingerprint without .hg directory; will be set to '{fingerprint}'.")
84 else:
85 idents = utils.runExternal("hg id", fatal=True)
86 ident = re.split(r"\s+", idents)
87 if len(ident) == 0:
88 raise RuntimeError("Unable to determine hg version")
90 fingerprint = utils.runExternal("hg ident --id", fatal=True).strip()
91 if re.search(r"\+", ident[0]):
92 fingerprint += " *"
94 return fingerprint