def main (): import os import sys import traceback import site programDir = os.getcwd () .replace ('\\', '/') transpilerDir = os.path.dirname (os.path.abspath (__file__)) .replace ('\\', '/') modulesDir = '{}/modules'.format (transpilerDir) # Both CPython and Transcrypt will use all dirs in sys.path as search roots for modules # CPython will also search relatively from each module, Transcrypt only from the main module sys.path = [programDir, modulesDir] + sys.path # Under Linux sys.path does not always contain programDir sys.modules.pop ('org', None) # Unload org from a packages dir, if it's there. from org.transcrypt import __base__ # May reload org from a packages dir (or load org from different location) from org.transcrypt import utils from org.transcrypt import compiler licensePath = '{}/{}'.format (transpilerDir, 'license_reference.txt') if not os.path.isfile (licensePath): utils.log (True, 'Error: missing license reference file\n') return utils.log (True, '{} (TM) Python to JavaScript Small Sane Subset Transpiler Version {}\n', __base__.__envir__.transpiler_name.capitalize (), __base__.__envir__.transpiler_version) utils.log (True, 'Copyright (C) Geatec Engineering. License: Apache 2.0\n\n') utils.commandArgs.parse () if utils.commandArgs.license: with open (licensePath) as licenseFile: bar = 80 * '*' utils.log (True, '\n{}\n\n', bar) utils.log (True, '{}\n', licenseFile.read ()) utils.log (True, '{}\n\n', bar) if not utils.commandArgs.source: return if utils.commandArgs.run: with open (utils.commandArgs.source) as sourceFile: exec (sourceFile.read ()) else: try: compiler.Program (sys.path) except utils.Error as error: utils.log (True, '\n{}\n', error) # Don't log anything else, even in verbose mode, since this would only be confusing if utils.debug: utils.log (True, '{}\n', traceback.format_exc ()) except Exception as exception: utils.log (True, '\n{}', exception) # Have to log something else, because a general exception isn't informative enough utils.log (False, '{}\n', traceback.format_exc ()) utils.log (True, 'Ready\n')
def main(): licensePath = '{}/{}'.format(transpilerDir, 'license_reference.txt') if not os.path.isfile(licensePath): utils.log(True, 'Error: missing license reference file\n') return exitNoLicense utils.log( True, '{} (TM) Python to JavaScript Small Sane Subset Transpiler Version {}\n', __base__.__envir__.transpiler_name.capitalize(), __base__.__envir__.transpiler_version) utils.log(True, 'Copyright (C) Geatec Engineering. License: Apache 2.0\n\n') utils.commandArgs.parse() if utils.commandArgs.license: with open(licensePath) as licenseFile: bar = 80 * '*' utils.log(True, '\n{}\n\n', bar) utils.log(True, '{}\n', licenseFile.read()) utils.log(True, '{}\n\n', bar) if not utils.commandArgs.source: return exitSourceNotGiven # Should never be here, dealth with by command arg checks already if utils.commandArgs.run: try: with open(utils.commandArgs.source) as sourceFile: exec(sourceFile.read()) return exitSuccess except Exception as exception: utils.log( True, 'Error: cannot run {} using CPython: {}\n'.format( utils.commandArgs.source, str(exception).replace(' (<string>', '').replace(')', ''))) return exitCannotRunSource else: try: compiler.Program(compilerPath) return exitSuccess except utils.Error as error: utils.log(True, '\n{}\n', error) # Don't log anything else, even in verbose mode, since this would only be confusing if utils.commandArgs.dextex: utils.log(True, '{}\n', traceback.format_exc()) return exitSpecificCompileError except Exception as exception: utils.log(True, '\n{}', exception) # Have to log something else, because a general exception isn't informative enough utils.log(True, '{}\n', traceback.format_exc()) return exitGeneralCompileError
def main(): exitCode = exitCommandArgsError def exitHandler(): if exitCode == exitSuccess: utils.log(True, '\nReady\n') else: utils.log(True, '\nAborted\n') atexit.register(exitHandler) def setExitCode(anExitCode): nonlocal exitCode exitCode = anExitCode return exitCode try: __envir__ = utils.Any() with tokenize.open( f'{modulesDir}/org/transcrypt/__envir__.js') as envirFile: exec(envirFile.read()) __envir__.executor_name = __envir__.interpreter_name utils.log( True, '{} (TM) Python to JavaScript Small Sane Subset Transpiler Version {}\n', __envir__.transpiler_name.capitalize(), __envir__.transpiler_version) utils.log(True, 'Copyright (C) Geatec Engineering. License: Apache 2.0\n\n') utils.log(True, '\n') licensePath = '{}/{}'.format(transpilerDir, 'license_reference.txt') if not os.path.isfile(licensePath): utils.log(True, 'Error: missing license reference file\n') return setExitCode(exitNoLicense) utils.commandArgs.parse() if utils.commandArgs.license: with open(licensePath) as licenseFile: bar = 80 * '*' utils.log(True, '\n{}\n\n', bar) utils.log(True, '{}\n', licenseFile.read()) utils.log(True, '{}\n\n', bar) if utils.commandArgs.star: webbrowser.open('https://github.com/qquick/Transcrypt') if not utils.commandArgs.source: return setExitCode( exitSourceNotGiven ) # Should never be here, dealth with by command arg checks already # Prepend paths that are needed by transpiled or executed user code, since they have to be searched first # So user code favors Transcrypt modules over CPython modules extraDirs = utils.commandArgs.xpath.replace( '#', ' ').split('$') if utils.commandArgs.xpath else [] sourcePath = utils.commandArgs.source.replace( '\\', '/' ) # May be absolute or relative, in the latter case it may or may not specify a directory if '/' in sourcePath: # If directory specified sourceDir = sourcePath.rsplit('/', 1)[0] # Use it as source directory else: # Else sourceDir = os.getcwd().replace( '\\', '/') # Use current working directory as source directory projectDirs = [sourceDir] + extraDirs sys.path[0:0] = projectDirs global transpilationDirs transpilationDirs[0:0] = projectDirs __symbols__ = utils.commandArgs.symbols.split( '$') if utils.commandArgs.symbols else [] if utils.commandArgs.complex: __symbols__.append('__complex__') if utils.commandArgs.sform: __symbols__.append('__sform__') if utils.commandArgs.xtiny: __symbols__.append('__xtiny__') __symbols__.append('__py{}.{}__'.format(*sys.version_info[:2])) if utils.commandArgs.esv: __symbols__.append('__esv{}__'.format(utils.commandArgs.esv)) else: __symbols__.append('__esv{}__'.format( utils.defaultJavaScriptVersion)) # Import (ignored when transpiling) late, since commandArgs must be set already from org.transcrypt.stubs.browser import __set_stubsymbols__ # Make symbols available to CPython, seems that exec can't do that directly __set_stubsymbols__(__symbols__) if utils.commandArgs.run: try: with open(utils.commandArgs.source + '.py') as sourceFile: exec(sourceFile.read(), globals(), locals()) return setExitCode(exitSuccess) except Exception as exception: utils.log( True, 'Error: cannot run {} using CPython: {}\n'.format( utils.commandArgs.source, str(exception).replace(' (<string>', '').replace(')', ''))) utils.log(True, traceback.format_exc()) return setExitCode(exitCannotRunSource) else: try: compiler.Program(transpilationDirs, __symbols__, __envir__) return setExitCode(exitSuccess) except utils.Error as error: utils.log(True, '\n{}\n', error) # Don't log anything else, even in verbose mode, since this would only be confusing if utils.commandArgs.dextex: utils.log(True, '{}\n', traceback.format_exc()) return setExitCode(exitSpecificCompileError) except Exception as exception: utils.log(True, '\n{}', exception) # Have to log something else, because a general exception isn't informative enough utils.log(True, '{}\n', traceback.format_exc()) return setExitCode(exitGeneralCompileError) except utils.CommandArgsError: return setExitCode(exitCommandArgsError) except utils.CommandArgsExit: return setExitCode(exitSuccess)
def main(): exitCode = exitCommandArgsError def exitHandler(): if exitCode == exitSuccess: utils.log(True, '\nReady\n') else: utils.log(True, '\nAborted\n') atexit.register(exitHandler) def setExitCode(anExitCode): nonlocal exitCode exitCode = anExitCode return exitCode try: licensePath = '{}/{}'.format(transpilerDir, 'license_reference.txt') if not os.path.isfile(licensePath): utils.log(True, 'Error: missing license reference file\n') return setExitCode(exitNoLicense) utils.log( True, '{} (TM) Python to JavaScript Small Sane Subset Transpiler Version {}\n', __base__.__envir__.transpiler_name.capitalize(), __base__.__envir__.transpiler_version) utils.log(True, 'Copyright (C) Geatec Engineering. License: Apache 2.0\n\n') utils.commandArgs.parse() if utils.commandArgs.license: with open(licensePath) as licenseFile: bar = 80 * '*' utils.log(True, '\n{}\n\n', bar) utils.log(True, '{}\n', licenseFile.read()) utils.log(True, '{}\n\n', bar) if utils.commandArgs.star: webbrowser.open('https://github.com/JdeH/Transcrypt') if not utils.commandArgs.source: return setExitCode( exitSourceNotGiven ) # Should never be here, dealth with by command arg checks already __symbols__ = utils.commandArgs.symbols.split( '$') if utils.commandArgs.symbols else [] if utils.commandArgs.complex: __symbols__.append('__complex__') __symbols__.append('__py{}.{}__'.format(*sys.version_info[:2])) if utils.commandArgs.esv: __symbols__.append('__esv{}__'.format(utils.commandArgs.esv)) else: __symbols__.append('__esv{}__'.format( utils.defaultJavaScriptVersion)) from org.transcrypt.stubs.browser import __set_stubsymbols__ # Import (ignored when transpiling) late, since commandArgs must be set already __set_stubsymbols__( __symbols__ ) # Make symbols available to CPython, seems that exec can't do that directly if utils.commandArgs.run: try: with open(utils.commandArgs.source) as sourceFile: exec(sourceFile.read()) return setExitCode(exitSuccess) except Exception as exception: utils.log( True, 'Error: cannot run {} using CPython: {}\n'.format( utils.commandArgs.source, str(exception).replace(' (<string>', '').replace(')', ''))) utils.log(True, traceback.format_exc()) return setExitCode(exitCannotRunSource) else: try: compiler.Program(compilerPath, __symbols__) return setExitCode(exitSuccess) except utils.Error as error: utils.log(True, '\n{}\n', error) # Don't log anything else, even in verbose mode, since this would only be confusing if utils.commandArgs.dextex: utils.log(True, '{}\n', traceback.format_exc()) return setExitCode(exitSpecificCompileError) except Exception as exception: utils.log(True, '\n{}', exception) # Have to log something else, because a general exception isn't informative enough utils.log(True, '{}\n', traceback.format_exc()) return setExitCode(exitGeneralCompileError) except utils.CommandArgsError: return setExitCode(exitCommandArgsError) except utils.CommandArgsExit: return setExitCode(exitSuccess)
import transcrypt.__main__ import sys, os sys.path.append (os.path.join(os.path.abspath(transcrypt.__main__.__file__), "modules")) from org.transcrypt import utils from org.transcrypt import compiler try: compiler.Program (transpilationDirs, __symbols__, __envir__) return setExitCode (exitSuccess) except utils.Error as error: utils.log (True, '\n{}\n', error) # Don't log anything else, even in verbose mode, since this would only be confusing if utils.commandArgs.dextex: utils.log (True, '{}\n', traceback.format_exc ()) return setExitCode (exitSpecificCompileError) except Exception as exception: utils.log (True, '\n{}', exception) # Have to log something else, because a general exception isn't informative enough utils.log (True, '{}\n', traceback.format_exc ()) return setExitCode (exitGeneralCompileError)
bar = 80 * '=' utils.log(True, '\n{}\n\n', bar) utils.log(True, '{}\n', licenseFile.read()) utils.log(True, '{}\n\n', bar) if not utils.commandArgs.source: exit(0) if utils.commandArgs.run: with open(utils.commandArgs.source) as sourceFile: exec( 'import sys\n' + 'sys.path [0] = sys.path [1 : ]\n' + # "import transcrypt" should refer to library rather than to this file 'sys.path.append (\'{}\')\n'.format(modulesDir) + sourceFile.read(), ) else: try: compiler.Program([programDir, modulesDir]) except utils.Error as error: utils.log(True, '\n{}\n', error) # Don't log anything else, even in verbose mode, since this would only be confusing if utils.debug: utils.log(True, '{}\n', traceback.format_exc()) except Exception as exception: utils.log(True, '\n{}', exception) # Have to log something else, because a general exception isn't informative enough utils.log(False, '{}\n', traceback.format_exc()) utils.log(True, 'Ready\n')