summaryrefslogtreecommitdiff
path: root/cpp/config/makegitignore.py
blob: 0d82f142a3f10acceb8d5bc669a0bce0609267aa (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
#!/usr/bin/env python
# **********************************************************************
#
# Copyright (c) 2003-2007 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

#
# 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()

    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)
        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"

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

    file.close()

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

makefiles = find(os.path.join(toplevel, "cpp"), "Makefile")
makefiles = makefiles + find(os.path.join(toplevel, "cppe"), "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, "cpp", "bin"), os.path.join(toplevel, "cpp", "lib"), \
                os.path.join(toplevel, "cppe", "bin"), os.path.join(toplevel, "cppe", "lib") ]
print excludePath
for (path, files) in gitIgnoreFiles.iteritems():
    if os.path.dirname(path) in excludePath:
        continue
    
    gitIgnore = open(path, "w")
    gitIgnore.write(preamble);
    gitIgnore.writelines(files)
    gitIgnore.close()