# Path: pyxc/util.py
# def usingPython3():
#     return sys.version_info[0] == 3
# 
# def writeExceptionJsonAndDie(e):
#     sys.stderr.write('%s\n' % json.dumps({
#         'name': e.__class__.__name__,
#         'message': exceptionRepr(),
#     }))
#     sys.exit(1)
# 
# class TempDir:
#     
#     def __init__(self):
#         self.path = tempfile.mkdtemp()
#     
#     def __enter__(self):
#         return self
#     
#     def __exit__(self, *args):
#         subprocess.check_call(['rm', '-rf', self.path])
# 
# def parentOf(path):
#     return '/'.join(path.rstrip('/').split('/')[:-1])
# 
# Path: pj/nodejs.py
# def runViaNode(path, codepath, exceptionServerHost, exceptionServerPort, runExceptionServer):
#     
#     path = os.path.abspath(path)
#     if not os.path.isfile(path):
#         raise Exception('File not found: ' + repr(path))
#     filename = path.split('/')[-1]
#     module = filename.split('.')[-2]
#     codepath = (codepath or []) + [parentOf(path)]
#     
#     if exceptionServerPort:
#         #LATER: nice fatal error if exception-server not installed
#         prependJs = "require('exception-server').devCombo(%s);\n" % json.dumps({
#             'connectTo': [exceptionServerHost, exceptionServerPort],
#         })
#     else:
#         prependJs = None
#     
#     info = pj.api_internal.buildBundle(
#                             module,
#                             path=codepath,
#                             createSourceMap=True,
#                             includeSource=True,
#                             prependJs=prependJs)
#     js = info['js']
#     sourceMap = info['sourceMap']
#     sourceDict = info['sourceDict']
#     
#     with TempDir() as td:
#         
#         jsPath = '%s/%s.js' % (td.path, filename)
#         with open(jsPath, 'wb') as f:
#             f.write(js.encode('utf-8'))
#         
#         exception_server_proc = None
#         if runExceptionServer:
#             exception_server_proc = startExceptionServer(
#                                         exceptionServerPort, js, sourceMap, sourceDict, jsPath)
#         try:
#             subprocess.check_call(['node', jsPath])
#             if exception_server_proc is not None:
#                 exception_server_proc.kill()
#         except Exception:
#             sys.stderr.write('[PYXC] Leaving exception server running until you Control-C...\n')
#             try:
#                 while True:
#                     time.sleep(1)
#             finally:
#                 if exception_server_proc is not None:
#                     exception_server_proc.kill()


import optparse, sys, os, json
import pj.api_internal
from pyxc.util import usingPython3, writeExceptionJsonAndDie, TempDir, parentOf
from pj.nodejs import runViaNode
            if not esHost:
                esHost = 'localhost'
        runViaNode(args[0], codepath, esHost, esPort, options.runExceptionServer)
    
    else:
        sys.stderr.write('Invalid args -- see http://pyxc.org/pj for usage.\n')
        sys.exit(1)


#### Code to Code
# See [pj.api_internal.codeToCode](api_internal.py)
def codeToCode():
    py = sys.stdin.read()
    try:
        js = pj.api_internal.codeToCode(py)
        sys.stdout.write(js)
    except Exception as e:
        writeExceptionJsonAndDie(e)


#### Build Bundle
# See [pj.api_internal.buildBundle](api_internal.py)
def buildBundle(mainModule, codepath, createSourceMap):
    try:
        info = pj.api_internal.buildBundle(
                            mainModule,
                            path=codepath,
                            createSourceMap=createSourceMap)
        sys.stdout.write(json.dumps(info))
    except Exception as e:

# Path: pyxc/util.py
# def usingPython3():
#     return sys.version_info[0] == 3
# 
# def writeExceptionJsonAndDie(e):
#     sys.stderr.write('%s\n' % json.dumps({
#         'name': e.__class__.__name__,
#         'message': exceptionRepr(),
#     }))
#     sys.exit(1)
# 
# class TempDir:
#     
#     def __init__(self):
#         self.path = tempfile.mkdtemp()
#     
#     def __enter__(self):
#         return self
#     
#     def __exit__(self, *args):
#         subprocess.check_call(['rm', '-rf', self.path])
# 
# def parentOf(path):
#     return '/'.join(path.rstrip('/').split('/')[:-1])
# 
# Path: pj/nodejs.py
# def runViaNode(path, codepath, exceptionServerHost, exceptionServerPort, runExceptionServer):
#     
#     path = os.path.abspath(path)
#     if not os.path.isfile(path):
#         raise Exception('File not found: ' + repr(path))
#     filename = path.split('/')[-1]
#     module = filename.split('.')[-2]
#     codepath = (codepath or []) + [parentOf(path)]
#     
#     if exceptionServerPort:
#         #LATER: nice fatal error if exception-server not installed
#         prependJs = "require('exception-server').devCombo(%s);\n" % json.dumps({
#             'connectTo': [exceptionServerHost, exceptionServerPort],
#         })
#     else:
#         prependJs = None
#     
#     info = pj.api_internal.buildBundle(
#                             module,
#                             path=codepath,
#                             createSourceMap=True,
#                             includeSource=True,
#                             prependJs=prependJs)
#     js = info['js']
#     sourceMap = info['sourceMap']
#     sourceDict = info['sourceDict']
#     
#     with TempDir() as td:
#         
#         jsPath = '%s/%s.js' % (td.path, filename)
#         with open(jsPath, 'wb') as f:
#             f.write(js.encode('utf-8'))
#         
#         exception_server_proc = None
#         if runExceptionServer:
#             exception_server_proc = startExceptionServer(
#                                         exceptionServerPort, js, sourceMap, sourceDict, jsPath)
#         try:
#             subprocess.check_call(['node', jsPath])
#             if exception_server_proc is not None:
#                 exception_server_proc.kill()
#         except Exception:
#             sys.stderr.write('[PYXC] Leaving exception server running until you Control-C...\n')
#             try:
#                 while True:
#                     time.sleep(1)
#             finally:
#                 if exception_server_proc is not None:
#                     exception_server_proc.kill()


# Path: pj/main.py
import optparse, sys, os, json
import pj.api_internal
from pyxc.util import usingPython3, writeExceptionJsonAndDie, TempDir, parentOf
from pj.nodejs import runViaNode

            if not esHost:
                esHost = 'localhost'
        runViaNode(args[0], codepath, esHost, esPort, options.runExceptionServer)
    
    else:
        sys.stderr.write('Invalid args -- see http://pyxc.org/pj for usage.\n')
        sys.exit(1)


#### Code to Code
# See [pj.api_internal.codeToCode](api_internal.py)
def codeToCode():
    py = sys.stdin.read()
    try:
        js = pj.api_internal.codeToCode(py)
        sys.stdout.write(js)
    except Exception as e:
        writeExceptionJsonAndDie(e)


#### Build Bundle
# See [pj.api_internal.buildBundle](api_internal.py)
def buildBundle(mainModule, codepath, createSourceMap):
    try:
        info = pj.api_internal.buildBundle(
                            mainModule,
                            path=codepath,
                            createSourceMap=createSourceMap)
        sys.stdout.write(json.dumps(info))
    except Exception as e:

        writeExceptionJsonAndDie(e)