git-importer/lib/obs_revision.py

73 lines
2.0 KiB
Python
Raw Permalink Normal View History

2022-10-17 14:53:18 +02:00
import datetime
import logging
import re
2022-10-17 19:54:47 +02:00
from urllib.error import HTTPError
2022-10-17 14:53:18 +02:00
2022-10-18 12:17:43 +02:00
class OBSRevision:
2022-11-01 11:34:10 +01:00
def __init__(self, obs, project, package):
2022-10-17 14:53:18 +02:00
self.obs = obs
self.project = project
self.package = package
self.commit = None
self.ignored = False
def parse(self, xml):
self.rev = int(xml.get("rev"))
2022-10-17 20:39:03 +02:00
self.unexpanded_srcmd5 = xml.find("srcmd5").text
2022-10-17 14:53:18 +02:00
# Replaced in check_expanded
2022-10-17 20:39:03 +02:00
self.srcmd5 = self.unexpanded_srcmd5
2022-10-17 14:53:18 +02:00
time = int(xml.find("time").text)
self.time = datetime.datetime.fromtimestamp(time)
userid = xml.find("user")
if userid is not None:
self.userid = userid.text
else:
self.userid = "unknown"
comment = xml.find("comment")
if comment is not None:
self.comment = comment.text or ""
else:
self.comment = ""
# Populated by check_link
self.linkrev = None
self.request_number = None
request_number = xml.find("requestid")
if request_number is not None:
self.request_number = int(request_number.text)
2022-10-17 14:53:18 +02:00
else:
# Sometimes requestid is missing, but can be extracted
# from "comment"
matched = re.match(
r"^Copy from .* based on submit request (\d+) from user .*$",
self.comment,
)
if matched:
self.request_number = int(matched.group(1))
2022-10-17 14:53:18 +02:00
return self
def __str__(self):
return f"Rev {self.project}/{self.package}/{self.rev}.0 Md5 {self.srcmd5} {self.time} {self.userid} {self.request_number}"
2022-10-17 14:53:18 +02:00
def __repr__(self):
return f"[{self.__str__()}]"
2022-10-18 13:13:52 +02:00
def read_link(self):
2022-10-17 14:53:18 +02:00
try:
2022-10-18 13:13:52 +02:00
return self.obs._xml(
2022-10-18 14:29:54 +02:00
f"source/{self.project}/{self.package}/_link",
rev=self.unexpanded_srcmd5,
2022-10-17 14:53:18 +02:00
)
except HTTPError as e:
if e.code == 404:
logging.debug("No _link for the revision")
2022-10-18 13:13:52 +02:00
return None
2022-10-17 14:53:18 +02:00
raise e