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
|
# By Simon Edwards <simon@simonzone.com>
# This file is in the public domain.
"""
Byte-compiles a given Python source file, generating a .pyc file or, if the
Python executable was invoked with -O, a .pyo file from it.
It uses the --destination-dir option to set the path to the source file (which
will appear in tracebacks, for example), so that if the .py file was in a build
root will appear with the right path.
"""
import argparse
import os
import py_compile
if __name__ == '__main__':
parser = argparse.ArgumentParser('Byte-compiles a Python source file.')
parser.add_argument('-d', '--destination-dir', required=True,
help='Location where the source file will be '
'installed, without any build roots.')
parser.add_argument('source_file',
help='Source file to byte-compile.')
args = parser.parse_args()
dfile = os.path.join(args.destination_dir,
os.path.basename(args.source_file))
py_compile.compile(args.source_file, dfile=dfile)
|