aboutsummaryrefslogtreecommitdiff
blob: 0581accea3ab49a375f299223ae744d817f8f558 (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
#!/usr/bin/env python3
#
# Copyright (c) Karl Trygve Kalleberg <karltk@gentoo.org>
# Copyright (c) Fabio Lessa <flessa@gmail.com>
# Copyright (c) 2005, Gentoo Foundation
#
# Python rewrite from the bash findclass initially performed
# by Fabio.
#
# Licensed under the GNU General Public License, v2.
#

import os
import re
import sys
import glob
from optparse import OptionParser
from subprocess import getstatusoutput
from java_config.jc_util import find_exec, collect_packages


__author__ = "Karl Trygve Kalleberg <karltk@gentoo.org> and Fabio Lessa <flessa@gmail.com>"
__version__ = "0.1.0"
__productname__ = "findclass"
__description__ = "Gentoo Java Class Query Tool"


def parse_args():
    usage = 'findclass [options] class.or.package.Name'
    about = __productname__ + " : " + __description__ + "\n" + \
        "Authors : " + __author__ + \
        "Version : " + __version__

    parser = OptionParser(usage, version=about)
    parser.add_option('-v', '--verbose', action='store_true',
                      dest='verbose', help='generate verbose output')
    opt, files = parser.parse_args()

    if len(files) < 1:
        parser.error("Must supply at least one class or package name")

    return opt, files


def main():
    opt, files = parse_args()

    jarcmd = find_exec('jar')

    javapaths = [ f.replace('.', '/') for f in files ]
    matchers = [ re.compile(p) for p in javapaths ]  

    for pkg in get_all_packages():
        if opt.verbose: print("Searching package %s" % pkg)
        for jar in collect_packages(pkg).split(':'):
            if opt.verbose: print("Searching jar %s" % jar)
            status, out = getstatusoutput("%s tvf %s" % (jarcmd, jar))
            for m in matchers:
                if m.search(out):
                    if opt.verbose: print("Found in %s" % pkg, end=' ')
                    print(jar)


def get_all_packages():
    pkg = glob.glob('/usr/share/*/package.env')
    pkg = [os.path.basename(os.path.dirname(i)) for i in pkg]

    classpath = glob.glob('/usr/share/*/classpath.env')
    classpath = [os.path.basename(os.path.dirname(i)) for i in classpath]

    dir = glob.glob('/usr/share/java/packages/*')
    dir = [os.path.basename(i) for i in dir]

    pkg.extend(classpath)
    pkg.extend(dir)
    return pkg


if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        print("Interrupted by user, aborting.")