fune/taskcluster/taskgraph/parameters.py
Mike Hommey 3e79befa4b Bug 1341214 - Add a small API to handle taskcluster queue and index requests. r=dustin
Various modules under taskcluster are doing ad-hoc url formatting or
requests to taskcluster services. While we could use the taskcluster
client python module, it's kind of overkill for the simple requests done
here. So instead of vendoring that module, create a smaller one with
a limited set of functions we need.

This changes the behavior of the get_artifact function to return a
file-like object when the file is neither a json nor a yaml, but that
branch was never used (and was actually returning an unassigned
variable, so it was broken anyways).

At the same time, make the function that does HTTP requests more
error-resistant, using urllib3's Retry with a backoff factor.

Also add a function that retrieves the list of artifacts, that while
currently unused, will be used by `mach artifact` shortly.

--HG--
extra : rebase_source : 06777dea62e884f546a5b951baad80fd8aec1f1e
2017-02-17 12:04:48 +09:00

87 lines
2.4 KiB
Python

# -*- coding: utf-8 -*-
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import absolute_import, print_function, unicode_literals
import json
import yaml
from mozbuild.util import ReadOnlyDict
# Please keep this list sorted and in sync with taskcluster/docs/parameters.rst
PARAMETER_NAMES = set([
'base_repository',
'build_date',
'filters',
'head_ref',
'head_repository',
'head_rev',
'level',
'message',
'moz_build_date',
'optimize_target_tasks',
'owner',
'project',
'pushdate',
'pushlog_id',
'target_tasks_method',
'triggered_by',
])
class Parameters(ReadOnlyDict):
"""An immutable dictionary with nicer KeyError messages on failure"""
def check(self):
names = set(self)
msg = []
missing = PARAMETER_NAMES - names
if missing:
msg.append("missing parameters: " + ", ".join(missing))
extra = names - PARAMETER_NAMES
if extra:
msg.append("extra parameters: " + ", ".join(extra))
if msg:
raise Exception("; ".join(msg))
def __getitem__(self, k):
if k not in PARAMETER_NAMES:
raise KeyError("no such parameter {!r}".format(k))
try:
return super(Parameters, self).__getitem__(k)
except KeyError:
raise KeyError("taskgraph parameter {!r} not found".format(k))
def load_parameters_file(options):
"""
Load parameters from the --parameters option
"""
import urllib
from taskgraph.util.taskcluster import get_artifact_url
filename = options['parameters']
if not filename:
return Parameters()
try:
# reading parameters from a local parameters.yml file
f = open(filename)
except IOError:
# fetching parameters.yml using task task-id or supplied url
if filename.startswith("task-id="):
task_id = filename.split("=")[1]
filename = get_artifact_url(task_id, 'public/parameters.yml')
f = urllib.urlopen(filename)
if filename.endswith('.yml'):
return Parameters(**yaml.safe_load(f))
elif filename.endswith('.json'):
return Parameters(**json.load(f))
else:
raise TypeError("Parameters file `{}` is not JSON or YAML".format(filename))