Read about our upcoming Code of Conduct on this issue

Commit bee38da7 authored by Georges Racinet's avatar Georges Racinet 🦑
Browse files

packaging: made a proper Python project with console scripts

This will allow sharing basic utilities, like branch name
parsing and the like, and allow for proper testing

On the conversion machine, we will install the console scripts
with `pip install --user -e`. This is what the Makefile defaults to,
and it leads to convert-pull-local, the first migrated script to
lie in `~/.local/bin`.

The source and destination paths are now interpreted relative
to the current working directory, which is more natural for
scripts that are installed in such a dedicated directory.
parent 52b6d33b0368
......@@ -4,5 +4,5 @@
# for testing of py-heptapod and HGitaly
image: registry.heptapod.net/heptapod/ci-images/fixed-pydeps
script:
- flake8 *.py
- pytest --doctest-modules fetch-watch-tags.py
- flake8 *.py heptapod_upstream_conversion
- pytest --doctest-modules fetch-watch-tags.py heptapod_upstream_conversion
......@@ -17,6 +17,7 @@
gitlab-runner/
gitlab-shell/
gitlab-workhorse/
*.egg-info/
# homedir files and directories
.bash*
......
PYTHON := python3
PIP_CMD := install --user -e
all: help
help:
@echo 'Available commands'
@echo ' install - install console scripts with `pip --user`'
@echo ''
@echo 'You may customize the target with the PYTHON and PIP_INSTALL '
@echo 'variables. Current values:'
@echo ' PYTHON: ${PYTHON}'
@echo ' PIP_CMD: ${PIP_CMD}'
install:
${PYTHON} -m pip ${PIP_CMD} .
convert-pull-local.py
\ No newline at end of file
#!/bin/sh
echo "This script has been replaced by a setuptools console script. "
echo "Please run ~/.local/bin/convert-local instead. "
echo "If not installed, run 'make help'"
\ No newline at end of file
#!/usr/bin/env python3
# Copyright 2020-2021 Georges Racinet <georges.racinet@octobus.net>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
#
# SPDX-License-Identifier: GPL-2.0-or-later
import argparse
import sys
......@@ -14,20 +19,7 @@
for segment in brname[:-len(STABLE_BRANCH_SUFFIX)].split('-'))
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
epilog=""
"Examples: `{self} gitlab-ce heptapod-rails` (the default)\n"
" `{self} --branch-prefix 13-9-stable`".format(self=sys.argv[0])
)
parser.add_argument("--heptapod-repo", default='heptapod-rails')
parser.add_argument("--gitlab-repo", default='gitlab-ce')
parser.add_argument("--branch-prefix")
cl_args = parser.parse_args()
def is_branch_to_pull(branch_name):
def is_branch_to_pull(cl_args, branch_name):
if not branch_name.endswith(STABLE_BRANCH_SUFFIX):
return False
prefix = cl_args.branch_prefix
......@@ -36,5 +28,18 @@
return True
heptapod_repo, gitlab_repo = cl_args.heptapod_repo, cl_args.gitlab_repo
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description="Perform the Git to Mercurial conversion, by execution "
"of a local `hg pull`. All paths are relative to the current working "
"directory",
epilog=""
"Examples: `{self} gitlab-ce heptapod-rails` (the default)\n"
" `{self} --branch-prefix 13-9-stable`".format(
self=sys.argv[0])
)
parser.add_argument("--heptapod-repo", default='heptapod-rails')
parser.add_argument("--gitlab-repo", default='gitlab-ce')
parser.add_argument("--branch-prefix")
......@@ -40,9 +45,7 @@
git_branches = check_output(('git', '-C', gitlab_repo, 'branch')
).decode().splitlines()
git_branches = [b.strip() for b in git_branches]
stable_branches = [(extract_stable_branch_version(br), br)
for br in git_branches if is_branch_to_pull(br)]
stable_branches.sort()
print("Sorted stable branches: %r" % [b[1] for b in stable_branches])
cl_args = parser.parse_args()
heptapod_repo, gitlab_repo = cl_args.heptapod_repo, cl_args.gitlab_repo
print("Conversion of Git commits from %r "
"in Mercurial changesets in %r" % (gitlab_repo, heptapod_repo))
......@@ -48,15 +51,24 @@
for version, branch in stable_branches:
print("Pulling Git branch %r -- checking prior conversion" % branch)
log = Popen(('hg', 'log', '-R', heptapod_repo,
'-r', "bookmark(%s)" % branch),
stderr=PIPE)
log.communicate()
if log.poll() == 0:
print(" already converted, pull should be small")
else:
print(" full branch conversion, sleeping first for 10s for "
"easy interruption")
time.sleep(10)
check_call(('hg', 'pull', '-R', heptapod_repo,
'-r', branch, gitlab_repo))
git_branches = check_output(('git', '-C', gitlab_repo, 'branch')
).decode().splitlines()
git_branches = [b.strip() for b in git_branches]
stable_branches = [(extract_stable_branch_version(br), br)
for br in git_branches
if is_branch_to_pull(cl_args, br)]
stable_branches.sort()
print("Sorted stable branches: %r" % [b[1] for b in stable_branches])
for version, branch in stable_branches:
print("Pulling Git branch %r -- checking prior conversion" % branch)
log = Popen(('hg', 'log', '-R', heptapod_repo,
'-r', "bookmark(%s)" % branch),
stderr=PIPE)
log.communicate()
if log.poll() == 0:
print(" already converted, pull should be small")
else:
print(" full branch conversion, sleeping first for 10s for "
"easy interruption")
time.sleep(10)
check_call(('hg', 'pull', '-R', heptapod_repo,
'-r', branch, gitlab_repo))
from setuptools import setup
MAIN_PACKAGE = 'heptapod_upstream_conversion'
scripts = { # name to (module in main package, function)
'convert-pull-local': ('convert', 'main'),
}
setup(
name='heptapod_upstream_conversion',
version='0.1.0dev0',
author='Georges Racinet',
author_email='georges.racinet@octobus.net',
url='https://foss.heptapod.net/heptapod/upstream-conversion',
description="Utilities for conversion of upstream GitLab Git "
"repositories into Heptapod Mercurial repositories",
long_description=open('README.md').read(),
long_description_content_type="text/markdown",
keywords='hg mercurial git heptapod gitlab',
license='GPLv2+',
# do not use find_packages, as it could recurse into the Git and
# Mercurial repositories
packages=[MAIN_PACKAGE],
entry_points=dict(
console_scripts=[
'{name}={pkg}.{mod}:{fun}'.format(
pkg=MAIN_PACKAGE, name=name, mod=mod, fun=fun)
for name, (mod, fun) in scripts.items()],
),
install_requires=()
)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment