~aelkner/schooltool/cambodia_fixes

« back to all changes in this revision

Viewing changes to remove-stale-bytecode.py

Created a skeleton for SchoolTool Calendaring 1.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python2.3
 
2
#
 
3
# SchoolTool - common information systems platform for school administration
 
4
# Copyright (c) 2003 Shuttleworth Foundation
 
5
#
 
6
# This program is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program; if not, write to the Free Software
 
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
#
 
20
"""
 
21
Recursively removes .pyc and .pyo files without a corresponding .py file.
 
22
 
 
23
Usage: %(progname)s [options] [dirname ...]
 
24
 
 
25
Options:
 
26
    -h, --help      this message
 
27
    -v, --verbose   print names of files before they are removed (default)
 
28
    -q, --quiet     the opposite of --verbose
 
29
    -n, --dry-run   do not actually remove the files (implies --verbose)
 
30
 
 
31
If dirname is ommitted, %(progname)s starts looking for stale
 
32
bytecode files in the current directory.
 
33
"""
 
34
 
 
35
import os
 
36
import sys
 
37
import getopt
 
38
 
 
39
 
 
40
def main(argv=sys.argv):
 
41
    progname = os.path.basename(argv[0])
 
42
    helpmsg = __doc__.strip() % {'progname': progname}
 
43
    verbose = True
 
44
    dry_run = False
 
45
    try:
 
46
        opts, args = getopt.getopt(argv[1:], 'vqnh',
 
47
                                   ['verbose', 'quiet', 'dry-run', 'help'])
 
48
    except getopt.error, e:
 
49
        print >> sys.stderr, e
 
50
        print >> sys.stderr, 'try %s --help' % progname
 
51
        sys.exit(1)
 
52
    for k, v in opts:
 
53
        if k in ('-v', '--verbose'):
 
54
            verbose = True
 
55
        elif k in ('-q', '--quiet'):
 
56
            verbose = False
 
57
        elif k in ('-n', '--dry-run'):
 
58
            dry_run = True
 
59
            verbose = True
 
60
        elif k in ('-h', '--help'):
 
61
            print helpmsg
 
62
            sys.exit(0)
 
63
    if not args:
 
64
        args = ['.']
 
65
    for root in args:
 
66
        for dirpath, dirnames, filenames in os.walk(root):
 
67
            filenames = map(os.path.normcase, filenames)
 
68
            for filename in filenames:
 
69
                if filename.endswith('.pyc') or filename.endswith('pyo'):
 
70
                    sourcename = filename[:-1]
 
71
                    if sourcename not in filenames:
 
72
                        fullname = os.path.join(dirpath, filename)
 
73
                        if verbose:
 
74
                            print "Removing", fullname
 
75
                        if not dry_run:
 
76
                            try:
 
77
                                os.unlink(fullname)
 
78
                            except os.error, e:
 
79
                                print >> sys.stderr, ("%s: %s: %s" %
 
80
                                                      (progname, fullname, e))
 
81
 
 
82
 
 
83
if __name__ == '__main__':
 
84
    main()