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

5
import os
6
7
8
9
import subprocess
import sys

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

19
20
21
22

project = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
wd = os.path.join(project, 'localconfig')
if not os.path.isdir(wd):
23
    os.makedirs(wd)
24

25
26
27
28
29
paths = os.getenv('PYTHONPATH', None)
if not paths:
    paths = sys.path
else:
    paths = paths.split(':')
30
31
if project not in paths:
    paths.insert(0, project)
32
env = os.environ.copy()
33
env['PYTHONPATH'] = ':'.join(p for p in paths if p)
34
env['WEBOOB_WORKDIR'] = wd
35
env['WEBOOB_DATADIR'] = wd
36
37
env['WEBOOB_BACKENDS'] = os.getenv('WEBOOB_LOCAL_BACKENDS',
                                   os.getenv('WEBOOB_BACKENDS',
38
                                             os.path.join(os.environ.get('XDG_CONFIG_HOME', os.path.join(os.path.expanduser('~'), '.config')), 'weboob', 'backends')))
39

40
modpath = os.getenv('WEBOOB_MODULES', os.path.join(project, 'modules'))
41
with open(os.path.join(wd, 'sources.list'), 'w') as f:
42
    f.write("file://%s\n" % modpath)
43
44
45

# Hide output unless there is an error
p = subprocess.Popen(
46
    [sys.executable, os.path.join(project, 'scripts', 'weboob-config'), 'update', '-d'],
47
    env=env,
48
    stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
49
50
s = p.communicate()
if p.returncode != 0:
51
    print(s[0])
52
53
    if p.returncode > 1:
        sys.exit(p.returncode)
54

55
56
57
58
59
if os.path.exists(script):
    spath = script
else:
    spath = os.path.join(project, 'scripts', script)

60
61
os.execvpe(
    sys.executable,
62
    [sys.executable, '-s'] + pyargs + [spath] + args,
63
    env)