pkgsrc-WIP-changes archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

cvs2hg: convert from sh(1) to python



Module Name:	pkgsrc-wip
Committed By:	Thomas Klausner <wiz%gatalith.at@localhost>
Pushed By:	wiz
Date:		Wed Jul 12 23:32:14 2023 +0200
Changeset:	232b0a200468a23566770c76057b6850c4277113

Modified Files:
	cvs2hg/Makefile
	cvs2hg/files/cvs2hg
	cvs2hg/files/cvs2hg.1

Log Message:
cvs2hg: convert from sh(1) to python

Add --branchmap support.

To see a diff of this commit:
https://wip.pkgsrc.org/cgi-bin/gitweb.cgi?p=pkgsrc-wip.git;a=commitdiff;h=232b0a200468a23566770c76057b6850c4277113

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

diffstat:
 cvs2hg/Makefile       |   2 +
 cvs2hg/files/cvs2hg   | 110 +++++++++++++++++++++++++++++++-------------------
 cvs2hg/files/cvs2hg.1 |  14 +++++--
 3 files changed, 82 insertions(+), 44 deletions(-)

diffs:
diff --git a/cvs2hg/Makefile b/cvs2hg/Makefile
index 1330784fb0..d1dbd29760 100644
--- a/cvs2hg/Makefile
+++ b/cvs2hg/Makefile
@@ -17,6 +17,7 @@ USE_LANGUAGES=		# empty
 INSTALLATION_DIRS=	bin ${PKGMANDIR}/man1
 
 PYTHON_VERSIONS_INCOMPATIBLE=	27
+REPLACE_PYTHON+=		cvs2hg
 
 post-extract:
 	${CP} ${FILESDIR}/cvs2hg ${WRKSRC}
@@ -30,5 +31,6 @@ do-install:
 	${INSTALL_SCRIPT} ${WRKDIR}/cvs2hg ${DESTDIR}${PREFIX}/bin
 	${INSTALL_MAN} ${FILESDIR}/cvs2hg.1 ${DESTDIR}${PREFIX}/${PKGMANDIR}/man1
 
+.include "../../lang/python/application.mk"
 .include "../../lang/python/pyversion.mk"
 .include "../../mk/bsd.pkg.mk"
diff --git a/cvs2hg/files/cvs2hg b/cvs2hg/files/cvs2hg
old mode 100644
new mode 100755
index b131b56fc2..47c5b1f41e
--- a/cvs2hg/files/cvs2hg
+++ b/cvs2hg/files/cvs2hg
@@ -1,6 +1,6 @@
-#!/bin/sh
+#!/usr/bin/env python3
 #
-#	$NetBSD$
+# $NetBSD$
 #
 # Copyright (c) 2023 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -29,47 +29,75 @@
 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 # POSSIBILITY OF SUCH DAMAGE.
 #
-set -e
 
-USAGE="usage: $0 [-a authormap] [-f fixup-sql-script] source-cvs-rsync-path target-path"
-authormap=""
-fixup=""
-merge_limit_seconds=""
-while getopts a:f:s: f
-do
-    case $f in
-	a) authormap="$OPTARG"
-	   case "$authormap" in
-	       /*);;
-	       *) authormap="../$OPTARG";;
-	   esac;;
-	f) fixup="-f$OPTARG";;
-	s) merge_limit_seconds="-s$OPTARG";;
-        \?) echo "$USAGE" >&2; exit 1;;
-    esac
-done
-shift $((OPTIND - 1))
+import argparse
+import os
+from pathlib import Path
+import subprocess
 
-if [ "$#" != 2 ]
-then
-	echo "$USAGE" >&2
-	exit 1
-fi
 
-SRC="$1"
-BASE="$2"
-cvs2fossil -m $fixup $merge_limit_seconds "$SRC" "$BASE"
-# creates "$BASE" and "$BASE".fossil, but we don't need "$BASE"
-rm -f "$BASE"
+def file_path(path):
+    '''Check if argument is a valid file path.'''
+    if Path(path).is_file():
+        return str(Path(path).resolve())
+    raise argparse.ArgumentTypeError(f"{path} is not a valid file path")
+
+
+def dir_path(path):
+    '''Check if argument is a valid directory path.'''
+    if Path(path).is_dir():
+        return str(Path(path).resolve())
+    raise argparse.ArgumentTypeError(f"{path} is not a valid directory path")
+
+
+parser = argparse.ArgumentParser(description='Convert CVS repository ' +
+                                 'to Mercurial')
+parser.add_argument('-a', dest='authormap', type=file_path,
+                    help='use %(dest)s to map UNIX logins to names ' +
+                    'and email addresses')
+parser.add_argument('-b', dest='branchmap', type=file_path,
+                    help='use %(dest)s to rename branches')
+parser.add_argument('-f', dest='fixup_sql_script', type=file_path,
+                    help='run %(dest)s on fossil database to fix problems')
+parser.add_argument('-s', dest='merge_limit', type=int,
+                    help='pass %(dest)s to cvs2fossil ' +
+                    '(merge window time in seconds)')
+parser.add_argument('source', type=dir_path,
+                    help='source CVS repository master')
+# may not exist yet, can't use dir_path
+parser.add_argument('destination', type=str,
+                    help='base name of target Mercurial repository')
+args = parser.parse_args()
+
+run_args = ['cvs2fossil']
+if args.fixup_sql_script:
+    run_args += ['-f', args.fixup_sql_script]
+if args.merge_limit:
+    run_args += ['-s', str(args.merge_limit)]
+run_args += [args.source, args.destination]
+
+# convert to fossil using cvs2fossil
+subprocess.run(run_args, check=True)
+# cvs2fossil creates {args.destination} and {args.destination}.fossil,
+# but we don't need the former
+os.remove(args.destination)
 # export from fossil
-fossil1 export -R "$BASE".fossil > "$BASE".fossil.export
+with open(f'{args.destination}.fossil.export', 'wb') as output:
+    subprocess.run(['fossil1', 'export', '-R', f'{args.destination}.fossil'],
+                   stdout=output, check=True)
 # import to hg
-hg init "$BASE".hg
-hg -R "$BASE".hg --config extensions.fastimport= fastimport "$BASE".fossil.export
-# fix author names
-if [ -f "$authormap" ]
-then
-	hg convert --authormap="$authormap" "$BASE.hg" "$BASE.rewrite.hg"
-	mv "$BASE.hg" "$BASE.hg.old"
-	mv "$BASE.rewrite.hg" "$BASE.hg"
-fi
+subprocess.run(['hg', 'init', f'{args.destination}.hg'], check=True)
+subprocess.run(['hg', '-R', f'{args.destination}.hg', '--config',
+                'extensions.fastimport=', 'fastimport',
+                f'{args.destination}.fossil.export'], check=True)
+# fix author or branch names
+if args.authormap or args.branchmap:
+    run_args = ['hg', 'convert']
+    if args.authormap:
+        run_args.append(f'--authormap={args.authormap}')
+    if args.branchmap:
+        run_args.append(f'--branchmap={args.branchmap}')
+    run_args += [f'{args.destination}.hg', f'{args.destination}.rewrite.hg']
+    subprocess.run(run_args, check=True)
+    os.rename(f'{args.destination}.hg', f'{args.destination}.hg.old')
+    os.rename(f'{args.destination}.rewrite.hg', f'{args.destination}.hg')
diff --git a/cvs2hg/files/cvs2hg.1 b/cvs2hg/files/cvs2hg.1
index ccc77c8598..e5b724b6aa 100644
--- a/cvs2hg/files/cvs2hg.1
+++ b/cvs2hg/files/cvs2hg.1
@@ -35,7 +35,8 @@
 .Nd convert CVS repository to Mercurial
 .Sh SYNOPSIS
 .Nm
-.Op Fl a Ar mailmap
+.Op Fl a Ar authormap
+.Op Fl b Ar brachmap
 .Op Fl f Ar fixup-sql-script
 .Op Fl s Ar merge-limit-seconds
 .Ar source
@@ -75,13 +76,20 @@ file, if provided.
 .Nm
 supports the following flags:
 .Bl -tag -width 10n
-.It Fl a Ar mailmap
+.It Fl a Ar authormap
 Pass a
 .Xr hg 1
 authormap file for creating proper author information using
 .Xr hg 1 Cm convert .
 The file should consist of lines of the format:
 .Dl login=Full Name <email@address>
+.It Fl b Ar branchmap
+Pass a
+.Xr hg 1
+branchmap file for renaming branches using
+.Xr hg 1 Cm convert .
+The file should consist of lines of the format:
+.Dl old_branch_name new_branch_name
 .It Fl f Ar fixup-sql-script
 Pass an SQL fixup script for
 .Xr cvs2fossil 1
@@ -100,7 +108,7 @@ src repository, if you have developer access, do:
 export REPO=src
 export LOGIN=username
 rsync -aS --delete -e ssh "$LOGIN"@cvs.NetBSD.org::cvsroot/"$REPO" "$REPO"-rsync
-cvs2hg -a authormap "$REPO"-rsync/"$REPO" "$REPO"
+cvs2hg -a authormap -b branchmap -s 300 "$REPO"-rsync/"$REPO" "$REPO"
 .Ed
 .Pp
 The conversion output is in


Home | Main Index | Thread Index | Old Index