Example #1
0
    def get(self, userid):
        '''
            Get users list
        '''
        userobj = abort_if_id_doesnt_exist(User, id=userid)
        if not userobj:
            return my_response(
                dict(result=False, message='ID is not exist', code=410))

        role_list = [{
            "roleid": i.id,
            "rolename": i.rolename
        } for i in userobj.role.all()]

        data = {
            'username': userobj.username,
            'id': userobj.id,
            'rolelist': role_list
        }

        log(level='info', message='Get User Message: userid=%s' % userid)

        response = dict(result=True, data=data)

        return my_response(response)
Example #2
0
    def put(self, roleid):
        '''
            roles add permsid
        '''
        permsid = request.json.get('permid', None)

        return permsid
        if not permsid:
            return json_message(200, 'error', 'Permsid is None')

        r = Role.query.get(roleid)

        for id in permsid:
            p = Perm.query.get(id)
            if not p:
                return json_message(200, 'error',
                                    'Permsid is None: id=%s ' % id)

        if r:
            for id in permsid:
                r.perms.append(Perm.query.get(id))
            db.session.add(r)
            db.session.commit()
        else:
            return json_message(410, 'error', 'None')

        log(level='info',
            message='Update role vs perm: perm=%s roleid=%s' %
            (permsid, roleid))

        return json_message(200, 'message', 'roleid add permsid  Success')
Example #3
0
    def delete(self, roleid):
        '''
            delete rolename
        '''
        #prolename=request.form['rolename']
        #rolename = request.json.get('rolename', None)

        args = self.reqparse.parse_args(strict=True)

        rolename = args.get('rolename')

        abort_if_id_doesnt_exist(Role, rolename=rolename)

        role = Role.query.filter_by(id=roleid).first()

        if rolename == role.rolename:
            #dbdel(Role, id=roleid)
            db.session.query(Role).filter_by(id=roleid).delete()
            db.session.commit()
        else:
            return json_message(410, 'error', 'Rolename Change Failed')

        log(level='warning',
            message='Delete role: name=%s id=%s' % (rolename, roleid))

        return json_message(200, 'message', 'role Delete Success')
Example #4
0
    def put(self, roleid):
        '''
            update roles
        '''

        abort_if_id_doesnt_exist(Role, id=roleid)

        args = self.reqparse.parse_args(strict=True)

        name = args.get('rolename')

        if not name:
            return json_message(200, 'error', 'the new rolename is None')

        query = db.session.query(Role)

        try:
            query.filter(Role.id == roleid).update({Role.rolename: name})
            query.session.commit()
        except Exception:
            return json_message(410, 'error', 'Rolename Change Failed')

        log(level='info',
            message='Update role: name=%s id=%s' % (name, roleid))

        return json_message(200, 'message', 'Rolnename Change Success')
Example #5
0
    def post(self):
        '''
            User Register
        '''

        args = self.reqparse.parse_args(strict=True)
        username = args.get('username')
        password = args.get('password')

        if not username or not password:
            return my_response(
                dict(result=False,
                     message='User or Password is None',
                     code=402))

        if User.query.filter_by(username=username).first() is not None:
            return my_response(
                dict(result=False,
                     message='A user with that username already exists',
                     code=409))

        userobj = User(username=username)
        userobj.hash_password(password)
        dbadd(userobj)

        log(level='info', message='Register Sucess: username=%s' % username)

        return my_response(dict(result=True, message='User Register Success'))
Example #6
0
 def test_true(self):
     # folder = getattr(self.test_true, "__name__")
     # 把case名写进testresult/folder文件下,方便其他方法读取方法名
     u.recordcase(self.test_true, "OtherTest")
     time.sleep(30)
     assert True
     u.log()
Example #7
0
def syscmdhandler(obj, action, args_list):
    #log('something here')
    if action in forbidden_list:
        return 'forbidden exec command: ' + action
    if action == 'deploy':
        try:
            ip = re_ipv4.findall(' '.join(args_list))
            if not ip:
                return 'please special server IP'
            ip = ip[0]
            name = args_list[0]
            announce('Starting update %s: %s NOW...'%(ip, name))

            if name == 'flash':
                status, out = commands.getstatusoutput('rsync -avz /terminus/hades/static/game/ %s:/terminus/hades/static/game/'%ip)
                file_list = commands.getoutput('ssh root@%s "find /terminus/hades/static/game/ -name game_config.xml"'%ip).split()
                for file in file_list:
                    cmd = 'sed -i "s/<web>http:\/\/.*\/<\/web>/<web>http:\/\/%s\/<\/web>/" %s'%(ip, file)
                    status, _out = commands.getstatusoutput("ssh %s '%s'"%(ip, cmd))
            elif name in ['hades', 'hera', 'zeus', 'poseidon', 'hermes']:
                status, out = commands.getstatusoutput('ssh %s "cd /terminus/%s; git reset --hard; git pull"'%(ip, name))
            else:
                return 'unknown project name'
        except Exception, e:
            log(e + '\n' + status)
            return e + '\n' + status
        if len(out) > 200:
            out = out[200:]
        return out + '\n已完成' if status == 0 else 'cmd error'
Example #8
0
 def test_false(self):
     u.recordcase(self.test_false, "OtherTest")
     time.sleep(10)
     try:
         assert True
     finally:
         u.log()
Example #9
0
 def test_fm_copy(self):
     u.recordcase(self.test_fm_copy, "FileManager.Priority1")
     self._launch_filemanager()
     d(resourceId="com.chaozhuo.filemanager:id/text_navigation_child", text=u"文档").click()
     time.sleep(2)
     d(resourceId="com.chaozhuo.filemanager:id/more").wait(5000)
     d(resourceId="com.chaozhuo.filemanager:id/more").click()
     time.sleep(2)
     d(resourceId="com.chaozhuo.filemanager:id/item_title", text=u"新建文件").click()
     time.sleep(2)
     d(resourceId="com.chaozhuo.filemanager:id/file_name_edit").set_text("test.txt")
     time.sleep(2)
     d.press("enter")
     time.sleep(2)
     d(resourceId="com.chaozhuo.filemanager:id/node_list").click()
     time.sleep(2)
     d(resourceId="com.chaozhuo.filemanager:id/more").click()
     time.sleep(2)
     d(resourceId="com.chaozhuo.filemanager:id/item_title", text=u"全选").click()
     time.sleep(2)
     d(resourceId="com.chaozhuo.filemanager:id/copy", text=u"复制").click()
     time.sleep(2)
     for i in xrange(self.loop):
         d(resourceId="com.chaozhuo.filemanager:id/more").click()
         time.sleep(2)
         d(resourceId="com.chaozhuo.filemanager:id/item_title", text=u"粘贴").click()
         time.sleep(2)
     u.log()
Example #10
0
    def put(self, userid):
        '''
            Update user's password
        '''
        userobj = abort_if_id_doesnt_exist(User, id=userid)
        if not userobj:
            return my_response(
                dict(result=False, message='ID is not exist', code=410))

        args = self.reqparse.parse_args(strict=True)
        password = args.get('password')

        if not password:
            return my_response(dict(result=False, message='Password is None'))

        query = db.session.query(User)
        newpasswd = pwd_context.encrypt(password)

        try:
            query.filter(User.id == userid).update({User.password: newpasswd})
        except Exception:
            log(level='warning',
                message='Update Userpassword Failed,userid=%s' % userid)
            return my_response(
                dict(result=False, message='Password Change Failed'))

        log(level='info', message='Update Userpassword: userid=%s' % userid)

        return my_response(dict(result=True,
                                message='Password Change Success'))
    def send(self, conn, msg, dst=None):
        if not dst:
            dst = self.proxy_peer
        if not dst:
            _, dst = self.peer_manager.get(conn.getpeername())

        log('>> send {} to {}'.format(msg.__class__.__name__, dst))
        return self._sendall(conn, msg.pack(src=self.name, dst=dst or ''))
Example #12
0
 def test_open_close(self):
     u.recordcase(self.test_open_close, "FileManager.Priority1")
     for i in xrange(self.loop):
         self._launch_filemanager()
         time.sleep(2)
         d(resourceId="android:id/mwCloseBtn").click()
         time.sleep(2)
     u.log()
Example #13
0
 def _calc_build_size(self, version: str) -> Iterator[util.FSize]:
     build_path = p.join(self._path, '%s-build' % version)
     for root, dirs, files in fs_walk(build_path):
         for f in files:
             try:
                 path = p.join(root, f)
                 yield util.FSize.create(version, path, fs_stat(path).st_size)
             except:
                 util.log(self, 'warn', "couldn't read file %s", path)
Example #14
0
    def get(self):
        '''
            Get users list
        '''
        userslist = [{
            'id': i.id,
            'username': unicode.encode(i.username, "utf-8")
        } for i in User.query.all()]

        log(level='info', message='Get User List')

        return my_response(dict(result=True, data=userslist))
Example #15
0
    def _on_address_message(self, protocol, sock, msg_wrapper):
        if self.direct_connections:

            msg = msg_wrapper.msg
            address = self._create_address(self.address[0], msg.address)

            try:
                self.commands.connect(address)
            except Exception as exc:
                log('Error connecting to {}: {}'.format(address, exc))

        protocol.send(sock, GetResources(), dst=msg_wrapper.src)
Example #16
0
    def post(self):
        '''
            Add Perms
        '''
        args = self.reqparse.parse_args()

        perm = Perm(**args)

        dbadd(perm)

        log(level='info', message='Perms add success')

        return my_response(dict(result=True, message='Perms add success'))
    def relay(self, conn, msg_wrapper):
        msg = msg_wrapper.msg
        src = msg_wrapper.src
        dst = msg_wrapper.dst

        if src != self.name and dst and dst != self.name:
            _, sock = self.peer_manager.get_by_name(dst)
            if not sock:
                raise ProtocolError('Unknown peer: {}'.format(dst))

            log('>> relay {} from {} to {}'.format(msg.__class__.__name__, src,
                                                   dst))
            self._sendall(sock, msg.pack(src=src, dst=dst))
            return True
    def start(self):

        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setblocking(0)
        self.working = True

        if self.proxy:
            self._connect(sock, self.proxy)
            self._do_work(sock, self.address)
        else:
            sock.bind(self.address)
            sock.listen(1)
            log('Listening on {}'.format(self.address))
            self._work(sock)
Example #19
0
    def delete(self, userid):
        '''
            Delete User
        '''
        userobj = abort_if_id_doesnt_exist(User, id=userid)
        if not userobj:
            return my_response(
                dict(result=False, message='ID is not exist', code=410))

        dbdel(User, id=userid)

        log(level='info', message='Delete User: userid=%s' % userid)

        return my_response(dict(result=True, message='User Delete Success'))
Example #20
0
    def delete(self, permid):
        '''
            Delete Perms
        '''
        permobj = abort_if_id_doesnt_exist(Perm, id=permid)
        if not permobj:
            return my_response(
                dict(result=False, message='ID is not exist', code=410))

        dbdel(Perm, id=permid)

        log(level='info', message='Delete Perm: id=%s' % permid)

        return my_response(dict(result=True, message='Perm Delete Success'))
Example #21
0
    def put(self, permid):
        '''
            Update Perms
        '''
        permobj = abort_if_id_doesnt_exist(Perm, id=permid)
        if not permobj:
            return my_response(
                dict(result=False, message='ID is not exist', code=410))

        args = self.reqparse.parse_args()

        dbupdate(Perm, permid, args)

        log(level='info', message='Update Perm: id=%s' % permid)

        return my_response(dict(result=True, message='Perm update Success'))
Example #22
0
    def get(self):
        '''
            Get all Perms
        '''
        permslist = [{
            'id': i.id,
            'menu': i.menu,
            'type': i.type,
            'uri': i.uri,
            'method': i.method,
            'pid': i.pid
        } for i in Perm.query.all()]

        log(level='info', message='Get Perms List')

        return my_response(dict(result=True, data=permslist))
Example #23
0
    def check_peers(peer_addresses):

        peers = IPFSCommands.peers()
        peer_hashes = [
            IPFSCommands.hash_from_address(p) for p in peer_addresses
        ]
        present = {k: False for k in peer_hashes}

        for line in peers:
            for peer_hash in peer_hashes:
                if line.find(peer_hash) != -1:
                    present[peer_hash] = True

        if not all([v for v in present.itervalues()]):
            log('Not connected to all of the peers')
            log(present)
Example #24
0
    def post(self):
        """
            add roles
        """
        args = self.reqparse.parse_args(strict=True)
        rolename = args.get('rolename')

        if not rolename:
            return json_message(200, 'error', 'the new rolename is None')

        u = Role(rolename=rolename)
        dbadd(u)

        log(level='info', message='Create new role: name=%s' % rolename)

        return json_message(200, 'message', 'Roles Register Success')
Example #25
0
 def test_fm_createfolder(self):
     u.recordcase(self.test_fm_createfolder, "FileManager.Priority1")
     for i in xrange(self.loop):
         self._launch_filemanager()
         d(resourceId="com.chaozhuo.filemanager:id/text_navigation_child", text=u"文档").click()
         time.sleep(2)
         d(resourceId="com.chaozhuo.filemanager:id/more").click()
         time.sleep(2)
         d(resourceId="com.chaozhuo.filemanager:id/item_title", text=u"新建文件夹").click()
         time.sleep(2)
         d(resourceId="com.chaozhuo.filemanager:id/file_name_edit").set_text("test" + str(i))
         time.sleep(2)
         d.press("enter")
         time.sleep(2)
         d(resourceId="android:id/mwCloseBtn").click()
         time.sleep(2)
     u.log()
def find_links_and_process(base_url):
    """
    Find all relevant links in the html at the given url, visit them and extract
    word problems.
    """
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'html.parser')
    all_links = [l.get('href') for l in soup.find_all('a')]
    bad_links = []
    for link in all_links:
        if not link[0].isupper():
            continue
        try:
            get_problems_from_url(base_url + '/' + link, link[:link.find('.')])
        except Exception:
            bad_links.append(link)
    if bad_links:
        log('bad links: %s' % bad_links, 'warning')
    log('time taken: %s' % (time.time() - start), 'info')
def get_problems_from_url(url, filename):
    """
    Scrape all the problems from the given url and write them to a file.
    """
    response = requests.get(url)

    soup = BeautifulSoup(response.text, 'html.parser')
    raw_questions = soup.find_all('div', 'QuestionText')
    questions = dict()
    for index, rq in enumerate(raw_questions):
        question_text = rq.contents
        raw_question = ' '.join([q for q in question_text if q.string])
        questions[index] = {'question': raw_question}

    answers = re.findall("\[3\]\[0\]=new Array\('(\d+)'", response.text)
    for index, answer in enumerate(answers):
        questions[index]['answer'] = int(answer)
    log('found %s questions in %s' % (len(questions), filename), 'debug')

    question_json = json.dumps(questions)
    with open('output/questions_' + filename + '.json', 'w') as out_file:
        out_file.write(question_json)
    def receive(self, conn):
        data = self._receive(conn, HEADER_SIZE)

        if len(data) == 0:
            raise ProtocolError('Connection terminated by other side')
        elif len(data) < HEADER_SIZE:
            raise ProtocolError(
                'Invalid message header of length {}: |{}|'.format(
                    len(data), data))

        version, msg_id, src_len, dst_len, data_len = Message.unpack_header(
            data)
        src = self._receive_len(conn, src_len)
        dst = self._receive_len(conn, dst_len)
        content = self._receive_len(conn, data_len)

        wrapper = MessageWrapper(self.to_message(version, msg_id, content),
                                 src, dst)

        log('>> receive {} from {} to {}'.format(
            wrapper.msg.__class__.__name__, wrapper.src, wrapper.dst))
        return wrapper
    def start(self):
        state = self.State(self.timeout)

        def job():
            try:
                for _ in self.logic:
                    pass
            except Exception as e:
                state.backtrace = traceback.format_exc()
                state.exception = e
            else:
                state.done = True

        self.logic.set_up(state)

        thread = Thread(target=job)
        thread.daemon = True
        thread.start()

        try:

            while not state.done and thread.is_alive:
                if state.timed_out(state.timeout):
                    raise Exception('Test timed out after {} s'
                                    .format(state.timeout))
                time.sleep(0.5)

            if not state.done:
                raise Exception('Test incomplete')

        except KeyboardInterrupt:
            pass

        except Exception as exc:
            log("Test session exception: {}".format(exc))

        finally:
            self.logic.tear_down()

            if state.exception:
                log('Test exception: {}'.format(state.exception))
                log(state.backtrace)

            log('Test state result:\n{}'.format(state))
 def _do_work(self, conn, address):
     try:
         self.on_connect(self, conn)
         while self.working:
             try:
                 message = self.receive(conn)
                 self.on_message(self, conn, message)
             except socket.error, e:
                 raise ProtocolError('Socket error: {}'.format(e))
     except ProtocolError as e:
         log('Protocol error [{}]: {}'.format(address, e))
     except Exception as e:
         log('Exception occurred [{}]: {}'.format(address, e))
         traceback.print_exc()
     finally:
         log('Closing {}'.format(address))
         self.on_disconnect(address)
         conn.close()
Example #31
0
    def insert_into(self, conn: Any) -> None:
        """
        Basically traverese over the directory and inserts all the junk into
        the database #YOLO #PleaseWriteMoreComprehensiveDocumentation #TODO
        """
        package_ids = {} # type Dict[str, int]
        with conn.cursor() as cursor:
            # create rows for each package, if not exist
            for package_info in util.get_sub_dirs(self._r_dir, _NOT_SECRET_DIR):
                package_meta = self._package_meta['target'][package_info.name]
                _id = queries.insert_package_get_id(cursor, package_info, package_meta)
                package_ids[package_info.name] = _id
                util.log(self, 'info', "inserting package for %s @ id %s", package_info.name, _id)

            for script in util.get_file_names(p.join(self._r_dir, '__checksums')):
                util.log(self, 'info', "inserting benchmark_script for %s", script.name)
                queries.insert_script(cursor, script)

            # create rows for each batch initiated, if not exist
            for key, meta in self._meta_data.items():
                queries.insert_batch(cursor, meta, package_ids)
                util.log(self, 'info', "inserting batch '%s' into db", key)

            for package_info in util.get_sub_dirs(self._r_dir, _NOT_SECRET_DIR):
                util.log(self, 'info', "checking benchmarks for %s", package_info.name)
                for sub_dir in util.get_sub_dirs(package_info.path):
                    meta = self._meta_data[sub_dir.name]

                    try:
                        task = PackageResultAnalyzer.create(meta, sub_dir.path, parent=self)
                    except errors.InvalidTaskDir as e:
                        util.log(self, 'warn', 'invalid task directory %s', e.task_dir)
                        continue

                    for result in task.results():
                        util.log(self, 'debug', "%s", result)
                        util.log(self, 'info', "inserting result for '%s' of batch(id='%s')", result.version, meta.id)
                        queries.insert_result(cursor, result, meta)