[hgext] allow using cwclientlib's config file instead of hgrc

authorJulien Cristau <julien.cristau@logilab.fr>
changesetb24777644004
branchdefault
phasedraft
hiddenyes
parent revision#5bed420e8c98 Added tag logilab-devtools-version-0.24.0, logilab-devtools-debian-version-0.24.0-1 for changeset 17078cc48fc0
child revision#4588c1d7a59f packaging overhaul, #a9ded970357f packaging overhaul, #331c488ee5af packaging overhaul
files modified by this revision
hgext/jpl/jplproxy.py
# HG changeset patch
# User Julien Cristau <julien.cristau@logilab.fr>
# Date 1432714616 -7200
# Wed May 27 10:16:56 2015 +0200
# Node ID b24777644004079b48f5d119197def351cf1b772
# Parent 5bed420e8c98b098494c596239ff7a3b0624c69e
[hgext] allow using cwclientlib's config file instead of hgrc

diff --git a/hgext/jpl/jplproxy.py b/hgext/jpl/jplproxy.py
@@ -9,11 +9,11 @@
1  from mercurial.i18n import _
2  from requests import ConnectionError, HTTPError
3 
4  import itertools
5 
6 -from cwclientlib import cwproxy
7 +from cwclientlib import cwproxy, cwproxy_for
8 
9  def wraprql(meth):
10      def wrapper(*args, **kwargs):
11          reply = meth(*args, **kwargs)
12          try:
@@ -25,14 +25,10 @@
13              return None
14          except HTTPError as exc:
15              return '\n'.join(("%s" % exc, reply.json()['reason']))
16      return wrapper
17 
18 -class MyProxy(cwproxy.CWProxy):
19 -    rql = wraprql(cwproxy.CWProxy.rql)
20 -    rqlio = wraprql(cwproxy.CWProxy.rqlio)
21 -
22  class RequestError(IOError):
23      """Exception raised when the request fails."""
24 
25  def getlglbopt(name, ui, opts, default=None, isbool=False):
26      value = default
@@ -53,25 +49,35 @@
27          base_url = getlglbopt('forge-url', ui, opts, default=URL)
28          verify = not getlglbopt('no-verify-ssl', ui, opts, isbool=True)
29          mech = getlglbopt('auth-mech', ui, opts)
30          auth = None
31 
32 -        if mech and mech not in ('signedrequest', 'kerberos'):
33 -            raise util.Abort(_('unknown authentication mechanisme specified with --auth-mech'))
34 +        if base_url.startswith('http'):
35 +            # legacy, all config in hgrc
36 +            if mech and mech not in ('signedrequest', 'kerberos'):
37 +                raise util.Abort(_('unknown authentication mechanisme specified with --auth-mech'))
38 +
39 +            if mech == 'signedrequest':
40 +                token = getlglbopt('auth-token', ui, opts)
41 +                secret = getlglbopt('auth-secret', ui, opts)
42 +                if not token or not secret:
43 +                    raise util.Abort(_('you must provide your authentication token and secret'))
44 
45 -        if mech == 'signedrequest':
46 -            token = getlglbopt('auth-token', ui, opts)
47 -            secret = getlglbopt('auth-secret', ui, opts)
48 -            if not token or not secret:
49 -                raise util.Abort(_('you must provide your authentication token and secret'))
50 +                auth = cwproxy.SignedRequestAuth(token, secret)
51 +            if mech == 'kerberos':
52 +                from requests_kerberos import HTTPKerberosAuth, OPTIONAL
53 +                auth = HTTPKerberosAuth(mutual_authentication=OPTIONAL)
54 
55 -            auth = cwproxy.SignedRequestAuth(token, secret)
56 -        if mech == 'kerberos':
57 -            from requests_kerberos import HTTPKerberosAuth, OPTIONAL
58 -            auth = HTTPKerberosAuth(mutual_authentication=OPTIONAL)
59 +            proxy = cwproxy.CWProxy(base_url, auth=auth, verify=verify)
60 +        else:
61 +            # use cwclientlib config file
62 +            proxy = cwproxy_for(base_url)
63 
64 -        yield MyProxy(base_url, auth=auth, verify=verify)
65 +        proxy.rql = wraprql(proxy.rql)
66 +        proxy.rqlio = wraprql(proxy.rqlio)
67 +        yield proxy
68 +
69      except ConnectionError as exc:
70          if ui.tracebackflag:
71              raise
72          try:
73              msg = exc[0].reason