aboutsummaryrefslogtreecommitdiff
path: root/lib/dodai/build/after_install.py
blob: 16c1ec6aee103e38a0b7f92c1c17812bd8d7c80f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import os
from collections import namedtuple
from subprocess import Popen
from subprocess import call
from subprocess import PIPE

def build_paths(home_dir):

    obj = namedtuple('Paths', 'wd, home, bin, src, python')

    wd = os.getcwd()
    if home_dir.startswith(os.sep):
        home = home_dir
    else:
        home = os.path.realpath(os.path.join(wd, home_dir))
    bin = os.path.join(home, 'bin')
    src = os.path.join(home, 'src')
    python = os.path.join(bin, 'python')

    return obj(wd, home, bin, src, python)


class EasyInstall(object):

    def __init__(self, paths):
        self._easy_install = os.path.join(paths.bin, 'easy_install')

    def __call__(self, package):
        cmd = [self._easy_install, '-Uv', package]
        call(cmd)


class HgClone(object):

    def __init__(self, paths, easy_install=EasyInstall):
        self._easy_install = easy_install(paths)
        self._paths = paths
        self._hg_ = None

    @property
    def _hg(self):
        if not self._hg_:
            cmd = ['which', 'hg']
            self._hg_ = Popen(cmd, stdout=PIPE, stderr=PIPE).communicate()[0]\
                            .strip()
            if not self._hg_:
                self._hg_ = os.path.join(self._paths.bin, 'hg')
                if not os.path.exists(self._hg_):
                    self._easy_install('mercurial')
        return self._hg_

    def __call__(self, url, dest):
        if not dest.startswith(os.sep):
            dest = os.path.join(self._paths.home, dest)
        cmd = [self._hg, 'clone', url, dest]
        call(cmd)


class SetupPy(object):

    FILE = 'setup.py'

    def __init__(self, paths):
        self._paths = paths

    def __call__(self, dir, command):
        path = os.path.join(self._paths.home, dir)
        name = os.path.join(path, self.FILE)
        if os.path.exists(name):
            os.chdir(path)
            cmd = [self._paths.python, name, command]
            call(cmd)

class RunPythonProgram(object):

    def __init__(self, paths):
        self._paths = paths

    def __call__(self, program):
        path = os.path.join(self._paths.bin, program)
        if os.path.exists(path):
            cmd = [self._paths.python, path]
            call(cmd)