Beispiel #1
0
 def __init__(self):
     self.c = Communicate()
     self.d = Detection()
     self.m = Measure()
     self.msg_list = MSG_LIST  # 提示消息字典
     self.cmd_dict = {}  # 命令字典
     self._func_dict()  # 填充cmd_dict,将类方法名中后缀为'_(\d+)'的方法添加进命令字典
 def test_lamp_simple_print(self):
     lamp = Communicate(self.lampExecutable, self.lampPrompt, self.lampExitCommand)
     
     output,errors = lamp.communicate('print, "%s"'%self.stringToPrint, waitTimeForTheCommandToGiveOutput=0.2)
     
     self.assertEqual(output.strip(),self.stringToPrint)
     self.assertEqual(errors.strip(),"")
     
     lamp.exit();
    def __init__(self, callbackFunc, hostname, messagetosend, endchar, timeout, attempts=1):
        '''Constructor'''
        Thread.__init__(self)
        self._name = "ClientCommThread"
        self._portname = None




        if callbackFunc != None:
            self._callback = Communicate()
            self._callback.myGUI_signal.connect(callbackFunc)

        if hostname != None:
            self._hostname = hostname

        if timeout != None:
            self._timeout = timeout


        if not StringUtils.isNoneOrEmpty(messagetosend):
            self._messagetosend = messagetosend

        if not StringUtils.isNoneOrEmpty(endchar):
            self._end_char = endchar

        if attempts != None:
            self._attemps = attempts

        self._stopevent = Event()
        self._stopevent.clear()

        self._wasStopped = Event()
        self._wasStopped.clear()
    def test_python_simple_print(self):
        '''
        To run just this test:
        python -m unittest -q testCommunicate.TestCommunicate.test_python_simple_print
        '''

        pyshell = Communicate(self.pythonExecutable, self.pythonPrompt, self.pythonExitCommand)
        time.sleep(0.2)
        output,errors = pyshell.communicate('print "%s"'%self.stringToPrint, waitTimeForTheCommandToGiveOutput=0)
        
        print 'O', output
        print 'E', errors
        
        self.assertEqual(output.strip(),self.stringToPrint)
        self.assertEqual(errors.strip(),"")
        
        pyshell.exit();
Beispiel #5
0
    def __init__(self,
                 callbackFunc,
                 serialport,
                 baudrate,
                 messagetosend,
                 endchar,
                 timeout,
                 attempts=2):
        '''Constructor'''
        Thread.__init__(self)
        self._name = "SerialCommThread"
        self._portname = None

        if callbackFunc != None:
            self._callback = Communicate()
            self._callback.myGUI_signal.connect(callbackFunc)

        if baudrate != None:
            self._baudrate = baudrate

        if timeout != None:
            self._timeout = timeout

        if serialport != None and baudrate != None:
            self._serialport = serialport
            self._serialport.baudrate = self._baudrate
            self._serialport.rtscts = False
            self._serialport.dsrdtr = False
            self._serialport.timeout = 10
            self._serialport.writeTimeout = 1
            self._serialport.stopbits = serial.STOPBITS_ONE
            self._serialport.parity = serial.PARITY_NONE
            self._serialport.bytesize = serial.EIGHTBITS
            self._serialport.xonxoff = True

            self._serialport.setDTR(True)
            self._serialport.setRTS(False)

            self._portname = self._serialport.name

            if timeout > 0:
                self._serialport.timeout = 0.25

        if not StringUtils.isNoneOrEmpty(messagetosend):
            self._messagetosend = messagetosend

        if not StringUtils.isNoneOrEmpty(endchar):
            self._end_char = endchar

        if attempts != None:
            self._attemps = attempts

        self._stopevent = Event()
        self._stopevent.clear()

        self._wasStopped = Event()
        self._wasStopped.clear()
    def __init__(self, args):
        '''Constructor'''
        Thread.__init__(self)
        self._args = args
        self._name = "DataListenerServer Thread"
        if args != None:
            self.mySrc = Communicate()
            self.mySrc.myGUI_signal.connect(args)

        self._stop_event = Event()
        self._stop_event.clear()
    def __init__(self,comm):
        '''Constructor'''
        Thread.__init__(self)
        self._comm = comm
        self._name = "UpdateFile Thread"
        
        if comm != None:
            self._src = Communicate()
            self._src.myGUI_signal.connect(comm)

        self._stopEvent = Event()
        self._stopEvent.clear() 
 def test_lamp_print_with_process_relaunch(self):
     lamp = Communicate(self.lampExecutable, self.lampPrompt, self.lampExitCommand)
     #time.sleep(0.2)
     output,errors = lamp.communicate('print, "%s"'%self.stringToPrint, waitTimeForTheCommandToGiveOutput=0.2)
     
     self.assertEqual(output.strip(),self.stringToPrint)
     self.assertEqual(errors.strip(),"")
     
     lamp.exit();
     
     output,errors = lamp.communicate('print, "%s"'%self.stringToPrint, waitTimeForTheCommandToGiveOutput=0.2)
     
     self.assertEqual(output.strip(),self.stringToPrint)
     self.assertEqual(errors.strip(),"")
     
     lamp.exit();
Beispiel #9
0
def executeProcess(manifest, role, extension_id, user_id, argv, timeout, stdin=None, rlimit_cpu=5, rlimit_rss=256):
    flavor = manifest.flavor

    if manifest.flavor not in configuration.extensions.FLAVORS:
        flavor = configuration.extensions.DEFAULT_FLAVOR

    executable = configuration.extensions.FLAVORS[flavor]["executable"]
    library = configuration.extensions.FLAVORS[flavor]["library"]

    process_argv = [executable,
                    "--rlimit-cpu=%ds" % rlimit_cpu,
                    "--rlimit-rss=%dm" % rlimit_rss,
                    os.path.join(library, "critic-launcher.js")]

    stdin_data = "%s\n" % json_encode({ "criticjs_path": os.path.join(library, "critic2.js"),
                                        "rlimit": { "cpu": rlimit_cpu,
                                                    "rss": rlimit_rss },
                                        "hostname": configuration.base.HOSTNAME,
                                        "dbname": configuration.database.PARAMETERS["database"],
                                        "dbuser": configuration.database.PARAMETERS["user"],
                                        "git": configuration.executables.GIT,
                                        "python": configuration.executables.PYTHON,
                                        "python_path": "%s:%s" % (configuration.paths.CONFIG_DIR,
                                                                  configuration.paths.INSTALL_DIR),
                                        "repository_work_copy_path": os.path.join(configuration.paths.DATA_DIR, "temporary", "EXTENSIONS"),
                                        "changeset_address": configuration.services.CHANGESET["address"],
                                        "maildelivery_pid_path": configuration.services.MAILDELIVERY["pidfile_path"],
                                        "is_development": configuration.debug.IS_DEVELOPMENT,
                                        "extension_path": manifest.path,
                                        "extension_id": extension_id,
                                        "user_id": user_id,
                                        "role": role.name(),
                                        "script_path": role.script,
                                        "fn": role.function,
                                        "argv": argv })

    if stdin is not None:
        stdin_data += stdin

    process = subprocess.Popen(process_argv, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=manifest.path)

    communicate = Communicate(process)
    communicate.setInput(stdin_data)
    communicate.setTimout(timeout)

    return communicate.run()[0]
Beispiel #10
0
class Scheduler(object, metaclass=type):
    def __init__(self):
        self.c = Communicate()
        self.d = Detection()
        self.m = Measure()
        self.msg_list = MSG_LIST  # 提示消息字典
        self.cmd_dict = {}  # 命令字典
        self._func_dict()  # 填充cmd_dict,将类方法名中后缀为'_(\d+)'的方法添加进命令字典

    def _func_dict(self):
        for func in dir(self):
            want_func = re.search(r'_\d+', func)
            if want_func:
                cmd_code = want_func.group()[-1]
                if self.cmd_dict.get(cmd_code):
                    logger.warning('{}与{}命令重复'.format(
                        func, self.cmd_dict.get(cmd_code)))
                else:
                    self.cmd_dict[cmd_code] = func

    def _parse_msg(self, msg):
        if '(Text)' in msg:
            try:
                cmd_code = re.search(r'(\d+)', msg).group(1)
                return cmd_code
            except AttributeError:
                logger.warning('输入的命令不存在,无法解析:{}'.format(msg))
                self.c.send_text(self.msg_list[101])
        else:
            logger.warning('不支持的消息类型,只支持字符型: {}'.format(msg))
            self.c.send_text(self.msg_list[100])

    def execute_cmd(self, msg):
        cmd_code = self._parse_msg(msg)
        func = self.cmd_dict.get(cmd_code)
        if func:
            eval('self.{}()'.format(func))

    @info_wrapper
    def test_communicate_0(self):
        self.c.send_text(self.msg_list[1])

    @info_wrapper
    def get_disallowed_process_list_1(self):
        lst = self.d.get_disallowed_process_running()
        print(lst)
        if lst:
            self.c.send_text(lst)
        else:
            self.c.send_text(self.msg_list[3])

    @info_wrapper
    def get_screenshot_2(self):
        f_name = self.m.get_screenshot()
        self.c.send_image(f_name)

    @info_wrapper
    def get_camera_3(self):
        f_name = self.m.get_camera()
        self.c.send_image(f_name)

    @info_wrapper
    def sleep_computer_4(self):
        self.m.sleep_computer()
        self.c.send_text(self.msg_list[4])

    @info_wrapper
    def close_computer_5(self):
        self.m.close_computer()
        self.c.send_text(self.msg_list[5])

    @info_wrapper
    def reboot_computer_6(self):
        self.m.reboot_computer()
        self.c.send_text(self.msg_list[6])

    @info_wrapper
    def cancel_reboot_computer_7(self):
        self.m.cancel_reboot_computer()
        self.c.send_text(self.msg_list[7])
Beispiel #11
0
def executeProcess(manifest,
                   role_name,
                   script,
                   function,
                   extension_id,
                   user_id,
                   argv,
                   timeout,
                   stdin=None,
                   rlimit_rss=256):
    flavor = manifest.flavor

    if manifest.flavor not in configuration.extensions.FLAVORS:
        flavor = configuration.extensions.DEFAULT_FLAVOR

    executable = configuration.extensions.FLAVORS[flavor]["executable"]
    library = configuration.extensions.FLAVORS[flavor]["library"]

    process_argv = [executable, os.path.join(library, "critic-launcher.js")]

    stdin_data = "%s\n" % json_encode({
        "criticjs_path":
        os.path.join(library, "critic.js"),
        "rlimit": {
            "rss": rlimit_rss
        },
        "hostname":
        configuration.base.HOSTNAME,
        "dbname":
        configuration.database.PARAMETERS["database"],
        "dbuser":
        configuration.database.PARAMETERS["user"],
        "git":
        configuration.executables.GIT,
        "python":
        configuration.executables.PYTHON,
        "python_path":
        "%s:%s" %
        (configuration.paths.CONFIG_DIR, configuration.paths.INSTALL_DIR),
        "repository_work_copy_path":
        configuration.extensions.WORKCOPY_DIR,
        "changeset_address":
        configuration.services.CHANGESET["address"],
        "branchtracker_pid_path":
        configuration.services.BRANCHTRACKER["pidfile_path"],
        "maildelivery_pid_path":
        configuration.services.MAILDELIVERY["pidfile_path"],
        "is_development":
        configuration.debug.IS_DEVELOPMENT,
        "extension_path":
        manifest.path,
        "extension_id":
        extension_id,
        "user_id":
        user_id,
        "role":
        role_name,
        "script_path":
        script,
        "fn":
        function,
        "argv":
        argv
    })

    if stdin is not None:
        stdin_data += stdin

    process = subprocess.Popen(process_argv,
                               stdin=subprocess.PIPE,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE,
                               cwd=manifest.path)

    communicate = Communicate(process)
    communicate.setInput(stdin_data)
    communicate.setTimeout(timeout)

    return communicate.run()[0]