lib.ExecContext: Add open() + close() around _run()

Enclose ExecContext._run() in an open() / close() - pair. This is convenient for the caller in that it doesn't need to take care of opening and closing for one call only, and inconvenient in that it forces the caller to conciously add an open() / close() - pair around multiple run() calls where it wants the context to stay open in between. Or use the ExecContext as a context manager.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-04-25 08:29:55 +02:00
commit a9475de48e
Signed by: Jan Lindemann
GPG key ID: 3750640C9E25DD61
2 changed files with 41 additions and 36 deletions

View file

@ -33,22 +33,15 @@ class CmdListRepos(Cmd): # export
case 'ssh': case 'ssh':
if re.match(r'ssh://.*git\.janware\.com/', args.base_url): if re.match(r'ssh://.*git\.janware\.com/', args.base_url):
from jw.pkg.lib.ec.SSHClient import SSHClient, ssh_client from jw.pkg.lib.ec.SSHClient import SSHClient, ssh_client
ssh: SSHClient|None = None ssh = ssh_client(args.base_url, interactive=self.app.interactive, verbose_default=self.app.verbose)
try: if username is not None:
ssh = ssh_client(args.base_url, interactive=self.app.interactive, verbose_default=self.app.verbose) ssh.set_username(username)
if username is not None: if password is not None:
ssh.set_username(username) ssh.set_password(password)
if password is not None: cmd = ['/opt/jw-pkg/bin/git-srv-admin.sh', '-u', args.from_owner, '-j', 'list-personal-projects']
ssh.set_password(password) result = await ssh.run(cmd)
cmd = ['/opt/jw-pkg/bin/git-srv-admin.sh', '-u', args.from_owner, '-j', 'list-personal-projects'] print('\n'.join(result.stdout.decode().splitlines()))
result = await ssh.run(cmd) return
print('\n'.join(result.stdout.decode().splitlines()))
return
except:
raise
finally:
if ssh is not None:
await ssh.close()
case 'https': case 'https':
from jw.pkg.lib.base import InputMode from jw.pkg.lib.base import InputMode
cmd_input = InputMode.NonInteractive cmd_input = InputMode.NonInteractive

View file

@ -287,22 +287,30 @@ class ExecContext(Base):
# be returned by CallContext and is very much allowed # be returned by CallContext and is very much allowed
assert cmd_input is not None, 'Invalid: cmd_input is None' assert cmd_input is not None, 'Invalid: cmd_input is None'
ret = Result(None, None, 1) # Enclose multiple run() calls in an additional open() / close() pair
with self.CallContext(self, title=title, cmd=cmd, cmd_input=cmd_input, mod_env=mod_env, wd=wd, # if you want the context to stay open between the calls
log_prefix='|', throw=throw, verbose=verbose) as cc: await self.open()
try:
ret = await self._run( try:
cmd = cc.cmd, ret = Result(None, None, 1)
wd = wd, with self.CallContext(self, title=title, cmd=cmd, cmd_input=cmd_input, mod_env=mod_env, wd=wd,
verbose = cc.verbose, log_prefix='|', throw=throw, verbose=verbose) as cc:
cmd_input = cc.cmd_input, try:
mod_env = cc.mod_env, ret = await self._run(
interactive = cc.interactive, cmd = cc.cmd,
log_prefix = cc.log_prefix wd = wd,
) verbose = cc.verbose,
except Exception as e: cmd_input = cc.cmd_input,
return cc.exception(ret, e) mod_env = cc.mod_env,
cc.check_exit_code(ret) interactive = cc.interactive,
log_prefix = cc.log_prefix
)
except Exception as e:
return cc.exception(ret, e)
cc.check_exit_code(ret)
finally:
await self.close()
return ret return ret
async def _sudo( async def _sudo(
@ -482,10 +490,14 @@ class ExecContext(Base):
cmds.append({'cmd': ['chmod', mode, out]}) cmds.append({'cmd': ['chmod', mode, out]})
if atomic: if atomic:
cmds.append({'cmd': ['mv', out, path]}) cmds.append({'cmd': ['mv', out, path]})
for cmd in cmds: await self.open()
log(DEBUG, f'{self.log_name}: Running {pretty_cmd(cmd['cmd'], wd)}') try:
ret = await __run(**cmd) for cmd in cmds:
return ret log(DEBUG, f'{self.log_name}: Running {pretty_cmd(cmd['cmd'], wd)}')
ret = await __run(**cmd)
return ret
finally:
await self.close()
except: except:
if throw: if throw:
raise raise