Пример #1
0
def main(args):
    rospy.init_node('wechat', anonymous=True)
    signal.signal(signal.SIGINT, quit)
    signal.signal(signal.SIGTERM, quit)
    if cm.get('setting', 'server_mode') == 'True':
        serverProcess = threading.Thread(target=run_server)
        serverProcess.start()
    cmdProcess = threading.Thread(target=cmd_input)
    cmdProcess.setDaemon(True)
    cmdProcess.start()
    while not rospy.is_shutdown():
        try:
            wechat.start()
        except KeyboardInterrupt:
            echo(Constant.LOG_MSG_QUIT)
            wechat.exit_code = 1
        else:
            Log.error(traceback.format_exc())
        finally:
            wechat.stop()

        # send a mail to tell the wxbot is failing
        subject = 'wxbot stop message'
        log_file = open(eval(cm.get('handler_fileHandler', 'args'))[0], 'r')
        mail_content = '<pre>' + str(
            wechat) + '\n\n-----\nLogs:\n-----\n\n' + ''.join(
                log_file.readlines()[-100:]) + '</pre>'
        sg.send_mail(subject, mail_content, 'text/html')
        log_file.close()

        if wechat.exit_code == 0:
            echo(Constant.MAIN_RESTART)
        else:
            # kill process
            os.system(Constant.LOG_MSG_KILL_PROCESS % os.getpid())
Пример #2
0
    def send_file(self, user_id, file_path):
        """
        @brief      send file
        @param      user_id  String
        @param      file_path  String
        @return     Bool: whether operation succeed
        """
        title = file_path.split('/')[-1]
        data = {
            'appid': Constant.API_WXAPPID,
            'title': title,
            'totallen': '',
            'attachid': '',
            'type': self.wx_conf['APPMSGTYPE_ATTACH'],
            'fileext': title.split('.')[-1],
        }

        response = self.webwxuploadmedia(file_path)
        if response is not None:
            data['totallen'] = response['StartPos']
            data['attachid'] = response['MediaId']
        else:
            Log.error('File upload error')
        
        return self.webwxsendappmsg(user_id, data)
Пример #3
0
    def run(self):
        '''
        Start process, the subclass must rewrite this function or 'handle' function
        when all the father processes quits, then break to quit
        '''

        if self.log:
            self.log = Log(self.name, toFile="plugin")
            self.log.info("plugin {0} start, ins:{1}, outs:{2}".format(
                self.name, self._ins, self._outs))
        else:
            self.log = False

        while True:
            try:
                data = self.get()
                #print "debug:", "plugin ", self.name, "getting", "ins<<<<<<<<", [str(x) for x in self._ins]
            except QueueEmpty:
                continue
            except IOError:
                break
            except EOFError:
                break
            except PluginExit:
                self.quit()
                #print "debug:", "plugin ", self.name, "quit"
                if self.log:
                    self.log.info("plugin {0} quit".format(self.name))
                break
            else:
                #print "debug:", "plugin ", self.name, "got", data
                self.handle(data)
            finally:
                time.sleep(self.timeout)
Пример #4
0
    def recover(self):
        """
        @brief      Recover from snapshot data.
        @return     Bool: whether operation succeed.
        """
        cm = ConfigManager()
        [self.uuid, self.redirect_uri, self.uin,
        self.sid, self.skey, self.pass_ticket,
        self.synckey, device_id, self.last_login] = cm.get_wechat_config()

        if device_id:
            self.device_id = device_id

        self.base_request = {
            'Uin': int(self.uin),
            'Sid': self.sid,
            'Skey': self.skey,
            'DeviceID': self.device_id,
        }

        # set cookie
        Log.debug('set cookie')
        self.cookie = set_cookie(self.cookie_file)

        return True
Пример #5
0
    def send_file(self, user_id, file_path):
        """
        @brief      send file
        @param      user_id  String
        @param      file_path  String
        @return     Bool: whether operation succeed
        """
        title = file_path.split('/')[-1]
        data = {
            'appid': Constant.API_WXAPPID,
            'title': title,
            'totallen': '',
            'attachid': '',
            'type': self.wx_conf['APPMSGTYPE_ATTACH'],
            'fileext': title.split('.')[-1],
        }

        response = self.webwxuploadmedia(file_path)
        if response is not None:
            data['totallen'] = response['StartPos']
            data['attachid'] = response['MediaId']
        else:
            Log.error('File upload error')

        return self.webwxsendappmsg(user_id, data)
Пример #6
0
 def close(self):
     """
     @brief      close connection to database
     """
     self.conn.commit()
     Log.debug('DB -> close')
     self.conn.close()
Пример #7
0
def auto_reload(mod):
    """
    @brief      reload modules
    @param      mod: the need reload modules
    """
    try:
        module = sys.modules[mod]
    except:
        Log.error(traceback.format_exc())
        return False

    filename = module.__file__
    # .pyc 修改时间不会变
    # 所以就用 .py 的修改时间
    if filename.endswith(".pyc"):
        filename = filename.replace(".pyc", ".py")
    mod_time = os.path.getmtime(filename)
    if not "loadtime" in module.__dict__:
        module.loadtime = 0

    try:
        if mod_time > module.loadtime:
            reload(module)
        else:
            return False
    except:
        Log.error(traceback.format_exc())
        return False

    module.loadtime = mod_time

    echo('[*] load \'%s\' successful.\n' % mod)
    return True
Пример #8
0
def auto_reload(mod):
    """
    @brief      reload modules
    @param      mod: the need reload modules
    """
    try:
        module = sys.modules[mod]
    except:
        Log.error(traceback.format_exc())
        return False

    filename = module.__file__
    # .pyc 修改时间不会变
    # 所以就用 .py 的修改时间
    if filename.endswith(".pyc"):
        filename = filename.replace(".pyc", ".py")
    mod_time = os.path.getmtime(filename)
    if not "loadtime" in module.__dict__:
        module.loadtime = 0

    try:
        if mod_time > module.loadtime:
            reload(module)
        else:
            return False
    except:
        Log.error(traceback.format_exc())
        return False

    module.loadtime = mod_time

    echo('[*] load \'%s\' successful.\n' % mod)
    return True
Пример #9
0
def upload_method(cron_id, vpn_ip, work_path, exchange, task_type):
    try:
        run_date = datetime.datetime.now().strftime("%Y%m%d")
        # remote check
        remote_check_flag = dir_check(vpn_ip, work_path)
        local_files = get_local_files(task_type, vpn_ip, work_path)
        print local_files
        # local check
        local_check_flag, size = loacal_files_check(local_files)
        if not local_files or not remote_check_flag or not local_check_flag:
            cron_trigger(cron_id, -1, size, run_date)
            return False
        his_cron_id = cron_trigger(cron_id, 0, size, run_date)
        if his_cron_id < 0:
            return False
        file_str = " ".join(local_files)
        cmd = "rsync -avz --progress %s %s@%s:%s" % (file_str, "mycapitaltrade", vpn_ip, work_path)
        print cmd
        tf_out = tempfile.NamedTemporaryFile()
        tf_out_r = open(tf_out.name, "r")
        proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=tf_out)
        while proc.poll() is None:
            multiple_listen_stdout(cron_id, his_cron_id, size, run_date, tf_out_r)
            time.sleep(0.5)
        else:
            multiple_listen_stdout(cron_id, his_cron_id, size, run_date, tf_out_r)
        tf_out_r.close()
        tf_out.close()
        return True
    except:
        Log.error("rsync log failed. %s" % traceback.format_exc())
        return False
Пример #10
0
    def recover(self):
        """
        @brief      Recover from snapshot data.
        @return     Bool: whether operation succeed.
        """
        cm = ConfigManager()
        [
            self.uuid, self.redirect_uri, self.uin, self.sid, self.skey,
            self.pass_ticket, self.synckey, device_id, self.last_login
        ] = cm.get_wechat_config()

        if device_id:
            self.device_id = device_id

        self.base_request = {
            'Uin': int(self.uin),
            'Sid': self.sid,
            'Skey': self.skey,
            'DeviceID': self.device_id,
        }

        # set cookie
        Log.debug('set cookie')
        self.cookie = set_cookie(self.cookie_file)

        return True
Пример #11
0
	def __adjustUrl(self, src_url):
		if not self.__feedingUrl:
			Log.e("{Spider.__adjustUrl} self.__feedingUrl is None")
			return src_url

		if src_url == '#':
			return self.__feedingUrl

		if src_url.startswith('//'):
			src_url = "http:" + src_url
			return src_url

		if src_url.startswith('http://') or src_url.startswith('https://'):
			return src_url

		if src_url.startswith('javascript:'):
			return src_url

		elems = src_url.split('/')
		feeding = self.__feedingUrl.split('/')

		for e in elems:
			if e == '.':
				elems.remove(e)

		while True:
			feeding.pop()
			if elems[0] != '..':
				break;
			elems.pop(0)

		url = feeding + elems
		return '/'.join(url)
Пример #12
0
 def close(self):
     """
     @brief      close connection to database
     """
     Log.debug('DB -> close')
     # 关闭数据库连接
     self.conn.close()
Пример #13
0
 def __init__(self, width=10, height=10, num_agents=2):
     super(RandomRewardGridEnv, self).__init__()
     self.map = GridMap(width=10, height=10)
     self.agents = list()
     for i in range(num_agents):
         self.agents.append(Agent(active_map=self.map))
         self.map.registerAgent(self.agents[-1])
     Log.warning("New env created with %d agents" % (len(self.agents)))
Пример #14
0
    def step(self, action):
        Log.debug("=================== New Step ===================")

        self.__recorder.record('step')
        ret_val = self.__doSequentialStep__(action)
        self.__recorder.record('step')

        return ret_val
Пример #15
0
 def delete_table(self, table):
     """
     @brief      Delete a table in database
     @param      table  String
     """
     sql = "DROP TABLE if exists %s;" % table
     Log.debug('DB -> %s' % sql)
     self.execute(sql)
Пример #16
0
 def select_max(self, table, field):
     sql = "SELECT MAX(%s) FROM %s" % (field, table)
     Log.debug('DB -> %s' % sql)
     result = self.execute(sql)
     if result:
         return result[0]['MAX(%s)' % field]
     else:
         return 0
Пример #17
0
 def startProcess(self, index):
     if self.procStates[index] != STATE_READY:
         Log.w(
             "processe{%d} is not ready, state {%d}, please call setupProccesse() before"
             % (index, self.procStates[index]))
         return False
     self.processes[index].start()
     return True
Пример #18
0
 def delete_table(self, table):
     """
     @brief      Delete a table in database
     @param      table  String
     """
     sql = "DROP TABLE if exists %s;" % table
     Log.debug('DB -> %s' % sql)
     self.execute(sql)
Пример #19
0
    def doActionAgentCollate(self, action_sequence):
        # Given an action space to perform, need to convert it into the
        # proper representation that the map can understand. In this case,
        # there are PUT and DEL actions.

        # Retrieve the current position of the agent within the active map
        current_pos = self.active_map.getAgentMapInfo(self)

        map_actions = defaultdict(list)
        for action in action_sequence:
            if (action == Agent.AgentActions.NO_MOVE):
                # The agent has decided to make no map-oriented moves
                pass
            elif (action == Agent.AgentActions.TURN_UP):
                # The agent is modifying an internal state and has no map updates
                # that it needs to report back to the map
                self.orientation = Agent.Orientations.UP
            elif (action == Agent.AgentActions.TURN_DOWN):
                # The agent is modifying an internal state and has no map updates
                # that it needs to report back to the map
                self.orientation = Agent.Orientations.DOWN
            elif (action == Agent.AgentActions.TURN_LEFT):
                # The agent is modifying an internal state and has no map updates
                # that it needs to report back to the map
                self.orientation = Agent.Orientations.LEFT
            elif (action == Agent.AgentActions.TURN_RIGHT):
                # The agent is modifying an internal state and has no map updates
                # that it needs to report back to the map
                self.orientation = Agent.Orientations.RIGHT
            elif (action == Agent.AgentActions.FORWARD):
                # Add the movement based on the current orientation of the
                # agent.
                new_x, new_y = current_pos.x, current_pos.y
                if (self.orientation == Agent.Orientations.UP):
                    new_y -= 1
                elif (self.orientation == Agent.Orientations.DOWN):
                    new_y += 1
                elif (self.orientation == Agent.Orientations.LEFT):
                    new_x -= 1
                elif (self.orientation == Agent.Orientations.RIGHT):
                    new_x += 1
                else:
                    raise AssertionError("Invalid Orientation")

                # If the new position is valid, move
                new_pos = self.active_map.GridPosition(new_x, new_y)
                if self.active_map.isValidPosition(new_pos):
                    map_actions[new_pos].append(
                        self.active_map.PutMapUpdate(self))
                    # Delete the agent from the current location
                    map_actions[current_pos].append(
                        self.active_map.DelMapUpdate(self))
                else:
                    Log.debug(
                        "invalid new map position - not generating anything")
            else:
                raise AssertionError("An invalid action has been specified")
        return map_actions
Пример #20
0
    def terminateProcess(self, index):
        if self.procStates[index] == STATE_TERMINATE:
            Log.w("processe{%d} has been terminated!" % index)
            return False

        os.kill(self.processes[index].pid, signal.SIGINT)
        self.processes[index].join()
        self.procStates[index] = STATE_TERMINATE
        return True
Пример #21
0
 def create_table(self, table, cols):
     """
     @brief      Creates a table in database
     @param      table  String
     @param      cols   String, the cols in table
     """
     sql = "CREATE TABLE if not exists %s (%s);" % (table, cols)
     Log.debug('DB -> %s' % sql)
     self.execute(sql)
Пример #22
0
 def create_db(self, db_name):
     """
     @brief      Creates a database
     @param      db_name  String
     """
     if self.conf['database'] not in self.show_database():
         sql = 'CREATE DATABASE IF NOT EXISTS %s CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci' % db_name
         Log.debug('DB -> %s' % sql)
         self.execute(sql)
Пример #23
0
 def insert(self, table, value):
     """
     @brief      Insert a row in table
     @param      table  String
     @param      value  Tuple
     """
     sql = ("INSERT INTO %s VALUES (" + ",".join(['?'] * len(value)) + ");") % table
     Log.debug('DB -> %s' % sql)
     self.execute(sql, value)
Пример #24
0
 def create_table(self, table, cols):
     """
     @brief      Creates a table in database
     @param      table  String
     @param      cols   String, the cols in table
     """
     sql = "CREATE TABLE if not exists %s (%s);" % (table, cols)
     Log.debug('DB -> %s' % sql)
     self.execute(sql)
Пример #25
0
 def insert(self, table, value):
     """
     @brief      Insert a row in table
     @param      table  String
     @param      value  Tuple
     """
     sql = ("INSERT INTO %s VALUES (" + ",".join(['?'] * len(value)) + ");") % table
     Log.debug('DB -> %s' % sql)
     self.execute(sql, value)
Пример #26
0
    def close(self):
        """
        @brief      close connection to database
        """
        Log.debug('DB -> close')
        # 关闭数据库连接
        self.conn.close()

        
Пример #27
0
 def create_db(self, db_name):
     """
     @brief      Creates a database
     @param      db_name  String
     """
     if self.conf['database'] not in self.show_database():
         sql = 'CREATE DATABASE IF NOT EXISTS %s CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci' % db_name
         Log.debug('DB -> %s' % sql)
         self.execute(sql)
Пример #28
0
 def insertmany(self, table, values):
     """
     @brief      Insert many rows in table
     @param      table  String
     @param      values  Array of tuple
     """
     col_name = self.table_cols[table][1:]
     sql = 'INSERT INTO %s(%s) VALUES (%s)' % (table, ','.join(col_name), ','.join(['%s'] * len(values[0])))
     Log.debug('DB -> %s' % sql)
     self.execute(sql, values)
Пример #29
0
 def insert(self, table, value):
     """
     @brief      Insert a row in table
     @param      table  String
     @param      value  Tuple
     """
     col_name = self.table_cols[table][1:]
     sql = "INSERT INTO %s(%s) VALUES (%s)" % (table, str(','.join(col_name)), array_join(value, ','))
     Log.debug('DB -> %s' % sql)
     self.execute(sql)
Пример #30
0
 def worker(pid, states):
     from time import sleep
     try:
         states[pid] = STATE_IDLE
         sleep(3)
         states[pid] = STATE_BUSY
         sleep(30)
         states[pid] = STATE_TERMINATE
     except KeyboardInterrupt:
         Log.d("worker end!")
Пример #31
0
 def delete(self, table, field='', condition=''):
     """
     @brief      execute sql commands, return result if it has
     @param      table  String
     @param      field  String
     @param      condition  String
     """
     sql = "DELETE FROM %s WHERE %s=%s" % (table, field, condition)
     Log.debug('DB -> %s' % sql)
     self.execute(sql)
Пример #32
0
 def registerAgent(self, agent, method='random', **kwargs):
     # Registering randomly
     if method == 'random':
         rand_x, rand_y = random.randint(0, self.width - 1), random.randint(
             0, self.height - 1)
         pos = GridMap.GridPosition(x=rand_x, y=rand_y)
     self._agent_to_pos[agent] = pos
     self._pos_to_agent[pos][agent] = agent
     Log.debug("Registered new agent %s with position %s" %
               (hex(id(agent)), str(pos)))
Пример #33
0
 def delete_table(self, table):
     """
     @brief      Delete a table in database
     @param      table  String
     """
     if table in self.table_cols:
         sql = "DROP TABLE IF EXISTS %s" % table
         Log.debug('DB -> %s' % sql)
         self.execute(sql)
         self.table_cols.pop(table)
Пример #34
0
 def delete_table(self, table):
     """
     @brief      Delete a table in database
     @param      table  String
     """
     if table in self.table_cols:
         sql = "DROP TABLE IF EXISTS %s" % table
         Log.debug('DB -> %s' % sql)
         self.execute(sql)
         self.table_cols.pop(table)
Пример #35
0
 def delete(self, table, field='', condition=''):
     """
     @brief      execute sql commands, return result if it has
     @param      table  String
     @param      field  String
     @param      condition  String
     """
     sql = "DELETE FROM %s WHERE %s=%s" % (table, field, condition)
     Log.debug('DB -> %s' % sql)
     self.execute(sql)
Пример #36
0
def str2qr_terminal(text):
    """
    @brief      convert string to qrcode matrix and outprint
    @param      text   The string
    """
    Log.debug(text)
    qr = qrcode.QRCode()
    qr.border = 1
    qr.add_data(text)
    mat = qr.get_matrix()
    print_qr(mat)
Пример #37
0
 def insert(self, table, value):
     """
     @brief      Insert a row in table
     @param      table  String
     @param      value  Tuple
     """
     col_name = self.table_cols[table][1:]
     sql = "INSERT INTO %s(%s) VALUES (%s)" % (table, str(
         ','.join(col_name)), array_join(value, ','))
     Log.debug('DB -> %s' % sql)
     self.execute(sql)
Пример #38
0
 def create_table(self, table, cols):
     """
     @brief      Creates a table in database
     @param      table  String
     @param      cols   String, the cols in table
     """
     if table not in self.table_cols:
         sql = 'CREATE TABLE IF NOT EXISTS %s(id int primary key auto_increment, %s) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci' % (table, cols)
         Log.debug('DB -> %s' % sql)
         self.execute(sql)
         self.table_cols[table] = ['id'] + [c.strip().split(' ')[0] for c in cols.split(',')]
Пример #39
0
 def insertmany(self, table, values):
     """
     @brief      Insert many rows in table
     @param      table  String
     @param      values  Array of tuple
     """
     col_name = self.table_cols[table][1:]
     sql = 'INSERT INTO %s(%s) VALUES (%s)' % (
         table, ','.join(col_name), ','.join(['%s'] * len(values[0])))
     Log.debug('DB -> %s' % sql)
     self.execute(sql, values)
Пример #40
0
 def load(self):
     try:
         with open(self.file_path, 'r') as fp:
             content = fp.read()
             fp.close()
             self.blackList = json.loads(content)
             return True
     except IOError:
         Log.w("{BlackSiteList.load} load file (%s) failed!",
               self.file_path)
         return False
Пример #41
0
 def check_schedule_task(self):
     # update group member list at 00:00 am every morning
     t = time.localtime()
     if t.tm_hour == 0 and t.tm_min <= 1:
         # update group member
         Log.debug('update group member list everyday')
         self.db.delete_table(Constant.TABLE_GROUP_LIST())
         self.db.delete_table(Constant.TABLE_GROUP_USER_LIST())
         self.db.create_table(Constant.TABLE_GROUP_LIST(), Constant.TABLE_GROUP_LIST_COL)
         self.db.create_table(Constant.TABLE_GROUP_USER_LIST(), Constant.TABLE_GROUP_USER_LIST_COL)
         self.wechat.fetch_group_contacts()
Пример #42
0
 def get_token(self):
     login_data = {
         'username': self.username,
         'password': self.password,
         'public_key': None
     }
     token = requests.post(url=self.url + '/api/v1/authentication/auth/',
                           data=login_data).text
     Log.debug('token is %s' % token)
     self.headers['Authorization'] = 'Bearer %s' % json.loads(
         token)['token']
Пример #43
0
def str2qr_terminal(text):
    """
    @brief      convert string to qrcode matrix and outprint
    @param      text   The string
    """
    Log.debug(text)
    qr = qrcode.QRCode()
    qr.border = 1
    qr.add_data(text)
    mat = qr.get_matrix()
    print_qr(mat)
Пример #44
0
def loacal_files_check(files):
    if not files:
        return False, 0
    try:
        total_size = 0
        for tfile in files:
            total_size += os.path.getsize(tfile)
        return True, total_size
    except os.error:
        Log.error("files %s not exist or can't access" % files)
        return False, 0
Пример #45
0
def get_config_strategy(file_content):
    ret_list = []
    try:
        config_obj = xmltodict.parse(file_content)
        st_obj = config_obj["MyExchange"]["strategies"]["strategy"]
        if not isinstance(st_obj, list):
            st_obj = [st_obj]
        for st in st_obj:
            ret_list.append(st["@model_file"])
    except Exception, ex:
        Log.error("xml parse error! %s" % ex)
Пример #46
0
    def get_aliyun_hosts(self,
                         register_prefix='',
                         default_re='',
                         extend_re='dev',
                         project=''):
        client = AcsClient(self.accessKeyID, self.accessKeySecret, self.region)
        request = DescribeInstancesRequest()
        request.set_accept_format('json')
        request.set_PageNumber(1)
        request.set_PageSize(50)
        response = client.do_action_with_exception(request)
        data = json.loads(response)
        PageNumber = int(data['TotalCount'] / 50 + 2)
        list = []
        search_shard = extend_mesage(project)
        for Number in range(1, PageNumber):
            request.set_PageNumber(Number)
            request.set_PageSize(50)
            response = client.do_action_with_exception(request)
            data = json.loads(response)

            for host in data['Instances']['Instance']:
                dict = {}
                id = re.split(r'-', host['SerialNumber'])
                id = ''.join(str(i) for i in id)
                dict['id'] = id
                dict['ip'] = host['NetworkInterfaces']['NetworkInterface'][0][
                    'PrimaryIpAddress']
                if register_prefix:
                    dict['hostname'] = str(
                        register_prefix) + host['InstanceName']
                else:
                    dict['hostname'] = host['InstanceName']
                dict['is_active'] = 'true'
                dict['platform'] = "Linux"
                try:
                    dict['comment'] = search_shard[dict['ip']]
                except KeyError:
                    if 'Tags' in host:
                        comment = host['Tags']['Tag'][0]['TagValue']
                        dict['comment'] = "[%s]" % comment
                    else:
                        dict['comment'] = "[123]"
                # if host['InstanceName'].startswith('G') or host['InstanceName'].startswith('c'):
                final_re = '|'.join(
                    default_re.split('|') + extend_re.split('|')).strip('|')
                if re.search(final_re, host['InstanceName']):
                    dict['group'] = ['dev']
                else:
                    dict['group'] = ['base']
                list.append(dict)
        Log.debug(list)
        return list
Пример #47
0
def ret_run(str, func, *args):
    t = time.time()
    r = False
    try:
        r = func(*args)
        return r
    except:
        if bool(config("setting", "maillisten")):
            sendmsg(config("run", "run_name"),
                    str + ":" + traceback.format_exc())
        Log.error(str + ":" + traceback.format_exc())
        return False
Пример #48
0
 def select(self, table, field='', condition=''):
     """
     @brief      select all result from table
     @param      table  String
     @param      field  String
     @param      condition  String
     @return     result  Tuple
     """
     sql = "SELECT * FROM %s" % table
     if field and condition:
         sql += " WHERE %s='%s'" % (field, condition)
     Log.debug('DB -> %s' % sql)
     return self.execute(sql)
Пример #49
0
    def update(self, table, dic, condition=''):
        k_arr = []
        v_arr = []
        for (k, v) in dic.items():
            k_arr.append('%s=?' % k)
            v_arr.append(v)

        sql = "UPDATE %s SET %s" % (table, ','.join(k_arr))
        if condition:
            sql += " WHERE %s" % condition

        Log.debug('DB -> %s' % sql)
        self.execute(sql, tuple(v_arr))
Пример #50
0
    def update(self, table, dic, condition=''):
        k_arr = []
        v_arr = []
        for (k, v) in dic.items():
            k_arr.append('%s=?' % k)
            v_arr.append(v)

        sql = "UPDATE %s SET %s" % (table, ','.join(k_arr))
        if condition:
            sql += " WHERE %s" % condition

        Log.debug('DB -> %s' % sql)
        self.execute(sql, tuple(v_arr))
Пример #51
0
def get_ev_files(vpn_ip, path):
    pre_dir = os.path.dirname(path)
    my_capital = os.path.join(pre_dir, "my_capital.config")
    cli = SSHClient()
    try:
        ret = cli.cmd(vpn_ip, "cmd.run", ["cat %s" % my_capital])
        if ret.get(vpn_ip).get("retcode") == 0:
            capital_content = ret.get(vpn_ip).get("return")
            sts = get_config_strategy(capital_content)
            print "sts: ", sts
            return strategy_map_ev(sts)
    except Exception, ex:
        traceback.print_exc()
        Log.error("get ev files error! %s" % ex)
Пример #52
0
    def snapshot(self):
        """
        @brief      Save basic infos for next login.
        @return     Bool: whether operation succeed.
        """
        try:
            conf = {
                'uuid': self.uuid,
                'redirect_uri': self.redirect_uri,
                'uin': self.uin,
                'sid': self.sid,
                'skey': self.skey,
                'pass_ticket': self.pass_ticket,
                'synckey': self.synckey,
                'device_id': self.device_id,
                'last_login': time.time(),
            }
            cm = ConfigManager()
            Log.debug('save wechat config')
            cm.set_wechat_config(conf)

            # save cookie
            Log.debug('save cookie')
            if self.cookie:
                self.cookie.save(ignore_discard=True)

            # save contacts
            Log.debug('save contacts')
            self.save_contacts()
        except Exception, e:
            Log.error(traceback.format_exc())
            return False
Пример #53
0
def post(url, params, jsonfmt=True):
    """
    @brief      http post request
    @param      url      String
    @param      params   Dict, post params
    @param      jsonfmt  Bool, whether is json format
    @return     http response
    """
    Log.debug('POST -> '+url)
    Log.debug(params)
    if jsonfmt:
        request = urllib2.Request(url=url, data=json.dumps(params, ensure_ascii=False).encode('utf8'))
        request.add_header(*Constant.HTTP_HEADER_CONTENTTYPE)
    else:
        request = urllib2.Request(url=url, data=urllib.urlencode(params))

    while True:
        try:
            response = urllib2.urlopen(request, timeout=30)
            data = response.read()
            response.close()

            if jsonfmt:
                Log.debug(data)
                return json.loads(data, object_hook=_decode_data)
            return data
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            Log.error(traceback.format_exc())

        time.sleep(1)
Пример #54
0
def run(str, func, *args):
    t = time.time()
    echo(str)
    r = False
    try:
        r = func(*args)
    except:
        Log.error(traceback.format_exc())
    if r:
        totalTime = int(time.time() - t)
        echo(Constant.RUN_RESULT_SUCCESS % totalTime)
    else:
        echo(Constant.RUN_RESULT_FAIL)
        exit()
Пример #55
0
 def recover_contacts(self):
     """
     @brief      recover contacts.
     @return     Bool: whether operation succeed.
     """
     try:
         self.User = pickle_load(self.pickle_file['User'])
         self.MemberList = pickle_load(self.pickle_file['MemberList'])
         self.GroupList = pickle_load(self.pickle_file['GroupList'])
         self.GroupMemeberList = pickle_load(self.pickle_file['GroupMemeberList'])
         self.SpecialUsersList = pickle_load(self.pickle_file['SpecialUsersList'])
         return True
     except Exception, e:
         Log.error(traceback.format_exc())
Пример #56
0
    def insertmany(self, table, values):
        """
        @brief      Insert many rows in table
        @param      table  String
        @param      values  Array of tuple
        """
        c = self.conn.cursor()
        self.lock.acquire()
        n = len(values[0])
        sql = ("INSERT INTO %s VALUES (" + ",".join(['?'] * n) + ");") % table
        Log.debug('DB -> %s' % sql)

        try:
            c.executemany(sql, values)
        except Exception, e:
            Log.error(traceback.format_exc())
Пример #57
0
    def run(self):
        '''
        Start process, the subclass must rewrite this function or 'handle' function
        when all the father processes quits, then break to quit
        '''

        if self.log:
            self.log = Log(self.name, toFile="plugin")
            self.log.info("plugin {0} start, ins:{1}, outs:{2}".format(self.name, self._ins, self._outs))
        else:
            self.log = False

        while True:
            try:
                data = self.get()
                #print "debug:", "plugin ", self.name, "getting", "ins<<<<<<<<", [str(x) for x in self._ins]
            except QueueEmpty:
                continue
            except IOError:
                break
            except EOFError:
                break
            except PluginExit:
                self.quit()
                #print "debug:", "plugin ", self.name, "quit"
                if self.log:
                    self.log.info("plugin {0} quit".format(self.name))
                break
            else:
                #print "debug:", "plugin ", self.name, "got", data
                self.handle(data)
            finally:
                time.sleep(self.timeout)
Пример #58
0
def save_file(filename, data, dirName):
    """
    @brief      Saves raw data to file.
    @param      filename  String
    @param      data      Binary data
    @param      dirName   String
    @return     file path
    """
    Log.debug('save file: ' + filename)
    fn = filename
    if not os.path.exists(dirName):
        os.makedirs(dirName)
    fn = os.path.join(dirName, filename)
    with open(fn, 'wb') as f:
        f.write(data)
    return fn
Пример #59
0
def set_cookie(cookie_file):
    """
    @brief      Load cookie from file
    @param      cookie_file
    @param      user_agent
    @return     cookie, LWPCookieJar
    """
    cookie = cookielib.LWPCookieJar(cookie_file)
    try:
        cookie.load(ignore_discard=True)
    except:
        Log.error(traceback.format_exc())
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
    opener.addheaders = Constant.HTTP_HEADER_USERAGENT
    urllib2.install_opener(opener)
    return cookie
Пример #60
0
def save_json(filename, data, dirName, mode='w+'):
    """
    @brief      Saves dict to json file.
    @param      filename  String
    @param      data      Dict
    @param      dirName   String
    @return     file path
    """
    Log.debug('save json: ' + filename)
    fn = filename
    if not os.path.exists(dirName):
        os.makedirs(dirName)
    fn = os.path.join(dirName, filename)
    with open(fn, mode) as f:
        f.write(json.dumps(data, indent=4)+'\n')
    return fn