From b37f43db1bf2505e5fc683d36a62df220064d8cf Mon Sep 17 00:00:00 2001 From: Sam Lang Date: Fri, 12 Apr 2013 12:55:54 -0500 Subject: [PATCH] lock: Fix import cycle breakage fa2049f caused an import cycle between lock.py and misc.py. Move the needed functions from lock.py to lockstatus.py so that we can avoid the import cycle. Signed-off-by: Sam Lang Conflicts: teuthology/lock.py --- teuthology/lock.py | 33 +++++++-------------------------- teuthology/lockstatus.py | 25 +++++++++++++++++++++++++ teuthology/misc.py | 4 ++-- teuthology/task/internal.py | 3 ++- 4 files changed, 36 insertions(+), 29 deletions(-) create mode 100644 teuthology/lockstatus.py diff --git a/teuthology/lock.py b/teuthology/lock.py index b63260ceae..ce79016746 100644 --- a/teuthology/lock.py +++ b/teuthology/lock.py @@ -1,5 +1,4 @@ import argparse -import httplib2 import json import logging import subprocess @@ -8,26 +7,15 @@ import yaml import re import collections +from teuthology import lockstatus as ls from teuthology import misc as teuthology log = logging.getLogger(__name__) -def _lock_url(ctx): - return ctx.teuthology_config['lock_server'] - -def send_request(method, url, body=None, headers=None): - http = httplib2.Http() - resp, content = http.request(url, method=method, body=body, headers=headers) - if resp.status == 200: - return (True, content, resp.status) - log.info("%s request to '%s' with body '%s' failed with response code %d", - method, url, body, resp.status) - return (False, None, resp.status) - def lock_many(ctx, num, machinetype, user=None, description=None): if user is None: user = teuthology.get_user() - success, content, status = send_request('POST', _lock_url(ctx), + success, content, status = ls.send_request('POST', ls._lock_url(ctx), urllib.urlencode(dict( user=user, num=num, @@ -47,7 +35,7 @@ def lock_many(ctx, num, machinetype, user=None, description=None): def lock(ctx, name, user=None): if user is None: user = teuthology.get_user() - success, _, _ = send_request('POST', _lock_url(ctx) + '/' + name, + success, _, _ = ls.send_request('POST', ls._lock_url(ctx) + '/' + name, urllib.urlencode(dict(user=user))) if success: log.debug('locked %s as %s', name, user) @@ -58,7 +46,7 @@ def lock(ctx, name, user=None): def unlock(ctx, name, user=None): if user is None: user = teuthology.get_user() - success, _ , _ = send_request('DELETE', _lock_url(ctx) + '/' + name + '?' + \ + success, _ , _ = ls.send_request('DELETE', ls._lock_url(ctx) + '/' + name + '?' + \ urllib.urlencode(dict(user=user))) if success: log.debug('unlocked %s', name) @@ -66,14 +54,8 @@ def unlock(ctx, name, user=None): log.error('failed to unlock %s', name) return success -def get_status(ctx, name): - success, content, _ = send_request('GET', _lock_url(ctx) + '/' + name) - if success: - return json.loads(content) - return None - def list_locks(ctx): - success, content, _ = send_request('GET', _lock_url(ctx)) + success, content, _ = ls.send_request('GET', ls._lock_url(ctx)) if success: return json.loads(content) return None @@ -88,7 +70,7 @@ def update_lock(ctx, name, description=None, status=None, sshpubkey=None): updated['sshpubkey'] = sshpubkey if updated: - success, _, _ = send_request('PUT', _lock_url(ctx) + '/' + name, + success, _, _ = ls.send_request('PUT', ls._lock_url(ctx) + '/' + name, body=urllib.urlencode(updated), headers={'Content-type': 'application/x-www-form-urlencoded'}) return success @@ -276,10 +258,9 @@ Lock, unlock, or query lock status of machines. assert ctx.desc is None, '--desc does nothing with --list' if machines: - statuses = [get_status(ctx, machine) for machine in machines] + statuses = [ls.get_status(ctx, machine) for machine in machines] else: statuses = list_locks(ctx) - if statuses: if not machines and ctx.owner is None and not ctx.all: ctx.owner = teuthology.get_user() diff --git a/teuthology/lockstatus.py b/teuthology/lockstatus.py new file mode 100644 index 0000000000..9e63614a6e --- /dev/null +++ b/teuthology/lockstatus.py @@ -0,0 +1,25 @@ +import json +import httplib2 +import logging + +log = logging.getLogger(__name__) + +def _lock_url(ctx): + return ctx.teuthology_config['lock_server'] + +def send_request(method, url, body=None, headers=None): + http = httplib2.Http() + resp, content = http.request(url, method=method, body=body, headers=headers) + if resp.status == 200: + return (True, content, resp.status) + log.info("%s request to '%s' with body '%s' failed with response code %d", + method, url, body, resp.status) + return (False, None, resp.status) + +def get_status(ctx, name): + success, content, _ = send_request('GET', _lock_url(ctx) + '/' + name) + if success: + return json.loads(content) + return None + + diff --git a/teuthology/misc.py b/teuthology/misc.py index aca1b3c638..c27a9b8cf2 100644 --- a/teuthology/misc.py +++ b/teuthology/misc.py @@ -13,7 +13,7 @@ import yaml import json from teuthology import safepath -from teuthology import lock +from teuthology import lockstatus from .orchestra import run log = logging.getLogger(__name__) @@ -37,7 +37,7 @@ def get_testdir(ctx): if not checked_jobid: jobids = {} for machine in ctx.config['targets'].iterkeys(): - status = lock.get_status(ctx, machine) + status = lockstatus.get_status(ctx, machine) jid = status['description'].split('/')[-1] jobids[jid] = 1 if len(jobids) > 1: diff --git a/teuthology/task/internal.py b/teuthology/task/internal.py index 3b1412e65f..bcb0abc08a 100644 --- a/teuthology/task/internal.py +++ b/teuthology/task/internal.py @@ -6,6 +6,7 @@ import os import time import yaml +from teuthology import lockstatus from teuthology import lock from teuthology import misc as teuthology from ..orchestra import run @@ -121,7 +122,7 @@ def check_lock(ctx, config): return log.info('Checking locks...') for machine in ctx.config['targets'].iterkeys(): - status = lock.get_status(ctx, machine) + status = lockstatus.get_status(ctx, machine) log.debug('machine status is %s', repr(status)) assert status is not None, \ 'could not read lock status for {name}'.format(name=machine) -- 2.39.5