160 lines
4.4 KiB
Python
160 lines
4.4 KiB
Python
#
|
|
# Copyright (C) 2017-2018 Bassem Girgis brgirgis@gmail.com
|
|
# All Rights Reserved
|
|
#
|
|
# NOTICE: All information contained herein is, and remains the property of
|
|
# Bassem Girgis brgirgis@gmail.com. The intellectual and technical concepts
|
|
# contained herein are proprietary to Bassem Girgis and may be covered by
|
|
# U.S. and Foreign Patents, patents in process, and are protected by trade
|
|
# secret or copyright law. Dissemination of this information or reproduction
|
|
# of this material is strictly forbidden, unless prior authorized written
|
|
# permission is obtained from Bassem Girgis.
|
|
#
|
|
|
|
import os
|
|
|
|
|
|
def compileFile(filePath, cFilePath, dFilePath, doRaise, nOptimization):
|
|
assert filePath[-3:] == '.py', \
|
|
'Cannot compile non-python files'
|
|
|
|
print('compiling', filePath)
|
|
|
|
try:
|
|
import py_compile
|
|
py_compile.compile(
|
|
file=filePath,
|
|
cfile=cFilePath,
|
|
dfile=dFilePath,
|
|
doraise=doRaise,
|
|
optimize=nOptimization
|
|
)
|
|
return True
|
|
except:
|
|
return False
|
|
|
|
|
|
def compileDir(srcPath, cPath, dPath, doRaise, nOptimization):
|
|
|
|
if not cPath:
|
|
cPath = srcPath
|
|
|
|
for root, _, files in os.walk(srcPath):
|
|
for fileName in files:
|
|
if '.py' != fileName[-3:]:
|
|
continue
|
|
|
|
filePath = os.path.join(root, fileName)
|
|
relFilePath = os.path.relpath(filePath, srcPath)
|
|
cFilePath = os.path.join(cPath, relFilePath) + 'c'
|
|
dFilePath = os.path.join(
|
|
dPath, relFilePath
|
|
) if dPath else relFilePath
|
|
if not compileFile(
|
|
filePath=filePath,
|
|
cFilePath=cFilePath,
|
|
dFilePath=dFilePath,
|
|
doRaise=doRaise,
|
|
nOptimization=nOptimization
|
|
):
|
|
return False
|
|
return True
|
|
|
|
|
|
def main():
|
|
|
|
import argparse
|
|
|
|
parser = argparse.ArgumentParser(
|
|
description='Utility to build python binaries.'
|
|
)
|
|
|
|
parser.add_argument(
|
|
'-i',
|
|
metavar='INPUTPATH',
|
|
dest='inputPath',
|
|
default=None,
|
|
help=('file to be processed')
|
|
)
|
|
parser.add_argument(
|
|
'-s',
|
|
metavar='SRCPATH',
|
|
dest='srcPath',
|
|
default=None,
|
|
help=('directory to be processed')
|
|
)
|
|
parser.add_argument(
|
|
'-o',
|
|
metavar='OUTPUTDIR',
|
|
dest='outputDir',
|
|
default=None,
|
|
help=('output directory')
|
|
)
|
|
parser.add_argument(
|
|
'-d',
|
|
metavar='DESTDIR',
|
|
dest='destDir',
|
|
default='',
|
|
help=('destination directory')
|
|
)
|
|
parser.add_argument(
|
|
'-e',
|
|
action='store_true',
|
|
dest='doraise',
|
|
help='raise if there are errors'
|
|
)
|
|
parser.add_argument(
|
|
'-l',
|
|
type=int,
|
|
default=-1,
|
|
dest='noptimize',
|
|
help=('level of optimization')
|
|
)
|
|
args = parser.parse_args()
|
|
|
|
if not args.inputPath:
|
|
assert args.srcPath, 'missing source path'
|
|
assert os.path.isdir(args.srcPath), 'source path does not exist'
|
|
return not compileDir(
|
|
srcPath=args.srcPath,
|
|
cPath=args.outputDir,
|
|
dPath=args.destDir,
|
|
doRaise=args.doraise,
|
|
nOptimization=args.noptimize
|
|
)
|
|
elif args.inputPath:
|
|
assert os.path.isfile(args.inputPath), 'input file does not exist'
|
|
|
|
relFilePath = None
|
|
if args.srcPath:
|
|
assert os.path.isdir(args.srcPath), 'source path does not exist'
|
|
relFilePath = os.path.relpath(args.inputPath, args.srcPath)
|
|
|
|
cFilePath = args.inputPath + 'c'
|
|
if args.srcPath and args.outputDir:
|
|
assert os.path.isdir(args.outputDir), 'output path does not exist'
|
|
cFilePath = os.path.join(args.outputDir, relFilePath) + 'c'
|
|
cFileDirPath = os.path.dirname(cFilePath)
|
|
if not os.path.exists(cFileDirPath):
|
|
os.makedirs(cFileDirPath)
|
|
|
|
dFilePath = relFilePath if relFilePath else args.inputPath
|
|
if args.srcPath and args.destDir:
|
|
dFilePath = os.path.join(args.destDir, relFilePath)
|
|
|
|
return not compileFile(
|
|
filePath=args.inputPath,
|
|
cFilePath=cFilePath,
|
|
dFilePath=dFilePath,
|
|
doRaise=args.doraise,
|
|
nOptimization=args.noptimize
|
|
)
|
|
|
|
args.print_help()
|
|
return True
|
|
|
|
|
|
if __name__ == '__main__':
|
|
import sys
|
|
sys.exit(main())
|