Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 1 | """Routine to "compile" a .py file to a .pyc (or .pyo) file. |
| 2 | |
| 3 | This module has intimate knowledge of the format of .pyc files. |
| 4 | """ |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 5 | |
Fred Drake | a96f1a3 | 2002-08-21 20:23:22 +0000 | [diff] [blame] | 6 | import __builtin__ |
Sjoerd Mullender | 2e5168c | 1995-07-19 11:21:47 +0000 | [diff] [blame] | 7 | import imp |
Fred Drake | a96f1a3 | 2002-08-21 20:23:22 +0000 | [diff] [blame] | 8 | import marshal |
| 9 | import os |
| 10 | import sys |
| 11 | import traceback |
| 12 | |
Sjoerd Mullender | 2e5168c | 1995-07-19 11:21:47 +0000 | [diff] [blame] | 13 | MAGIC = imp.get_magic() |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 14 | |
Fred Drake | 61cf440 | 2002-08-21 20:56:21 +0000 | [diff] [blame] | 15 | __all__ = ["compile", "main"] |
Skip Montanaro | c62c81e | 2001-02-12 02:00:42 +0000 | [diff] [blame] | 16 | |
Fred Drake | a96f1a3 | 2002-08-21 20:23:22 +0000 | [diff] [blame] | 17 | # Define an internal helper according to the platform |
| 18 | if os.name == "mac": |
| 19 | import macfs |
| 20 | def set_creator_type(file): |
| 21 | macfs.FSSpec(file).SetCreatorType('Pyth', 'PYC ') |
| 22 | else: |
| 23 | def set_creator_type(file): |
| 24 | pass |
| 25 | |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 26 | def wr_long(f, x): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 27 | """Internal; write a 32-bit int to a file in little-endian order.""" |
Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 28 | f.write(chr( x & 0xff)) |
| 29 | f.write(chr((x >> 8) & 0xff)) |
| 30 | f.write(chr((x >> 16) & 0xff)) |
| 31 | f.write(chr((x >> 24) & 0xff)) |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 33 | def compile(file, cfile=None, dfile=None): |
| 34 | """Byte-compile one Python source file to Python bytecode. |
| 35 | |
| 36 | Arguments: |
| 37 | |
| 38 | file: source filename |
| 39 | cfile: target filename; defaults to source with 'c' or 'o' appended |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 40 | ('c' normally, 'o' in optimizing mode, giving .pyc or .pyo) |
Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 41 | dfile: purported filename; defaults to source (this is the filename |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 42 | that will show up in error messages) |
Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 43 | |
| 44 | Note that it isn't necessary to byte-compile Python modules for |
| 45 | execution efficiency -- Python itself byte-compiles a module when |
| 46 | it is loaded, and if it can, writes out the bytecode to the |
| 47 | corresponding .pyc (or .pyo) file. |
| 48 | |
| 49 | However, if a Python installation is shared between users, it is a |
| 50 | good idea to byte-compile all modules upon installation, since |
| 51 | other users may not be able to write in the source directories, |
| 52 | and thus they won't be able to write the .pyc/.pyo file, and then |
| 53 | they would be byte-compiling every module each time it is loaded. |
| 54 | This can slow down program start-up considerably. |
| 55 | |
| 56 | See compileall.py for a script/module that uses this module to |
| 57 | byte-compile all installed files (or all files in selected |
| 58 | directories). |
| 59 | |
| 60 | """ |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 61 | f = open(file, 'U') |
Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 62 | try: |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 63 | timestamp = long(os.fstat(f.fileno()).st_mtime) |
Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 64 | except AttributeError: |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 65 | timestamp = long(os.stat(file).st_mtime) |
Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 66 | codestring = f.read() |
| 67 | f.close() |
| 68 | if codestring and codestring[-1] != '\n': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 69 | codestring = codestring + '\n' |
Guido van Rossum | f984a65 | 1998-09-29 15:57:42 +0000 | [diff] [blame] | 70 | try: |
| 71 | codeobject = __builtin__.compile(codestring, dfile or file, 'exec') |
| 72 | except SyntaxError, detail: |
Guido van Rossum | f984a65 | 1998-09-29 15:57:42 +0000 | [diff] [blame] | 73 | lines = traceback.format_exception_only(SyntaxError, detail) |
| 74 | for line in lines: |
Eric S. Raymond | 6b71e74 | 2001-02-09 08:56:30 +0000 | [diff] [blame] | 75 | sys.stderr.write(line.replace('File "<string>"', |
Fred Drake | a96f1a3 | 2002-08-21 20:23:22 +0000 | [diff] [blame] | 76 | 'File "%s"' % (dfile or file))) |
Guido van Rossum | f984a65 | 1998-09-29 15:57:42 +0000 | [diff] [blame] | 77 | return |
Raymond Hettinger | 16e3c42 | 2002-06-01 16:07:16 +0000 | [diff] [blame] | 78 | if cfile is None: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 79 | cfile = file + (__debug__ and 'c' or 'o') |
Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 80 | fc = open(cfile, 'wb') |
| 81 | fc.write('\0\0\0\0') |
| 82 | wr_long(fc, timestamp) |
| 83 | marshal.dump(codeobject, fc) |
| 84 | fc.flush() |
| 85 | fc.seek(0, 0) |
| 86 | fc.write(MAGIC) |
| 87 | fc.close() |
Fred Drake | a96f1a3 | 2002-08-21 20:23:22 +0000 | [diff] [blame] | 88 | set_creator_type(cfile) |
Fred Drake | 61cf440 | 2002-08-21 20:56:21 +0000 | [diff] [blame] | 89 | |
| 90 | def main(args=None): |
| 91 | """Compile several source files. |
| 92 | |
| 93 | The files named in 'args' (or on the command line, if 'args' is |
| 94 | not specified) are compiled and the resulting bytecode is cached |
| 95 | in the normal manner. This function does not search a directory |
| 96 | structure to locate source files; it only compiles files named |
| 97 | explicitly. |
| 98 | |
| 99 | """ |
| 100 | if args is None: |
| 101 | args = sys.argv[1:] |
| 102 | for filename in args: |
| 103 | compile(filename) |
| 104 | |
| 105 | if __name__ == "__main__": |
| 106 | main() |