File: multifind.py

package info (click to toggle)
fritzing 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 42,144 kB
  • sloc: cpp: 98,203; xml: 2,203; python: 803; sh: 274; ansic: 26; makefile: 22
file content (80 lines) | stat: -rw-r--r-- 2,134 bytes parent folder | download | duplicates (2)
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
# usage:
#    multifind.py -d [directory] [-f [text]]+   -s [suffix]
#
#    directory is a folder containing [suffix] files.
#    In each [suffix] file in the directory or its subfolders,
#    display all files that match all the texts ('and' search)

import getopt, sys, os, re

def usage():
    print """
usage:
    multifind.py -d [directory] [-f [text]]+   -s [suffix]

    directory is a folder containing [suffix] files.
    In each [suffix] file in the directory or its subfolders,
    display all files that match all the texts ('and' search)
    """



def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "hd:f:s:", ["help", "directory", "find", "suffix"])
    except getopt.GetoptError, err:
        # print help information and exit:
        print str(err) # will print something like "option -a not recognized"
        usage()
        sys.exit(2)
    inputDir = None
    findtexts = []
    suffix = ""

    for o, a in opts:
        #print o
        #print a
        if o in ("-d", "--directory"):
            inputDir = a
        elif o in ("-h", "--help"):
            usage()
            sys.exit(2)
        elif o in ("-f", "--find"):
            findtexts.append(a)
        elif o in ("-s", "--suffix"):
            suffix = a;

        else:
            assert False, "unhandled option"

    if(not(inputDir)):
        usage()
        sys.exit(2)

    if len(findtexts) <= 0:
        usage()
        sys.exit(2)

    for root, dirs, files in os.walk(inputDir, topdown=False):
        for filename in files:
            if (filename.endswith(suffix)):
                infile = open(os.path.join(root, filename), "r")
                content = infile.read();
                infile.close();

                found = 0
                for findtext in findtexts:
                    rslt = content.find(findtext)
                    if (rslt >= 0):
                        found += 1
                    else:
                        break

                if found == len(findtexts):
                    print "found {0}".format(os.path.join(root, filename))




if __name__ == "__main__":
    main()