2025-11-18 12:07:02 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
import os, sys, subprocess, json, time
|
|
|
|
|
from argparse import Namespace
|
|
|
|
|
from urllib.parse import urlparse
|
2025-11-20 11:29:50 +01:00
|
|
|
from enum import Enum, auto
|
|
|
|
|
|
|
|
|
|
class AskpassKey(Enum):
|
|
|
|
|
Username = auto()
|
|
|
|
|
Password = auto()
|
2025-11-18 12:07:02 +01:00
|
|
|
|
2025-11-20 10:44:14 +01:00
|
|
|
def pretty_cmd(cmd: list[str], wd=None):
|
2025-11-18 12:07:02 +01:00
|
|
|
tokens = [cmd[0]]
|
|
|
|
|
for token in cmd[1:]:
|
|
|
|
|
if token.find(' ') != -1:
|
|
|
|
|
token = '"' + token + '"'
|
|
|
|
|
tokens.append(token)
|
2025-11-20 10:44:14 +01:00
|
|
|
ret = ' '.join(tokens)
|
2025-11-18 12:07:02 +01:00
|
|
|
if wd is not None:
|
2025-11-20 10:44:14 +01:00
|
|
|
ret += f' in {wd}'
|
|
|
|
|
return ret
|
|
|
|
|
|
2025-11-20 11:29:50 +01:00
|
|
|
def run_cmd(cmd: list[str], wd=None, throw=True, verbose=False, cmd_input=None) -> str|None: # export
|
2025-11-20 10:44:14 +01:00
|
|
|
|
2025-11-18 12:07:02 +01:00
|
|
|
if verbose:
|
|
|
|
|
delim_len = 120
|
2025-11-20 10:44:14 +01:00
|
|
|
delim = f'---- running {pretty_cmd(cmd, wd)} -'
|
2025-11-18 12:07:02 +01:00
|
|
|
delim = delim + '-' * (delim_len - len(delim))
|
|
|
|
|
print(',' + delim + ' >')
|
|
|
|
|
|
|
|
|
|
cwd: str|None = None
|
|
|
|
|
if wd is not None:
|
|
|
|
|
cwd = os.getcwd()
|
|
|
|
|
os.chdir(wd)
|
|
|
|
|
|
|
|
|
|
ret = ''
|
|
|
|
|
try:
|
2025-11-20 11:29:50 +01:00
|
|
|
stdin = None
|
|
|
|
|
if cmd_input is not None:
|
|
|
|
|
stdin = subprocess.PIPE
|
|
|
|
|
p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=None, close_fds=True, stdin=stdin)
|
|
|
|
|
if cmd_input is not None:
|
|
|
|
|
ret = p.communicate(input=cmd_input)[0]
|
|
|
|
|
else:
|
|
|
|
|
for line in iter(p.stdout.readline, b''):
|
|
|
|
|
line = line.decode(sys.stdout.encoding)
|
|
|
|
|
ret += line
|
|
|
|
|
p.wait()
|
2025-11-18 12:07:02 +01:00
|
|
|
if verbose:
|
|
|
|
|
print('`' + delim + ' <')
|
|
|
|
|
if p.returncode:
|
|
|
|
|
if verbose:
|
|
|
|
|
print(' '.join(cmd) + ' failed')
|
2025-11-20 10:44:14 +01:00
|
|
|
raise Exception(time.strftime('%Y-%m-%d %H:%M') + f': Command returned an error: "{pretty_cmd(cmd, wd)}"')
|
2025-11-18 12:07:02 +01:00
|
|
|
finally:
|
|
|
|
|
if cwd:
|
|
|
|
|
os.chdir(cwd)
|
|
|
|
|
return ret
|
|
|
|
|
|
2025-11-20 11:29:50 +01:00
|
|
|
def run_curl(args: list[str], parse_json: bool=True, wd=None, throw=None, verbose=False, cmd_input=None) -> dict|str: # export
|
2025-11-18 12:07:02 +01:00
|
|
|
cmd = ['curl']
|
|
|
|
|
if not verbose:
|
|
|
|
|
cmd.append('-s')
|
|
|
|
|
cmd.extend(args)
|
2025-11-20 11:29:50 +01:00
|
|
|
ret = run_cmd(cmd, wd=wd, throw=throw, verbose=verbose, cmd_input=cmd_input)
|
2025-11-18 12:07:02 +01:00
|
|
|
if parse_json:
|
2025-11-20 10:44:14 +01:00
|
|
|
try:
|
|
|
|
|
return json.loads(ret)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f'Failed to parse {len(ret)} bytes output of command >{pretty_cmd(cmd, wd)}< ({e})', file=sys.stderr)
|
|
|
|
|
raise
|
2025-11-18 12:07:02 +01:00
|
|
|
return ret
|
|
|
|
|
|
2025-11-20 11:29:50 +01:00
|
|
|
def run_askpass(askpass_env: list[str], key: AskpassKey, host: str|None=None):
|
|
|
|
|
assert host is None # Currently unsupported
|
|
|
|
|
for var in askpass_env:
|
|
|
|
|
exe = os.getenv(var)
|
|
|
|
|
if exe is None:
|
|
|
|
|
continue
|
|
|
|
|
exe_arg = ''
|
|
|
|
|
match var:
|
|
|
|
|
case 'GIT_ASKPASS':
|
|
|
|
|
match key:
|
|
|
|
|
case AskpassKey.Username:
|
|
|
|
|
exe_arg += 'Username'
|
|
|
|
|
case AskpassKey.Password:
|
|
|
|
|
exe_arg += 'Password'
|
|
|
|
|
case 'SSH_ASKPASS':
|
|
|
|
|
match key:
|
|
|
|
|
case AskpassKey.Username:
|
|
|
|
|
continue # Can't get user name from SSH_ASKPASS
|
|
|
|
|
case AskpassKey.Password:
|
|
|
|
|
exe_arg += 'Password'
|
|
|
|
|
ret = run_cmd([exe, exe_arg], throw=False)
|
|
|
|
|
if ret is not None:
|
|
|
|
|
return ret
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def get_username(args: Namespace|None=None, url: str|None=None, askpass_env: list[str]=[]) -> str: # export
|
2025-11-18 12:07:02 +01:00
|
|
|
url_user = None if url is None else urlparse(url).username
|
|
|
|
|
if args is not None:
|
|
|
|
|
if args.username is not None:
|
|
|
|
|
if url_user is not None and url_user != args.username:
|
|
|
|
|
raise Exception(f'Username mismatch: called with --username="{args.username}", URL has user name "{url_user}"')
|
|
|
|
|
return args.username
|
2025-11-20 11:29:50 +01:00
|
|
|
if url_user is not None:
|
2025-11-18 12:07:02 +01:00
|
|
|
return url_user
|
2025-11-20 11:29:50 +01:00
|
|
|
return run_askpass(askpass_env, AskpassKey.Username)
|
2025-11-18 12:07:02 +01:00
|
|
|
|
|
|
|
|
def get_password(args: Namespace|None=None, url: str|None=None, askpass_env: list[str]=[]) -> str: # export
|
|
|
|
|
if args is None and url is None and not askpass_env:
|
|
|
|
|
raise Exception(f'Neither URL nor command-line arguments nor askpass environment variable available, can\'t get password')
|
|
|
|
|
if args is not None and hasattr(args, 'password'): # use getattr(), because we don't necessarily want to have insecure --password among options
|
|
|
|
|
ret = getattr(args, 'password')
|
|
|
|
|
if ret is not None:
|
|
|
|
|
return ret
|
|
|
|
|
if url is not None:
|
|
|
|
|
parsed = urlparse(url)
|
|
|
|
|
if parsed.password is not None:
|
|
|
|
|
return parsed.password
|
2025-11-20 11:29:50 +01:00
|
|
|
return run_askpass(askpass_env, AskpassKey.Password)
|