summaryrefslogtreecommitdiffstats
path: root/Tools/scripts/chkdepschain.py
blob: f8d5c757507aee7683518439f011562932e8f6a9 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/local/bin/python
#
# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42, (c) Poul-Henning Kamp):
# Maxim Sobolev <sobomax@FreeBSD.org> wrote this file.  As long as you retain
# this notice you can do whatever you want with this stuff. If we meet some
# day, and you think this stuff is worth it, you can buy me a beer in return.
#
# Maxim Sobolev
# ----------------------------------------------------------------------------
#
# $FreeBSD$
#
# MAINTAINER= sobomax@FreeBSD.org <- any unapproved commits to this file are
#                                    highly discouraged!!!
#

import os, os.path, popen2, types, sys, getopt, pickle

# Global constants and semi-constants
PKG_DBDIR = '/var/db/pkg'
PORTSDIR = '/usr/ports'
ROOT_PORTMK = '/usr/share/mk/bsd.port.mk'
PLIST_FILE = '+CONTENTS'
ORIGIN_PREF = '@comment ORIGIN:'
MAKEFILE = 'Makefile'
MAKE = 'make'
ERR_PREF = 'Error:'
WARN_PREF = 'Warning:'

# Global variables
#
# PortInfo cache
picache = {}

# Useful aliases
op_isdir = os.path.isdir
op_join = os.path.join
op_split = os.path.split
op_abspath = os.path.abspath


#
# Query origin of specified installed package.
#
def getorigin(pkg):
    plist = op_join(PKG_DBDIR, pkg, PLIST_FILE)
    for line in open(plist).xreadlines():
	if line.startswith(ORIGIN_PREF):
	    origin = line[len(ORIGIN_PREF):].strip()
	    break
    else:
	raise RuntimeError('%s: no origin recorded' % plist)

    return origin


#
# Execute external command and return content of its stdout.
#
def getcmdout(cmdline, filterempty = 1):
    pipe = popen2.Popen3(cmdline, 1)
    results = pipe.fromchild.readlines()
    for stream in (pipe.fromchild, pipe.tochild, pipe.childerr):
        stream.close()

    if pipe.wait() != 0:
        if type(cmdline) is types.StringType:
            cmdline = (cmdline)
        raise IOError('%s: external command returned non-zero error code' % \
          cmdline[0])

    if filterempty != 0:
	results = filter(lambda line: len(line.strip()) > 0, results)

    return results


#
# For a specified path (either dir or makefile) query requested make(1)
# variables and return them as a tuple in exactly the same order as they
# were specified in function call, i.e. querymakevars('foo', 'A', 'B') will
# return a tuple with a first element being the value of A variable, and
# the second one - the value of B.
#
def querymakevars(path, *vars):
    if op_isdir(path):
	path = op_join(path, MAKEFILE)
    dirname, makefile = op_split(path)
    cmdline = [MAKE, '-f', makefile]
    savedir = os.getcwd()
    os.chdir(dirname)
    try:
        for var in vars:
	    cmdline.extend(('-V', var))

        results = map(lambda line: line.strip(), getcmdout(cmdline, 0))
    finally:
        os.chdir(savedir)

    return tuple(results)


def parsedeps(depstr):
    return tuple(map(lambda dep: dep.split(':'), depstr.split()))


#
# For a specified port return either a new instance of the PortInfo class,
# or existing instance from the cache.
#
def getpi(path):
    path = op_abspath(path)
    if not picache.has_key(path):
        picache[path] = PortInfo(path)
    return picache[path]


#
# Format text string according to requested constrains. Useful when you have
# to display multi-line, variable width message on terminal.
#
def formatmsg(msg, wrapat = 78, seclindent = 0):
    words = msg.split()
    result = ''
    position = 0
    for word in words:
        if position + 1 + len(word) > wrapat:
            result += '\n' + ' ' * seclindent + word
            position = seclindent + len(word)
        else:
            if position != 0:
                result += ' '
                position += 1
            result += word
            position += len(word)

    return result


#
# Class that contain main info about the port
#
class PortInfo:
    PKGNAME = None
    CATEGORIES = None
    MAINTAINER = None
    BUILD_DEPENDS = None
    LIB_DEPENDS = None
    RUN_DEPENDS = None
    PKGORIGIN = None
    # Cached values, to speed-up things
    __deps = None
    __bt_deps = None
    __rt_deps = None

    def __init__(self, path):
        self.PKGNAME, self.CATEGORIES, self.MAINTAINER, self.BUILD_DEPENDS, \
          self.LIB_DEPENDS, self.RUN_DEPENDS, self.PKGORIGIN = \
          querymakevars(path, 'PKGNAME', 'CATEGORIES', 'MAINTAINER', \
          'BUILD_DEPENDS', 'LIB_DEPENDS', 'RUN_DEPENDS', 'PKGORIGIN')

    def __str__(self):
        return 'PKGNAME:\t%s\nCATEGORIES:\t%s\nMAINTAINER:\t%s\n' \
          'BUILD_DEPENDS:\t%s\nLIB_DEPENDS:\t%s\nRUN_DEPENDS:\t%s\n' \
          'PKGORIGIN:\t%s' % (self.PKGNAME, self.CATEGORIES, self.MAINTAINER, \
          self.BUILD_DEPENDS, self.LIB_DEPENDS, self.RUN_DEPENDS, \
          self.PKGORIGIN)

    def getdeps(self):
        if self.__deps == None:
            result = []
            for depstr in self.BUILD_DEPENDS, self.LIB_DEPENDS, \
              self.RUN_DEPENDS:
                deps = tuple(map(lambda dep: dep[1], parsedeps(depstr)))
                result.append(deps)
            self.__deps = tuple(result)
        return self.__deps

    def get_bt_deps(self):
        if self.__bt_deps == None:
            topdeps = self.getdeps()
            topdeps = list(topdeps[0] + topdeps[1])
            for dep in topdeps[:]:
                botdeps = filter(lambda dep: dep not in topdeps, \
                  getpi(dep).get_rt_deps())
                topdeps.extend(botdeps)
            self.__bt_deps = tuple(topdeps)
        return self.__bt_deps
        
    def get_rt_deps(self):
        if self.__rt_deps == None:
            topdeps = self.getdeps()
            topdeps = list(topdeps[1] + topdeps[2])
            for dep in topdeps[:]:
                botdeps = filter(lambda dep: dep not in topdeps, \
                  getpi(dep).get_rt_deps())
                topdeps.extend(botdeps)
            self.__rt_deps = tuple(topdeps)
        return self.__rt_deps


def write_msg(*message):
    if type(message) == types.StringType:
        message = message,
    message = tuple(filter(lambda line: line != None, message))
    sys.stderr.writelines(message)


#
# Print optional message and usage information and exit with specified exit
# code.
#
def usage(code, msg = None):
    myname = os.path.basename(sys.argv[0])
    if msg != None:
        msg = str(msg) + '\n'
    write_msg(msg, "Usage: %s [-rb] [-l|L cachefile] [-s cachefile]\n" % \
      myname)
    sys.exit(code)


def main():
    global picache

    # Parse command line arguments
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'erbl:L:s:')
    except getopt.GetoptError, msg:
        usage(2, msg)

    if len(args) > 0 or len(opts) == 0 :
        usage(2)

    cachefile = None
    chk_bt_deps = 0
    chk_rt_deps = 0
    warn_as_err = 0
    for o, a in opts:
        if o == '-b':
            chk_bt_deps = 1
        elif o == '-r':
            chk_rt_deps = 1
        elif o in ('-l', '-L'):
            # Try to load saved PortInfo cache
            try:
                picache = pickle.load(open(a))
            except:
                picache = {}
            try:
                if o == '-L':
                    os.unlink(a)
            except:
                pass
        elif o == '-s':
            cachefile = a
        elif o == '-e':
            warn_as_err = 1

    # Load origins of all installed packages
    instpkgs = os.listdir(PKG_DBDIR)
    instpkgs = filter(lambda pkg: op_isdir(op_join(PKG_DBDIR, pkg)), instpkgs)
    origins = {}
    for pkg in instpkgs:
        origins[pkg] = getorigin(pkg)

    # Resolve dependencies for the current port
    info = getpi(os.getcwd())
    deps = []
    if chk_bt_deps != 0:
        deps.extend(filter(lambda d: d not in deps, info.get_bt_deps()))
    if chk_rt_deps != 0:
        deps.extend(filter(lambda d: d not in deps, info.get_rt_deps()))

    # Perform validation
    nerrs = 0
    nwarns = 0
    if warn_as_err == 0:
        warn_pref = WARN_PREF
    else:
        warn_pref = ERR_PREF
    err_pref = ERR_PREF
    for dep in deps:
        pi = getpi(dep)
        if pi.PKGORIGIN not in origins.values():
            print formatmsg(seclindent = 7 * 0, msg = \
              '%s package %s (%s) belongs to dependency chain, but ' \
              'isn\'t installed.' % (err_pref, pi.PKGNAME, pi.PKGORIGIN))
            nerrs += 1
        elif pi.PKGNAME not in origins.keys():
            for instpkg in origins.keys():
                if origins[instpkg] == pi.PKGORIGIN:
                    break
            print formatmsg(seclindent = 9 * 0, msg = \
              '%s package %s (%s) belongs to dependency chain, but ' \
              'package %s is installed instead. Perhaps it\'s an older ' \
              'version - update is highly recommended.' % (warn_pref, \
              pi.PKGNAME, pi.PKGORIGIN, instpkg))
            nwarns += 1

    # Save PortInfo cache if requested
    if cachefile != None:
        try:
            pickle.dump(picache, open(cachefile, 'w'))
        except:
            pass

    if warn_as_err != 0:
        nerrs += nwarns

    return nerrs


PORTSDIR, PKG_DBDIR = querymakevars(ROOT_PORTMK, 'PORTSDIR', 'PKG_DBDIR')

if __name__ == '__main__':
    try:
        sys.exit(main())
    except KeyboardInterrupt:
        pass
OpenPOWER on IntegriCloud