[projects/pld-ftp-admin] pfa-{mvpkg,testmvpkg,checktree}: use argparse, add --help
arekm
arekm at pld-linux.org
Sat Oct 24 11:46:54 CEST 2026
commit dc1d8ff838102fa3faa93636ae4b73ccbff41a8e
Author: Arkadiusz Miśkiewicz <arekm at maven.pl>
Date: Tue Oct 20 15:45:18 2026 +0200
pfa-{mvpkg,testmvpkg,checktree}: use argparse, add --help
Old options stay; usage errors exit with 2.
bin/pfa-checktree | 50 ++++++++++++++++--------------
bin/pfa-mvpkg | 81 +++++++++++++++++++++++++-----------------------
bin/pfa-testmvpkg | 64 +++++++++++++++++++++-----------------
shell/bash-completion.sh | 7 +++--
4 files changed, 110 insertions(+), 92 deletions(-)
---
diff --git a/bin/pfa-checktree b/bin/pfa-checktree
index c3fcd77..fe03cc7 100755
--- a/bin/pfa-checktree
+++ b/bin/pfa-checktree
@@ -1,46 +1,50 @@
#!/usr/bin/env python3
# vi: encoding=utf-8 ts=8 sts=4 sw=4 et
-from __future__ import print_function
+import argparse
+import curses
+import os
+import sys
+
+parser = argparse.ArgumentParser(
+ description='Browse the packages of an ftp tree and the problems moving them to another tree would cause.')
+parser.add_argument('srctree', help='ftp tree to move from, e.g. test')
+parser.add_argument('dsttree', help='ftp tree to move to, e.g. ready')
+args = parser.parse_args()
-import sys, os
+# config reads ~/.ftpadmrc on import; parse options first so --help works without it
sys.path.insert(0, os.environ['HOME']+'/pld-ftp-admin/modules')
+import ftpio
import ftptree
-ftptree.quietmode=True
from common import checkdir
-import ftpio
-import config
-cval=config.value
-import curses
+ftptree.quietmode=True
-if len(sys.argv) != 3:
- print("ERR: wrong number of arguments")
- print("check-tree.py srctree dsttree")
- sys.exit(1)
+srcname = args.srctree
+dstname = args.dsttree
-checkdir(sys.argv[1])
-checkdir(sys.argv[2])
+checkdir(srcname)
+checkdir(dstname)
ftpio.connect('check-tree')
-if not ftpio.lock(sys.argv[1], True):
- print("ERR: %s tree already locked" % sys.argv[1])
+if not ftpio.lock(srcname, True):
+ print("ERR: %s tree already locked" % srcname)
sys.exit(1)
-if not ftpio.lock(sys.argv[2], True):
- ftpio.unlock(sys.argv[1])
- print("ERR: %s tree already locked" % sys.argv[2])
+if not ftpio.lock(dstname, True):
+ ftpio.unlock(srcname)
+ print("ERR: %s tree already locked" % dstname)
sys.exit(1)
-srctree=ftptree.FtpTree(sys.argv[1], loadall=True)
-dsttree=ftptree.FtpTree(sys.argv[2])
+srctree=ftptree.FtpTree(srcname, loadall=True)
+dsttree=ftptree.FtpTree(dstname)
srctree.checktree(dsttree)
# We've loaded all interesting info, so we can continue without locks
-ftpio.unlock(sys.argv[1])
-ftpio.unlock(sys.argv[2])
+ftpio.unlock(srcname)
+ftpio.unlock(dstname)
# The list(set(list)) cast is a fancy way to remove dups from a list
names=sorted(list(set([pkg.name for pkg in srctree.values()])))
@@ -299,5 +303,5 @@ class Screen:
if key == getattr(curses, bind[0]):
getattr(self, '_Screen__do_%s' % bind[1])()
-s = Screen(sys.argv[1], sys.argv[2], pkglist)
+s = Screen(srcname, dstname, pkglist)
diff --git a/bin/pfa-mvpkg b/bin/pfa-mvpkg
index b849eb2..fc295b3 100755
--- a/bin/pfa-mvpkg
+++ b/bin/pfa-mvpkg
@@ -1,80 +1,83 @@
#!/usr/bin/env python3
# vi: encoding=utf-8 ts=8 sts=4 sw=4 et
-from __future__ import print_function
-
-import sys, os
+import argparse
+import os
+import sys
+
+parser = argparse.ArgumentParser(description='Move packages between ftp trees and mail a notification.')
+parser.add_argument('-nb', dest='nocheckbuild', action='store_true',
+ help='do not check if builds are finished')
+parser.add_argument('srctree', help='ftp tree to move from, e.g. test')
+parser.add_argument('dsttree', help='ftp tree to move to, e.g. ready')
+parser.add_argument('packages', nargs='+', metavar='package',
+ help='package to move, e.g. foo-1.0-1')
+args = parser.parse_intermixed_args()
+
+# config reads ~/.ftpadmrc on import; parse options first so --help works without it
sys.path.insert(0, os.environ['HOME']+'/pld-ftp-admin/modules')
+import ftpio
import ftptree
from common import checkdir
-import ftpio
-from mailer import Message
from config import archived_trees, logs_list
+from mailer import Message
os.umask(0o022)
-nocheckbuild = False
-if len(sys.argv) > 4 and sys.argv[1] == '-nb':
- nocheckbuild = True
- sys.argv = sys.argv[1:]
-
-if len(sys.argv) < 4:
- print("ERR: not enough parameters given", file=sys.stderr)
- print("move.py [options] src-tree dst-tree package [package2 package3 ...]", file=sys.stderr)
- print("\nOptions:", file=sys.stderr)
- print(" -nb Do not check if builds are finished.\n", file=sys.stderr)
- sys.exit(1)
+nocheckbuild = args.nocheckbuild
+srcname = args.srctree
+dstname = args.dsttree
-checkdir(sys.argv[1])
-checkdir(sys.argv[2])
+checkdir(srcname)
+checkdir(dstname)
-if sys.argv[2] in archived_trees:
- archivetreename = ".archive/" + sys.argv[2]
+if dstname in archived_trees:
+ archivetreename = ".archive/" + dstname
checkdir(archivetreename)
else:
archivetreename = None
ftpio.connect('move')
-if not ftpio.lock(sys.argv[1], True):
- print("ERR: %s tree already locked" % sys.argv[1], file=sys.stderr)
+if not ftpio.lock(srcname, True):
+ print("ERR: %s tree already locked" % srcname, file=sys.stderr)
sys.exit(1)
-if not ftpio.lock(sys.argv[2], True):
- ftpio.unlock(sys.argv[1])
- print("ERR: %s tree already locked" % sys.argv[2], file=sys.stderr)
+if not ftpio.lock(dstname, True):
+ ftpio.unlock(srcname)
+ print("ERR: %s tree already locked" % dstname, file=sys.stderr)
sys.exit(1)
-if archivetreename != None and archivetreename != sys.argv[1] and not ftpio.lock(archivetreename, True):
- ftpio.unlock(sys.argv[2])
- ftpio.unlock(sys.argv[1])
+if archivetreename != None and archivetreename != srcname and not ftpio.lock(archivetreename, True):
+ ftpio.unlock(dstname)
+ ftpio.unlock(srcname)
print("ERR: %s tree already locked" % archivetreename, file=sys.stderr)
sys.exit(1)
try:
- srctree = ftptree.FtpTree(sys.argv[1], loadall = True)
- dsttree = ftptree.FtpTree(sys.argv[2])
+ srctree = ftptree.FtpTree(srcname, loadall = True)
+ dsttree = ftptree.FtpTree(dstname)
if archivetreename != None:
archivetree = ftptree.FtpTree(archivetreename)
else:
archivetree = None
if nocheckbuild:
srctree.do_checkbuild = False
- pkgs = list(set(sys.argv[3:]))
+ pkgs = list(set(args.packages))
srctree.mark4moving(pkgs)
srctree.movepkgs(dsttree, archivetree = archivetree)
except ftptree.SomeError:
# In case of problems we need to unlock the trees before exiting
- ftpio.unlock(sys.argv[1])
- ftpio.unlock(sys.argv[2])
+ ftpio.unlock(srcname)
+ ftpio.unlock(dstname)
if archivetreename != None:
ftpio.unlock(archivetreename)
sys.exit(1)
-ftpio.unlock(sys.argv[1])
-ftpio.unlock(sys.argv[2])
-if archivetreename != None and archivetreename != sys.argv[1]:
+ftpio.unlock(srcname)
+ftpio.unlock(dstname)
+if archivetreename != None and archivetreename != srcname:
ftpio.unlock(archivetreename)
if logs_list == None:
@@ -99,7 +102,7 @@ except KeyError as e:
m = Message()
m.set_headers(
to = logs_list,
- subject = 'MOVED: %s => %s... %d packages' % (sys.argv[1], sys.argv[2], len(pkgs))
+ subject = 'MOVED: %s => %s... %d packages' % (srcname, dstname, len(pkgs))
)
m.write(
@@ -108,8 +111,8 @@ m.write(
'---- Package name ---- Package built by:\n\n'
) % {
'ftpadmin' : ftpadmin,
- 'srctree' : sys.argv[1],
- 'dsttree' : sys.argv[2],
+ 'srctree' : srcname,
+ 'dsttree' : dstname,
'count' : len(pkgs),
}
)
diff --git a/bin/pfa-testmvpkg b/bin/pfa-testmvpkg
index 66bb3ee..6020f33 100755
--- a/bin/pfa-testmvpkg
+++ b/bin/pfa-testmvpkg
@@ -1,59 +1,67 @@
#!/usr/bin/env python3
# vi: encoding=utf-8 ts=8 sts=4 sw=4 et
-from __future__ import print_function
+import argparse
+import os
+import sys
-import sys, os
+parser = argparse.ArgumentParser(
+ description='Check whether packages can be moved between ftp trees, without moving them.')
+parser.add_argument('srctree', help='ftp tree to move from, e.g. test')
+parser.add_argument('dsttree', help='ftp tree to move to, e.g. ready')
+parser.add_argument('packages', nargs='+', metavar='package',
+ help='package to check, e.g. foo-1.0-1')
+args = parser.parse_args()
+
+# config reads ~/.ftpadmrc on import; parse options first so --help works without it
sys.path.insert(0, os.environ['HOME']+'/pld-ftp-admin/modules')
+import ftpio
import ftptree
from common import checkdir
-import ftpio
-from config import archived_trees, logs_list
+from config import archived_trees
-if len(sys.argv) < 4:
- print("ERR: not enough parameters given")
- print("test-move.py src-tree dst-tree package [package2 package3 ...]")
- sys.exit(1)
+srcname = args.srctree
+dstname = args.dsttree
-checkdir(sys.argv[1])
-checkdir(sys.argv[2])
+checkdir(srcname)
+checkdir(dstname)
-if sys.argv[2] in archived_trees:
- archivetreename = ".archive/" + sys.argv[2]
+if dstname in archived_trees:
+ archivetreename = ".archive/" + dstname
checkdir(archivetreename)
else:
archivetreename = None
ftpio.connect('test-move')
-if not ftpio.lock(sys.argv[1], True):
- print("ERR: %s tree already locked" % sys.argv[1])
+if not ftpio.lock(srcname, True):
+ print("ERR: %s tree already locked" % srcname)
sys.exit(1)
-if not ftpio.lock(sys.argv[2], True):
- ftpio.unlock(sys.argv[1])
- print("ERR: %s tree already locked" % sys.argv[2])
+if not ftpio.lock(dstname, True):
+ ftpio.unlock(srcname)
+ print("ERR: %s tree already locked" % dstname)
sys.exit(1)
-if archivetreename != None and archivetreename != sys.argv[1] and not ftpio.lock(archivetreename, True):
- ftpio.unlock(sys.argv[2])
- ftpio.unlock(sys.argv[1])
+if archivetreename != None and archivetreename != srcname and not ftpio.lock(archivetreename, True):
+ ftpio.unlock(dstname)
+ ftpio.unlock(srcname)
print("ERR: %s tree already locked" % archivetreename)
sys.exit(1)
try:
- srctree = ftptree.FtpTree(sys.argv[1], loadall = True)
- dsttree = ftptree.FtpTree(sys.argv[2])
+ srctree = ftptree.FtpTree(srcname, loadall = True)
+ dsttree = ftptree.FtpTree(dstname)
if archivetreename != None:
archivetree = ftptree.FtpTree(archivetreename)
else:
archivetree = None
- pkgs = list(set(sys.argv[3:]))
+ pkgs = list(set(args.packages))
srctree.mark4moving(pkgs)
except ftptree.SomeError:
# In case of problems we need to unlock the trees before exiting
- ftpio.unlock(sys.argv[1])
- ftpio.unlock(sys.argv[2])
+ ftpio.unlock(srcname)
+ ftpio.unlock(dstname)
if archivetreename != None:
ftpio.unlock(archivetreename)
sys.exit(1)
@@ -61,7 +69,7 @@ except ftptree.SomeError:
# We don't 'try' as in move.py cause this function doesn't force exit
srctree.testmove(dsttree, archivetree = archivetree)
-ftpio.unlock(sys.argv[1])
-ftpio.unlock(sys.argv[2])
-if archivetreename != None and archivetreename != sys.argv[1]:
+ftpio.unlock(srcname)
+ftpio.unlock(dstname)
+if archivetreename != None and archivetreename != srcname:
ftpio.unlock(archivetreename)
diff --git a/shell/bash-completion.sh b/shell/bash-completion.sh
index 5bf70da..0af06d4 100644
--- a/shell/bash-completion.sh
+++ b/shell/bash-completion.sh
@@ -110,7 +110,7 @@ complete -F _pfa-signpkg pfa-signpkg pfa-rmpkg
have pfa-mvpkg &&
_pfa-mvpkg()
{
- local cur arg
+ local cur arg opts='-h --help'
COMPREPLY=()
_get_comp_words_by_ref cur
@@ -118,7 +118,10 @@ _pfa-mvpkg()
# The first two arguments are tree names; the rest are files in a dir
_count_args :
- if [[ $args == 1 ]] || [[ $args == 2 ]]; then
+ if [[ "$cur" == -* ]]; then
+ [[ ${1##*/} == pfa-mvpkg ]] && opts+=' -nb'
+ COMPREPLY=( $( compgen -W "$opts" -- "$cur" ) )
+ elif [[ $args == 1 ]] || [[ $args == 2 ]]; then
_pfa_tree "$cur"
else
_get_first_arg
================================================================
---- gitweb:
http://git.pld-linux.org/gitweb.cgi/projects/pld-ftp-admin.git/commitdiff/e14cb7d4d74f8bdd9368d9645ecaf49593cf6335
More information about the pld-cvs-commit
mailing list