From bfe8fe85e86fbe01ef29cebecbcc9d646e171724 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Wed, 20 Jul 2022 14:47:24 +0200 Subject: First dummy exporter --- osc_fast_export.py | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100755 osc_fast_export.py (limited to 'osc_fast_export.py') diff --git a/osc_fast_export.py b/osc_fast_export.py new file mode 100755 index 0000000..3b7c727 --- /dev/null +++ b/osc_fast_export.py @@ -0,0 +1,84 @@ +#!/usr/bin/python +# https://git-scm.com/book/en/v2/Git-and-Other-Systems-Migrating-to-Git#_custom_importer + +import collections +import configparser +from datetime import datetime +import subprocess + +import xml.etree.ElementTree as ET + +authorsfile = "authorsfile.txt" + +# For reading section-less config files +# https://stackoverflow.com/a/2819788/164233 +def FakeSecHead(fp): + yield "[asection]\n" + yield from fp + + +config = configparser.ConfigParser() +config.read_file(FakeSecHead(open(authorsfile))) +authors = dict(config.items("asection")) + + +class LogEntry( + collections.namedtuple("LogEntry", ["rev", "md5", "author", "date", "msg"]) +): + def __str__(self): + return ( + f"{self.rev}, {self.md5[:12]}, {authors.get(self.author, '')}," + + f" {datetime.isoformat(self.date)}:\n{self.msg}" + ) + + +def osc_log(): + try: + log_str = subprocess.run( + ["osc", "log", "--xml"], check=True, text=True, stdout=subprocess.PIPE + ).stdout + except subprocess.CalledProcessError: + raise + tree = ET.fromstring(log_str) + log_list = [ + LogEntry( + int(entry.attrib["revision"]), + entry.attrib["srcmd5"], + entry.findtext("author"), + datetime.strptime(entry.findtext("date"), "%Y-%m-%d %H:%M:%S"), + entry.findtext("msg"), + ) + for entry in tree.iter("logentry") + ] + log_list.reverse() + return log_list + + +def export_data(dt): + return f"data {len(dt)}\n{dt}" + + +def print_export(entry): + mark = entry.rev + author = authors.get(entry.author, "") + date = int(datetime.timestamp(entry.date)) + + print("commit refs/heads/master") + print(f"mark :{mark}") + print(f"committer {author} {date} +0000") + print(export_data(entry.msg)) + if last_mark: + print(f"from :{last_mark}") + print("deleteall") + print("M 644 inline tralala") + print(export_data("tralalala")) + + return mark + + +if __name__ == "__main__": + last_mark = None + + for logentry in osc_log(): + last_mark = print_export(logentry) + print("done") -- cgit