aboutsummaryrefslogtreecommitdiff
path: root/enumerate_files.py
blob: 06ebf9d44407cab106e04ee9e63a723ce32d1bb0 (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
import fnmatch
import os
import sys

dirs = [ ]
types = [ ]
excludes = [ ]
files = [ ]

# Default to accepting a list of directories first
curArray = dirs

# Iterate over the arguments and add them to the arrays
for i in range(1, len(sys.argv)):
    arg = sys.argv[i]

    if arg == "-dirs":
        curArray = dirs
        continue

    if arg == "-types":
        curArray = types
        continue

    if arg == "-excludes":
        curArray = excludes
        continue

    curArray.append(arg)

# If no directories were specified, use the current directory
if len(dirs) == 0:
    dirs.append(".")

# If no types were specified, accept all types
if len(types) == 0:
    types.append("*")

# Walk the directories listed and compare with type and exclude lists
for rootdir in dirs:
    for root, dirnames, filenames in os.walk(rootdir):
        for file in filenames:
            # Skip files that are "hidden"
            if file.startswith("."):
                continue;

            fullPath = os.path.join(root, file).replace("\\", "/")
            for type in types:
                if fnmatch.fnmatchcase(fullPath, type):
                    excluded = False
                    for exclude in excludes:
                        if fnmatch.fnmatchcase(fullPath, exclude):
                            excluded = True
                            break

                    if not excluded:
                        files.append(fullPath)
                        break

files.sort()
for file in files:
    print file