def check_token(*args, **kwargs): if not g.token: abort(401, "Not Authorized") if isinstance(type, list): if g.token.get('type', None) not in type: abort(401, "Not Authorized") return func(*args, **kwargs)
def get_tracked_files(): command = 'git ls-tree HEAD -r --full-tree --name-only' status, tracked = util.getstatusoutput(command) if status != 0: util.abort('Failed to list tracked files.') return tracked
def check_permissions(permission, payload): if 'permissions' not in payload: abort(400) if permission is not None and permission not in payload['permissions']: abort(403) return True
def build(mode): if args.verbose: print('Building ' + mode + ' mode cctest...') command = 'scons mode=%s simulator=%s target=cctest -j%u' % \ (mode, args.simulator, args.jobs) status, output = util.getstatusoutput(command) if status != 0: print(output) util.abort('Failed building cctest: ' + command)
def build(mode): if args.verbose: print('Building ' + mode + ' mode cctest...') command = 'scons mode=%s simulator=%s all -j%u' % \ (mode, args.simulator, args.jobs) status, output = util.getstatusoutput(command) if status != 0: print(output) util.abort('Failed building cctest: ' + command)
def check_token(*args, **kwargs): if not g.token: abort(403, "Access Denied") if g.token.get('user', {}).get('role', None) in roles: return func(*args, **kwargs) abort(403, "Access Denied")
def clean(mode): if args.verbose: print('Cleaning ' + mode + ' mode cctest...') command = 'scons mode=%s simulator=%s target=cctest --clean' % \ (mode, args.simulator) status, output = util.getstatusoutput(command) if status != 0: print(output) util.abort('Failed cleaning cctest: ' + command)
def bump_and_push(version: str): subprocess.run(["make", "bump"], env={"SEMGREP_VERSION": version}) changed_files = [d[1] for d in diffs()] if set(changed_files) != BUMP_FILES: abort(2, f"Expected diffs only in {BUMP_FILES}") git("add", *BUMP_FILES) git("commit", "-m", f"Release {version}") git("push", "origin", "HEAD")
def clean(mode): if args.verbose: print('Cleaning ' + mode + ' mode cctest...') command = 'scons mode=%s simulator=%s all --clean' % \ (mode, args.simulator) status, output = util.getstatusoutput(command) if status != 0: print(output) util.abort('Failed cleaning cctest: ' + command)
def get_tracked_files(): command = 'git ls-tree ' branch = get_current_branch() options = ' -r --full-tree --name-only' status, tracked = util.getstatusoutput(command + branch + options) if status != 0: util.abort('Failed to list tracked files.') return tracked
def get_untracked_files(): status, output = util.getstatusoutput('git status -s') if status != 0: util.abort('Failed to get git status.') untracked_regexp = re.compile('\?\?.*(src/|test/|tools/).*(.cc$|.h$)') files_in_watched_folder = lambda n: untracked_regexp.search(n) != None untracked_files = filter(files_in_watched_folder, output.split('\n')) return untracked_files
def ReadManifest(filters): status, output = util.getstatusoutput(args.cctest + ' --list') if status != 0: util.abort('Failed to list all tests') names = output.split() for f in filters: names = filter(re.compile(f).search, names) return map(Test, names)
def GetTests(runner, filters=[]): rc, output = util.getstatusoutput(runner + ' --list') if rc != 0: util.abort('Failed to list all tests') tests = output.split() for f in filters: tests = filter(re.compile(f).search, tests) return tests
def GetTests(runner, filters = []): rc, output = util.getstatusoutput(runner + ' --list') if rc != 0: util.abort('Failed to list all tests') tests = output.split() for f in filters: print f tests = filter(re.compile(f).search, tests) return tests
def decode_token(token): """ Try to decode token, or return 401 :param token: token from request :return: decoded token or 401 """ try: return jwt.decode(token, key=app.secret_key, algorithms='HS256') except InvalidTokenError: abort(401, "Invalid token")
def BuildRequiredObjects(arguments): status, output = util.getstatusoutput('scons ' + 'mode=' + arguments.mode + ' ' + 'simulator=' + arguments.simulator + ' ' + 'target=cctest ' + '--jobs=' + str(arguments.jobs)) if status != 0: print(output) util.abort('Failed bulding cctest')
def ReadManifest(runner, filters = [], debugger = False, coloured_trace = False, verbose = False): status, output = util.getstatusoutput(runner + ' --list') if status != 0: util.abort('Failed to list all tests') names = output.split() for f in filters: names = filter(re.compile(f).search, names) return map(lambda x: Test(x, runner, debugger, coloured_trace, verbose), names)
def cut(release_branch: str): if diffs(): abort( 2, f"Git checkout is not clean. Please stash all changes and retry. Changes:\n{diffs()}", ) git("checkout", "develop") git("pull", "--ff-only") git("submodule", "update", "--recursive") git("checkout", "-b", release_branch) git("push", "origin", "HEAD")
def GetTests(runner, filters=[]): cmd = runner + ' --list' rc, output = util.getstatusoutput(cmd) if rc != 0: util.abort("Failed to list all tests. Output of " + cmd + ":\n" + output) tests = output.split() for f in filters: tests = filter(re.compile(f).search, tests) return tests
def add_commit_push(version: str): """ Add all changes to files in BUMP_FILES, creates a release commit, and pushes said commit Assumes all files in BUMP_FILES were modified """ changed_files = [d[1] for d in diffs()] if set(changed_files) != BUMP_FILES: abort(2, f"Expected diffs only in {BUMP_FILES}") git("add", *BUMP_FILES) git("commit", "-m", f"Release {version}") git("push", "origin", "HEAD")
def ClangTidyIsAvailable(clang_tidy): if not util.IsCommandAvailable(clang_tidy): return False cmd = '%s -version' % clang_tidy rc, version = util.getstatusoutput(cmd) if rc != 0: util.abort("Failed to execute %s: %s" % (cmd, version)) m = re.search("LLVM version (\d)\.(\d)\.\d.*$", version.decode(), re.M) if not m: util.abort("Failed to get clang-tidy's version: %s" % version) major, minor = m.groups() return int(major) == CLANG_TIDY_VERSION_MAJOR and \ int(minor) == CLANG_TIDY_VERSION_MINOR
def template(tpl, template_adapter=SimpleTemplate, **kwargs): if tpl not in TEMPLATES or DEBUG: settings = kwargs.get('template_settings',{}) lookup = kwargs.get('template_lookup', TEMPLATE_PATH) if isinstance(tpl, template_adapter): TEMPLATES[tpl] = tpl if settings: TEMPLATES[tpl].prepare(**settings) elif "\n" in tpl or "{" in tpl or "%" in tpl or '$' in tpl: TEMPLATES[tpl] = template_adapter(source=tpl, lookup=lookup, **settings) else: TEMPLATES[tpl] = template_adapter(name=tpl, lookup=lookup, **settings) if not TEMPLATES[tpl]: abort(500, 'Template (%s) not found' % tpl) return TEMPLATES[tpl].render(**kwargs)
def post(self): """ return token if login succeed or return 404 """ b = request.get_json() user = User.objects(email=b["email"]).first() if not user or not check_password(user['password'], b['password']): abort(404, "Wrong username/password combination") token = encode_user_token(user.get_id()) res = OK('Logged in', {"token": token.decode("utf-8")}) return res
def do(config): emos = obtain_latest_emos_content(config) for key in ('custom', 'recuited'): if key in emos: del emos[key] env = Environment(loader=FileSystemLoader(BASE), extensions=['jinja2.ext.autoescape']) template = env.get_template('emos-template.html') content = template.render(emos=emos, now=datetime.now().strftime('%Y-%m-%d')) htmlfile = expanduser(config.get('emos_html', '')) try: with codecs.open(htmlfile, 'w', 'UTF-8') as f: f.write(content) except IOError: abort('bad config.emos_html: ' + htmlfile)
async def view_lesson(lesson_id: int, payload=Depends(requires_auth())): lesson = await Lesson.get_or_none(id=lesson_id) or abort(404) _lv, created = await LessonViewed.get_or_create( user_sub=payload['sub'], lesson=lesson, ) return {'created': created}
def get(self): args = region_parser.parse_args() lat_lu = args['latitude'] + args['latitudeDelta'] / 2.0 lng_lu = args['longitude'] - args['longitudeDelta'] / 2.0 lat_rb = args['latitude'] - args['latitudeDelta'] / 2.0 lng_rb = args['longitude'] + args['longitudeDelta'] / 2.0 if not all([-180.0 < x < 180.0 for x in [lng_lu, lng_rb]]): abort(400, "invalid longitude") if not all([-90.0 < x < 90.0 for x in [lat_lu, lat_rb]]): abort(400, "invalid latitude") locs = Location.objects( geoLocation__geo_within_box=[(lng_lu, lat_lu), (lng_rb, lat_rb)]) return filter_locations(locs, args, args.get('count'))
def ListTests(cctest, filters): status, output = util.getstatusoutput(cctest + ' --list') if status != 0: util.abort('Failed to list all tests') available_tests = output.split() if filters: filters = map(re.compile, filters) def is_selected(test_name): for e in filters: if e.search(test_name): return True return False return filter(is_selected, available_tests) else: return available_tests
def get_os(): """ Get a canonical form of sys.platform. """ if sys.platform.startswith('darwin'): return 'darwin' elif sys.platform.startswith('linux'): return 'linux' elif sys.platform.startswith('openbsd'): return 'openbsd' elif sys.platform.startswith('sunos'): return 'solaris' elif sys.platform.startswith('win32'): return 'windows' elif sys.platform.startswith('cygwin'): return 'cygwin' else: abort(1, 'Unknown operating system ' + sys.platform)
def close_workspace(self): """ Close the workspace page. Returns :class:`ProjectsListPage`. """ self.browser.execute_script('openmdao.Util.closeWebSockets();') NotifierPage.wait(self.browser, self.port) self('project_menu').click() # Sometimes chromedriver hangs here, so we click in separate thread. # It's a known issue on the chromedriver site. closer = threading.Thread(target=self._closer) closer.daemon = True closer.start() closer.join(60) if closer.is_alive(): abort(True) raise SkipTest("Can't close workspace, driver hung :-(") from project import ProjectsListPage return ProjectsListPage.verify(self.browser, self.port)
def template(tpl, template_adapter=SimpleTemplate, **kwargs): if tpl not in TEMPLATES or DEBUG: settings = kwargs.get('template_settings', {}) lookup = kwargs.get('template_lookup', TEMPLATE_PATH) if isinstance(tpl, template_adapter): TEMPLATES[tpl] = tpl if settings: TEMPLATES[tpl].prepare(**settings) elif "\n" in tpl or "{" in tpl or "%" in tpl or '$' in tpl: TEMPLATES[tpl] = template_adapter(source=tpl, lookup=lookup, **settings) else: TEMPLATES[tpl] = template_adapter(name=tpl, lookup=lookup, **settings) if not TEMPLATES[tpl]: abort(500, 'Template (%s) not found' % tpl) return TEMPLATES[tpl].render(**kwargs)
def post(self): """ Create new user :return: 200 or 400 """ b = request.get_json() username = b["username"] email = b["email"] valid, msg = check_username(username) if not valid: abort(400, msg) if not check_email(email): abort(400, "invalid email") user = User.objects(username=username).first() if user: abort(400, "User already exists") user = User.objects(email=email).first() if user: abort(400, "email has already been registered") password = b.get('password', '') valid, msg = check_password(password) if not valid: abort(400, msg) user = { "id": uuid.uuid4(), "email": email, "username": username, "password": encrypt_passwd(password), } user = User(**user) user.save() token = encode_user_token(user.get_id()) res = OK('Logged in', {"token": token.decode("utf-8")}) return res
def create_app(config_strategy, override_settings=None, logger=None): """ Create a Flask application using the factory pattern. """ if config_strategy is None: abort('Configuration strategy not specified.') cfg = create_config(config_strategy=config_strategy, override_settings=override_settings) app = Flask(__name__, static_folder=cfg.STATIC_DIR, template_folder=cfg.TEMPLATE_DIR) if logger: logger_ = logging.getLogger(logger) app.logger.handlers = logger_.handlers app.logger.setLevel(logger_.level) app.logger.info(f'Using logger: {logger}') else: logging_map = { 'debug': logging.DEBUG, 'info': logging.INFO, 'warning': logging.WARNING, 'error': logging.ERROR, 'critical': logging.CRITICAL, } logging_level = logging_map[os.getenv('LOG_LEVEL', 'debug')] app.logger.setLevel(logging_level) app.logger.info('No logger specified, streaming logs to output.') app.config.from_object(cfg) init_jinja_env(app) init_blueprints(app) init_extensions(app) init_error_handlers(app) init_db(app) return app
def enrich_user_token(token): """ Add more information for user token :param token: decoded user token :return: enriched token """ if not ("user" in token and "id" in token["user"]): abort(401, "Invalid token") user_id = token["user"]["id"] user = User.objects(id=user_id).first() if not user: abort(401, "Invalid user") token['user']['role'] = user['role'] token['user']['active'] = user['active'] token['user']['email'] = user['email'] token['user']['username'] = user['username'] return token
def do(config): log_dir = expanduser(config.get('log_dir', '')) if not (isdir(log_dir) and os.access(log_dir, os.W_OK)): abort('bad config.log_dir: ' + log_dir) content_curr = obtain_latest_emos_content(config) content_prev = load_saved_content(log_dir) if not content_prev: save_content(log_dir, content_curr) return emos_curr = extract_emo_keys(content_curr) emos_prev = extract_emo_keys(content_prev) added = emos_curr - emos_prev deled = emos_prev - emos_curr if added or deled: print 'added:', added print 'deled:', deled do_post(config, added, deled) save_content(log_dir, content_curr)
def FilterKnownValgrindTestFailures(tests): rc, output = util.getstatusoutput('valgrind --version') if rc != 0: util.abort('Failed to get the Valgrind version.') version = re.search('^valgrind-([0-9]+)\.([0-9]+)\.([0-9]+)', output) if not version: util.abort('Failed to get the Valgrind version.') major = int(version.group(1)) minor = int(version.group(2)) if major > 3 or (major == 3 and minor > 10): return tests # Valgrind versions before 3.11 have issues with fused multiply-add, # so disable the affected tests. known_valgrind_test_failures = { 'AARCH64_SIM_fmadd_d', 'AARCH64_SIM_fmadd_s', 'AARCH64_SIM_fmla_2D', 'AARCH64_SIM_fmla_2D_2D_D', 'AARCH64_SIM_fmla_2S', 'AARCH64_SIM_fmla_2S_2S_S', 'AARCH64_SIM_fmla_4S', 'AARCH64_SIM_fmla_4S_4S_S', 'AARCH64_SIM_fmla_D_D_D', 'AARCH64_SIM_fmls_2D', 'AARCH64_SIM_fmls_2D_2D_D', 'AARCH64_SIM_fmls_2S', 'AARCH64_SIM_fmls_2S_2S_S', 'AARCH64_SIM_fmls_4S', 'AARCH64_SIM_fmls_4S_4S_S', 'AARCH64_SIM_fmls_D_D_D', 'AARCH64_SIM_fmsub_d', 'AARCH64_SIM_fmsub_s', 'AARCH64_SIM_fnmadd_d', 'AARCH64_SIM_fnmadd_s', 'AARCH64_SIM_fnmsub_d', 'AARCH64_SIM_fnmsub_s', 'AARCH64_SIM_frecps_2D', 'AARCH64_SIM_frecps_D', 'AARCH64_SIM_frsqrts_2D', 'AARCH64_SIM_frsqrts_D' } for t in sorted(known_valgrind_test_failures): print('Skipping ' + t + '...') return filter(lambda x: x not in known_valgrind_test_failures, tests)
def FilterKnownValgrindTestFailures(tests): rc, output = util.getstatusoutput('valgrind --version') if rc != 0: util.abort('Failed to get the Valgrind version.') version = re.search('^valgrind-([0-9]+)\.([0-9]+)\.([0-9]+)', output) if not version: util.abort('Failed to get the Valgrind version.') major = int(version.group(1)) minor = int(version.group(2)) if major > 3 or (major == 3 and minor > 10): return tests reason = "Valgrind versions before 3.11 have issues with fused multiply-add, " \ "so disable the affected tests." known_valgrind_test_failures = { 'AARCH64_SIM_fmadd_d', 'AARCH64_SIM_fmadd_s', 'AARCH64_SIM_fmla_2D', 'AARCH64_SIM_fmla_2D_2D_D', 'AARCH64_SIM_fmla_2S', 'AARCH64_SIM_fmla_2S_2S_S', 'AARCH64_SIM_fmla_4S', 'AARCH64_SIM_fmla_4S_4S_S', 'AARCH64_SIM_fmla_D_D_D', 'AARCH64_SIM_fmls_2D', 'AARCH64_SIM_fmls_2D_2D_D', 'AARCH64_SIM_fmls_2S', 'AARCH64_SIM_fmls_2S_2S_S', 'AARCH64_SIM_fmls_4S', 'AARCH64_SIM_fmls_4S_4S_S', 'AARCH64_SIM_fmls_D_D_D', 'AARCH64_SIM_fmsub_d', 'AARCH64_SIM_fmsub_s', 'AARCH64_SIM_fnmadd_d', 'AARCH64_SIM_fnmadd_s', 'AARCH64_SIM_fnmsub_d', 'AARCH64_SIM_fnmsub_s', 'AARCH64_SIM_frecps_2D', 'AARCH64_SIM_frecps_D', 'AARCH64_SIM_frsqrts_2D', 'AARCH64_SIM_frsqrts_D' } filtered_list = [x for x in tests if x not in known_valgrind_test_failures] return (filtered_list, len(tests) - len(filtered_list), reason)
def get_current_branch(): status, branches = util.getstatusoutput('git branch') if status != 0: util.abort('Failed to run git branch.') match = re.search("^\* (.*)$", branches, re.MULTILINE) if not match: util.abort('Failed to find the current branch.') branch = match.group(1); # If we are not on a named branch, return the hash of the HEAD commit. # This can occur (for example) if a specific revision is checked out by # commit hash, or during a rebase. if branch == '(no branch)': status, commit = util.getstatusoutput('git log -1 --pretty=format:"%H"') if status != 0: util.abort('Failed to run git log.') match = re.search('^[0-9a-fA-F]{40}$', commit, re.MULTILINE) if not match: util.abort('Failed to find the current revision.') branch = match.group(0) return branch
print("Using database '%s':" % (DB.upper())) print("Connecting to database ..."), cnxn_str = "DSN=%s;UID=%s;PWD=%s" % (DB, USER, PASSWD) cnxn = pyodbc.connect(cnxn_str) util.done() except pyodbc.Error, connect_err: util.fail() print(connect_err) exit() cursor = cnxn.cursor() try: meter_list = get_meter_list(extract_file) except ValueError, badHeader: print(badHeader) usage() util.abort(cursor, cnxn) print("Begin data extraction ...\n") err_count = 0 for meter_row in meter_list: err_count += extract(meter_row, cursor, cnxn) print("\nExtraction finished.") if (err_count == 0): print("No errors encountered.") else: print("%d of %d IDs failed to process" % (err_count, len(meter_list))) util.close_cnxn(cursor, cnxn) def extract(mtr_row, my_cursor, my_cnxn): """ Extract reading data for the meter described in MTR_ROW to a csv file in the
args = BuildOptions(root_dir) # Run each simulator test (SIM_*) with the --sim_test_trace option, and use # the output to create the traces header (from --out). In addition, the # test-simulator-traces-a64.h file, the master trace file, which includes all # other trace files is generated. # Create master trace file. master_trace_f = open(args.out, 'w') master_trace_f.write(copyright_header) master_trace_f.write(master_trace_header) master_trace_f.write('\n\n') # Find the simulator tests. status, output = util.getstatusoutput(args.runner + ' --list') if status != 0: util.abort('Failed to list all tests') tests = filter(lambda t: 'SIM_' in t, output.split()) tests.sort() for test in tests: # Run each test. print 'Generating trace for ' + test; cmd = ' '.join([args.runner, '--sim_test_trace', test]) status, output = util.getstatusoutput(cmd) if status != 0: util.abort('Failed to run ' + cmd + '.') # Create a new trace header file. trace_filename = test.lower().replace('_', '-') + "-trace-a64.h" trace_f = open("test/traces/a64/" + trace_filename, 'w') trace_f.write(copyright_header) trace_f.write(trace_header)
def CleanBuildSystem(): status, output = util.getstatusoutput('scons mode=release --clean') if status != 0: util.abort('Failed to clean in release mode.') status, output = util.getstatusoutput('scons mode=debug --clean') if status != 0: util.abort('Failed to clean in debug mode.')
redirect = r.headers['Location'] up = urlparse(redirect) qu = urllib.parse.parse_qs(up.query) print (qu['lti_errormsg'][0]) # print (qu['detail'][0]) print('Loading secret for',CFG.oauth_consumer_key,'from the database') sql = 'SELECT secret FROM lti_key WHERE key_key = %s' cursor.execute(sql, (CFG.oauth_consumer_key, )) result = cursor.fetchone() if ( result == None ) : print('Unable to load secret for key',CFG.oauth_consumer_key) U.abort() conn.commit() CFG.oauth_secret = result['secret'] header = {'Content-Type' : 'application/x-www-form-urlencoded'} print('Sending a launch with a good secret... ',end='') r = U.launch(CFG,url,post) U.verifyDb(conn,post) print('Sending minimal launch to check DB persistence... ',end='') post = {} post.update(POST.core) post['resource_link_id'] = link1 post['context_id'] = context1
logging.error(data) continue for rec in data.get("responses", []): content = rec.get("content", u"") do_post(balancer, content) logging.debug([content, balancer.complement]) complement = balancer.complement if 0 < len(complement): for text in uniquify(split_text(complement)): logging.info(" posting: %s" % text) params = dict(plurk_id=plurk_id, qualifier=":", content=text.encode("UTF-8")) ok, status, data = api.post("/Responses/responseAdd", params) if not ok: logging.error("""api.post('/Responses/responseAdd', dict(content=%s))""" % complement) logging.error("status: %s" % status) logging.error(data) break sleep(interval) if __name__ == "__main__": import sys if 2 <> len(sys.argv): abort("usage: %s config.yml\n" % sys.argv[0]) from config import load do(load(sys.argv[1]))
params = dict( oauth_signature_method='HMAC-SHA1', oauth_nonce=oauth.generate_nonce(), oauth_timestamp=oauth.generate_timestamp(), oauth_token=rt.key, oauth_token_secret=rt.secret, oauth_verifier=v) req = oauth.Request.from_consumer_and_token( consumer=consumer, token=rt, http_method='POST', http_url=ACCESS_TOKEN_URL, parameters=params, is_form_encoded=True) req.sign_request(oauth.SignatureMethod_HMAC_SHA1(), consumer, rt) res = c.request(ACCESS_TOKEN_URL, method='POST', headers=req.to_header()) r = urlparse.parse_qs(res[1]) print " key: '%s'" % r['oauth_token'][0] print " sec: '%s'" % r['oauth_token_secret'][0] if __name__ == '__main__': import sys if 2 <> len(sys.argv): abort('usage: %s config.yml\n' % sys.argv[0]) from config import load do(load(sys.argv[1])['api'])
matched = 0 f = open(args.out, 'r+') # Use readline (rather than for..in) so we can truncate at the right place. line = f.readline(); while line: if line.strip() == marker[matched]: matched = matched + 1 if matched == len(marker): f.truncate() break else: matched = 0 line = f.readline() if matched != len(marker): util.abort('Failed to find output section in ' + args.out + '.') # Find the simulator tests. status, output = util.getstatusoutput(args.cctest + ' --list') if status != 0: util.abort('Failed to list all tests') tests = filter(lambda t: 'SIM_' in t, output.split()) tests.sort() # Run each test. for test in tests: cmd = ' '.join([args.cctest, '--sim_test_trace', test]) status, output = util.getstatusoutput(cmd) if status != 0: util.abort('Failed to run ' + cmd + '.') f.write('\n\n' + output)