async def request(context, url, timeout=60, method='get', good=(200, ), retry=tuple(range(500, 512)), return_type='text', **kwargs): """Async aiohttp request wrapper. Args: context (scriptworker.context.Context): the scriptworker context. url (str): the url to request timeout (int, optional): timeout after this many seconds. Default is 60. method (str, optional): The request method to use. Default is 'get'. good (list, optional): the set of good status codes. Default is (200, ) retry (list, optional): the set of status codes that result in a retry. Default is tuple(range(500, 512)). return_type (str, optional): The type of value to return. Takes 'json' or 'text'; other values will return the response object. Default is text. **kwargs: the kwargs to send to the aiohttp request function. Returns: object: the response text() if return_type is 'text'; the response json() if return_type is 'json'; the aiohttp request response object otherwise. Raises: ScriptWorkerRetryException: if the status code is in the retry list. ScriptWorkerException: if the status code is not in the retry list or good list. """ session = context.session with aiohttp.Timeout(timeout): log.debug("{} {}".format(method.upper(), url)) async with session.request(method, url, **kwargs) as resp: log.debug("Status {}".format(resp.status)) message = "Bad status {}".format(resp.status) if resp.status in retry: raise ScriptWorkerRetryException(message) if resp.status not in good: raise ScriptWorkerException(message) if return_type == 'text': return await resp.text() elif return_type == 'json': return await resp.json() else: return resp
def makedirs(path: str) -> None: """Equivalent to mkdir -p. Args: path (str): the path to mkdir -p Raises: ScriptWorkerException: if path exists already and the realpath is not a dir. """ if path: if not os.path.exists(path): log.debug("makedirs({})".format(path)) os.makedirs(path) else: realpath = os.path.realpath(path) if not os.path.isdir(realpath): raise ScriptWorkerException("makedirs: {} already exists and is not a directory!".format(path))
async def async_main(context): """Run all the vcs things. Args: context (TreeContext): the treescript context. """ connector = aiohttp.TCPConnector() async with aiohttp.ClientSession(connector=connector) as session: context.session = session work_dir = context.config['work_dir'] actions_to_perform = task_action_types(context.task, context.config) await log_mercurial_version(context) if not await validate_robustcheckout_works(context): raise ScriptWorkerException("Robustcheckout can't run on our version of hg, aborting") await checkout_repo(context, work_dir) if actions_to_perform: await do_actions(context, actions_to_perform, work_dir) log.info("Done!")
async def always_fail(*args, **kwargs): global retry_count retry_count.setdefault("always_fail", 0) retry_count["always_fail"] += 1 raise ScriptWorkerException("fail")
async def foo(arg, credentials): # arg.credentials will be a dict copy of a frozendict. assert credentials == dict(creds) raise ScriptWorkerException("foo")
async def async_error(context): exception = ScriptWorkerException("async_error!") exception.exit_code = 42 raise exception
async def always_fail(*args, **kwargs): global retry_count retry_count['always_fail'] += 1 raise ScriptWorkerException("fail")
async def raise_swe(*args, **kwargs): raise ScriptWorkerException("foo")
async def async_error(context): exception = ScriptWorkerException('async_error!') exception.exit_code = 42 raise exception