diff --git a/python_appimage/__main__.py b/python_appimage/__main__.py index ccac0b3..2f2f94e 100644 --- a/python_appimage/__main__.py +++ b/python_appimage/__main__.py @@ -75,6 +75,11 @@ def main(): action='store_true', default=False) + list_parser = subparsers.add_parser('list', + description='List Python versions installed in a manylinux image') + list_parser.add_argument('tag', + help='manylinux image tag (e.g. 2010_x86_64)') + which_parser = subparsers.add_parser('which', description='Locate a binary dependency') which_parser.add_argument('binary', choices=binaries, diff --git a/python_appimage/commands/list.py b/python_appimage/commands/list.py new file mode 100644 index 0000000..e3bb3ab --- /dev/null +++ b/python_appimage/commands/list.py @@ -0,0 +1,41 @@ +import os + +from ..utils.docker import docker_run +from ..utils.log import log +from ..utils.tmp import TemporaryDirectory + + +__all__ = ['execute'] + + +def _unpack_args(args): + '''Unpack command line arguments + ''' + return (args.tag,) + + +def execute(tag): + '''List python versions installed in a manylinux image + ''' + + with TemporaryDirectory() as tmpdir: + script = ( + 'for dir in $(ls /opt/python | grep "^cp[0-9]"); do', + ' version=$(/opt/python/$dir/bin/python -c "import sys; ' \ + 'sys.stdout.write(sys.version.split()[0])")', + ' echo "$dir $version"', + 'done', + ) + if tag.startswith('2_'): + image = 'manylinux_' + tag + else: + image = 'manylinux' + tag + result = docker_run( + 'quay.io/pypa/' + image, + script, + capture = True + ) + for line in result.split(os.linesep): + if line: + log('LIST', "{1:7} -> /opt/python/{0:}".format( + *line.split())) diff --git a/python_appimage/utils/docker.py b/python_appimage/utils/docker.py index 862357d..1713191 100644 --- a/python_appimage/utils/docker.py +++ b/python_appimage/utils/docker.py @@ -4,11 +4,12 @@ import stat import subprocess import sys +from .compat import decode from .log import log from .system import system -def docker_run(image, extra_cmds): +def docker_run(image, extra_cmds, capture=False): '''Execute commands within a docker container ''' @@ -42,10 +43,16 @@ def docker_run(image, extra_cmds): 'type=bind,source={:},target=/pwd'.format(os.getcwd()), image, '/bin/bash', bash_arg)) + if capture: + opts = {'stderr': subprocess.PIPE, 'stdout': subprocess.PIPE} + else: + opts = {} log('RUN', image) - p = subprocess.Popen(cmd, shell=True) - p.communicate() + p = subprocess.Popen(cmd, shell=True, **opts) + r = p.communicate() if p.returncode != 0: if p.returncode == 139: sys.stderr.write("segmentation fault when running Docker (139)\n") sys.exit(p.returncode) + if capture: + return decode(r[0])