Skip to content

Commit 5172002

Browse files
committed
Refactor code handling _scmsync.obsinfo to obs_api.scmsync_obsinfo.ScmsyncObsinfo class
1 parent 56b8aa6 commit 5172002

File tree

2 files changed

+59
-11
lines changed

2 files changed

+59
-11
lines changed

osc/core.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3160,17 +3160,9 @@ def checkout_package(
31603160
if revision is not None:
31613161
# search for the git sha sum based on the OBS DISTURL package source revision
31623162
# we need also take into account that the url was different at that point of time
3163-
url = shasum = None
3164-
u = makeurl(apiurl, ['source', project, package, '_scmsync.obsinfo'], {'rev': revision})
3165-
f = http_GET(u)
3166-
for line in f.readlines():
3167-
if line.startswith(b"revision: "):
3168-
shasum = line[10:].rstrip()
3169-
if line.startswith(b"url: "):
3170-
url = line[5:].rstrip()
3171-
if shasum is None:
3172-
raise oscerr.OscIOError(None, 'Unable to find git shasum for given revision')
3173-
scm_url = url + b'#' + shasum
3163+
from .obs_api.scmsync_obsinfo import ScmsyncObsinfo
3164+
scmsync_obsinfo = ScmsyncObsinfo.from_api(apiurl, project, package, rev=revision)
3165+
scm_url = f"{scmsync_obsinfo.url}#{scmsync_obsinfo.revision}"
31743166

31753167
os.putenv("OSC_VERSION", get_osc_version())
31763168
run_external(['/usr/lib/obs/service/obs_scm_bridge', '--outdir', directory, '--url', scm_url])

osc/obs_api/scmsync_obsinfo.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import typing
2+
3+
from ..util.models import * # pylint: disable=wildcard-import,unused-wildcard-import
4+
5+
6+
class ScmsyncObsinfo(BaseModel):
7+
"""
8+
Class for handling _scmsync.obsinfo files
9+
"""
10+
11+
mtime: int = Field()
12+
commit: str = Field()
13+
url: str = Field()
14+
revision: str = Field()
15+
16+
@classmethod
17+
def from_string(cls, data: str) -> "ScmsyncObsinfo":
18+
kwargs = {}
19+
for line in data.splitlines():
20+
line = line.strip()
21+
if not line:
22+
continue
23+
key, value = line.split(": ", 1)
24+
field = cls.__fields__.get(key, None)
25+
if field and field.type is int:
26+
value = int(value)
27+
kwargs[key] = value
28+
return cls(**kwargs)
29+
30+
@classmethod
31+
def from_file(cls, file: Union[str, typing.IO]) -> "ScmsyncObsinfo":
32+
if isinstance(file, str):
33+
with open(file, "r", encoding="utf-8") as f:
34+
return cls.from_string(f.read())
35+
data = file.read()
36+
if isinstance(data, bytes):
37+
data = data.decode("utf-8")
38+
return cls.from_string(data)
39+
40+
@classmethod
41+
def from_api(cls, apiurl: str, project: str, package: str, *, rev: str) -> "ScmsyncObsinfo":
42+
import urllib.error
43+
from .. import oscerr
44+
from ..connection import http_request
45+
from ..core import makeurl
46+
47+
url_path = ["source", project, package, "_scmsync.obsinfo"]
48+
url_query = {"rev": rev}
49+
url = makeurl(apiurl, url_path, url_query)
50+
try:
51+
response = http_request("GET", url)
52+
except urllib.error.HTTPError as e:
53+
if e.status == 404:
54+
raise oscerr.NotFoundAPIError(f"File '_scmsync.obsinfo' was not found in {project}/{package}, rev={rev}")
55+
raise
56+
return cls.from_file(response)

0 commit comments

Comments
 (0)