示例#1
0
def _runViaSubprocessIfNeeded(name,
                              args,
                              kwargs,
                              input,
                              subprocessArgs,
                              parseJson=False):

    if usingPython3():
        import pj.api_internal
        f = getattr(pj.api_internal, name)
        return f(*args, **kwargs)
    else:

        if isinstance(input, unicode):
            input = input.encode('utf-8')

        pythonPath = parentOf(parentOf(os.path.abspath(__file__)))
        if os.environ.get('PYTHONPATH'):
            pythonPath += ':' + os.environ.get('PYTHONPATH')

        subprocessArgs = [
            '/usr/bin/env',
            'PYTHONPATH=%s' % pythonPath,
            'python3.1',
            parentOf(os.path.abspath(__file__)) + '/pj',
        ] + subprocessArgs

        if input is None:
            p = subprocess.Popen(subprocessArgs,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)
            out, err = p.communicate()
        else:
            p = subprocess.Popen(subprocessArgs,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 stdin=subprocess.PIPE)
            out, err = p.communicate(input=input)

        if p.returncode == 0:
            result = unicode(out, 'utf-8')
            if parseJson:
                result = json.loads(result)
            return result
        else:
            try:
                errInfo = json.loads(err)
            except ValueError:
                errInfo = {
                    'name': 'Exception',
                    'message': unicode(err, 'utf-8'),
                }
            if hasattr(pyxc_exceptions, errInfo['name']):
                exceptionClass = getattr(pyxc_exceptions, errInfo['name'])
                raise exceptionClass(errInfo['message'])
            else:
                raise Exception('%s\n--------\n%s' %
                                (errInfo['name'], errInfo['message']))
示例#2
0
def startExceptionServer(exceptionServerPort, js, sourceMap, sourceDict, jsPath):
    path = '%s/nodejs_exception_server.js' % parentOf(__file__)
    p = subprocess.Popen(
                            ['node', path, str(exceptionServerPort)],
                            stdout=subprocess.PIPE)
    
    try:
        line = p.stdout.readline()
        assert line.startswith(b'Server ready, PYXC-PJ. Go, go, go!'), repr(line)
    
        # Send it information
        jsdata = js.encode('utf-8')
        jshash = hashlib.sha1(jsdata).hexdigest()
        tups = [('mapping', jshash, sourceMap.encode('utf-8'))]
        for k, d in sourceDict.items():
            data = d['code'].encode('utf-8')
            tups.append((
                            'code',
                            hashlib.sha1(data).hexdigest(),
                            data))
        url = 'http://localhost:%d/api/log.js' % exceptionServerPort
        for typename, k, v in tups:
            simplePost(url, POST={
                'type': typename,
                'code_sha1': k,
                'v': v,
            })
    
    except Exception:
        p.kill()
        raise
    
    return p
示例#3
0
文件: api.py 项目: GunioRobot/pyxc-pj
def _runViaSubprocessIfNeeded(name, args, kwargs, input, subprocessArgs, parseJson=False):
    
    if usingPython3():
        import pj.api_internal
        f = getattr(pj.api_internal, name)
        return f(*args, **kwargs)
    else:
        
        if isinstance(input, unicode):
            input = input.encode('utf-8')
        
        pythonPath = parentOf(parentOf(os.path.abspath(__file__)))
        if os.environ.get('PYTHONPATH'):
            pythonPath += ':' + os.environ.get('PYTHONPATH')
        
        subprocessArgs = ['/usr/bin/env',
                                    'PYTHONPATH=%s' % pythonPath,
                                    'python3.1',
                                    parentOf(os.path.abspath(__file__)) + '/pj',
                                    ] + subprocessArgs
        
        if input is None:
            p = subprocess.Popen(subprocessArgs, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            out, err = p.communicate()
        else:
            p = subprocess.Popen(subprocessArgs, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
            out, err = p.communicate(input=input)
        
        if p.returncode == 0:
            result = unicode(out, 'utf-8')
            if parseJson:
                result = json.loads(result)
            return result
        else:
            try:
                errInfo = json.loads(err)
            except ValueError:
                errInfo = {
                    'name': 'Exception',
                    'message': unicode(err, 'utf-8'),
                }
            if hasattr(pyxc_exceptions, errInfo['name']):
                exceptionClass = getattr(pyxc_exceptions, errInfo['name'])
                raise exceptionClass(errInfo['message'])
            else:
                raise Exception('%s\n--------\n%s' % (errInfo['name'], errInfo['message']))
示例#4
0
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()
示例#5
0
def loadTransformationsDict(parentModule):
    # transformationsDict = {
    #     'NodeName': [...transformation functions...]
    # }
    d = {}
    astNames = list(getPythonAstNames())
    filenames = rfilter(r'^[^.]+\.py$',
                        os.listdir(parentOf(parentModule.__file__)))
    for filename in filenames:
        if filename != '__init__.py':
            modName = 'pj.transformations.%s' % filename.split('.')[0]
            __import__(modName)
            mod = sys.modules[modName]
            for name in dir(mod):
                if name in astNames:
                    assert name not in d
                    value = getattr(mod, name)
                    if not isinstance(value, list) or isinstance(value, tuple):
                        value = [value]
                    d[name] = value
    return d
示例#6
0
def loadTransformationsDict(parentModule):
    # transformationsDict = {
    #     'NodeName': [...transformation functions...]
    # }
    d = {}
    astNames = list(getPythonAstNames())
    filenames = rfilter(
                            r'^[^.]+\.py$',
                            os.listdir(parentOf(parentModule.__file__)))
    for filename in filenames:
        if filename != '__init__.py':
            modName = 'pj.transformations.%s' % filename.split('.')[0]
            __import__(modName)
            mod = sys.modules[modName]
            for name in dir(mod):
                if name in astNames:
                    assert name not in d
                    value = getattr(mod, name)
                    if not isinstance(value, list) or isinstance(value, tuple):
                        value = [value]
                    d[name] = value
    return d
示例#7
0
import os, sys, time
from subprocess import check_call

import pj.api
from pyxc.util import parentOf

EXAMPLES_ROOT = parentOf(parentOf(os.path.abspath(__file__)))
PATH = [
    '%s/colorflash/js' % EXAMPLES_ROOT,
    '%s/mylib/js' % EXAMPLES_ROOT,
]


def main():

    check_call(['mkdir', '-p', 'build'])

    js = None

    for closureMode in ['', 'pretty', 'simple']:

        filename = {
            '': 'colorflash.raw.js',
            'pretty': 'colorflash.pretty.js',
            'simple': 'colorflash.min.simple.js',
        }[closureMode]

        path = 'build/%s' % filename

        sys.stderr.write('%s... ' % path)
        start = time.time()
示例#8
0
import os, sys, time
from subprocess import check_call

import pj.api
from pyxc.util import parentOf


EXAMPLES_ROOT = parentOf(parentOf(os.path.abspath(__file__)))
PATH = [
    '%s/colorflash/js' % EXAMPLES_ROOT,
    '%s/mylib/js' % EXAMPLES_ROOT,
]


def main():
    
    check_call(['mkdir', '-p', 'build'])
    
    js = None
    
    for closureMode in ['', 'pretty', 'simple']:
        
        filename = {
            '': 'colorflash.raw.js',
            'pretty': 'colorflash.pretty.js',
            'simple': 'colorflash.min.simple.js',
        }[closureMode]
        
        path = 'build/%s' % filename