summaryrefslogtreecommitdiff
blob: fd4b0101fe3647ddf0306763c6b15a0b24257a69 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/python -O
#
# /usr/sbin/webapp-config
#       Python script for managing the deployment of web-based
#       applications
#
#       Originally written for the Gentoo Linux distribution
#
# Copyright (c) 1999-2007 Authors
#       Released under v2 of the GNU GPL
#
# Author(s)     Stuart Herbert
#               Renat Lumpau   <rl03@gentoo.org>
#               Gunnar Wrobel  <wrobel@gentoo.org>
#
# ========================================================================
'''
This helper module intends to provide a wrapper for some Gentoo
specific features used by webapp-config. This might make it easier
to use the tool on other distributions.
'''

__version__ = "$Id: wrapper.py 283 2006-04-20 22:53:04Z wrobel $"

# ========================================================================
# Dependencies
# ------------------------------------------------------------------------

import os, string

from WebappConfig.debug       import OUT

from WebappConfig.debug   import OUT
from WebappConfig.version import WCVERSION

# ========================================================================
# Portage Wrapper
# ------------------------------------------------------------------------

protect_prefix  = '._cfg'
update_command  = 'etc-update'

# Link for bug reporting
bugs_link  = 'http://bugs.gentoo.org/'

def config_protect(cat, pn, pvr, pm):
    '''Return CONFIG_PROTECT (used by protect.py)'''
    if pm == "portage":
        try:
            import portage
        except ImportError as e:
            OUT.die("Portage libraries not found, quitting:\n%s" % e)

        return portage.settings['CONFIG_PROTECT']

    elif pm == "paludis":
        cmd="paludis --log-level silent --no-color --environment-variable %s/%s CONFIG_PROTECT" % (cat,pn)

        fi, fo, fe = os.popen3(cmd)
        fi.close()
        result_lines = fo.readlines()
        fo.close()
        fe.close()

        return string.join(result_lines, ' ').strip()
    else:
        OUT.die("Unknown package manager: " + pm)

def config_libdir(pm):
    OUT.die("I shouldn't get called at all")

def want_category(config):
    '''Check if the package manager requires category info

    Portage: optional
    Paludis: mandatory
    '''

    if config.config.get('USER', 'package_manager') == "portage":
        return
    elif config.config.get('USER', 'package_manager') == "paludis":
        if not config.config.has_option('USER', 'cat'):
            OUT.die("Package name must be in the form CAT/PN")
    else:
        OUT.die("Unknown package manager: " + pm)

def get_root(config):
    '''Returns the $ROOT variable'''
    if config.config.get('USER', 'package_manager') == "portage":
        try:
            import portage
        except ImportError as e:
            OUT.die("Portage libraries not found, quitting:\n%s" % e)

        return portage.root

    elif config.config.get('USER', 'package_manager') == "paludis":
        cat = config.maybe_get('cat')
        pn  = config.maybe_get('pn')

        if cat and pn:
            cmd="paludis --log-level silent --no-color --environment-variable %s/%s ROOT" % (cat,pn)

            fi, fo, fe = os.popen3(cmd)
            fi.close()
            result_lines = fo.readlines()
            fo.close()
            fe.close()

            if result_lines[0].strip():
                return result_lines[0].strip()
            else:
                return '/'
        else:
            return '/'
    else:
        OUT.die("Unknown package manager: " + pm)

def package_installed(full_name, pm):
    '''
    This function identifies installed packages.
    The Portage part is stolen from gentoolkit.
    We are not using gentoolkit directly as it doesn't seem to support ${ROOT}
    '''

    if pm == "portage":
        try:
            import portage
        except ImportError as e:
            OUT.die("Portage libraries not found, quitting:\n%s" % e)

        try:
             t = portage.db[portage.root]["vartree"].dbapi.match(full_name)
        # catch the "ambiguous package" Exception
        except ValueError as e:
            if isinstance(e[0], list):
                t = []
                for cp in e[0]:
                    t += portage.db[portage.root]["vartree"].dbapi.match(cp)
            else:
                raise ValueError(e)
        return t

    elif pm == "paludis":

        cmd="paludis --best-version %s" % (full_name)

        fi, fo, fe = os.popen3(cmd)
        fi.close()
        result_lines = fo.readlines()
        error_lines  = fe.readlines()
        fo.close()
        fe.close()

        if error_lines:
            for i in error_lines:
                OUT.warn(i)

        return string.join(result_lines, ' ')

    else:
        OUT.die("Unknown package manager: " + pm)

if __name__ == '__main__':
    OUT.info('\nPACKAGE MANAGER WRAPPER')
    OUT.info('---------------\n')
    if package_installed('=app-admin/webapp-config-' + WCVERSION, 'portage'):
        a = 'YES'
    else:
        a = 'NO'

    OUT.info('package_installed("webapp-config-'
             + WCVERSION + '") : ' + a + '\n')
    OUT.info('config_protect : ' + config_protect('app-admin','webapp-config',WCVERSION,'portage'))
    OUT.info('protect_prefix : ' + protect_prefix)
    OUT.info('update_command : ' + update_command)
    OUT.info('bugs_link : ' + bugs_link)