2022-10-17 15:17:25 +02:00
|
|
|
import logging
|
2022-11-01 09:03:03 +01:00
|
|
|
import os
|
2022-10-24 12:01:28 +02:00
|
|
|
import xml.etree.ElementTree as ET
|
2022-10-17 15:17:25 +02:00
|
|
|
|
2022-11-01 09:03:03 +01:00
|
|
|
import yaml
|
|
|
|
|
2022-10-17 19:54:47 +02:00
|
|
|
from lib.binary import is_binary_or_large
|
2022-10-17 20:39:03 +02:00
|
|
|
from lib.db import DB
|
2022-10-18 13:13:52 +02:00
|
|
|
from lib.db_revision import DBRevision
|
2022-10-17 19:54:47 +02:00
|
|
|
from lib.git import Git
|
|
|
|
from lib.obs import OBS
|
2022-10-18 12:17:43 +02:00
|
|
|
from lib.obs_revision import OBSRevision
|
2022-11-02 07:20:53 +01:00
|
|
|
from lib.proxy_sha256 import ProxySHA256, md5
|
|
|
|
from lib.tree_builder import AbstractWalker, TreeBuilder
|
2022-10-26 14:42:39 +02:00
|
|
|
from lib.user import User
|
|
|
|
|
2022-11-01 13:09:27 +01:00
|
|
|
|
2022-10-17 15:17:25 +02:00
|
|
|
class Importer:
|
2022-11-01 11:34:10 +01:00
|
|
|
def __init__(self, projects, package, repodir):
|
2022-10-17 15:17:25 +02:00
|
|
|
# The idea is to create each commit in order, and draw the
|
|
|
|
# same graph described by the revisions timeline. For that we
|
|
|
|
# need first to fetch all the revisions and sort them
|
|
|
|
# linearly, based on the timestamp.
|
|
|
|
#
|
|
|
|
# After that we recreate the commits, and if one revision is a
|
|
|
|
# request that contains a target inside the projects in the
|
|
|
|
# "history", we create a merge commit.
|
|
|
|
#
|
|
|
|
# Optionally, if a flag is set, we will try to find a common
|
|
|
|
# "Initial commit" from a reference branch (the first one in
|
|
|
|
# "projects", that is safe to assume to be "openSUSE:Factory".
|
|
|
|
# This is not always a good idea. For example, in a normal
|
|
|
|
# situation the "devel" project history is older than
|
|
|
|
# "factory", and we can root the tree on it. But for some
|
|
|
|
# other projects we lost partially the "devel" history project
|
|
|
|
# (could be moved), and "factory" is not the root.
|
|
|
|
|
|
|
|
self.package = package
|
|
|
|
|
|
|
|
self.obs = OBS()
|
|
|
|
self.git = Git(
|
|
|
|
repodir,
|
|
|
|
committer="Git OBS Bridge",
|
|
|
|
committer_email="obsbridge@suse.de",
|
|
|
|
).create()
|
2022-11-01 09:03:03 +01:00
|
|
|
self.state_file = os.path.join(self.git.path, ".git", "_flat_state.yaml")
|
2022-10-17 15:17:25 +02:00
|
|
|
self.proxy_sha256 = ProxySHA256(self.obs, enabled=True)
|
2022-11-01 11:23:40 +01:00
|
|
|
self.gc_interval = 200
|
2022-10-17 15:17:25 +02:00
|
|
|
|
|
|
|
# Add the "devel" project
|
|
|
|
(project, branch, api_url) = projects[0]
|
|
|
|
assert project == "openSUSE:Factory"
|
|
|
|
self.obs.change_url(api_url)
|
|
|
|
devel_project = self.obs.devel_project(project, package)
|
|
|
|
if devel_project:
|
|
|
|
self.projects = [(devel_project, "devel", api_url)] + projects
|
|
|
|
else:
|
|
|
|
self.projects = projects
|
|
|
|
|
|
|
|
# Associate the branch and api_url information per project
|
|
|
|
self.projects_info = {
|
|
|
|
project: (branch, api_url) for (project, branch, api_url) in self.projects
|
|
|
|
}
|
|
|
|
|
|
|
|
def download(self, revision):
|
|
|
|
obs_files = self.obs.files(revision.project, revision.package, revision.srcmd5)
|
|
|
|
git_files = {
|
|
|
|
(f.name, f.stat().st_size, md5(f))
|
|
|
|
for f in self.git.path.iterdir()
|
|
|
|
if f.is_file() and f.name not in (".gitattributes")
|
|
|
|
}
|
|
|
|
|
|
|
|
# Overwrite ".gitattributes" with the
|
|
|
|
self.git.add_default_lfs_gitattributes(force=True)
|
|
|
|
|
|
|
|
# Download each file in OBS if it is not a binary (or large)
|
|
|
|
# file
|
|
|
|
for (name, size, file_md5) in obs_files:
|
|
|
|
# this file creates easily 100k commits and is just useless data :(
|
|
|
|
# unfortunately it's stored in the same meta package as the project config
|
|
|
|
if revision.package == "_project" and name == "_staging_workflow":
|
|
|
|
continue
|
|
|
|
# have such files been detected as text mimetype before?
|
|
|
|
is_text = self.proxy_sha256.is_text(name)
|
|
|
|
if not is_text and is_binary_or_large(name, size):
|
|
|
|
file_sha256 = self.proxy_sha256.get_or_put(
|
|
|
|
revision.project,
|
|
|
|
revision.package,
|
|
|
|
name,
|
|
|
|
revision.srcmd5,
|
|
|
|
file_md5,
|
|
|
|
size,
|
|
|
|
)
|
|
|
|
self.git.add_lfs(name, file_sha256["sha256"], size)
|
|
|
|
else:
|
|
|
|
if (name, size, file_md5) not in git_files:
|
2022-11-01 11:23:40 +01:00
|
|
|
logging.debug(f"Download {name}")
|
2022-10-17 15:17:25 +02:00
|
|
|
self.obs.download(
|
|
|
|
revision.project,
|
|
|
|
revision.package,
|
|
|
|
name,
|
|
|
|
revision.srcmd5,
|
|
|
|
self.git.path,
|
|
|
|
)
|
|
|
|
# Validate the MD5 of the downloaded file
|
|
|
|
if md5(self.git.path / name) != file_md5:
|
|
|
|
raise Exception(f"Download error in {name}")
|
|
|
|
self.git.add(name)
|
|
|
|
|
|
|
|
# Remove extra files
|
|
|
|
obs_names = {n for (n, _, _) in obs_files}
|
|
|
|
git_names = {n for (n, _, _) in git_files}
|
|
|
|
for name in git_names - obs_names:
|
2022-11-01 11:23:40 +01:00
|
|
|
logging.debug(f"Remove {name}")
|
2022-10-17 15:17:25 +02:00
|
|
|
self.git.remove(name)
|
|
|
|
|
2022-11-01 11:23:40 +01:00
|
|
|
def set_gc_interval(self, gc):
|
|
|
|
self.gc_interval = gc
|
|
|
|
|
2022-10-18 12:17:43 +02:00
|
|
|
def update_db_package(self, db, project, package):
|
|
|
|
root = self.obs._history(project, package)
|
|
|
|
if root is None:
|
|
|
|
return
|
2022-10-18 14:29:54 +02:00
|
|
|
latest = DBRevision.latest_revision(db, project, package)
|
2022-10-18 12:17:43 +02:00
|
|
|
for r in root.findall("revision"):
|
2022-11-01 11:34:10 +01:00
|
|
|
rev = OBSRevision(self.obs, project, package).parse(r)
|
2022-10-18 12:17:43 +02:00
|
|
|
if not latest or rev.rev > latest.rev:
|
2022-10-18 13:13:52 +02:00
|
|
|
dbrev = DBRevision.import_obs_rev(db, rev)
|
2022-10-18 21:36:58 +02:00
|
|
|
try:
|
|
|
|
root = rev.read_link()
|
|
|
|
except ET.ParseError:
|
|
|
|
dbrev.set_broken(db)
|
|
|
|
continue
|
2022-10-18 15:40:11 +02:00
|
|
|
if root is not None:
|
2022-10-18 13:13:52 +02:00
|
|
|
tprj = root.get("project") or project
|
2022-10-18 14:29:54 +02:00
|
|
|
tpkg = root.get("package") or package
|
2022-10-18 13:13:52 +02:00
|
|
|
dbrev.links_to(db, tprj, tpkg)
|
2022-10-18 12:17:43 +02:00
|
|
|
|
2022-10-24 12:01:28 +02:00
|
|
|
def find_linked_revs(self, db):
|
2022-10-26 15:49:14 +02:00
|
|
|
with db.cursor() as cur:
|
2022-10-24 12:01:28 +02:00
|
|
|
cur.execute(
|
2022-10-26 15:49:14 +02:00
|
|
|
"""SELECT * from revisions WHERE id in (SELECT l.revision_id FROM links l
|
|
|
|
LEFT JOIN linked_revs lrevs ON lrevs.revision_id=l.revision_id
|
|
|
|
WHERE lrevs.id IS NULL) and broken is FALSE;"""
|
2022-10-24 12:01:28 +02:00
|
|
|
)
|
2022-10-26 15:49:14 +02:00
|
|
|
for row in cur.fetchall():
|
|
|
|
rev = DBRevision(row)
|
|
|
|
linked_rev = rev.linked_rev(db)
|
|
|
|
if not linked_rev:
|
|
|
|
logging.debug(f"No link {rev}")
|
|
|
|
continue
|
|
|
|
cur.execute(
|
|
|
|
"""INSERT INTO linked_revs (revision_id, linked_id)
|
|
|
|
VALUES (%s,%s)""",
|
|
|
|
(rev.dbid, linked_rev.dbid),
|
|
|
|
)
|
2022-10-24 12:01:28 +02:00
|
|
|
|
|
|
|
def fetch_all_linked_packages(self, db, project, package):
|
2022-10-26 15:49:14 +02:00
|
|
|
with db.cursor() as cur:
|
|
|
|
cur.execute(
|
|
|
|
"""SELECT DISTINCT l.project, l.package from links l JOIN revisions r
|
|
|
|
on r.id=l.revision_id WHERE r.project=%s AND r.package=%s""",
|
|
|
|
(project, package),
|
|
|
|
)
|
|
|
|
for row in cur.fetchall():
|
|
|
|
(lproject, lpackage) = row
|
|
|
|
self.update_db_package(db, lproject, lpackage)
|
2022-10-24 12:01:28 +02:00
|
|
|
|
2022-10-24 20:21:53 +02:00
|
|
|
def find_fake_revisions(self, db):
|
2022-10-26 15:49:14 +02:00
|
|
|
with db.cursor() as cur:
|
|
|
|
cur.execute(
|
2022-10-27 07:33:46 +02:00
|
|
|
"SELECT * from revisions WHERE id in (SELECT linked_id from linked_revs WHERE considered=FALSE)"
|
2022-10-26 15:49:14 +02:00
|
|
|
)
|
|
|
|
for row in cur.fetchall():
|
2022-10-26 22:18:16 +02:00
|
|
|
self._find_fake_revision(db, DBRevision(row))
|
2022-10-26 15:49:14 +02:00
|
|
|
|
|
|
|
def _find_fake_revision(self, db, rev):
|
|
|
|
prev = rev.previous_commit(db)
|
|
|
|
if not prev:
|
|
|
|
with db.cursor() as cur:
|
2022-10-24 20:21:53 +02:00
|
|
|
cur.execute(
|
2022-10-27 10:16:20 +02:00
|
|
|
"UPDATE linked_revs SET considered=TRUE where linked_id=%s",
|
2022-10-24 20:21:53 +02:00
|
|
|
(rev.dbid,),
|
|
|
|
)
|
2022-10-26 15:49:14 +02:00
|
|
|
return
|
|
|
|
with db.cursor() as cur:
|
|
|
|
cur.execute(
|
2022-10-26 22:18:16 +02:00
|
|
|
"""SELECT * FROM revisions WHERE id IN
|
2022-10-24 20:21:53 +02:00
|
|
|
(SELECT revision_id from linked_revs WHERE linked_id=%s)
|
|
|
|
AND commit_time <= %s ORDER BY commit_time""",
|
|
|
|
(prev.dbid, rev.commit_time),
|
|
|
|
)
|
|
|
|
last_linked = None
|
2022-10-26 15:49:14 +02:00
|
|
|
for linked in cur.fetchall():
|
2022-10-24 20:21:53 +02:00
|
|
|
linked = DBRevision(linked)
|
|
|
|
nextrev = linked.next_commit(db)
|
|
|
|
if nextrev and nextrev.commit_time < rev.commit_time:
|
|
|
|
continue
|
|
|
|
last_linked = linked
|
|
|
|
cur.execute(
|
2022-10-27 10:16:20 +02:00
|
|
|
"UPDATE linked_revs SET considered=TRUE where linked_id=%s",
|
2022-10-24 20:21:53 +02:00
|
|
|
(rev.dbid,),
|
|
|
|
)
|
2022-10-26 15:49:14 +02:00
|
|
|
if not last_linked:
|
|
|
|
return
|
|
|
|
|
|
|
|
with db.cursor() as cur:
|
|
|
|
linked = last_linked
|
|
|
|
cur.execute(
|
|
|
|
"SELECT 1 FROM fake_revs where revision_id=%s AND linked_id=%s",
|
|
|
|
(rev.dbid, linked.dbid),
|
|
|
|
)
|
|
|
|
if cur.fetchone():
|
|
|
|
cur.execute(
|
2022-10-27 10:16:20 +02:00
|
|
|
"UPDATE linked_revs SET considered=TRUE where linked_id=%s",
|
2022-10-26 15:49:14 +02:00
|
|
|
(rev.dbid,),
|
2022-10-24 20:21:53 +02:00
|
|
|
)
|
2022-10-26 15:49:14 +02:00
|
|
|
return
|
|
|
|
fake_rev = linked.rev + rev.rev / 1000.0
|
|
|
|
comment = f"Updating link to change in {rev.project}/{rev.package} revision {rev.rev}"
|
|
|
|
cur.execute(
|
|
|
|
"""INSERT INTO revisions (project,package,rev,unexpanded_srcmd5,
|
|
|
|
commit_time, userid, comment) VALUES(%s,%s,%s,%s,%s,%s,%s) RETURNING id""",
|
|
|
|
(
|
|
|
|
linked.project,
|
|
|
|
linked.package,
|
|
|
|
fake_rev,
|
|
|
|
linked.unexpanded_srcmd5,
|
|
|
|
rev.commit_time,
|
|
|
|
"buildservice-autocommit",
|
|
|
|
comment,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
new_id = cur.fetchone()[0]
|
|
|
|
cur.execute(
|
|
|
|
"""INSERT INTO linked_revs (revision_id, linked_id) VALUES (%s,%s)""",
|
|
|
|
(new_id, rev.dbid),
|
|
|
|
)
|
|
|
|
cur.execute(
|
|
|
|
"""INSERT INTO fake_revs (revision_id, linked_id) VALUES (%s,%s)""",
|
|
|
|
(rev.dbid, linked.dbid),
|
|
|
|
)
|
2022-10-24 20:21:53 +02:00
|
|
|
|
2022-10-26 21:47:39 +02:00
|
|
|
def revisions_without_files(self, db):
|
|
|
|
with db.cursor() as cur:
|
|
|
|
cur.execute(
|
|
|
|
"SELECT * FROM revisions WHERE broken=FALSE AND expanded_srcmd5 IS NULL"
|
|
|
|
)
|
|
|
|
return [DBRevision(row) for row in cur.fetchall()]
|
|
|
|
|
2022-10-31 07:03:19 +01:00
|
|
|
def export_as_git(self):
|
|
|
|
db = DB()
|
2022-10-31 08:03:01 +01:00
|
|
|
tree = TreeBuilder(db).build(self.package)
|
2022-11-02 07:20:53 +01:00
|
|
|
flats = tree.as_flat_list()
|
2022-10-31 08:03:01 +01:00
|
|
|
|
2022-10-31 16:31:10 +01:00
|
|
|
branch_state = {"factory": None, "devel": None}
|
2022-11-01 09:03:03 +01:00
|
|
|
state_data = dict()
|
|
|
|
if os.path.exists(self.state_file):
|
|
|
|
with open(self.state_file, "r") as f:
|
|
|
|
state_data = yaml.safe_load(f)
|
|
|
|
if type(state_data) != dict:
|
|
|
|
state_data = {}
|
|
|
|
left_to_commit = []
|
2022-11-02 07:20:53 +01:00
|
|
|
for flat in reversed(flats):
|
2022-11-01 09:03:03 +01:00
|
|
|
found_state = False
|
|
|
|
for branch in ["factory", "devel"]:
|
|
|
|
if flat.commit.dbid == state_data.get(branch):
|
|
|
|
branch_state[branch] = flat.commit
|
|
|
|
flat.commit.git_commit = self.git.branch_head(branch)
|
|
|
|
logging.debug(
|
|
|
|
f"Found {self.git.path}'s {branch} branch in state {flat}"
|
|
|
|
)
|
|
|
|
left_to_commit = []
|
|
|
|
found_state = True
|
|
|
|
if not found_state:
|
|
|
|
left_to_commit.append(flat)
|
2022-11-01 11:23:40 +01:00
|
|
|
|
|
|
|
gc_cnt = self.gc_interval
|
|
|
|
if len(left_to_commit) > 0:
|
|
|
|
self.git.gc()
|
2022-11-01 09:03:03 +01:00
|
|
|
for flat in left_to_commit:
|
2022-11-01 11:23:40 +01:00
|
|
|
gc_cnt -= 1
|
|
|
|
if gc_cnt <= 0 and self.gc_interval:
|
|
|
|
self.git.gc()
|
|
|
|
gc_cnt = self.gc_interval
|
2022-11-01 09:03:03 +01:00
|
|
|
logging.debug(f"Committing {flat}")
|
2022-10-31 16:31:10 +01:00
|
|
|
self.commit_flat(db, flat, branch_state)
|
2022-10-31 08:50:05 +01:00
|
|
|
|
2022-10-31 16:31:10 +01:00
|
|
|
def limit_download(self, file):
|
|
|
|
if file.endswith(".spec") or file.endswith(".changes"):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def commit_flat(self, db, flat, branch_state):
|
2022-10-31 08:50:05 +01:00
|
|
|
parents = []
|
|
|
|
self.git.checkout(flat.branch)
|
|
|
|
if flat.parent1:
|
|
|
|
parents.append(flat.parent1.git_commit)
|
|
|
|
if flat.parent2:
|
|
|
|
parents.append(flat.parent2.git_commit)
|
2022-10-31 16:31:10 +01:00
|
|
|
to_download, to_delete = flat.commit.calc_delta(db, branch_state[flat.branch])
|
|
|
|
for file in to_delete:
|
|
|
|
if not self.limit_download(file):
|
|
|
|
continue
|
|
|
|
self.git.remove(file)
|
|
|
|
for file in to_download:
|
|
|
|
if not self.limit_download(file):
|
|
|
|
continue
|
|
|
|
self.obs.download(
|
|
|
|
flat.commit.project,
|
|
|
|
flat.commit.package,
|
|
|
|
file,
|
|
|
|
flat.commit.expanded_srcmd5,
|
|
|
|
self.git.path,
|
|
|
|
)
|
|
|
|
self.git.add(file)
|
2022-10-31 08:50:05 +01:00
|
|
|
|
|
|
|
commit = self.git.commit(
|
|
|
|
f"OBS User {flat.commit.userid}",
|
|
|
|
"null@suse.de",
|
|
|
|
flat.commit.commit_time,
|
|
|
|
# TODO: Normalize better the commit message
|
|
|
|
f"{flat.commit.comment}\n\n{flat.commit}",
|
|
|
|
allow_empty=True,
|
|
|
|
parents=parents,
|
|
|
|
)
|
|
|
|
flat.commit.git_commit = commit
|
2022-10-31 16:31:10 +01:00
|
|
|
branch_state[flat.branch] = flat.commit
|
2022-11-01 09:03:03 +01:00
|
|
|
with open(self.state_file, "w") as f:
|
|
|
|
data = {}
|
|
|
|
for branch in ["factory", "devel"]:
|
|
|
|
commit = branch_state[branch]
|
|
|
|
if commit:
|
|
|
|
data[branch] = commit.dbid
|
|
|
|
yaml.dump(data, f)
|
2022-10-31 07:03:19 +01:00
|
|
|
|
2022-10-17 20:39:03 +02:00
|
|
|
def import_into_db(self):
|
|
|
|
db = DB()
|
2022-10-18 12:17:43 +02:00
|
|
|
for project, _, api_url in self.projects:
|
|
|
|
self.obs.change_url(api_url)
|
|
|
|
self.update_db_package(db, project, self.package)
|
2022-10-24 20:21:53 +02:00
|
|
|
self.fetch_all_linked_packages(db, project, self.package)
|
|
|
|
# all remaining, no filtering here
|
|
|
|
self.find_linked_revs(db)
|
2022-10-26 11:58:01 +02:00
|
|
|
|
2022-10-27 07:33:46 +02:00
|
|
|
missing_users = User.missing_users(db)
|
|
|
|
for userid in missing_users:
|
|
|
|
missing_user = self.obs.user(userid)
|
|
|
|
if missing_user:
|
|
|
|
missing_user.import_into_db(db)
|
2022-10-26 13:46:02 +02:00
|
|
|
|
2022-10-27 07:33:46 +02:00
|
|
|
self.find_fake_revisions(db)
|
|
|
|
for rev in self.revisions_without_files(db):
|
|
|
|
with db.cursor() as cur:
|
|
|
|
cur.execute(
|
|
|
|
"""SELECT unexpanded_srcmd5 from revisions WHERE
|
|
|
|
id=(SELECT linked_id FROM linked_revs WHERE revision_id=%s)""",
|
|
|
|
(rev.dbid,),
|
|
|
|
)
|
|
|
|
linked_rev = cur.fetchone()
|
|
|
|
if linked_rev:
|
|
|
|
linked_rev = linked_rev[0]
|
|
|
|
list = self.obs.list(
|
|
|
|
rev.project, rev.package, rev.unexpanded_srcmd5, linked_rev
|
|
|
|
)
|
|
|
|
if list:
|
|
|
|
rev.import_dir_list(db, list)
|
|
|
|
md5 = rev.calculate_files_hash(db)
|
2022-10-24 12:01:28 +02:00
|
|
|
with db.cursor() as cur:
|
|
|
|
cur.execute(
|
2022-10-27 07:33:46 +02:00
|
|
|
"UPDATE revisions SET files_hash=%s WHERE id=%s",
|
|
|
|
(md5, rev.dbid),
|
2022-10-24 12:01:28 +02:00
|
|
|
)
|
2022-10-27 07:33:46 +02:00
|
|
|
else:
|
|
|
|
rev.set_broken(db)
|
|
|
|
|
|
|
|
for number in DBRevision.requests_to_fetch(db):
|
|
|
|
self.obs.request(number).import_into_db(db)
|
2022-10-19 14:00:07 +02:00
|
|
|
|
2022-10-18 19:29:25 +02:00
|
|
|
db.conn.commit()
|