# 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
    
    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:

# 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

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