File: py_compilefiles

package info (click to toggle)
python-central 0.6.16%2Bnmu1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 280 kB
  • ctags: 234
  • sloc: python: 2,585; perl: 522; sh: 135; makefile: 58
file content (117 lines) | stat: -rwxr-xr-x 3,723 bytes parent folder | download | duplicates (3)
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
#! /usr/bin/python

import os
import sys
import py_compile

def compile_files(files, ddir=None, force=0, rx=None, quiet=0, ignore=0):
    """Byte-compile all file.
    file:      the file to byte-compile
    ddir:      if given, purported directory name (this is the
               directory name that will show up in error messages)
    force:     if 1, force compilation, even if timestamps are up-to-date
    quiet:     if 1, be quiet during compilation

    """

    success = 1
    dfile = None
    for fullname in files:
        if rx is not None:
            mo = rx.search(fullname)
            if mo:
                continue
        if os.path.isdir(fullname):
            continue
        elif not os.path.isfile(fullname):
            print "file does not exist:", fullname
            success = 0
        elif fullname[-3:] == '.py':
            cfile = fullname + (__debug__ and 'c' or 'o')
            ftime = os.stat(fullname).st_mtime
            try: ctime = os.stat(cfile).st_mtime
            except os.error: ctime = 0
            if (ctime > ftime) and not force: continue
            if not quiet:
                print 'Compiling', fullname, '...'
            try:
                ok = py_compile.compile(fullname, None, dfile, True)
            except KeyboardInterrupt:
                raise KeyboardInterrupt
            except py_compile.PyCompileError,err:
                if quiet:
                    print 'Compiling', fullname, '...'
                print err.msg
                success = 0
            except (MemoryError, SyntaxError),err:
                if quiet:
                    print 'Compiling', fullname, '...'
                print err.msg
                success = 0
            except IOError, e:
                print "Sorry", e
                success = 0
            else:
                if ok == 0:
                    success = 0
    if not success and ignore:
        print "Errors were ignored."
    return success or ignore

def main():
    """Script main program."""
    import getopt
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'lfiqd:x:')
    except getopt.error, msg:
        print msg
        print "usage: python compilefiles.py [-f] [-q] [-i] " \
              "[-x regexp] [file ...] [-]"
        print "-f: force rebuild even if timestamps are up-to-date"
        print "-i: ignore errors during byte compilation"
        print "-q: quiet operation"
        print "-x regexp: skip files matching the regular expression regexp"
        print "   the regexp is search for in the full path of the file"
        sys.exit(2)
    ddir = None
    force = 0
    quiet = 0
    ignore = 0
    rx = None
    for o, a in opts:
        if o == '-d': ddir = a
        if o == '-f': force = 1
        if o == '-i': ignore = 1
        if o == '-q': quiet = 1
        if o == '-x':
            import re
            rx = re.compile(a)
    if ddir:
        if len(args) != 1:
            print "-d destdir require exactly one directory argument"
            sys.exit(2)
    success = 1

    try:
        files = []
        for arg in args:
            if arg == '-':
                while 1:
                    line = sys.stdin.readline()
                    if not line:
                        break
                    files.append(line[:-1])
            else:
                files.append(arg)
        if not files:
            print "py_compilefiles: no files to compile"
        elif not compile_files(files, ddir, force, rx, quiet, ignore):
            success = 0
    except KeyboardInterrupt:
        print "\n[interrupt]"
        success = 0
    return success

if __name__ == '__main__':
    exit_status = int(not main())
    sys.exit(exit_status)