aboutsummaryrefslogtreecommitdiff
path: root/update-gerrit-keys.py
diff options
context:
space:
mode:
authorMatt Hart <matthew.hart@linaro.org>2015-11-25 17:32:35 +0000
committerMatt Hart <matthew.hart@linaro.org>2015-11-30 11:38:20 +0000
commit66ccf8049c935afcfa4f55f17e67ca2449c8873a (patch)
tree9cf62491d935dd07c20347ab5d668ed1c307677c /update-gerrit-keys.py
parent9d0b206237b22c25a0541c2e5a8373d3caae9a3a (diff)
Catch import errors when using old requests
Our precise boxes have such an old version of python-requests that the API has changed. This catches the import error to use the older function calls Change-Id: Ie36a2ef77c7d11f206e3051ef8c2639b7510dd59
Diffstat (limited to 'update-gerrit-keys.py')
-rwxr-xr-xupdate-gerrit-keys.py26
1 files changed, 13 insertions, 13 deletions
diff --git a/update-gerrit-keys.py b/update-gerrit-keys.py
index a8fba93..c0f69bd 100755
--- a/update-gerrit-keys.py
+++ b/update-gerrit-keys.py
@@ -23,11 +23,20 @@ args = parser.parse_args()
logging.basicConfig(level=getattr(logging, args.loglevel.upper()))
logging.getLogger("requests").setLevel(logging.WARNING)
log = logging.getLogger("update-gerrit-keys")
+
verify_ssl = True
if args.noverify:
log.debug("Not verifying SSL certificates")
verify_ssl = False
+try:
+ from requests.auth import HTTPDigestAuth
+ reqargs={"auth": HTTPDigestAuth(args.username, args.password),
+ "verify": verify_ssl}
+except ImportError:
+ log.info("Using old version of python-requests (--noverify not supported")
+ reqargs= {"auth": ('digest', args.username, args.password)}
+
def strip_gerrit_junk(string):
# https://gerrit-review.googlesource.com/Documentation/rest-api.html#output
@@ -37,10 +46,7 @@ def strip_gerrit_junk(string):
def list_keys(username):
log.info("Listing keys for user: %s", username)
url = "%s/a/accounts/%s/sshkeys/" % (args.base, username)
- r = requests.get(url,
- auth=requests.auth.HTTPDigestAuth(args.username,
- args.password),
- verify=verify_ssl)
+ r = requests.get(url, **reqargs)
keydict = {}
if r.status_code == 200:
try:
@@ -60,10 +66,7 @@ def add_key(pubkey, username):
log.debug("Adding pubkey %s to user %s", pubkey, username)
url = "%s/a/accounts/%s/sshkeys/" % (args.base, username)
if not args.dryrun:
- r = requests.post(url, data=pubkey.encode("utf-8"),
- auth=requests.auth.HTTPDigestAuth(args.username,
- args.password),
- verify=verify_ssl)
+ r = requests.post(url, data=pubkey.encode("utf-8"), **reqargs)
if r.status_code == 201:
return True
return False
@@ -75,10 +78,7 @@ def del_key(username, key_id):
log.debug("Deleting key %s by id from user %s", key_id, username)
url = "%s/a/accounts/%s/sshkeys/%i" % (args.base, username, key_id)
if not args.dryrun:
- r = requests.delete(url,
- auth=requests.auth.HTTPDigestAuth(args.username,
- args.password),
- verify=verify_ssl)
+ r = requests.delete(url, **reqargs)
if r.status_code == 204:
return True
return False
@@ -97,7 +97,7 @@ result = linaro_ldap.get_users_and_keys(only_validated=True)
for user, keysets in result.iteritems():
gerritkeys = list_keys(user)
- if gerritkeys == False:
+ if gerritkeys is False:
continue
simplegerritkeys = gerritkeys.values()
simpleldapkeys = keysets_to_list(keysets)