Coverage for python/lsst/sconsUtils/vcs/svn.py: 7%
87 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-26 11:16 -0800
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-26 11:16 -0800
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 svn, using `os.popen`.
24If ever we want to do anything clever, we should use one of
25the supported svn/python packages
26"""
28import os
29import re
30import sys
33def isSvnFile(file):
34 """Is file under svn control?"""
36 return re.search(r"is not a working copy", "".join(os.popen(f"svn info {file} 2>&1").readlines())) is None
39def getInfo(file="."):
40 """Return a dictionary of all the information returned by "svn info" for
41 the specified file"""
43 if not isSvnFile(file):
44 raise RuntimeError(f"{file} is not under svn control")
46 infoList = os.popen(f"svn info {file}").readlines()
48 info = {}
49 for line in infoList:
50 mat = re.search(r"^([^:]+)\s*:\s*(.*)", line)
51 if mat:
52 info[mat.group(1)] = mat.group(2)
54 return info
57def isTrunk(file="."):
58 """Is file on the trunk?"""
60 info = getInfo(file)
62 return re.search(r"/trunk($|/)", info["URL"]) is not None
65def revision(file=None, lastChanged=False):
66 """Return file's Revision as a string.
68 If file is None return
69 a tuple (oldestRevision, youngestRevision, flags) as reported
70 by svnversion; e.g. (4123, 4168, ("M", "S")) (oldestRevision
71 and youngestRevision may be equal)
72 """
74 if file:
75 info = getInfo(file)
77 if lastChanged:
78 return info["Last Changed Rev"]
79 else:
80 return info["Revision"]
82 if lastChanged:
83 raise RuntimeError("lastChanged makes no sense if file is None")
85 res = os.popen("svnversion . 2>&1").readline()
87 if res == "exported\n":
88 raise RuntimeError("No svn revision information is available")
90 versionRe = r"^(?P<oldest>\d+)(:(?P<youngest>\d+))?(?P<flags>[MS]*)"
91 mat = re.search(versionRe, res)
92 if mat:
93 matches = mat.groupdict()
94 if not matches["youngest"]:
95 matches["youngest"] = matches["oldest"]
96 # OK, we have only one revision present. Find the newest revision
97 # that actually changed anything in this product and ignore
98 # "oldest" (#522)
99 res = os.popen("svnversion --committed . 2>&1").readline()
100 mat = re.search(versionRe, res)
101 if mat:
102 matches = mat.groupdict()
103 return matches["youngest"], matches["youngest"], tuple(matches["flags"])
105 return matches["oldest"], matches["youngest"], tuple(matches["flags"])
107 raise RuntimeError(f'svnversion returned unexpected result "{res[:-1]}"')
110def guessVersionName(HeadURL):
111 """Guess a version name given a HeadURL."""
113 if re.search(r"/trunk$", HeadURL):
114 versionName = ""
115 elif re.search(r"/branches/(.+)$", HeadURL):
116 versionName = "branch_%s+" % re.search(r"/branches/(.+)$", HeadURL).group(1)
117 elif re.search(r"/tags/(.+)$", HeadURL):
118 versionName = "%s" % re.search(r"/tags/(.*)$", HeadURL).group(1)
120 return versionName # no need for a "+svnXXXX"
121 elif re.search(r"/tickets/(\d+)$", HeadURL):
122 versionName = "ticket_%s+" % re.search(r"/tickets/(\d+)$", HeadURL).group(1)
123 else:
124 print(f"Unable to guess versionName name from {HeadURL}", file=sys.stderr)
125 versionName = "unknown+"
127 try: # Try to lookup the svn versionName
128 (oldest, youngest, flags) = revision()
130 okVersion = True
131 if "M" in flags:
132 msg = "You are installing, but have unchecked in files"
133 okVersion = False
134 if "S" in flags:
135 msg = "You are installing, but have switched SVN URLs"
136 okVersion = False
137 if oldest != youngest:
138 msg = f"You have a range of revisions in your tree ({oldest}:{youngest}); adopting {youngest}"
139 okVersion = False
141 if not okVersion:
142 raise RuntimeError(f"Problem with determining svn revision: {msg}")
144 versionName += "svn" + youngest
145 except OSError:
146 return "unknown"
148 return versionName
151def parseVersionName(versionName):
152 """A callback that knows about the LSST convention that a tagname such as
153 ticket_374 means the top of ticket 374, and ticket_374+svn6021
154 means revision 6021 on ticket 374.
156 You may replace "ticket" with "branch" if you wish.
158 The "versionName" may actually be the directory part of a URL, and
159 ``.../(branches|tags|tickets)/tagname`` is also supported
160 """
162 mat = re.search(r"/(branche|tag|ticket)s/(\d+(?:\.\d+)*)(?:([-+])((svn)?(\d+)))?$", versionName)
163 if not mat:
164 mat = re.search(r"/(branch|ticket)_(\d+)(?:([-+])svn(\d+))?$", versionName)
165 if mat:
166 type = mat.group(1)
167 if type == "branches":
168 type = "branch"
169 ticket = mat.group(2)
170 pm = mat.group(3) # + or -
171 revision = mat.group(4)
172 if revision:
173 revision = re.sub("^svn", "", revision)
175 return (type, ticket, revision, pm)
177 return (None, None, None, None)