local_run.py 2.06 KB
Newer Older
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3 4
from __future__ import print_function

5
import os
6 7
import subprocess
import sys
8
import tempfile
9 10

if len(sys.argv) < 2:
11
    print("Usage: %s SCRIPTNAME [args]" % sys.argv[0])
12 13
    sys.exit(1)
else:
14 15 16 17 18 19
    args = sys.argv[1:]
    pyargs = []
    while args and args[0].startswith('-'):
        pyargs.append(args.pop(0))
    script = args.pop(0)

20 21 22

project = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))

23 24 25 26 27 28 29 30 31 32 33

def get_project_dir(name):
    wd = os.path.join(project, name)
    if not os.path.isdir(wd):
        os.makedirs(wd)
    return wd


wd = get_project_dir('localconfig')
venv = get_project_dir('localenv')

34 35
env = os.environ.copy()
env['WEBOOB_WORKDIR'] = wd
36
env['WEBOOB_DATADIR'] = wd
37 38
env['WEBOOB_BACKENDS'] = os.getenv('WEBOOB_LOCAL_BACKENDS',
                                   os.getenv('WEBOOB_BACKENDS',
39
                                             os.path.join(os.environ.get('XDG_CONFIG_HOME', os.path.join(os.path.expanduser('~'), '.config')), 'weboob', 'backends')))
40

41
modpath = os.getenv('WEBOOB_MODULES', os.path.join(project, 'modules'))
42 43

with tempfile.NamedTemporaryFile(mode='w', dir=wd, delete=False) as f:
44
    f.write("file://%s\n" % modpath)
45
os.rename(f.name, os.path.join(wd, 'sources.list'))
46

47

48
# Hide output unless there is an error
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
def run_quiet(cmd):
    p = subprocess.Popen(
        cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env,
    )
    s = p.communicate()
    if p.returncode != 0:
        print(s[0].decode('utf-8'))
        if p.returncode > 1:
            sys.exit(p.returncode)


venv_exe = os.path.join(venv, 'bin', 'python')
run_quiet([
    sys.executable, '-m', 'virtualenv', '--system-site-packages',
    '--python', sys.executable, venv,
])
run_quiet([
    venv_exe, '-m', 'pip', 'install', '--no-deps', '--editable', project,
])
run_quiet([os.path.join(venv, 'bin', 'weboob-config'), 'update', '-d'])
69

70 71 72
if os.path.exists(script):
    spath = script
else:
73
    spath = os.path.join(venv, 'bin', script)
74

75
os.execvpe(
76 77
    venv_exe,
    [venv_exe, '-s'] + pyargs + [spath] + args,
78
    env)