forked from importers/git-importer
Compare commits
1 Commits
devel
...
use_git_fo
Author | SHA1 | Date | |
---|---|---|---|
|
3d1684f01b |
19
README.md
19
README.md
@ -1,18 +1,5 @@
|
||||
Installation
|
||||
------------
|
||||
|
||||
sudo zypper in python3-psycopg
|
||||
sudo su - postgres
|
||||
createdb -O <LOCAL_USER> imported_git`
|
||||
sudo zypper in python3-psycopg
|
||||
sudo su - postgres
|
||||
# `createdb -O <LOCAL_USER> imported_git`
|
||||
|
||||
To reset the database, drop table scheme
|
||||
|
||||
|
||||
Gitea parameters
|
||||
----------------
|
||||
|
||||
* `GITEA_HOST` - default: src.opensuse.org
|
||||
* `GITEA_USER` - Used to generate SSH links for push. Default: gitea
|
||||
* `GITEA_ORG` - target organization to push to
|
||||
* `GITEA_DEFAULT_BRANCH` - default branch
|
||||
|
||||
|
@ -42,8 +42,8 @@ PROJECTS = [
|
||||
]
|
||||
|
||||
|
||||
def export_package(project, package, repodir, cachedir, gc):
|
||||
exporter = GitExporter(URL_OBS, project, package, repodir, cachedir)
|
||||
def export_package(package, repodir, cachedir, gc):
|
||||
exporter = GitExporter(URL_OBS, "openSUSE:Factory", package, repodir, cachedir)
|
||||
exporter.set_gc_interval(gc)
|
||||
exporter.export_as_git()
|
||||
|
||||
@ -51,12 +51,6 @@ def export_package(project, package, repodir, cachedir, gc):
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="OBS history importer into git")
|
||||
parser.add_argument("packages", help="OBS package names", nargs="*")
|
||||
parser.add_argument(
|
||||
"-p",
|
||||
"--project",
|
||||
default="openSUSE:Factory",
|
||||
help="Project to import/export, default is openSUSE:Factory",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-r",
|
||||
"--repodir",
|
||||
@ -116,13 +110,10 @@ def main():
|
||||
if not args.cachedir:
|
||||
args.cachedir = pathlib.Path("~/.cache/git-import/").expanduser()
|
||||
|
||||
importer = Importer(URL_OBS, args.project, args.packages)
|
||||
importer = Importer(URL_OBS, "openSUSE:Factory", args.packages)
|
||||
importer.import_into_db()
|
||||
for package in args.packages:
|
||||
if not importer.package_with_scmsync(package):
|
||||
export_package(args.project, package, args.repodir, args.cachedir, args.gc)
|
||||
else:
|
||||
logging.debug(f"{args.project}/{package} has scmsync links - skipping export")
|
||||
export_package(package, args.repodir, args.cachedir, args.gc)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
37
lib/git.py
37
lib/git.py
@ -48,7 +48,6 @@ class Git:
|
||||
def open(self):
|
||||
if not self.exists():
|
||||
self.git_run(["init", "--object-format=sha256", "-b", "factory"])
|
||||
self.git_run(["config", "lfs.allowincompletepush", "true"])
|
||||
|
||||
def is_dirty(self):
|
||||
"""Check if there is something to commit"""
|
||||
@ -86,7 +85,7 @@ class Git:
|
||||
"""Checkout into the branch HEAD"""
|
||||
new_branch = False
|
||||
if branch not in self.branches():
|
||||
self.git_run(["switch", "-q", "--orphan", branch])
|
||||
self.git_run(["branch", "-q", branch, "HEAD"])
|
||||
new_branch = True
|
||||
else:
|
||||
ref = f"refs/heads/{branch}"
|
||||
@ -181,13 +180,7 @@ class Git:
|
||||
# logging.warning(f"Error removing file {path}: {e}")
|
||||
|
||||
def add(self, filename):
|
||||
self.git_run(["add", ":(literal)" + str(filename)])
|
||||
|
||||
def add_default_gitignore(self):
|
||||
if not (self.path / ".gitignore").exists():
|
||||
with (self.path / ".gitignore").open("w") as f:
|
||||
f.write(".osc\n")
|
||||
self.add(".gitignore")
|
||||
self.git_run(["add", filename])
|
||||
|
||||
def add_default_lfs_gitattributes(self, force=False):
|
||||
if not (self.path / ".gitattributes").exists() or force:
|
||||
@ -242,7 +235,7 @@ class Git:
|
||||
|
||||
def remove(self, file: pathlib.Path):
|
||||
self.git_run(
|
||||
["rm", "-q", "-f", "--ignore-unmatch", ":(literal)" + file.name],
|
||||
["rm", "-q", "-f", "--ignore-unmatch", file.name],
|
||||
)
|
||||
patterns = self.get_specific_lfs_gitattributes()
|
||||
if file.name in patterns:
|
||||
@ -252,27 +245,15 @@ class Git:
|
||||
def add_gitea_remote(self, package):
|
||||
repo_name = package.replace("+", "_")
|
||||
org_name = "rpm"
|
||||
gitea_user = "gitea"
|
||||
gitea_host = "src.opensuse.org"
|
||||
default_branch = "factory"
|
||||
|
||||
if os.getenv("GITEA_HOST"):
|
||||
gitea_host = getenv("GITEA_HOST")
|
||||
if os.getenv("GITEA_USER"):
|
||||
gitea_user = getenv("GITEA_USER")
|
||||
if os.getenv("GITEA_ORG"):
|
||||
org_name = getenv("GITEA_ORG")
|
||||
if os.getenv("GITEA_DEFAULT_BRANCH"):
|
||||
default_branch = getenv("GITEA_DEFAULT_BRANCH")
|
||||
|
||||
if not os.getenv("GITEA_TOKEN"):
|
||||
logging.warning("Not adding a remote due to missing $GITEA_TOKEN")
|
||||
return
|
||||
|
||||
url = f"https://{gitea_host}/api/v1/org/{org_name}/repos"
|
||||
url = f"https://src.opensuse.org/api/v1/org/{org_name}/repos"
|
||||
response = requests.post(
|
||||
url,
|
||||
data={"name": repo_name, "object_format_name": "sha256", "default_branch": default_branch},
|
||||
data={"name": repo_name},
|
||||
headers={"Authorization": f"token {os.getenv('GITEA_TOKEN')}"},
|
||||
timeout=10,
|
||||
)
|
||||
@ -280,7 +261,7 @@ class Git:
|
||||
# 201 Created
|
||||
if response.status_code not in (201, 409):
|
||||
print(response.data)
|
||||
url = f"{gitea_user}@{gitea_host}:{org_name}/{repo_name}.git"
|
||||
url = f"gitea@src.opensuse.org:{org_name}/{repo_name}.git"
|
||||
self.git_run(
|
||||
["remote", "add", "origin", url],
|
||||
)
|
||||
@ -290,11 +271,13 @@ class Git:
|
||||
["remote"],
|
||||
stdout=subprocess.PIPE,
|
||||
).stdout.decode("utf-8"):
|
||||
logging.warning("Not pushing to remote because no 'origin' configured")
|
||||
logger.warning("Not pushing to remote because no 'origin' configured")
|
||||
return
|
||||
|
||||
cmd = ["push"]
|
||||
if force:
|
||||
cmd.append("-f")
|
||||
cmd += ["origin", "--all"]
|
||||
cmd.append("origin")
|
||||
cmd.append("refs/heads/factory")
|
||||
cmd.append("refs/heads/devel")
|
||||
self.git_run(cmd)
|
||||
|
@ -2,6 +2,8 @@ import logging
|
||||
import os
|
||||
|
||||
import yaml
|
||||
from hashlib import md5
|
||||
from pathlib import Path
|
||||
|
||||
from lib.binary import is_binary_or_large
|
||||
from lib.db import DB
|
||||
@ -29,7 +31,7 @@ class GitExporter:
|
||||
self.git.open()
|
||||
else:
|
||||
self.git.create()
|
||||
# self.git.add_gitea_remote(package)
|
||||
self.git.add_gitea_remote(package)
|
||||
self.state_file = os.path.join(self.git.path, ".git", "_flat_state.yaml")
|
||||
self.gc_interval = 200
|
||||
self.cachedir = cachedir
|
||||
@ -86,11 +88,6 @@ class GitExporter:
|
||||
logging.debug(f"Committing {flat}")
|
||||
self.commit_flat(flat, branch_state)
|
||||
|
||||
# make sure that we create devel branch
|
||||
if not branch_state["devel"]:
|
||||
logging.debug("force creating devel")
|
||||
self.git.set_branch_head("devel", self.git.branch_head("factory"))
|
||||
|
||||
self.git.push(force=True)
|
||||
|
||||
def run_gc(self):
|
||||
@ -138,6 +135,12 @@ class GitExporter:
|
||||
return True
|
||||
return flat.parent1 == branch_state[flat.branch]
|
||||
|
||||
def file_md5(self, file):
|
||||
m = md5()
|
||||
with open(file, 'rb') as f:
|
||||
m.update(f.read())
|
||||
return m.hexdigest()
|
||||
|
||||
def commit_flat(self, flat, branch_state):
|
||||
parents = []
|
||||
self.git.checkout(flat.branch)
|
||||
@ -155,13 +158,41 @@ class GitExporter:
|
||||
|
||||
# create file if not existant
|
||||
self.git.add_default_lfs_gitattributes(force=False)
|
||||
self.git.add_default_gitignore()
|
||||
|
||||
to_download, to_delete = flat.commit.calc_delta(branch_state[flat.branch])
|
||||
for file in to_delete:
|
||||
self.git.remove(file)
|
||||
for file, size, md5 in to_download:
|
||||
self.commit_file(flat, file, size, md5)
|
||||
new_files = flat.commit.files_list()
|
||||
cur_files = os.listdir(self.git.path)
|
||||
for cf in cur_files:
|
||||
if cf[0] == '.':
|
||||
continue
|
||||
found = False
|
||||
for nf in new_files:
|
||||
if nf['name'] == cf:
|
||||
found = True
|
||||
break
|
||||
|
||||
if found:
|
||||
# check if file is modified
|
||||
file_path = self.git.path.joinpath(cf)
|
||||
stat = file_path.stat()
|
||||
if stat.st_size != nf['size'] or self.file_md5(file_path) != nf['md5']:
|
||||
logging.debug(f"updating {file_path.name}")
|
||||
self.commit_file(flat, Path(cf), nf['size'], nf['md5'])
|
||||
else:
|
||||
logging.debug(f"leaving {file_path.name}")
|
||||
else:
|
||||
# file not exist in new commit
|
||||
self.git.remove(Path(cf))
|
||||
|
||||
|
||||
# new files?
|
||||
for file in new_files:
|
||||
found = False
|
||||
for cf in cur_files:
|
||||
if file['name'] == cf:
|
||||
found = True
|
||||
break
|
||||
if not found:
|
||||
self.commit_file(flat, Path(file['name']), file['size'], file['md5'])
|
||||
|
||||
commit = self.git.commit(
|
||||
flat.user.realname,
|
||||
|
@ -26,16 +26,13 @@ class Importer:
|
||||
# Import multiple Factory packages into the database
|
||||
self.packages = packages
|
||||
self.project = project
|
||||
self.scmsync_cache = dict()
|
||||
self.packages_with_scmsync = set()
|
||||
|
||||
self.db = DB()
|
||||
self.obs = OBS(api_url)
|
||||
assert not self.has_scmsync(project)
|
||||
assert project == "openSUSE:Factory"
|
||||
self.refreshed_packages = set()
|
||||
self.gone_packages_set = None
|
||||
|
||||
|
||||
def import_request(self, number):
|
||||
self.obs.request(number).import_into_db(self.db)
|
||||
|
||||
@ -216,10 +213,6 @@ class Importer:
|
||||
return
|
||||
logging.debug(f"Refresh {project}/{package}")
|
||||
self.refreshed_packages.add(key)
|
||||
if self.has_scmsync(project) or self.has_scmsync(key):
|
||||
self.packages_with_scmsync.add(package)
|
||||
logging.debug(f"{project}/{package} already in Git - skipping")
|
||||
return
|
||||
self.update_db_package(project, package)
|
||||
self.fetch_all_linked_packages(project, package)
|
||||
|
||||
@ -262,18 +255,3 @@ class Importer:
|
||||
for line in f.readlines():
|
||||
self.gone_packages_set.add(line.strip())
|
||||
return key in self.gone_packages_set
|
||||
|
||||
def has_scmsync(self, key):
|
||||
if key in self.scmsync_cache:
|
||||
return self.scmsync_cache[key]
|
||||
|
||||
root = self.obs._meta(key)
|
||||
scmsync_exists = False
|
||||
if root is not None:
|
||||
scmsync_exists = root.find('scmsync') is not None
|
||||
self.scmsync_cache[key] = scmsync_exists
|
||||
return scmsync_exists
|
||||
|
||||
def package_with_scmsync(self, package):
|
||||
return package in self.packages_with_scmsync
|
||||
|
||||
|
@ -83,7 +83,6 @@ class LFSOid:
|
||||
self.register()
|
||||
|
||||
def check(self):
|
||||
return True
|
||||
url = f"http://localhost:9999/check/{self.sha256}/{self.size}"
|
||||
response = requests.get(
|
||||
url,
|
||||
|
10
lib/obs.py
10
lib/obs.py
@ -73,11 +73,11 @@ class OBS:
|
||||
logging.debug(f"GET {url}")
|
||||
return ET.parse(osc.core.http_GET(url)).getroot()
|
||||
|
||||
def _meta(self, key, **params):
|
||||
def _meta(self, project, package, **params):
|
||||
try:
|
||||
root = self._xml(f"source/{key}/_meta", **params)
|
||||
root = self._xml(f"source/{project}/{package}/_meta", **params)
|
||||
except HTTPError:
|
||||
logging.error(f"Project/Package [{key} {params}] has no meta")
|
||||
logging.error(f"Package [{project}/{package} {params}] has no meta")
|
||||
return None
|
||||
return root
|
||||
|
||||
@ -118,13 +118,13 @@ class OBS:
|
||||
return root
|
||||
|
||||
def exists(self, project, package):
|
||||
root = self._meta(f"{project}/{package}")
|
||||
root = self._meta(project, package)
|
||||
if root is None:
|
||||
return False
|
||||
return root.get("project") == project
|
||||
|
||||
def devel_project(self, project, package):
|
||||
root = self._meta(f"{project}/{package}")
|
||||
root = self._meta(project, package)
|
||||
devel = root.find("devel")
|
||||
if devel is None:
|
||||
return None
|
||||
|
@ -2,58 +2,36 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
import pika
|
||||
import random
|
||||
import time
|
||||
import sys
|
||||
|
||||
MY_TASKS_DIR = Path(__file__).parent / "tasks"
|
||||
|
||||
connection = pika.BlockingConnection(pika.URLParameters("amqps://opensuse:opensuse@rabbit.opensuse.org"))
|
||||
channel = connection.channel()
|
||||
|
||||
def listen_events():
|
||||
connection = pika.BlockingConnection(
|
||||
pika.URLParameters("amqps://opensuse:opensuse@rabbit.opensuse.org")
|
||||
)
|
||||
channel = connection.channel()
|
||||
channel.exchange_declare(exchange='pubsub', exchange_type='topic', passive=True, durable=True)
|
||||
|
||||
channel.exchange_declare(
|
||||
exchange="pubsub", exchange_type="topic", passive=True, durable=False
|
||||
)
|
||||
result = channel.queue_declare("", exclusive=True)
|
||||
queue_name = result.method.queue
|
||||
|
||||
result = channel.queue_declare("", exclusive=True)
|
||||
queue_name = result.method.queue
|
||||
channel.queue_bind(exchange='pubsub',
|
||||
queue=queue_name,routing_key='#')
|
||||
|
||||
channel.queue_bind(
|
||||
exchange="pubsub", queue=queue_name, routing_key="opensuse.obs.package.commit"
|
||||
)
|
||||
print(' [*] Waiting for logs. To exit press CTRL+C')
|
||||
|
||||
print(" [*] Waiting for logs. To exit press CTRL+C")
|
||||
|
||||
def callback(ch, method, properties, body):
|
||||
if method.routing_key not in ("opensuse.obs.package.commit",):
|
||||
def callback(ch, method, properties, body):
|
||||
if method.routing_key not in ("opensuse.obs.package.commit",):
|
||||
return
|
||||
body = json.loads(body)
|
||||
if 'project' in body and 'package' in body and body['project'] == 'openSUSE:Factory':
|
||||
if '/' in body['package']:
|
||||
return
|
||||
body = json.loads(body)
|
||||
if (
|
||||
"project" in body
|
||||
and "package" in body
|
||||
and body["project"] == "openSUSE:Factory"
|
||||
):
|
||||
if "/" in body["package"]:
|
||||
return
|
||||
|
||||
(MY_TASKS_DIR / body["package"]).touch()
|
||||
print(" [x] %r:%r" % (method.routing_key, body["package"]))
|
||||
(MY_TASKS_DIR / body['package']).touch()
|
||||
print(" [x] %r:%r" % (method.routing_key, body['package']))
|
||||
|
||||
channel.basic_consume(queue_name, callback, auto_ack=True)
|
||||
channel.basic_consume(queue_name,
|
||||
callback,
|
||||
auto_ack=True)
|
||||
|
||||
channel.start_consuming()
|
||||
|
||||
|
||||
def main():
|
||||
while True:
|
||||
try:
|
||||
listen_events()
|
||||
except (pika.exceptions.ConnectionClosed, pika.exceptions.AMQPHeartbeatTimeout):
|
||||
time.sleep(random.randint(10, 100))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
channel.start_consuming()
|
||||
|
@ -1,19 +0,0 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
cd /space/dmueller/git-importer
|
||||
|
||||
source credentials.sh
|
||||
|
||||
while true; do
|
||||
for i in $PWD/tasks/*; do
|
||||
if test -f "$i"; then
|
||||
echo "$(date): Importing $(basename $i)"
|
||||
if ! python3 ./git-importer.py -c repos/.cache $(basename $i); then
|
||||
mkdir -p $PWD/failed-tasks
|
||||
mv -f $i $PWD/failed-tasks
|
||||
fi
|
||||
rm -f $i
|
||||
fi
|
||||
done
|
||||
inotifywait -q -e create $PWD/tasks
|
||||
done
|
Loading…
Reference in New Issue
Block a user