def main_handler(self, method='GET'):
        # get request url (without url params) and remove trailing /
        config.debug('>>> handling request')
        config.debug(self.path)

        request_url = self.path.split('?')[0]
        if request_url is not '/':
            request_url = request_url.rstrip('/')

        handler = None
        try:
            handler = self.mappings[request_url][method]
            #config.debug(handler)
        except KeyError:
            # no mapping found for the request
            self.send_response(404)
            self.end_headers()
            return

        try:
            handler(self)
        except KeyError:
            # method not found
            self.send_response(501)
            self.end_headers()
            return
    def main_handler(self, method="GET"):
        # get request url (without url params) and remove trailing /
        config.debug(">>> handling request")
        config.debug(self.path)

        request_url = self.path.split("?")[0]
        if request_url is not "/":
            request_url = request_url.rstrip("/")

        handler = None
        try:
            handler = self.mappings[request_url][method]
            # config.debug(handler)
        except KeyError:
            # no mapping found for the request
            self.send_response(404)
            self.end_headers()
            return

        try:
            handler(self)
        except KeyError:
            # method not found
            self.send_response(501)
            self.end_headers()
            return
Exemple #3
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-m', '--mmpath') 
    args = parser.parse_args()
    config.mm_path = args.mmpath
    try:
        server_threaded.run()
    except:
        config.debug("Server at port 9000 already running")
def run(context_path='', port=9000):
    gc.debug('starting local MavensMate UI server')
    base_dir = os.path.normpath(os.path.abspath(os.path.curdir))
    sys.path.insert(0, base_dir)
    handler.Handler.mappings = endpoints.mappings
    server = ThreadedHTTPServer((context_path, port), handler.Handler)
    server_thread = threading.Thread(target=server.serve_forever)
    server_thread.daemon = True
    server_thread.start()
Exemple #5
0
def run(context_path='', port=9000):
    gc.debug('starting local MavensMate UI server')
    base_dir = os.path.normpath(os.path.abspath(os.path.curdir))
    sys.path.insert(0, base_dir)
    handler.Handler.mappings = endpoints.mappings
    server = ThreadedHTTPServer((context_path, port), handler.Handler)
    server_thread = threading.Thread(target=server.serve_forever)
    server_thread.daemon = True
    server_thread.start()
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-m', '--mmpath')
    args = parser.parse_args()
    config.mm_path = args.mmpath
    try:
        server_threaded.run()
    except:
        config.debug("Server at port 9000 already running")
def unit_test_request(request_handler):
    '''
        POST /project/unit_test
        {
            "classes" : [
                "UnitTestClass1", "UnitTestClass2"
            ],
            "run_all_tests" : false
        }
    '''
    gc.debug('in unit test method!')
    run_async_operation(request_handler, 'unit_test')
def unit_test_request(request_handler):
    '''
        POST /project/unit_test
        {
            "classes" : [
                "UnitTestClass1", "UnitTestClass2"
            ],
            "run_all_tests" : false
        }
    '''
    gc.debug('in unit test method!')
    run_async_operation(request_handler, 'unit_test')
    def run(self):
        mm_response = None
        args = self.get_arguments()
        global_config.debug('>>> running thread arguments on next line!')
        global_config.debug(args)
        if self.debug_mode or 'darwin' not in sys.platform:
            print(self.payload)
            python_path = sublime.load_settings('mavensmate.sublime-settings').get('mm_python_location')

            if 'darwin' in sys.platform or sublime.load_settings('mavensmate.sublime-settings').get('mm_debug_location') != None:
                mm_loc = sublime.load_settings('mavensmate.sublime-settings').get('mm_debug_location')
            else:
                mm_loc = os.path.join(config.mm_dir,"mm","mm.py")
            #p = subprocess.Popen("{0} {1} {2}".format(python_path, pipes.quote(mm_loc), args), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
        
            if 'linux' in sys.platform or 'darwin' in sys.platform:
                #osx, linux
                p = subprocess.Popen('\'{0}\' \'{1}\' {2}'.format(python_path, mm_loc, self.get_arguments()), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
            else:
                #windows
                if sublime.load_settings('mavensmate.sublime-settings').get('mm_debug_mode', False):
                    #user wishes to use system python
                    python_path = sublime.load_settings('mavensmate.sublime-settings').get('mm_python_location')
                    p = subprocess.Popen('"{0}" "{1}" {2}'.format(python_path, mm_loc, self.get_arguments()), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
                else:
                    python_path = os.path.join(os.environ["ProgramFiles"],"MavensMate","App","python.exe")
                    if not os.path.isfile(python_path):
                        python_path = python_path.replace("Program Files", "Program Files (x86)")
                    p = subprocess.Popen('"{0}" -E "{1}" {2}'.format(python_path, mm_loc, self.get_arguments()), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)

            #process = subprocess.Popen("{0} {1} {2}".format(python_path, pipes.quote(mm_loc), self.get_arguments()), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
        else:
            p = subprocess.Popen("{0} {1}".format(pipes.quote(self.mm_path), args), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
        #print("PAYLOAD: ",self.payload)
        #print("PAYLOAD TYPE: ",type(self.payload))
        if self.payload != None and type(self.payload) is str:
            self.payload = self.payload.encode('utf-8')
        p.stdin.write(self.payload)
        p.stdin.close()
        if p.stdout is not None: 
            mm_response = p.stdout.readlines()
        elif p.stderr is not None:
            mm_response = p.stderr.readlines()
        
        #response_body = '\n'.join(mm_response.decode('utf-8'))
        strs = []
        for line in mm_response:
            strs.append(line.decode('utf-8'))   
        response_body = '\n'.join(strs)

        global_config.debug('>>> got a response body')
        global_config.debug(response_body)

        if '--html' not in args:
            try:
                valid_json = json.loads(response_body)
            except:
                response_body = generate_error_response(response_body)

        self.response = response_body
def status_request(request_handler):
    gc.debug('>>> status request')
    params, json_string, plugin_client = get_request_params(request_handler)
    gc.debug('>>> params: ')
    gc.debug(params)
    try:
        request_id = params['id']
    except:
        request_id = params['id'][0]
    gc.debug('>>> request id: ' + request_id)
    gc.debug('>>> async queue: ')
    gc.debug(async_request_queue)

    if request_id not in async_request_queue:
        response = { 'status' : 'error', 'id' : request_id, 'body' : 'Request ID was not found in async request queue.' }
        response_body = json.dumps(response)
        respond(request_handler, response_body, 'text/json')
    else:
        async_thread = async_request_queue[request_id]
        gc.debug('found thread in request queue, checking if alive')
        gc.debug(async_thread.is_alive())
        if async_thread.is_alive():
            gc.debug('>>> request is not ready')
            respond_with_async_request_id(request_handler, request_id)
        elif async_thread.is_alive() == False:
            gc.debug('>>> request is ready, returning response')
            async_request_queue.pop(request_id, None)
            respond(request_handler, async_thread.response, 'text/json')
def run_async_operation(request_handler, operation_name):
    gc.debug('>>> running an async operation')
    request_id = util.generate_request_id()
    params, raw_post_body, plugin_client = get_request_params(request_handler)
    if operation_name == None and "command" in params:
        operation_name = params["command"]
    gc.debug(request_id)
    gc.debug(params)
    gc.debug(raw_post_body)
    
    worker_thread = BackgroundWorker(operation_name, params, True, request_id, raw_post_body, plugin_client)
    gc.debug('worker created')
    worker_thread.start()
    gc.debug('worker thread started')
    async_request_queue[request_id] = worker_thread
    gc.debug('placed into queue')

    return respond_with_async_request_id(request_handler, request_id)
def respond_with_async_request_id(request_handler, request_id):
    response = {'status': 'pending', 'id': request_id}
    json_response_body = json.dumps(response)
    gc.debug(json_response_body)
    respond(request_handler, json_response_body, 'text/json')
def status_request(request_handler):
    gc.debug('>>> status request')
    params, json_string, plugin_client = get_request_params(request_handler)
    gc.debug('>>> params: ')
    gc.debug(params)
    try:
        request_id = params['id']
    except:
        request_id = params['id'][0]
    gc.debug('>>> request id: ' + request_id)
    gc.debug('>>> async queue: ')
    gc.debug(async_request_queue)

    if request_id not in async_request_queue:
        response = {
            'status': 'error',
            'id': request_id,
            'body': 'Request ID was not found in async request queue.'
        }
        response_body = json.dumps(response)
        respond(request_handler, response_body, 'text/json')
    else:
        async_thread = async_request_queue[request_id]
        gc.debug('found thread in request queue, checking if alive')
        gc.debug(async_thread.is_alive())
        if async_thread.is_alive():
            gc.debug('>>> request is not ready')
            respond_with_async_request_id(request_handler, request_id)
        elif async_thread.is_alive() == False:
            gc.debug('>>> request is ready, returning response')
            async_request_queue.pop(request_id, None)
            respond(request_handler, async_thread.response, 'text/json')
def run_async_operation(request_handler, operation_name):
    gc.debug('>>> running an async operation')
    request_id = util.generate_request_id()
    params, raw_post_body, plugin_client = get_request_params(request_handler)
    if operation_name == None and "command" in params:
        operation_name = params["command"]
    gc.debug(request_id)
    gc.debug(params)
    gc.debug(raw_post_body)

    worker_thread = BackgroundWorker(operation_name, params, True, request_id,
                                     raw_post_body, plugin_client)
    gc.debug('worker created')
    worker_thread.start()
    gc.debug('worker thread started')
    async_request_queue[request_id] = worker_thread
    gc.debug('placed into queue')

    return respond_with_async_request_id(request_handler, request_id)
    def run(self):
        mm_response = None
        args = self.get_arguments()
        global_config.debug(">>> running thread arguments on next line!")
        global_config.debug(args)
        mm_path = self.settings.get("mm_path")
        if self.settings.get("mm_developer_mode", False):  # user wishes to run mm.py via python install
            python_path = self.settings.get("mm_python_location")
            mm_mm_py_location = self.settings.get("mm_mm_py_location")

            if "linux" in sys.platform or "darwin" in sys.platform:
                # osx, linux
                process = subprocess.Popen(
                    "'{0}' '{1}' {2}".format(python_path, mm_mm_py_location, self.get_arguments()),
                    stdin=subprocess.PIPE,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.STDOUT,
                    shell=True,
                )
            else:
                # windows
                process = subprocess.Popen(
                    '"{0}" "{1}" {2}'.format(python_path, mm_mm_py_location, self.get_arguments()),
                    stdin=subprocess.PIPE,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.STDOUT,
                    shell=True,
                )

        else:  # running mm executable normally
            if mm_path == "default":  # default location is in plugin root 'mm' directory
                if sys.platform == "linux" or sys.platform == "darwin":
                    mm_path = os.path.join(sublime.packages_path(), "User", "MavensMate", "mm", "mm")
                else:
                    mm_path = os.path.join(sublime.packages_path(), "User", "MavensMate", "mm", "mm.exe")

            if "linux" in sys.platform or "darwin" in sys.platform:
                global_config.debug("mm command: ")
                global_config.debug("{0} {1}".format(pipes.quote(mm_path), self.get_arguments()))
                process = subprocess.Popen(
                    "{0} {1}".format(pipes.quote(mm_path), self.get_arguments()),
                    stdin=subprocess.PIPE,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.STDOUT,
                    shell=True,
                )
            else:  # windows
                global_config.debug("mm command: ")
                global_config.debug('"{0}" {1}'.format(mm_path, self.get_arguments()))
                process = subprocess.Popen(
                    '"{0}" {1}'.format(mm_path, self.get_arguments()),
                    stdin=subprocess.PIPE,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.STDOUT,
                    shell=True,
                )

        if self.payload != None and type(self.payload) is str:
            self.payload = self.payload.encode("utf-8")
        process.stdin.write(self.payload)
        process.stdin.close()
        if process.stdout is not None:
            mm_response = process.stdout.readlines()
        elif process.stderr is not None:
            mm_response = process.stderr.readlines()

        # response_body = '\n'.join(mm_response.decode('utf-8'))
        strs = []
        for line in mm_response:
            strs.append(line.decode("utf-8"))
        response_body = "\n".join(strs)

        global_config.debug(">>> got a response body")
        global_config.debug(response_body)

        if "--html" not in args:
            try:
                valid_json = json.loads(response_body)
            except:
                response_body = generate_error_response(response_body)

        self.response = response_body
def respond_with_async_request_id(request_handler, request_id):
    response = { 'status' : 'pending', 'id' : request_id }
    json_response_body = json.dumps(response)
    gc.debug(json_response_body)
    respond(request_handler, json_response_body, 'text/json')
    def run(self):
        mm_response = None
        args = self.get_arguments()
        global_config.debug('>>> running thread arguments on next line!')
        global_config.debug(args)
        mm_path = self.settings.get('mm_path')
        if self.settings.get(
                'mm_developer_mode',
                False):  #user wishes to run mm.py via python install
            python_path = self.settings.get('mm_python_location')
            mm_mm_py_location = self.settings.get('mm_mm_py_location')

            if 'linux' in sys.platform or 'darwin' in sys.platform:
                #osx, linux
                process = subprocess.Popen('\'{0}\' \'{1}\' {2}'.format(
                    python_path, mm_mm_py_location, self.get_arguments()),
                                           stdin=subprocess.PIPE,
                                           stdout=subprocess.PIPE,
                                           stderr=subprocess.STDOUT,
                                           shell=True)
            else:
                #windows
                process = subprocess.Popen('"{0}" "{1}" {2}'.format(
                    python_path, mm_mm_py_location, self.get_arguments()),
                                           stdin=subprocess.PIPE,
                                           stdout=subprocess.PIPE,
                                           stderr=subprocess.STDOUT,
                                           shell=True)

        else:  #running mm executable normally
            if mm_path == 'default':  #default location is in plugin root 'mm' directory
                if sys.platform == 'linux' or sys.platform == 'darwin':
                    mm_path = os.path.join(sublime.packages_path(), "User",
                                           "MavensMate", "mm", "mm")
                else:
                    mm_path = os.path.join(sublime.packages_path(), "User",
                                           "MavensMate", "mm", "mm.exe")

            if 'linux' in sys.platform or 'darwin' in sys.platform:
                global_config.debug('mm command: ')
                global_config.debug("{0} {1}".format(pipes.quote(mm_path),
                                                     self.get_arguments()))
                process = subprocess.Popen("{0} {1}".format(
                    pipes.quote(mm_path), self.get_arguments()),
                                           stdin=subprocess.PIPE,
                                           stdout=subprocess.PIPE,
                                           stderr=subprocess.STDOUT,
                                           shell=True)
            else:  #windows
                global_config.debug('mm command: ')
                global_config.debug('"{0}" {1}'.format(mm_path,
                                                       self.get_arguments()))
                process = subprocess.Popen('"{0}" {1}'.format(
                    mm_path, self.get_arguments()),
                                           stdin=subprocess.PIPE,
                                           stdout=subprocess.PIPE,
                                           stderr=subprocess.STDOUT,
                                           shell=True)

        if self.payload != None and type(self.payload) is str:
            self.payload = self.payload.encode('utf-8')
        process.stdin.write(self.payload)
        process.stdin.close()
        if process.stdout is not None:
            mm_response = process.stdout.readlines()
        elif process.stderr is not None:
            mm_response = process.stderr.readlines()

        #response_body = '\n'.join(mm_response.decode('utf-8'))
        strs = []
        for line in mm_response:
            strs.append(line.decode('utf-8'))
        response_body = '\n'.join(strs)

        global_config.debug('>>> got a response body')
        global_config.debug(response_body)

        if '--html' not in args:
            try:
                valid_json = json.loads(response_body)
            except:
                response_body = generate_error_response(response_body)

        self.response = response_body
    def run(self):
        mm_response = None
        args = self.get_arguments()
        global_config.debug('>>> running thread arguments on next line!')
        global_config.debug(args)
        if self.debug_mode or 'darwin' not in sys.platform:
            print(self.payload)
            python_path = sublime.load_settings(
                'mavensmate.sublime-settings').get('mm_python_location')

            if 'darwin' in sys.platform or sublime.load_settings(
                    'mavensmate.sublime-settings').get(
                        'mm_debug_location') != None:
                mm_loc = sublime.load_settings(
                    'mavensmate.sublime-settings').get('mm_debug_location')
            else:
                mm_loc = os.path.join(config.mm_dir, "mm", "mm.py")
            #p = subprocess.Popen("{0} {1} {2}".format(python_path, pipes.quote(mm_loc), args), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)

            if 'linux' in sys.platform or 'darwin' in sys.platform:
                #osx, linux
                p = subprocess.Popen('\'{0}\' \'{1}\' {2}'.format(
                    python_path, mm_loc, self.get_arguments()),
                                     stdin=subprocess.PIPE,
                                     stdout=subprocess.PIPE,
                                     stderr=subprocess.STDOUT,
                                     shell=True)
            else:
                #windows
                if sublime.load_settings('mavensmate.sublime-settings').get(
                        'mm_debug_mode', False):
                    #user wishes to use system python
                    python_path = sublime.load_settings(
                        'mavensmate.sublime-settings').get(
                            'mm_python_location')
                    p = subprocess.Popen('"{0}" "{1}" {2}'.format(
                        python_path, mm_loc, self.get_arguments()),
                                         stdin=subprocess.PIPE,
                                         stdout=subprocess.PIPE,
                                         stderr=subprocess.STDOUT,
                                         shell=True)
                else:
                    python_path = os.path.join(os.environ["ProgramFiles"],
                                               "MavensMate", "App",
                                               "python.exe")
                    if not os.path.isfile(python_path):
                        python_path = python_path.replace(
                            "Program Files", "Program Files (x86)")
                    p = subprocess.Popen('"{0}" -E "{1}" {2}'.format(
                        python_path, mm_loc, self.get_arguments()),
                                         stdin=subprocess.PIPE,
                                         stdout=subprocess.PIPE,
                                         stderr=subprocess.STDOUT,
                                         shell=True)

            #process = subprocess.Popen("{0} {1} {2}".format(python_path, pipes.quote(mm_loc), self.get_arguments()), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
        else:
            p = subprocess.Popen("{0} {1}".format(pipes.quote(self.mm_path),
                                                  args),
                                 stdin=subprocess.PIPE,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.STDOUT,
                                 shell=True)
        #print("PAYLOAD: ",self.payload)
        #print("PAYLOAD TYPE: ",type(self.payload))
        if self.payload != None and type(self.payload) is str:
            self.payload = self.payload.encode('utf-8')
        p.stdin.write(self.payload)
        p.stdin.close()
        if p.stdout is not None:
            mm_response = p.stdout.readlines()
        elif p.stderr is not None:
            mm_response = p.stderr.readlines()

        #response_body = '\n'.join(mm_response.decode('utf-8'))
        strs = []
        for line in mm_response:
            strs.append(line.decode('utf-8'))
        response_body = '\n'.join(strs)

        global_config.debug('>>> got a response body')
        global_config.debug(response_body)

        if '--html' not in args:
            try:
                valid_json = json.loads(response_body)
            except:
                response_body = generate_error_response(response_body)

        self.response = response_body