Skip to content

Commit

Permalink
Refactor code handling _scmsync.obsinfo to obs_api.scmsync_obsinfo.Sc…
Browse files Browse the repository at this point in the history
…msyncObsinfo class
  • Loading branch information
dmach committed Oct 7, 2024
1 parent 56b8aa6 commit c670c93
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
14 changes: 3 additions & 11 deletions osc/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3160,17 +3160,9 @@ def checkout_package(
if revision is not None:
# search for the git sha sum based on the OBS DISTURL package source revision
# we need also take into account that the url was different at that point of time
url = shasum = None
u = makeurl(apiurl, ['source', project, package, '_scmsync.obsinfo'], {'rev': revision})
f = http_GET(u)
for line in f.readlines():
if line.startswith(b"revision: "):
shasum = line[10:].rstrip()
if line.startswith(b"url: "):
url = line[5:].rstrip()
if shasum is None:
raise oscerr.OscIOError(None, 'Unable to find git shasum for given revision')
scm_url = url + b'#' + shasum
from .obs_api.scmsync_obsinfo import ScmsyncObsinfo

Check notice

Code scanning / CodeQL

Cyclic import Note

Import of module
osc.obs_api.scmsync_obsinfo
begins an import cycle.
scmsync_obsinfo = ScmsyncObsinfo.from_api(apiurl, project, package, rev=revision)
scm_url = f"{scmsync_obsinfo.url}#{scmsync_obsinfo.revision}"

os.putenv("OSC_VERSION", get_osc_version())
run_external(['/usr/lib/obs/service/obs_scm_bridge', '--outdir', directory, '--url', scm_url])
Expand Down
42 changes: 42 additions & 0 deletions osc/obs_api/scmsync_obsinfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import typing

from ..util.models import * # pylint: disable=wildcard-import,unused-wildcard-import

Check notice

Code scanning / CodeQL

Cyclic import Note

Import of module
osc.util.models
begins an import cycle.


class ScmsyncObsinfo(BaseModel):
"""
Class for handling _scmsync.obsinfo files
"""

mtime: int = Field()
commit: str = Field()
url: str = Field()
revision: str = Field()

@classmethod
def from_string(cls, data: str) -> "ScmsyncObsinfo":
kwargs = {}
for line in data.splitlines():
line = line.strip()
if not line:
continue
key, value = line.split(": ", 1)
field = cls.__fields__.get(key, None)
if field and field.type is int:
value = int(value)
kwargs[key] = value
return cls(**kwargs)

@classmethod
def from_file(cls, file: Union[str, typing.IO]) -> "ScmsyncObsinfo":
if isinstance(file, str):
with open(file, "r", encoding="utf-8") as f:
return cls.from_string(f.read())
return cls.from_string(file.read())

@classmethod
def from_api(cls, apiurl: str, project: str, package: str, *, rev: str) -> "ScmsyncObsinfo":
url_path = ["source", project, package, "_scmsync.obsinfo"]
url_query = {"rev": rev}
response = cls.xml_request("GET", apiurl, url_path, url_query)
return cls.from_file(response, apiurl=apiurl)

0 comments on commit c670c93

Please sign in to comment.