1# 2# Copyright 2016 Pixar 3# 4# Licensed under the Apache License, Version 2.0 (the "Apache License") 5# with the following modification; you may not use this file except in 6# compliance with the Apache License and the following modification to it: 7# Section 6. Trademarks. is deleted and replaced with: 8# 9# 6. Trademarks. This License does not grant permission to use the trade 10# names, trademarks, service marks, or product names of the Licensor 11# and its affiliates, except as required to comply with Section 4(c) of 12# the License and to reproduce the content of the NOTICE file. 13# 14# You may obtain a copy of the Apache License at 15# 16# http://www.apache.org/licenses/LICENSE-2.0 17# 18# Unless required by applicable law or agreed to in writing, software 19# distributed under the Apache License with the above modification is 20# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 21# KIND, either express or implied. See the Apache License for the specific 22# language governing permissions and limitations under the Apache License. 23# 24# 25# Usage: compilePython source.py dest.pyc 26# 27# This program compiles python code, providing a reasonable 28# gcc-esque error message if errors occur. 29# 30# parameters: 31# src.py - the source file to report errors for 32# file.py - the installed location of the file 33# file.pyc - the precompiled python file 34 35from __future__ import print_function 36 37import sys 38import py_compile 39 40if len(sys.argv) < 4: 41 print("Usage: {} src.py file.py file.pyc".format(sys.argv[0])) 42 sys.exit(1) 43 44try: 45 py_compile.compile(sys.argv[2], sys.argv[3], sys.argv[1], doraise=True) 46except py_compile.PyCompileError as compileError: 47 exc_value = compileError.exc_value 48 if compileError.exc_type_name == SyntaxError.__name__: 49 # py_compile.compile stashes the type name and args of the exception 50 # in the raised PyCompileError rather than the exception itself. This 51 # is especially annoying because the args member of some SyntaxError 52 # instances are lacking the source information tuple, but do have a 53 # usable lineno. 54 error = exc_value[0] 55 try: 56 linenumber = exc_value[1][1] 57 line = exc_value[1][3] 58 print('{}:{}: {}: "{}"'.format(sys.argv[1], linenumber, error, line)) 59 except IndexError: 60 print('{}: Syntax error: "{}"'.format(sys.argv[1], error)) 61 else: 62 print("{}: Unhandled compile error: ({}) {}".format( 63 sys.argv[1], compileError.exc_type_name, exc_value)) 64 sys.exit(1) 65except: 66 exc_type, exc_value, exc_traceback = sys.exc_info() 67 print("{}: Unhandled exception: {}".format(sys.argv[1], exc_value)) 68 sys.exit(1) 69