1#!/usr/bin/env python
2
3import os
4from sys import argv
5
6# Define the starting directory.
7
8startdir = ""
9
10if len(argv) == 2:
11    startdir = os.path.join(argv[1])
12else:
13    startdir = os.path.join("..") # MoinMoin
14
15# Define a blacklist.
16
17blacklist = ["_tests",
18             os.path.join("script", "old"),
19             "support",
20             os.path.join("filter", "EXIF.py"),
21             os.path.join("web", "static", "htdocs"),
22            ]
23
24# Define an output file for the filenames.
25
26outname_in = "POTFILES.in"
27outname_final = "POTFILES"
28
29# Functions.
30
31def get_files((files, prefix, blacklist), d, names):
32
33    """
34    Store pathnames in 'files', removing 'prefix', excluding those mentioned
35    in the 'blacklist', building such pathnames from the directory 'd' and
36    the given 'names'.
37    """
38
39    for name in names:
40        if name.endswith(".py"):
41            path = os.path.join(d, name)
42
43            # Strip the prefix.
44            if path.startswith(prefix):
45                path = path[len(prefix):]
46
47            # Test for exact blacklist matches.
48            if path in blacklist:
49                continue
50
51            # Test for directory blacklist matches.
52            found = 0
53            for blackitem in blacklist:
54                if path.startswith(blackitem):
55                    found = 1
56                    break
57
58            if not found:
59                files.append(path)
60
61def find_files(startdir, blacklist):
62    "Find files under 'startdir' excluding those in the 'blacklist'."
63
64    # Calculate the prefix from the start directory.
65    prefix = os.path.join(startdir, "")
66
67    # Start with an empty list of files.
68
69    files = []
70    os.path.walk(startdir, get_files, (files, prefix, blacklist))
71    files.sort()
72    return files
73
74if __name__ == "__main__":
75
76    # Find those files using the module defaults.
77    files = find_files(startdir, blacklist)
78
79    # Write the names out.
80    outfile = open(outname_in, "w")
81    try:
82        for file in files:
83            outfile.write(file + "\n")
84    finally:
85        outfile.close()
86
87    # Write the processed list out, ready for other purposes.
88    outfile = open(outname_final, "w")
89    outfile.write("POTFILES = \\\n")
90    try:
91        for file in files[:-1]:
92            outfile.write("\t" + os.path.join(startdir, file) + " \\\n")
93        if files[-1]:
94            file = files[-1]
95            outfile.write("\t" + os.path.join(startdir, file) + "\n")
96    finally:
97        outfile.close()
98
99# vim: tabstop=4 expandtab shiftwidth=4
100