aboutsummaryrefslogtreecommitdiff
path: root/lib/dodai/build/after_install.py
blob: d84739e091277dd2efbb2336a1ba813d95d9264b (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#
#    Copyright 2010 Leonard Thomas
#
#    This file is part of Dodai.
#
#    Dodai is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Dodai is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Dodai.  If not, see <http://www.gnu.org/licenses/>.



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')
                    self._hg_ = os.path.join(self._paths.bin, 'hg')
        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)