Commit graph

54 commits

Author SHA1 Message Date
df40af9fc3 lib.App.call_async(): Add method

Use the AsyncRunner class introduced in the previous commit to add a call_async() method, allowing to run async functions from sync functions by spawning an extra event loop.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-20 10:30:25 +01:00
67e51cf07c lib.AsyncRunner: Add class

Add class AsyncRunner. This is a wrapper around the ceremony needed to spawn an extra event loop in a synchronous function which wants to call an async function.

Guido van Rossum considers it bad design that such a function exists in the first place. While that may be true in the long run also for jw-pkg, at this point I'm unwilling to flag every lazyly initialized App property as async. It's not clear, yet, which will be async and which not, and I dread the churn. So I will accept this as a minimally invasive helper for now. When the API has stabilized, a design without it may be better.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-20 10:30:25 +01:00
95a384bfff lib.App.eloop: Add property

Expose App's __eloop member containing the application's main event loop to allow outside async event loop trickery.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-20 10:30:25 +01:00
52dd3b8f21 lib.ExecContext.run(): Push code up into base class

Take implementation burden from the derived classes _run() callback by moving the respective code into the run() wrapper methods of the base class.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-20 10:30:25 +01:00
f2ffe85b61 lib.SSHClientInternal: No key_filename in connect()

Remove the key_filename parameter from the call to Paramiko's connect(). It's user-dependent, and the current DevOps implementation relies on having a SSH_AUTH_SOCK in the environment, anyway.

Signed-off-by: janware DevOps <devops@janware.com>
2026-03-20 10:30:25 +01:00
49daa86696 lib.SSHClient: Log more details around exceptions

Add a wrapper around urlparse() and Paramiko's connect() function, in order to log some more info in case an exception is thrown.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-20 10:30:25 +01:00
76b702f5b4 lib.ExecContext.create(): Add method

Add a class method to instantiate an ExecContext by its URI.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-20 10:30:25 +01:00
5f6ec5a182 lib.SSHClient: Retire non-EC API

Remove .run_cmd(), forcing future clients to use the ExecContext aligned API.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-20 10:30:25 +01:00
888c1e7f16 lib.ExecContext.__init__(): Add parameter uri

Take a positional uri argument to the constructor of ExecContext, forcing SSHClient to follow suit. The latter was instantiated with a hostname as only argument up to now, which still works as a special case of an uri.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-20 10:30:25 +01:00
a76a6c9316 lib.SSHClient: Move public methods down

Code beautification chore: Move the public methods of SSHClients to the bottom of the class to be consistent with other classes.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-20 10:30:25 +01:00
284eb30ecf lib.SSHClient: Derive from ExecContext

Make SSHClient an ExecContext by implementing _run() and _sudo().

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-20 10:30:25 +01:00
989e2c93e3 lib.SSHClient.run_cmd(): Align prototype with EC

Align the prototype of SSHClient.run_cmd() to ExecContext.run(). This is a push towards making the SSHClient code an ExceContext, too. Some arguments still log a warning or outright raise NotImplementedError.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-20 10:30:25 +01:00
d0776db01f lib.SSHClient.run_cmd(): Accept cmd: list[str]

Make SSHClient accept a list of strings for the cmd argument to align with the other run_cmd() functions in jw-pkg.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-17 16:23:34 +01:00
58142a1115 lib.Distro.pkg_files(): Fix argument name
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-17 07:35:21 +01:00
824de4eca4 lib.distros.*.Distro: Align PM prototypes

Make all backend package manager prototypes have the same arguments:

yum(self, args: list[str], verbose: bool=True, sudo: bool=True)

This also implies having them behave equally verbose, unless otherwise specified by the caller. This changes the default for Debian.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-16 14:53:58 +01:00
cff63786e9 lib.Distro: Allow empty packages list

In commands taking lists of packages, namely install, delete and pkg_files, don't bother asking the backend. Uniformly log a warning and return successfully.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-15 16:41:46 +01:00
b2847809c1 lib.Types: Make debug logging optional

lib.Types class detection is too chatty. Make that a ctor option.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-15 10:42:24 +01:00
72bd5e3555 lib.Local.run(): Be less dramatic about exit != 0

Don't mention "error" in log message for exit codes > 0 from spawned processes, because sometimes they don't mean an error.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-09 20:03:01 +01:00
6df4c86fc5 lib.App: Add property cmdline

Add the property App.cmdline, containing the invoking command line as a string.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-09 20:00:30 +01:00
bd38700f67 lib.Distro: Add .id

Allow to query the distribution ID a Distro was instantiated with via the .id property.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-07 14:35:54 +01:00
67a2931f5e App: Support --verbose

Add the --verbose global option, which is made available as the App.verbose property.

Some functions still take a verbose parameter, but the type of these parameters is converted from bool to bool|None. The idea is that, if they are None, their verbosity falls back to the global default.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-06 19:02:22 +01:00
525fa34387 lib.pm: Add parameter ec: ExecContext to functions

Functions in lib.pm (i.e. run_dpkg(), run_rpm() and friends) also get an ExecContext-type parameter. Use them in lib/distros/*/Distro.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-06 19:02:22 +01:00
1325222fbd lib.ExecContext,Local: Remove callback default params

Remove defaults from protected callback function parameters. They have to be decided by the base class's public API.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-06 19:02:22 +01:00
7fdba1b5db lib.util: Add ec: ExecContext to all subprocesses

Allow to pass an optional execution context to all functions spawning a subprocess defined in lib.util.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-06 15:14:13 +01:00
fadf1bca49 lib.util.run_cmd(): Add parameter ec: ExecContext

Allow to specify the ExecContext in a call to run_cmd(). This effectively makes run_cmd() an thin wrapper around ExecContext.run(), which is what's going to be used in the future. The wrapper is for backwards-compatibility.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-06 15:14:13 +01:00
3e897f4df8 lib.Distro, ExecContext: Add classes, refactor lib.distro

The code below lib.distro, as left behind by the previous commit, is geared towards being directly used as a command-line API. This commit introduces the abstract base class Distro, a proxy for distribution-specific interactions. The proxy abstracts distro specifics into an API with proper method prototypes, not argparse.Namespace contents, and can thus be more easily driven by arbitrary code.

The Distro class is initialized with a member variable of type ExecContext, another new class introduced by this commit. It is designed to abstract the communication channel to the distribution instance. Currently only one specialization exists, Local, which interacts with the distribution and root file system it is running in, but is planned to be subclassed to support interaction via SSH, serial, chroot, or chains thereof.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-06 14:56:46 +01:00
7e7cee6d11 cmds.distro: Move all modules to lib

Functions abstracting the distribution are not only needed in the context of the distro subcommand, but also by other code, so make the bulk of the code abstracting the distribution available in some place more universally useful than below cmds.distro.

This commit leaves the source files mostly unchanged. They are only patched to fix import paths, so that functionality is preserved. Refactoring the code from command-line API to library API will be done by the next commit.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-06 12:06:28 +01:00
ae902250bd jw/pkg/lib/util.run_cmd(): Chunked read_stream()

jw-pkg distro dup got hung in a chroot environment. strace shows that write(2) into a pipe is the hanging syscall, with the write buffer hinting at zypper dup output.

I strongly suspect that run_cmd() tries to write stdout into the pipe which read_stream() fails to empty. So, make read_stream() more resilient by using read(4096) instead of readline(), which I suspect to be prone to hang on overlong lines.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-05 06:42:57 +00:00
0c1c2b9351 lib.util.run_cmd(): Reduce interactive logging

run_cmd() with cmd_input == mode:interactive and verbose == true logs output too often. First, __log() is called, then pty.spawn() writes everything it reads from the PTY master to the terminal.

The fix it to not call __log() from _read() for the PTY reader.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-04 16:02:07 +00:00
8bc22a3a68 lib.util.run_cmd(): Fix docstring

The docstring of run_cmd()'s signature documents a wrong return value, fix that.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-04 14:48:26 +01:00
7fcb031795 jw.pkg.lib.App.__run(): Use return value as exit status

If a Cmd-classes's _run() method returns an integer between 0 and 255, use that as the program's exit status.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-03 11:23:32 +01:00
565946643b jw.pkg.*.run_xxx(): Return exit status

Most run_xxx() return stdout and stderr. There's no way, really, for the caller to get hold of the exit code of the spawned executable. It can pass throw=true, catch, and assume a non-zero exit status. But that's not semantically clean, since the spawned function can well be a test function which is expected to return a non-zero status code, and the caller might be interested in what code that was, exactly.

The clearest way to solve this is to return the exit code as well. This commit does that.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-03 11:23:30 +01:00
afc77ab61d jw.pkg.lib.util.run_curl(): Beautify logging

Make some incomprensible parser error messages if run_curl() returns nothing slightly less incomprehensible.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-02-27 17:03:26 +01:00
b81406e11a run_cmd() and friends: Make args a list[str]

This is a code maintenance commit: some run_xxx() helper functions take a string, some a list, and some just digest all arguments and pass them on as a list to exec() to be executed. That's highly inconsistent. This commit changes that to list-only.

Except for the run_cmd() method of SSHClient, which is still run as a shell method, because, erm, it's a shell. Might be changed in the future for consistency reasons.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-02-27 09:09:10 +01:00
2906c697de jw.pkg.lib.util.run_cmd(): Add stderr to exception

If an error happens, append stderr to the exception thrown.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-02-24 14:53:17 +01:00
c8036ad216 jw-pkg.sh: Print help for missing subcommands

Print a help message if no subcommand is specified for one of the comamnds "distro" and "projects".

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-02-24 14:52:25 +01:00
e5e0cf9930 jw.pkg.lib.Cmd._run(): Call parent._run()

jw.pkg.lib.Cmd._run() is abstract, but it's nice to give it a default implementation which calls self.parent._run() in case parent is also a command class. That allows for some default processing in _run() for each node up the parent chain.

The children / derived classes just need to make sure all classes in the hierarchy do:

async def _run(self, args): return await super()._run(args) ... add main command logic here ..
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-02-20 19:30:22 +01:00
6916d7edc8 jw.pkg.lib.util.run_sudo(): Add parameter verbose

Add parameter verbose to run_sudo() and pass it on to run_cmd().

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-02-20 19:30:22 +01:00
b3fd624a3f jw.pkg.lib.util.get_profile_env(): add -> keep

Replace the boolean parameter "add" with the richer "keep":

- False -> Don't keep anything - True -> Keep what's in the current environment - List of strings -> Keep those variables

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-02-19 07:52:53 +01:00
2d1beeebb0 jw.pkg.lib.util.get_profile_env(): Add function

Add a function get_profile_env(), a function returning environment variables from /etc/profile. Pass add=True to add its contents to the existing environment dictionary, overwriting old entries, or pass False to get the pristine content.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-02-18 14:18:46 +01:00
e104fa2e46 jw.pkg.lib.util.run_cmd(): Add output_encoding

Add a parameter "output_encoding" to run_cmd(). The parameter allows the caller to specify if the output encoding should be detected as is by passing None (the default), if the output should be returned as undecoded bytes by passing the special string "bytes", or if the output should be treated as the encoding with the specified name and decoded to strings.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-02-18 13:38:12 +01:00
82c6a44ad6 jw.pkg.lib.util.run_cmd(): Fix mode:auto

In cmd_input == "mode:auto", the interactive variable is currently not set due to a typo. Fix that.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-02-18 13:31:26 +01:00
f8dc8ee6d1 jw.pkg.lib.util.run_cmd(): Honour env in PTY mode

The evironment passed to run_cmd() via env is currently not honoured with mode:interactive. Fix that.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-02-18 13:29:48 +01:00
75faf02232 jw.pkg.lib.util.run_cmd(): Add title parameter

Allow the caller to choose which title should be used for the command's logging output box.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-02-18 11:31:53 +01:00
7eb15f2477 jw.pkg.lib: Don't log {e}

Don't log an Exception as {e} but as str(e) producing nicer output. Or as repr(e) if a backtrace is requested, because to people who can read backtraces, type info might be of interest. Also, remove pointless time stamps, those belong into the logging framework.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-02-18 11:31:13 +01:00
b1d4e20295 jw.pkg.util: Add sudo()

Move the body of BackendCmd.sudo() into a function. The rationale behind that is that its functionality is independent of the calling object for the most part, so having it in a function instead of a method is the more modular pattern.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-02-18 01:20:25 +01:00
d50a33d9ab jw.pkg.cmds.lib.Cmd: Define run()

Define run(), which calls _run() in the abstract base class Cmd, not in lib.Cmd. Otherwise lib.Cmd is not abstract, which will predictably confuse including code outside of jw-pkg.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-02-10 10:00:46 +01:00
53ba9e6fbe lib.App: Stay functional without autocomplete

If Python's autocomplete is not installed, jw-pkg.py fails to run commands. Fix that in order stay compatible with minimal excecution environments.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-02-02 12:15:41 +01:00
4274a71c62 lib.util.run_cmd(): Rewrite it to be async

run_cmd() is synchronous. Now that all commands are asynchronous, we can await it, so rewrite it to be asynchronous, too.

Other changes:

- Make it return stderr as well in case its needed

- Drop into a pseuto-tty if - cmd_input == "mode:interactive" or - cmd_input == "mode:auto" and stdin is a TTY
- Add argument env, defaulting to None. If it's a dict, it will be the environment the command is run in

This entails making all functions using run_cmd() async, too, including run_curl(), get_username() and get_password().

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-01-28 17:41:40 +01:00
f175f9d5c9 lib.Cmd: Add argument "parent" to __init__()

During __init__(), commands have no idea of their parent. This is not a problem as of now, but is easy to fix, and it's architecturally desirable to be prepared just in case, so add the parent argument to the ctor before more commands are added.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-01-28 15:24:24 +01:00