# Path: pyxc/util.py
# 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])
# 
# def simplePost(url, POST={}):
#     try:
#         import urllib.parse, urllib.request
#         data = urllib.parse.urlencode(POST)
#         return urllib.request.urlopen(url, data).read()
#     except ImportError:
#         import urllib, urllib2
#         data = urllib.urlencode(POST)
#         return urllib2.urlopen(urllib2.Request(url, data)).read()


import sys, os, json, subprocess, re, hashlib, time
import urllib.request
import urllib.parse
import pj.api_internal
from pyxc.util import TempDir, parentOf, simplePost

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:

# Path: pyxc/util.py
# 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])
# 
# def simplePost(url, POST={}):
#     try:
#         import urllib.parse, urllib.request
#         data = urllib.parse.urlencode(POST)
#         return urllib.request.urlopen(url, data).read()
#     except ImportError:
#         import urllib, urllib2
#         data = urllib.urlencode(POST)
#         return urllib2.urlopen(urllib2.Request(url, data)).read()


# Path: pj/nodejs.py
import sys, os, json, subprocess, re, hashlib, time
import urllib.request
import urllib.parse
import pj.api_internal
from pyxc.util import TempDir, parentOf, simplePost


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()