Pip action implementation

This commit is contained in:
jpic 2020-05-31 03:39:26 +02:00
parent c570f0fd6d
commit a07d9c5e67
2 changed files with 48 additions and 38 deletions

View File

@ -1,59 +1,69 @@
from glob import glob from glob import glob
import os import os
from urllib import request
from .base import Action from .base import Action
class Pip(Action): class Pip(Action):
"""Pip abstraction layer.""" """Pip abstraction layer."""
def __init__(self, *pip_packages):
self.pip_packages = pip_packages
def __init__(self, *pip_packages, pip=None, requirements=None): async def __call__(self, target):
self.requirements = requirements # ensure python presence
super().__init__(*pip_packages, pip=pip, requirements=requirements) results = await target.which('python3', 'python')
if results:
python = results[0]
else:
raise Exception('Could not find pip nor python')
async def call(self, *args, **kwargs): # ensure pip module presence
pip = self.kwargs.get('pip', None) result = await target.exec(python, '-m', 'pip', raises=False)
if not pip: if result.rc != 0:
pip = await self.which('pip3', 'pip', 'pip2') if not os.path.exists('get-pip.py'):
if pip: req = request.urlopen(
pip = pip[0] 'https://bootstrap.pypa.io/get-pip.py'
else:
from .packages import Packages
action = self.action(
Packages,
'python3,apk', 'python3-pip,apt',
args=args, kwargs=kwargs
) )
await action(*args, **kwargs) content = req.read()
pip = await self.which('pip3', 'pip', 'pip2') with open('get-pip.py', 'wb+') as f:
if not pip: f.write(content)
raise Exception('Could not install a pip command')
else:
pip = pip[0]
await target.copy('get-pip.py', '.')
await target.exec(python, 'get-pip.py')
# choose a cache directory
if 'CACHE_DIR' in os.environ: if 'CACHE_DIR' in os.environ:
cache = os.path.join(os.getenv('CACHE_DIR'), 'pip') cache = os.path.join(os.getenv('CACHE_DIR'), 'pip')
else: else:
cache = os.path.join(os.getenv('HOME'), '.cache', 'pip') cache = os.path.join(os.getenv('HOME'), '.cache', 'pip')
if getattr(self, 'mount', None): # and mount it
if getattr(target, 'mount', None):
# we are in a target which shares a mount command # we are in a target which shares a mount command
await self.mount(cache, '/root/.cache/pip') await target.mount(cache, '/root/.cache/pip')
await self.exec(f'{pip} install --upgrade pip')
# https://github.com/pypa/pip/issues/5599 source = []
if 'pip' not in self.kwargs: nonsource = []
pip = 'python3 -m pip' for package in self.pip_packages:
if os.path.exists(package):
source.append(package)
else:
nonsource.append(package)
source = [p for p in self.args if p.startswith('/') or p.startswith('.')] if nonsource:
if source: await target.exec(
await self.exec( python, '-m', 'pip',
f'{pip} install --upgrade --editable {" ".join(source)}' 'install', '--upgrade',
*nonsource
) )
nonsource = [p for p in self.args if not p.startswith('/')] if source:
if nonsource: await target.exec(
await self.exec(f'{pip} install --upgrade {" ".join(nonsource)}') python, '-m', 'pip',
'install', '--upgrade', '--editable',
*source
)
if self.requirements: def __str__(self):
await self.exec(f'{pip} install --upgrade -r {self.requirements}') return f'Pip({", ".join(self.pip_packages)})'

View File

@ -7,10 +7,10 @@ from shlax.shortcuts import *
shlax = Container( shlax = Container(
build=Buildah( build=Buildah(
User('app', '/app', getenv('_CONTAINERS_ROOTLESS_UID')),
Packages('python38', 'buildah', 'unzip', 'findutils'), Packages('python38', 'buildah', 'unzip', 'findutils'),
User('app', '/app', getenv('_CONTAINERS_ROOTLESS_UID')),
Copy('setup.py', 'shlax', '/app'), Copy('setup.py', 'shlax', '/app'),
#Pip('/app', pip='pip3.8'), Pip('/app'),
base='quay.io/podman/stable', base='quay.io/podman/stable',
commit='shlax', commit='shlax',
), ),