[elbe-devel] [PATCH 2/2] elbepack: db: move repo actions out of dbaction.py

Thomas Weißschuh thomas.weissschuh at linutronix.de
Thu Jul 25 14:25:41 CEST 2024


The db actions are highly specific to the db subcommand.
There is no reason to keep them somewhere else.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 debian/python3-elbe-buildenv.install |   1 -
 elbepack/commands/db.py              | 196 +++++++++++++++++++++++++++++++++-
 elbepack/dbaction.py                 | 197 -----------------------------------
 3 files changed, 193 insertions(+), 201 deletions(-)

diff --git a/debian/python3-elbe-buildenv.install b/debian/python3-elbe-buildenv.install
index 43a86242b034..e432483ed29a 100644
--- a/debian/python3-elbe-buildenv.install
+++ b/debian/python3-elbe-buildenv.install
@@ -5,7 +5,6 @@ usr/lib/python3.*/*-packages/elbepack/commands/gen_update.py
 usr/lib/python3.*/*-packages/elbepack/makofiles/environment-setup-elbe.mako
 usr/lib/python3.*/*-packages/elbepack/makofiles/toolchain-shar-extract.sh.mako
 usr/lib/python3.*/*-packages/elbepack/db.py
-usr/lib/python3.*/*-packages/elbepack/dbaction.py
 usr/lib/python3.*/*-packages/elbepack/dump.py
 usr/lib/python3.*/*-packages/elbepack/ziparchives.py
 usr/lib/python3.*/*-packages/elbepack/repomanager.py
diff --git a/elbepack/commands/db.py b/elbepack/commands/db.py
index cfc2ea4cb699..403e27962627 100644
--- a/elbepack/commands/db.py
+++ b/elbepack/commands/db.py
@@ -3,16 +3,206 @@
 # SPDX-FileCopyrightText: 2014, 2017 Linutronix GmbH
 
 import argparse
+import sys
+from getpass import getpass
+from shutil import copyfileobj
 
-from elbepack.cli import add_arguments_from_decorated_function
-from elbepack.dbaction import db_actions
+from elbepack.cli import add_argument, add_arguments_from_decorated_function
+from elbepack.db import ElbeDB, ElbeDBError
+
+
+ at add_argument('--name', default='root')
+ at add_argument('--fullname', default='Admin')
+ at add_argument('--password', default='foo')
+ at add_argument('--email', default='root at localhost')
+ at add_argument('--noadmin', dest='admin', default=True, action='store_false')
+def _init(self, args):
+    ElbeDB.init_db(args.name, args.fullname, args.password,
+                   args.email, args.admin)
+
+
+ at add_argument('--fullname')
+ at add_argument('--password')
+ at add_argument('--email')
+ at add_argument('--admin', default=False, action='store_true')
+ at add_argument('username')
+def _add_user(self, args):
+    if not args.password:
+        password = getpass('Password for the new user: ')
+    else:
+        password = args.password
+
+    db = ElbeDB()
+    db.add_user(args.username, args.fullname, password, args.email, args.admin)
+
+
+ at add_argument('--delete-projects', dest='delete_projects',
+              default=False, action='store_true')
+ at add_argument('--quiet', dest='quiet',
+              default=False, action='store_true')
+ at add_argument('userid', type=int)
+def _del_user(args):
+    db = ElbeDB()
+
+    projects = db.del_user(args.userid)
+
+    if projects:
+        if not args.opt.quiet:
+            if args.opt.delete_projects:
+                print('removing projects owned by the deleted user:')
+            else:
+                print('keeping projects owned by the deleted user:')
+
+    for p in projects:
+        if not args.opt.quiet:
+            print(f'{p.builddir}: {p.name} [{p.version}] {p.edit}')
+        if args.opt.delete_projects:
+            try:
+                db.del_project(p.builddir)
+            except ElbeDBError as e:
+                print(f'  ==> {e} ')
+
+
+def _list_projects(args):
+    db = ElbeDB()
+    projects = db.list_projects()
+
+    for p in projects:
+        print(f'{p.builddir}: {p.name} [{p.version}] {p.edit}')
+
+
+def _list_users(args):
+    db = ElbeDB()
+    users = db.list_users()
+
+    for u in users:
+        print(f'{u.name}: {u.fullname} <{u.email}>')
+
+
+ at add_argument('--user', dest='user',
+              help='user name of the designated project owner')
+ at add_argument('project_dir')
+def _create_project(args):
+    db = ElbeDB()
+    owner_id = db.get_user_id(args.user)
+    db.create_project(args.project_dir, owner_id)
+
+
+ at add_argument('project_dir')
+def _del_project(args):
+    db = ElbeDB()
+    db.del_project(args.project_dir)
+
+
+ at add_argument('project_dir')
+ at add_argument('xml')
+def _set_xml(args):
+    db = ElbeDB()
+    db.set_xml(args.project_dir, args.xml)
+
+
+ at add_argument('project_dir')
+def _build(args):
+    db = ElbeDB()
+    db.set_busy(args.project_dir, ['empty_project', 'needs_build', 'has_changes',
+                                   'build_done', 'build_failed'])
+    try:
+        ep = db.load_project(args.project_dir)
+        ep.build()
+        db.update_project_files(ep)
+    except Exception as e:
+        db.update_project_files(ep)
+        db.reset_busy(args.project_dir, 'build_failed')
+        print(str(e))
+        return
+    db.reset_busy(args.project_dir, 'build_done')
+
+
+ at add_argument('project_dir')
+def _get_files(args):
+    db = ElbeDB()
+    files = db.get_project_files(args.project_dir)
+    for f in files:
+        if f.description:
+            print(f'{f.name:40}  {f.description}')
+        else:
+            print(f.name)
+
+
+ at add_argument('--clean', dest='clean', default=False, action='store_true')
+ at add_argument('project_dir')
+def _reset_project(args):
+    db = ElbeDB()
+    db.reset_project(args.project_dir, args.clean)
+
+
+ at add_argument('project_dir')
+ at add_argument('version')
+def _set_project_version(args):
+    db = ElbeDB()
+    db.set_project_version(args.project_dir, args.version)
+
+
+ at add_argument('project_dir')
+def _list_versions(args):
+    db = ElbeDB()
+    versions = db.list_project_versions(args.project_dir)
+
+    for v in versions:
+        if v.description:
+            print(f'{v.version}: {v.description}')
+        else:
+            print(v.version)
+
+
+ at add_argument('--description', dest='description')
+ at add_argument('project_dir')
+def _save_version(args):
+    db = ElbeDB()
+    db.save_version(args.project_dir, args.description)
+
+
+ at add_argument('project_dir')
+ at add_argument('version')
+def _del_version(args):
+    db = ElbeDB()
+    db.del_version(args.project_dir, args.version)
+
+
+ at add_argument('project_dir')
+ at add_argument('version')
+def _print_version_xml(args):
+    db = ElbeDB()
+    filename = db.get_version_xml(args.project_dir, args.version)
+    with open(filename) as f:
+        copyfileobj(f, sys.stdout)
+
+
+_actions = {
+    'init':                _init,
+    'add_user':            _add_user,
+    'del_user':            _del_user,
+    'list_projects':       _list_projects,
+    'list_users':          _list_users,
+    'create_project':      _create_project,
+    'del_project':         _del_project,
+    'set_xml':             _set_xml,
+    'build':               _build,
+    'get_files':           _get_files,
+    'reset_project':       _reset_project,
+    'set_project_version': _set_project_version,
+    'list_versions':       _list_versions,
+    'save_version':        _save_version,
+    'del_versions':        _del_version,
+    'print_version_xml':   _print_version_xml,
+}
 
 
 def run_command(argv):
     aparser = argparse.ArgumentParser(prog='elbe db')
     subparsers = aparser.add_subparsers(required=True)
 
-    for action_name, do_action in db_actions.items():
+    for action_name, do_action in _actions.items():
         action_parser = subparsers.add_parser(action_name)
         action_parser.set_defaults(func=do_action)
         add_arguments_from_decorated_function(action_parser, do_action)
diff --git a/elbepack/dbaction.py b/elbepack/dbaction.py
deleted file mode 100644
index 0eeef8331c54..000000000000
--- a/elbepack/dbaction.py
+++ /dev/null
@@ -1,197 +0,0 @@
-# ELBE - Debian Based Embedded Rootfilesystem Builder
-# SPDX-License-Identifier: GPL-3.0-or-later
-# SPDX-FileCopyrightText: 2014-2017 Linutronix GmbH
-
-import sys
-from getpass import getpass
-from shutil import copyfileobj
-
-from elbepack.cli import add_argument
-from elbepack.db import ElbeDB, ElbeDBError
-
-
- at add_argument('--name', default='root')
- at add_argument('--fullname', default='Admin')
- at add_argument('--password', default='foo')
- at add_argument('--email', default='root at localhost')
- at add_argument('--noadmin', dest='admin', default=True, action='store_false')
-def _init(self, args):
-    ElbeDB.init_db(args.name, args.fullname, args.password,
-                   args.email, args.admin)
-
-
- at add_argument('--fullname')
- at add_argument('--password')
- at add_argument('--email')
- at add_argument('--admin', default=False, action='store_true')
- at add_argument('username')
-def _add_user(self, args):
-    if not args.password:
-        password = getpass('Password for the new user: ')
-    else:
-        password = args.password
-
-    db = ElbeDB()
-    db.add_user(args.username, args.fullname, password, args.email, args.admin)
-
-
- at add_argument('--delete-projects', dest='delete_projects',
-              default=False, action='store_true')
- at add_argument('--quiet', dest='quiet',
-              default=False, action='store_true')
- at add_argument('userid', type=int)
-def _del_user(args):
-    db = ElbeDB()
-
-    projects = db.del_user(args.userid)
-
-    if projects:
-        if not args.opt.quiet:
-            if args.opt.delete_projects:
-                print('removing projects owned by the deleted user:')
-            else:
-                print('keeping projects owned by the deleted user:')
-
-    for p in projects:
-        if not args.opt.quiet:
-            print(f'{p.builddir}: {p.name} [{p.version}] {p.edit}')
-        if args.opt.delete_projects:
-            try:
-                db.del_project(p.builddir)
-            except ElbeDBError as e:
-                print(f'  ==> {e} ')
-
-
-def _list_projects(args):
-    db = ElbeDB()
-    projects = db.list_projects()
-
-    for p in projects:
-        print(f'{p.builddir}: {p.name} [{p.version}] {p.edit}')
-
-
-def _list_users(args):
-    db = ElbeDB()
-    users = db.list_users()
-
-    for u in users:
-        print(f'{u.name}: {u.fullname} <{u.email}>')
-
-
- at add_argument('--user', dest='user',
-              help='user name of the designated project owner')
- at add_argument('project_dir')
-def _create_project(args):
-    db = ElbeDB()
-    owner_id = db.get_user_id(args.user)
-    db.create_project(args.project_dir, owner_id)
-
-
- at add_argument('project_dir')
-def _del_project(args):
-    db = ElbeDB()
-    db.del_project(args.project_dir)
-
-
- at add_argument('project_dir')
- at add_argument('xml')
-def _set_xml(args):
-    db = ElbeDB()
-    db.set_xml(args.project_dir, args.xml)
-
-
- at add_argument('project_dir')
-def _build(args):
-    db = ElbeDB()
-    db.set_busy(args.project_dir, ['empty_project', 'needs_build', 'has_changes',
-                                   'build_done', 'build_failed'])
-    try:
-        ep = db.load_project(args.project_dir)
-        ep.build()
-        db.update_project_files(ep)
-    except Exception as e:
-        db.update_project_files(ep)
-        db.reset_busy(args.project_dir, 'build_failed')
-        print(str(e))
-        return
-    db.reset_busy(args.project_dir, 'build_done')
-
-
- at add_argument('project_dir')
-def _get_files(args):
-    db = ElbeDB()
-    files = db.get_project_files(args.project_dir)
-    for f in files:
-        if f.description:
-            print(f'{f.name:40}  {f.description}')
-        else:
-            print(f.name)
-
-
- at add_argument('--clean', dest='clean', default=False, action='store_true')
- at add_argument('project_dir')
-def _reset_project(args):
-    db = ElbeDB()
-    db.reset_project(args.project_dir, args.clean)
-
-
- at add_argument('project_dir')
- at add_argument('version')
-def _set_project_version(args):
-    db = ElbeDB()
-    db.set_project_version(args.project_dir, args.version)
-
-
- at add_argument('project_dir')
-def _list_versions(args):
-    db = ElbeDB()
-    versions = db.list_project_versions(args.project_dir)
-
-    for v in versions:
-        if v.description:
-            print(f'{v.version}: {v.description}')
-        else:
-            print(v.version)
-
-
- at add_argument('--description', dest='description')
- at add_argument('project_dir')
-def _save_version(args):
-    db = ElbeDB()
-    db.save_version(args.project_dir, args.description)
-
-
- at add_argument('project_dir')
- at add_argument('version')
-def _del_version(args):
-    db = ElbeDB()
-    db.del_version(args.project_dir, args.version)
-
-
- at add_argument('project_dir')
- at add_argument('version')
-def _print_version_xml(args):
-    db = ElbeDB()
-    filename = db.get_version_xml(args.project_dir, args.version)
-    with open(filename) as f:
-        copyfileobj(f, sys.stdout)
-
-
-db_actions = {
-    'init':                _init,
-    'add_user':            _add_user,
-    'del_user':            _del_user,
-    'list_projects':       _list_projects,
-    'list_users':          _list_users,
-    'create_project':      _create_project,
-    'del_project':         _del_project,
-    'set_xml':             _set_xml,
-    'build':               _build,
-    'get_files':           _get_files,
-    'reset_project':       _reset_project,
-    'set_project_version': _set_project_version,
-    'list_versions':       _list_versions,
-    'save_version':        _save_version,
-    'del_versions':        _del_version,
-    'print_version_xml':   _print_version_xml,
-}

-- 
2.45.2



More information about the elbe-devel mailing list