]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - check-unused-files.py
e9edd0516500be768d6ef8ae00d9735a0070100b
[packages/rpm-build-tools.git] / check-unused-files.py
1 #!/usr/bin/python
2
3 import subprocess
4 import sys
5 import os
6 import fnmatch
7
8 def 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
14 if len(sys.argv) == 2:
15     spec = sys.argv[1]
16 else:
17     # try autodetecting
18     spec = "%s.spec" % os.path.basename(os.getcwd())
19
20 if not os.path.isfile(spec):
21     print >> sys.stderr, "%s: %s doesn't exist!" % (sys.argv[0], spec)
22     sys.exit(1)
23
24 dir = os.path.dirname(spec)
25 if dir == '':
26     dir = '.'
27
28 (out, err) = specdump(spec)
29 if err:
30     print >> sys.stderr, "%s: %s" % (sys.argv[0], err)
31     sys.exit(1)
32
33 files = []
34
35 for 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
42 obsolete = []
43
44 # files to exclude
45 exclude = ['log.*', '.#*', '*~', '*.orig', '*.sw?', '.bigfiles', 'sources']
46
47 # read .gitignore, distfiles files are filled there
48 if os.path.isfile('%s/.gitignore' % dir):
49     f = open('%s/.gitignore' % dir , 'r')
50     for l in f.readlines():
51         exclude.append(l.rstrip())
52
53 def 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')
61 gitfiles = git_entries(dir)
62
63 def blacklisted(file):
64     if file == os.path.basename(spec):
65         return True
66
67     if file in [ '.', '..', '.git', 'CVS', 'TODO']:
68         return True
69
70     if os.path.isdir(file):
71         return True
72
73     for pat in exclude:
74         if fnmatch.fnmatch(file, pat):
75             return True
76
77     return False
78
79 for file in os.listdir(dir):
80     file = os.path.basename(file)
81     if blacklisted(file):
82         continue
83
84     if not file in gitfiles:
85         print "Not in repo: %s" % file
86         continue
87
88     if file not in files:
89         print "Obsolete file: %s" % file
90         obsolete.append(file)
91
92 if obsolete:
93     print
94     print "git rm %s" % " ".join(obsolete)
95     print "git commit -m '- drop obsolete files' %s" % " ".join(obsolete)
This page took 0.039419 seconds and 2 git commands to generate.