Esempio n. 1
0
 def Daemonize(self):
     if daemonable:
         logfile = self.absPath('daemon-%d.log' % self.qq)
         PRINT('将以 daemon 模式运行, log 文件: %s' % logfile)
         daemonize(logfile)
     else:
         PRINT('错误:无法以 daemon 模式运行')
         sys.exit(1)
Esempio n. 2
0
def QTerm():
    try:
        # python qterm.py -s
        # python qterm.py [PORT] [COMMAND]
        if len(sys.argv) == 2 and sys.argv[1] == '-s':
            QTermServer(DEFPORT).Test()
        else:
            if len(sys.argv) >= 2 and sys.argv[1].isdigit():
                port = int(sys.argv[1])
                command = ' '.join(sys.argv[2:]).strip()
            else:
                port = DEFPORT
                command = ' '.join(sys.argv[1:]).strip()

            if command:
                coding = sys.getfilesystemencoding()
                PRINT(query(port, command.decode(coding).encode('utf8')))

    except KeyboardInterrupt:
        pass
Esempio n. 3
0
    def readConfFile(self):
        confPath = self.ConfPath()
        strConfPath = SYSTEMSTR2STR(confPath)
        conf = rootConf.copy()

        if os.path.exists(confPath):
            try:
                with open(confPath, 'rb') as f:
                    cusConf = ast.literal_eval(BYTES2STR(f.read()))

                if type(cusConf) is not dict:
                    raise ConfError('文件内容必须是一个 dict')

                if type(cusConf.get('默认配置', {})) is not dict:
                    raise ConfError('默认配置必须是一个 dict')

                if self.user is not None:
                    if self.user not in cusConf:
                        raise ConfError('用户 %s 不存在' % self.user)                        
                    elif type(cusConf[self.user]) is not dict:
                        raise ConfError('用户 %s 的配置必须是一个 dict'%self.user)                    
                    else:
                        names = ['默认配置', self.user]
                else:
                    names = ['默认配置']

                for name in names:
                    for k, v in list(cusConf.get(name, {}).items()):
                        if k not in conf:
                            raise ConfError('不存在的配置选项 %s.%s ' % (name, k))                               
                        elif type(v) is not type(conf[k]):
                            t = type(conf[k]).__name__
                            raise ConfError('%s.%s 必须是一个 %s' % (name, k, t))
                        else:
                            conf[k] = v

            except (IOError, SyntaxError, ValueError, ConfError) as e:
                PRINT('配置文件 %s 错误: %s\n' % (strConfPath, e), end='')
                sys.exit(1)

        else:
            PRINT('未找到配置文件“%s”,将使用默认配置' % strConfPath)
            try:
                with open(confPath, 'wb') as f:
                    f.write(STR2BYTES(sampleConfStr))
            except IOError:
                pass
            else:
                PRINT('已创建一个默认配置文件“%s”' % strConfPath)
            
            if self.user is not None:
                PRINT('用户 %s 不存在\n' % self.user, end='')
                sys.exit(1)

        for k, v in list(conf.items()):
            if getattr(self, k, None) is None:
                setattr(self, k, v)

        if self.pluginPath and not os.path.isdir(STR2SYSTEMSTR(self.pluginPath)):
            PRINT('配置文件 %s 错误: 插件目录 “%s” 不存在\n' % \
                  (strConfPath, self.pluginPath), end='')
            sys.exit(1)
Esempio n. 4
0
    def readCmdLine(self, argv):
        if argv is None:
            argv = sys.argv[1:]

        parser = argparse.ArgumentParser(add_help=False)

        parser.add_argument('-h', '--help', action='store_true')

        parser.add_argument('-u', '--user', default=None)

        parser.add_argument('-q', '--qq', type=int)

        parser.add_argument('-v', '--verifyKey', type=str)

        parser.add_argument('-b', '--bench')

        parser.add_argument('-p', '--port', type=int)

        parser.add_argument('-ip', '--host')      

        parser.add_argument('-d', '--debug', action='store_true', default=None)        

        parser.add_argument('-nd', '--nodebug', action='store_true')        

        parser.add_argument('-r', '--restartOnOffline', action='store_true', default=None)

        parser.add_argument('-nr', '--norestart', action='store_true') 

        parser.add_argument('-dm', '--daemon', action='store_true', default=None)

        parser.add_argument('-ndm', '--nodaemon', action='store_true')

        parser.add_argument('-pp', '--pluginPath')

        parser.add_argument('-pl', '--plugins')

        try:
            opts = parser.parse_args(argv)
        except:
            PRINT(usage)
            sys.exit(1)

        if opts.help:
            PRINT(usage)
            sys.exit(0)

        if opts.nodebug:
            opts.debug = False

        if opts.norestart:
            opts.restartOnOffline = False

        if opts.nodaemon:
            opts.daemon = False

        delattr(opts, 'nodebug')
        delattr(opts, 'norestart')
        delattr(opts, 'nodaemon')

        if not opts.bench:
            opts.bench = os.getcwd()

        opts.bench = os.path.abspath(opts.bench)
        sys.path.insert(0, opts.bench)
        opts.benchstr = SYSTEMSTR2STR(opts.bench)

        if not os.path.exists(opts.bench):
            try:
                os.mkdir(opts.bench)
            except Exception as e:
                PRINT('无法创建工作目录 %s , %s' % (opts.benchstr, e))
                sys.exit(1)
        elif not os.path.isdir(opts.bench):
            PRINT('无法创建工作目录 %s ' % opts.benchstr)
            sys.exit(1)

        if opts.plugins:
            opts.plugins = SYSTEMSTR2STR(opts.plugins).split(',')

        if opts.pluginPath:
            opts.pluginPath = SYSTEMSTR2STR(opts.pluginPath)

        for k, v in list(opts.__dict__.items()):
            if getattr(self, k, None) is None:
                setattr(self, k, v)
Esempio n. 5
0

def Main():
    try:
        if sys.argv[-1] == '--subprocessCall':
            isSubprocessCall = True
            sys.argv.pop()
        else:
            isSubprocessCall = False

        conf = QConf()
        if not conf.restartOnOffline or isSubprocessCall:
            bot = QQBot(conf=conf)
            bot.Login()
            sys.exit(bot.Run())
        else:
            if sys.argv[0].endswith('py') or sys.argv[0].endswith('pyc'):
                args = ['python'] + sys.argv
            else:
                args = sys.argv[:]
            args += ['--mailAuthCode', conf.mailAuthCode, '--subprocessCall']
            while subprocess.call(args) != 0:
                INFO('重新启动 QQBot ')
    except KeyboardInterrupt:
        sys.exit(0)


if __name__ == '__main__':
    from utf8logger import PRINT
    PRINT('请运行 python ../main.py')