[elbe-devel] [PATCH 2/2] elbepack: move entrypoint into package

Thomas Weißschuh thomas.weissschuh at linutronix.de
Tue Apr 9 15:59:09 CEST 2024


This makes the entrypoint compatible with zipapp, console_scripts,
python -m, etc.
It also makes it easier to package elbe.

For backwards compatibility provide a stub file.
Elbe can now be used via "./elbe", "python -melbepack" or the generated
console script.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 conftest.py                     |  8 --------
 debian/python3-elbe-bin.install |  3 +++
 elbe                            | 40 ++-----------------------------------
 elbepack/__main__.py            |  7 +++++++
 elbepack/directories.py         |  4 +---
 elbepack/main.py                | 44 +++++++++++++++++++++++++++++++++++++++++
 setup.py                        |  6 +++++-
 7 files changed, 62 insertions(+), 50 deletions(-)

diff --git a/conftest.py b/conftest.py
index 523c3f243bb9..0706e6ac093e 100644
--- a/conftest.py
+++ b/conftest.py
@@ -1,13 +1,5 @@
-import os
-
 import pytest
 
-import elbepack.directories
-
-
-def pytest_sessionstart(session):
-    elbepack.directories.elbe_exe = os.path.join(os.path.dirname(__file__), 'elbe')
-
 
 # https://stackoverflow.com/a/61193490
 def pytest_addoption(parser):
diff --git a/debian/python3-elbe-bin.install b/debian/python3-elbe-bin.install
index c39a83debc5d..508f944bf6e6 100644
--- a/debian/python3-elbe-bin.install
+++ b/debian/python3-elbe-bin.install
@@ -1,5 +1,8 @@
 ./usr/bin/*
 ./usr/share/man/man1/elbe.1
+./usr/lib/python3.*/*-packages/elbe-*.egg-info/*
+./usr/lib/python3.*/*-packages/elbepack/__main__.py
+./usr/lib/python3.*/*-packages/elbepack/main.py
 ./usr/lib/python3.*/*-packages/elbepack/commands/add.py
 ./usr/lib/python3.*/*-packages/elbepack/commands/add.py
 ./usr/lib/python3.*/*-packages/elbepack/commands/check_updates.py
diff --git a/elbe b/elbe
index 9f4905743681..4d3f4b7e94e8 100755
--- a/elbe
+++ b/elbe
@@ -1,41 +1,5 @@
 #!/usr/bin/env python3
-#
-# ELBE - Debian Based Embedded Rootfilesystem Builder
-# SPDX-License-Identifier: GPL-3.0-or-later
-# SPDX-FileCopyrightText: 2013-2017 Linutronix GmbH
 
-import importlib
-import sys
+from elbepack.main import main
 
-from elbepack.directories import get_cmdlist
-from elbepack.version import elbe_version
-
-
-def usage():
-    print('elbe v%s' % elbe_version)
-    print("need a subcommand: e.g. \'elbe initvm\'. \n\
-    Available subcommands are: \n")
-    for i in get_cmdlist():
-        print('        * %s' % i)
-
-
-if len(sys.argv) < 2:
-    usage()
-    sys.exit(20)
-
-if sys.argv[1] == '--version':
-    print('elbe v%s' % (elbe_version))
-    sys.exit(0)
-
-cmd_list = get_cmdlist()
-
-if sys.argv[1] not in cmd_list:
-    print('Unknown subcommand !\n')
-    usage()
-    sys.exit(20)
-
-modname = 'elbepack.commands.' + sys.argv[1]
-
-cmdmod = importlib.import_module(modname)
-
-cmdmod.run_command(sys.argv[2:])
+main()
diff --git a/elbepack/__main__.py b/elbepack/__main__.py
new file mode 100644
index 000000000000..2a9ad67d5f78
--- /dev/null
+++ b/elbepack/__main__.py
@@ -0,0 +1,7 @@
+# ELBE - Debian Based Embedded Rootfilesystem Builder
+# SPDX-License-Identifier: GPL-3.0-or-later
+# SPDX-FileCopyrightText: 2024 Linutronix GmbH
+
+from elbepack.main import main
+
+main()
diff --git a/elbepack/directories.py b/elbepack/directories.py
index 83eb24cbfcc7..731fd26dc03f 100644
--- a/elbepack/directories.py
+++ b/elbepack/directories.py
@@ -2,8 +2,6 @@
 # SPDX-License-Identifier: GPL-3.0-or-later
 # SPDX-FileCopyrightText: 2015, 2017 Linutronix GmbH
 
-import __main__
-import os
 import subprocess
 import sys
 from pkgutil import iter_modules
@@ -12,7 +10,7 @@ import elbepack.commands
 
 
 # set global variables that are used in other modules via imports
-elbe_exe = os.path.abspath(os.path.realpath(__main__.__file__))
+elbe_exe = '-melbepack'
 
 
 def get_cmdlist():
diff --git a/elbepack/main.py b/elbepack/main.py
new file mode 100644
index 000000000000..6e2217b3f2bb
--- /dev/null
+++ b/elbepack/main.py
@@ -0,0 +1,44 @@
+# ELBE - Debian Based Embedded Rootfilesystem Builder
+# SPDX-License-Identifier: GPL-3.0-or-later
+# SPDX-FileCopyrightText: 2013-2017 Linutronix GmbH
+
+import importlib
+import sys
+
+from elbepack.directories import get_cmdlist
+from elbepack.version import elbe_version
+
+
+def usage():
+    print('elbe v%s' % elbe_version)
+    print("need a subcommand: e.g. \'elbe initvm\'. \n\
+    Available subcommands are: \n")
+    for i in get_cmdlist():
+        print('        * %s' % i)
+
+
+def main():
+    # with python -melbepack the optparse usage message would show __main__.py
+    if sys.argv[0].endswith('__main__.py'):
+        sys.argv[0] = 'elbe'
+
+    if len(sys.argv) < 2:
+        usage()
+        sys.exit(20)
+
+    if sys.argv[1] == '--version':
+        print('elbe v%s' % (elbe_version))
+        sys.exit(0)
+
+    cmd_list = get_cmdlist()
+
+    if sys.argv[1] not in cmd_list:
+        print('Unknown subcommand !\n')
+        usage()
+        sys.exit(20)
+
+    modname = 'elbepack.commands.' + sys.argv[1]
+
+    cmdmod = importlib.import_module(modname)
+
+    cmdmod.run_command(sys.argv[2:])
diff --git a/setup.py b/setup.py
index d00f3981e2b2..12fbff8ae729 100644
--- a/setup.py
+++ b/setup.py
@@ -62,7 +62,11 @@ setup(name='elbe',
                                  'xsdtoasciidoc.mako',
                                  'schema/dbsfed.xsd',
                                  'schema/xml.xsd']},
-      scripts=['elbe'],
+      entry_points={
+          'console_scripts': [
+              'elbe=elbepack.main:main',
+          ],
+      },
       cmdclass={'install': my_install},
       install_requires=['lxml',
                         'Mako',

-- 
2.44.0



More information about the elbe-devel mailing list