From f1468431bfa04350183c4708dd40ab180b53c4db Mon Sep 17 00:00:00 2001 From: Steve Kowalik Date: Thu, 10 Sep 2015 15:52:29 +1000 Subject: [PATCH] Refactor verification for ZanataRestService Rather than pass verify to each function that requires it, refactor it down the stack into __init__. Now that we have moved to using translate.o.o which has a trusted certificate, switch the default to True. Add a command-line argument --no-verify to create-zanata-xml to ease testing against translate-dev. Change-Id: I0f2314a8e82c3b7f1c96f28408c36ea1dd786e2f --- jenkins/scripts/ZanataUtils.py | 18 ++++++++++-------- jenkins/scripts/create-zanata-xml.py | 4 +++- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/jenkins/scripts/ZanataUtils.py b/jenkins/scripts/ZanataUtils.py index d43f958c48..59b5b37788 100755 --- a/jenkins/scripts/ZanataUtils.py +++ b/jenkins/scripts/ZanataUtils.py @@ -62,7 +62,7 @@ class IniConfig: class ZanataRestService: - def __init__(self, zconfig, content_type='application/xml'): + def __init__(self, zconfig, content_type='application/xml', verify=True): self.url = zconfig.url if "charset" not in content_type: content_type = "%s;charset=utf8" % content_type @@ -70,14 +70,16 @@ class ZanataRestService: 'Content-Type': content_type, 'X-Auth-User': zconfig.username, 'X-Auth-Token': zconfig.key} + self.verify = verify def _construct_url(self, url_fragment): return urljoin(self.url, url_fragment) - def query(self, url_fragment, verify=False, raise_errors=True): + def query(self, url_fragment, raise_errors=True): request_url = self._construct_url(url_fragment) try: - r = requests.get(request_url, verify=verify, headers=self.headers) + r = requests.get(request_url, verify=self.verify, + headers=self.headers) except requests.exceptions.ConnectionError: raise ValueError('Connection error') if raise_errors and r.status_code != 200: @@ -87,10 +89,10 @@ class ZanataRestService: raise ValueError('Did not recieve any data from %s' % request_url) return r - def push(self, url_fragment, data, verify=False): + def push(self, url_fragment, data): request_url = self._construct_url(url_fragment) try: - return requests.put(request_url, verify=verify, + return requests.put(request_url, verify=self.verify, headers=self.headers, data=json.dumps(data)) except requests.exceptions.ConnectionError: raise ValueError('Connection error') @@ -108,8 +110,8 @@ class ProjectConfig: xmlfile (str): path to zanata.xml to read or write rules (list): list of two-ples with pattern and rules """ - def __init__(self, zconfig, xmlfile, rules, **kwargs): - self.rest_service = ZanataRestService(zconfig) + def __init__(self, zconfig, xmlfile, rules, verify, **kwargs): + self.rest_service = ZanataRestService(zconfig, verify=verify) self.xmlfile = xmlfile self.rules = self._parse_rules(rules) if os.path.isfile(os.path.abspath(xmlfile)): @@ -181,7 +183,7 @@ class ProjectConfig: """ r = self.rest_service.query( '/rest/projects/p/%s/iterations/i/%s/config' - % (self.project, self.version), verify=verify) + % (self.project, self.version)) project_config = r.content p = etree.XMLParser(remove_blank_text=True) try: diff --git a/jenkins/scripts/create-zanata-xml.py b/jenkins/scripts/create-zanata-xml.py index 682b134ce9..b58d8c2ee2 100755 --- a/jenkins/scripts/create-zanata-xml.py +++ b/jenkins/scripts/create-zanata-xml.py @@ -33,6 +33,8 @@ def get_args(): 'match .pot files to translations. Can be specified ' 'multiple times, and if no rules are specified a ' 'default will be used.') + parser.add_argument('--no-verify', action='store_false', dest='verify', + help='Do not perform HTTPS certificate verification') parser.add_argument('-f', '--file', required=True) return parser.parse_args() @@ -44,7 +46,7 @@ def main(): rules = args.rule or [default_rule] try: zc = IniConfig(os.path.expanduser('~/.config/zanata.ini')) - ProjectConfig(zc, args.file, rules, project=args.project, + ProjectConfig(zc, args.file, rules, args.verify, project=args.project, version=args.version, srcdir=args.srcdir, txdir=args.txdir, excludes=args.excludes)