示例#1
0
    def update_condition(self, cond):
        """更新,如果数据表中没有要更新的
           cond 格式
           {
                update:{col:value}
                where:
           }
        """
        check_dict_data(cond, {"update": [dict]})
        u_l = ["%s=%s" % (key, self.convert_value(value)) for (key, value) in cond["update"].items()]
        if len(u_l) == 0:
            Log.debug("update_condition: no item to update")
            return 0
        update_parse = ",".join(u_l)

        where_parse = ""
        if "where" in cond:
            where_parse = self.gen_where_parse(cond["where"])
        sql_str = "update %s set %s %s;" % (self.str_table(), update_parse, where_parse)

        return self.execute(sql_str)
示例#2
0
    def update_condition(self, cond):
        """更新,如果数据表中没有要更新的
           cond 格式
           {
                update:{col:value}
                where:
           }
        """
        check_dict_data(cond, {"update":[dict]})
        u_l = ["%s=%s" %(key, self.convert_value(value)) for (key, value) in cond["update"].items()]
        if len(u_l) == 0:
            Log.debug("update_condition: no item to update")
            return 0
        update_parse = ",".join(u_l)

        where_parse = ""
        if "where" in cond:
            where_parse = self.gen_where_parse(cond["where"])
        sql_str = ("update %s set %s %s;"
                   %(self.str_table(), update_parse, where_parse))

        return self.execute(sql_str)
示例#3
0
def url_post(url, req, timeout=30):
    import urllib2
    try:
        data = urllib2.Request(url, req)
        fd = urllib2.urlopen(data, None, timeout)
        data = ""
        while 1:
            recv = fd.read(1024)
            if len(recv) == 0:
                break
            data += recv

        #只是为了打log
        log_data = data
        if len(log_data) > 512:
            log_data = "%s..." % log_data[0:512]
        Log.info("send[%s] to url[%s], return[%s]" % (req, url, log_data))

        return data.strip()
    except Exception, e:
        Log.info("send[%s] to url[%s], error[%s]" % (req, url, str(e)))
        raise
示例#4
0
def url_post(url, req, timeout=30):
    import urllib2

    try:
        data = urllib2.Request(url, req)
        fd = urllib2.urlopen(data, None, timeout)
        data = ""
        while 1:
            recv = fd.read(1024)
            if len(recv) == 0:
                break
            data += recv

        # 只是为了打log
        log_data = data
        if len(log_data) > 512:
            log_data = "%s..." % log_data[0:512]
        Log.info("send[%s] to url[%s], return[%s]" % (req, url, log_data))

        return data.strip()
    except Exception, e:
        Log.info("send[%s] to url[%s], error[%s]" % (req, url, str(e)))
        raise
示例#5
0
 def execute(self, str_sql):
     Log.debug("exec sql[%s]" %str_sql)
     ret = self._conn.execute(str_sql)
     Log.debug("exec sql success, effect %d rows" %(ret))
     return ret
示例#6
0
 def rollback(self):
     Log.debug("db client rollback");
     self._conn.rollback()
示例#7
0
文件: utils.py 项目: shazi129/MyCgi
def execute_cmd(cmd, **kwargs):
    """Helper method to execute command with optional retry

       :param cmd:                command will be executed
       :param process_input:      Send to opened process
       :param check_exit_code:    Single bool, int, or list of allowed
                                  exit codes.
       :preexec_fn                funtion before subprocess execute
       :param timeout             timout
       :returns: a tuple, (stdout, stderr), or None failed
    """
    process_input = kwargs.pop('process_input', None)
    check_exit_code = kwargs.pop('check_exit_code', [0])
    ignore_exit_code = True

    if isinstance(check_exit_code, bool):
        ignore_exit_code = not check_exit_code
        check_exit_code = [0]
    elif isinstance(check_exit_code, int):
        check_exit_code = [check_exit_code]
    shell = kwargs.pop('shell', True)
    timeout = int(kwargs.pop('timeout', 0))
    preexec_fn = kwargs.pop('preexec_fn', None)
    cmd = str(cmd)

    if len(kwargs):
        raise ValueError, ('Got unknown keyword args in '
                           'utils.utils.execute_cmd:%r' % kwargs)
    Log.debug('Running cmd (subprocess): %s' %cmd)

    try:
        import subprocess
        obj = subprocess.Popen(cmd, stdout     = subprocess.PIPE,
                                    stdin      = subprocess.PIPE,
                                    stderr     = subprocess.PIPE,
                                    preexec_fn = preexec_fn,
                                    shell      = shell)

        if process_input is not None:
            obj.stdin.write(process_input)
            obj.stdin.close()

        result = None
        if timeout <= 0:
            stdout = obj.stdout.read()
            stderr = obj.stderr.read()
            result = (stdout, stderr)
        else:
            used_time = 0
            wait_time = 0.5
            while used_time <= timeout:
                have_ret = obj.poll()
                if have_ret is None:
                    time.sleep(wait_time)
                    used_time += wait_time
                else:
                    stdout = obj.stdout.read()
                    stderr = obj.stderr.read()
                    result = (stdout, stderr)
                    break
            if used_time >= timeout:
                try:
                    obj.terminate()
                except AttributeError:
                    os.kill(obj.pid, 15)
                    Log.debug("PIPE.terminate is not supported")
                raise RuntimeError, 'Time out, cmd:%s' %cmd

        _returncode = obj.returncode
        Log.debug('return code was %s' %_returncode)
        if not ignore_exit_code and _returncode not in check_exit_code:
            raise RuntimeError, ("exit_code:%s, stdout:%s, stderror:%s, cmd:%s"
                      %(str(_returncode), str(result[0]), str(result[1]), cmd))
        return result
    except Exception, e:
        raise RuntimeError, 'Error in utils.utils.execute_cmd: %s' %(e)
示例#8
0
 def execute(self, str_sql):
     Log.debug("exec sql[%s]" % str_sql)
     ret = self._conn.execute(str_sql)
     Log.debug("exec sql success, effect %d rows" % (ret))
     return ret
示例#9
0
 def rollback(self):
     Log.debug("db client rollback")
     self._conn.rollback()
示例#10
0
def execute_cmd(cmd, **kwargs):
    """Helper method to execute command with optional retry

       :param cmd:                command will be executed
       :param process_input:      Send to opened process
       :param check_exit_code:    Single bool, int, or list of allowed
                                  exit codes.
       :preexec_fn                funtion before subprocess execute
       :param timeout             timout
       :returns: a tuple, (stdout, stderr), or None failed
    """
    process_input = kwargs.pop('process_input', None)
    check_exit_code = kwargs.pop('check_exit_code', [0])
    ignore_exit_code = True

    if isinstance(check_exit_code, bool):
        ignore_exit_code = not check_exit_code
        check_exit_code = [0]
    elif isinstance(check_exit_code, int):
        check_exit_code = [check_exit_code]
    shell = kwargs.pop('shell', True)
    timeout = int(kwargs.pop('timeout', 0))
    preexec_fn = kwargs.pop('preexec_fn', None)
    cmd = str(cmd)

    if len(kwargs):
        raise ValueError, ('Got unknown keyword args in '
                           'utils.utils.execute_cmd:%r' % kwargs)
    Log.debug('Running cmd (subprocess): %s' % cmd)

    try:
        import subprocess
        obj = subprocess.Popen(cmd,
                               stdout=subprocess.PIPE,
                               stdin=subprocess.PIPE,
                               stderr=subprocess.PIPE,
                               preexec_fn=preexec_fn,
                               shell=shell)

        if process_input is not None:
            obj.stdin.write(process_input)
            obj.stdin.close()

        result = None
        if timeout <= 0:
            stdout = obj.stdout.read()
            stderr = obj.stderr.read()
            result = (stdout, stderr)
        else:
            used_time = 0
            wait_time = 0.5
            while used_time <= timeout:
                have_ret = obj.poll()
                if have_ret is None:
                    time.sleep(wait_time)
                    used_time += wait_time
                else:
                    stdout = obj.stdout.read()
                    stderr = obj.stderr.read()
                    result = (stdout, stderr)
                    break
            if used_time >= timeout:
                try:
                    obj.terminate()
                except AttributeError:
                    os.kill(obj.pid, 15)
                    Log.debug("PIPE.terminate is not supported")
                raise RuntimeError, 'Time out, cmd:%s' % cmd

        _returncode = obj.returncode
        Log.debug('return code was %s' % _returncode)
        if not ignore_exit_code and _returncode not in check_exit_code:
            raise RuntimeError, (
                "exit_code:%s, stdout:%s, stderror:%s, cmd:%s" %
                (str(_returncode), str(result[0]), str(result[1]), cmd))
        return result
    except Exception, e:
        raise RuntimeError, 'Error in utils.utils.execute_cmd: %s' % (e)