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