summaryrefslogtreecommitdiff
path: root/objective-c/config/makegitignore.py
blob: 0444f3997ad21bc4f68cdfd4bab12fa3b62d3c7f (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
#!/usr/bin/env python
# **********************************************************************
#
# Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved.
#
# This copy of Ice is licensed to you under the terms described in the
# ICE_LICENSE file included in this distribution.
#
# **********************************************************************

import os, sys, shutil, fnmatch, re, time, getopt

#
# NOTE: This scripts generates .gitignore files in directories
# containing Makefile files with targets. The content of the
# .gitignore file is generated by parsing the output of make -n
# clean. 
#
# In other words, the .gitignore file contains ignore rules for files
# produced by the Makefile and supposed to be cleaned by make clean.
#

progname = os.path.basename(sys.argv[0])
preamble = "// Generated by " + progname
preamble = preamble + """

// IMPORTANT: Do not edit this file -- any edits made here will be lost!
"""

#
# Find files matching a pattern.
#
def find(path, patt):
    result = [ ]
    files = os.listdir(path)
    for x in files:
        fullpath = os.path.join(path, x);
        if os.path.isdir(fullpath) and not os.path.islink(fullpath):
            result.extend(find(fullpath, patt))
        elif fnmatch.fnmatch(x, patt):
            result.append(fullpath)
    return result


def createGitIgnore(filename, gitIgnoreFiles):
    file = open(filename, "r")
    lines = file.readlines()
    cwd = os.getcwd()
    cwdStack = [] # Working directory stack
    newLines = [ ]
    ignore = ["*.o", "*.bak", "core"]

    for x in lines:
        x = x.strip()
        if x.startswith("rm -f"):
            x = x.replace("rm -f", "", 1)
        elif x.startswith("rm -rf"):
            x = x.replace("rm -rf", "", 1)
        elif x.startswith("making clean in"):
            # Don't clean sub-directories
            break
        else:
            continue

        if len(x) == 0:
            continue

        files = x.split()
        for f in files:
            if f in ignore:
                continue

            if f.startswith(".."):
                k = os.path.join(cwd, os.path.dirname(f), ".gitignore")
                v = os.path.basename(f) + "\n"
            else:
                k = os.path.join(cwd, ".gitignore")
                v = f + "\n"

            if v.find(".so.") > 0:
                continue                   
            elif v.endswith(".so\n"):
                v = v.replace(".so", ".*")
            elif v.endswith(".dylib\n"):
                v = v.replace(".dylib", ".*")
                if v.find('.', 0, len(v) - 3) > 0:
                    continue

            k = os.path.normpath(k)
            if not gitIgnoreFiles.has_key(k):
                gitIgnoreFiles[k] = [ ]
            gitIgnoreFiles[k].append(v)

    file.close()

def usage():
    print "Usage: " + sys.argv[0] + " [options]"
    print
    print "Options:"
    print "-e    Run for Ice-E."
    print "-h    Show this message."
    print

icee = False
try:
    opts, args = getopt.getopt(sys.argv[1:], "he")
except getopt.GetoptError:
    usage()
    sys.exit(1)
for o, a in opts:
    if o == "-h":
        usage()
        sys.exit(0)
    elif o == "-e":
        icee = True
if len(args) != 0:
    usage()
    sys.exit(1)


#
# Find where the root of the tree is.
#
for toplevel in [".", "..", "../..", "../../..", "../../../.."]:
    toplevel = os.path.abspath(toplevel)
    if os.path.exists(os.path.join(toplevel, "objective-c", "config", "makegitignore.py")):
        break
else:
    print("cannot find top-level directory")
    sys.exit(1)

makefiles = find(os.path.join(toplevel, "objective-c"), "Makefile")
cwd = os.getcwd()
gitIgnoreFiles = { }
for i in makefiles:
    os.chdir(os.path.dirname(i))
    if not os.system('grep -q TARGETS Makefile'):
        try:
            os.system("make -n clean > .tmp-gitignore")
            createGitIgnore(".tmp-gitignore", gitIgnoreFiles)
            os.remove(".tmp-gitignore")
        except:
            os.remove(".tmp-gitignore")
            raise
    os.chdir(cwd)
    
os.chdir(cwd)

excludePath = [ os.path.join(toplevel, "objective-c", "bin"), os.path.join(toplevel, "objective-c", "lib") ]
for (path, files) in gitIgnoreFiles.iteritems():
    if os.path.dirname(path) in excludePath:
        continue
    if not os.path.exists(path):
        print files
    gitIgnore = open(path, "w")
    gitIgnore.write(preamble);
    gitIgnore.writelines(files)
    gitIgnore.close()