local_run.py 1.86 KB
Newer Older
1
2
#!/usr/bin/env python
# -*- 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
23

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):
24
    os.makedirs(wd)
25

26
27
28
29
30
paths = os.getenv('PYTHONPATH', None)
if not paths:
    paths = sys.path
else:
    paths = paths.split(':')
31
32
if project not in paths:
    paths.insert(0, project)
33
env = os.environ.copy()
34
env['PYTHONPATH'] = ':'.join(p for p in paths if p)
35
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
p = subprocess.Popen(
49
    [sys.executable, os.path.join(project, 'scripts', 'weboob-config'), 'update', '-d'],
50
    env=env,
51
    stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
52
53
s = p.communicate()
if p.returncode != 0:
54
    print(s[0])
55
56
    if p.returncode > 1:
        sys.exit(p.returncode)
57

58
59
60
61
62
if os.path.exists(script):
    spath = script
else:
    spath = os.path.join(project, 'scripts', script)

63
64
os.execvpe(
    sys.executable,
65
    [sys.executable, '-s'] + pyargs + [spath] + args,
66
    env)