File indexing completed on 2024-04-14 03:59:08

0001 # By Simon Edwards <simon@simonzone.com>
0002 # This file is in the public domain.
0003 
0004 """
0005 Byte-compiles a given Python source file, generating a .pyc file or, if the
0006 Python executable was invoked with -O, a .pyo file from it.
0007 It uses the --destination-dir option to set the path to the source file (which
0008 will appear in tracebacks, for example), so that if the .py file was in a build
0009 root will appear with the right path.
0010 """
0011 
0012 import argparse
0013 import os
0014 import py_compile
0015 
0016 
0017 if __name__ == '__main__':
0018     parser = argparse.ArgumentParser('Byte-compiles a Python source file.')
0019     parser.add_argument('-d', '--destination-dir', required=True,
0020                         help='Location where the source file will be '
0021                         'installed, without any build roots.')
0022     parser.add_argument('source_file',
0023                         help='Source file to byte-compile.')
0024 
0025     args = parser.parse_args()
0026 
0027     dfile = os.path.join(args.destination_dir,
0028                          os.path.basename(args.source_file))
0029     py_compile.compile(args.source_file, dfile=dfile)