]>
Commit | Line | Data |
---|---|---|
7b2ce93a AM |
1 | #!/usr/bin/python |
2 | ||
3 | import subprocess | |
4 | import sys | |
5 | import os | |
8445e70f | 6 | import fnmatch |
7b2ce93a | 7 | |
3e5d0ee4 ER |
8 | def filterout_warnings(err, separator="\n"): |
9 | def filter(): | |
10 | for line in err.split(separator): | |
11 | if line.startswith("warning: "): | |
12 | continue | |
13 | yield line | |
14 | ||
15 | return separator.join(list(filter())) | |
16 | ||
86df512b ER |
17 | def specdump(spec): |
18 | p = subprocess.Popen(['rpm-specdump', spec], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
19 | (out, err) = p.communicate(None) | |
20 | p.wait() | |
3e5d0ee4 ER |
21 | |
22 | err = filterout_warnings(err) | |
23 | ||
86df512b ER |
24 | return (out, err) |
25 | ||
861e13b1 ER |
26 | if len(sys.argv) == 2: |
27 | spec = sys.argv[1] | |
28 | else: | |
29 | # try autodetecting | |
30 | spec = "%s.spec" % os.path.basename(os.getcwd()) | |
7b2ce93a AM |
31 | |
32 | if not os.path.isfile(spec): | |
33 | print >> sys.stderr, "%s: %s doesn't exist!" % (sys.argv[0], spec) | |
34 | sys.exit(1) | |
35 | ||
36 | dir = os.path.dirname(spec) | |
37 | if dir == '': | |
38 | dir = '.' | |
39 | ||
86df512b | 40 | (out, err) = specdump(spec) |
e77c91c6 AM |
41 | if err: |
42 | print >> sys.stderr, "%s: %s" % (sys.argv[0], err) | |
43 | sys.exit(1) | |
7b2ce93a AM |
44 | |
45 | files = [] | |
46 | ||
47 | for l in out.split('\n'): | |
48 | data = l.split() | |
49 | if len(data) != 3 or data[0] != 's': | |
50 | continue | |
51 | file = os.path.basename(data[2]) | |
52 | files.append(file) | |
53 | ||
54 | obsolete = [] | |
55 | ||
b668f2cd | 56 | # files to exclude |
6c3972e0 | 57 | exclude = ['log.*', '.#*', '*~', '*.orig', '*.sw?', '.bigfiles', 'sources'] |
b668f2cd | 58 | |
b8f9713d ER |
59 | # read .gitignore, distfiles files are filled there |
60 | if os.path.isfile('%s/.gitignore' % dir): | |
61 | f = open('%s/.gitignore' % dir , 'r') | |
b668f2cd ER |
62 | for l in f.readlines(): |
63 | exclude.append(l.rstrip()) | |
64 | ||
b8f9713d ER |
65 | def git_entries(file): |
66 | p = subprocess.Popen(['git', 'ls-files'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
67 | (out, err) = p.communicate(None) | |
68 | p.wait() | |
69 | if err: | |
70 | print >> sys.stderr, "%s: %s" % (sys.argv[0], err) | |
71 | sys.exit(1) | |
72 | return out.split('\n') | |
73 | gitfiles = git_entries(dir) | |
4f1efb15 | 74 | |
8445e70f | 75 | def blacklisted(file): |
84e1e6d0 ER |
76 | if file == os.path.basename(spec): |
77 | return True | |
78 | ||
b8f9713d | 79 | if file in [ '.', '..', '.git', 'CVS', 'TODO']: |
8445e70f ER |
80 | return True |
81 | ||
1e0688bb AM |
82 | if os.path.isdir(file): |
83 | return True | |
84 | ||
b668f2cd | 85 | for pat in exclude: |
8445e70f ER |
86 | if fnmatch.fnmatch(file, pat): |
87 | return True | |
88 | ||
89 | return False | |
90 | ||
91 | for file in os.listdir(dir): | |
92 | file = os.path.basename(file) | |
93 | if blacklisted(file): | |
82a3c8b3 | 94 | continue |
8445e70f | 95 | |
b8f9713d ER |
96 | if not file in gitfiles: |
97 | print "Not in repo: %s" % file | |
4f1efb15 ER |
98 | continue |
99 | ||
7b2ce93a AM |
100 | if file not in files: |
101 | print "Obsolete file: %s" % file | |
102 | obsolete.append(file) | |
103 | ||
c7c88163 AM |
104 | if obsolete: |
105 | ||
b8f9713d ER |
106 | print "git rm %s" % " ".join(obsolete) |
107 | print "git commit -m '- drop obsolete files' %s" % " ".join(obsolete) |