mirror of
https://github.com/ansible/awx.git
synced 2026-05-16 13:57:39 -02:30
Merge pull request #1164 from cclauss/use-new-style-exceptions
Modernize Python 2 code to get ready for Python 3
This commit is contained in:
@@ -33,7 +33,7 @@ class OrderedDictLoader(yaml.SafeLoader):
|
|||||||
key = self.construct_object(key_node, deep=deep)
|
key = self.construct_object(key_node, deep=deep)
|
||||||
try:
|
try:
|
||||||
hash(key)
|
hash(key)
|
||||||
except TypeError, exc:
|
except TypeError as exc:
|
||||||
raise yaml.constructor.ConstructorError(
|
raise yaml.constructor.ConstructorError(
|
||||||
"while constructing a mapping", node.start_mark,
|
"while constructing a mapping", node.start_mark,
|
||||||
"found unacceptable key (%s)" % exc, key_node.start_mark
|
"found unacceptable key (%s)" % exc, key_node.start_mark
|
||||||
|
|||||||
@@ -1173,7 +1173,7 @@ class InventorySerializer(BaseSerializerWithVariables):
|
|||||||
if host_filter:
|
if host_filter:
|
||||||
try:
|
try:
|
||||||
SmartFilter().query_from_string(host_filter)
|
SmartFilter().query_from_string(host_filter)
|
||||||
except RuntimeError, e:
|
except RuntimeError as e:
|
||||||
raise models.base.ValidationError(e)
|
raise models.base.ValidationError(e)
|
||||||
return host_filter
|
return host_filter
|
||||||
|
|
||||||
|
|||||||
@@ -2045,7 +2045,7 @@ class InventoryDetail(ControlledByScmMixin, RetrieveUpdateDestroyAPIView):
|
|||||||
try:
|
try:
|
||||||
obj.schedule_deletion(getattr(request.user, 'id', None))
|
obj.schedule_deletion(getattr(request.user, 'id', None))
|
||||||
return Response(status=status.HTTP_202_ACCEPTED)
|
return Response(status=status.HTTP_202_ACCEPTED)
|
||||||
except RuntimeError, e:
|
except RuntimeError as e:
|
||||||
return Response(dict(error=_("{0}".format(e))), status=status.HTTP_400_BAD_REQUEST)
|
return Response(dict(error=_("{0}".format(e))), status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -275,7 +275,7 @@ class SettingsWrapper(UserSettingsHolder):
|
|||||||
setting_ids[setting.key] = setting.id
|
setting_ids[setting.key] = setting.id
|
||||||
try:
|
try:
|
||||||
value = decrypt_field(setting, 'value')
|
value = decrypt_field(setting, 'value')
|
||||||
except ValueError, e:
|
except ValueError as e:
|
||||||
#TODO: Remove in Tower 3.3
|
#TODO: Remove in Tower 3.3
|
||||||
logger.debug('encountered error decrypting field: %s - attempting fallback to old', e)
|
logger.debug('encountered error decrypting field: %s - attempting fallback to old', e)
|
||||||
value = old_decrypt_field(setting, 'value')
|
value = old_decrypt_field(setting, 'value')
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ def open_fifo_write(path, data):
|
|||||||
This blocks the thread until an external process (such as ssh-agent)
|
This blocks the thread until an external process (such as ssh-agent)
|
||||||
reads data from the pipe.
|
reads data from the pipe.
|
||||||
'''
|
'''
|
||||||
os.mkfifo(path, 0600)
|
os.mkfifo(path, 0o600)
|
||||||
thread.start_new_thread(lambda p, d: open(p, 'w').write(d), (path, data))
|
thread.start_new_thread(lambda p, d: open(p, 'w').write(d), (path, data))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -356,7 +356,7 @@ class SmartFilterField(models.TextField):
|
|||||||
value = urllib.unquote(value)
|
value = urllib.unquote(value)
|
||||||
try:
|
try:
|
||||||
SmartFilter().query_from_string(value)
|
SmartFilter().query_from_string(value)
|
||||||
except RuntimeError, e:
|
except RuntimeError as e:
|
||||||
raise models.base.ValidationError(e)
|
raise models.base.ValidationError(e)
|
||||||
return super(SmartFilterField, self).get_prep_value(value)
|
return super(SmartFilterField, self).get_prep_value(value)
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ class Command(BaseCommand):
|
|||||||
|
|
||||||
def handle(self, *args, **kwargs):
|
def handle(self, *args, **kwargs):
|
||||||
if getattr(settings, 'AWX_ISOLATED_PRIVATE_KEY', False):
|
if getattr(settings, 'AWX_ISOLATED_PRIVATE_KEY', False):
|
||||||
print settings.AWX_ISOLATED_PUBLIC_KEY
|
print(settings.AWX_ISOLATED_PUBLIC_KEY)
|
||||||
return
|
return
|
||||||
|
|
||||||
key = rsa.generate_private_key(
|
key = rsa.generate_private_key(
|
||||||
@@ -41,4 +41,4 @@ class Command(BaseCommand):
|
|||||||
) + " generated-by-awx@%s" % datetime.datetime.utcnow().isoformat()
|
) + " generated-by-awx@%s" % datetime.datetime.utcnow().isoformat()
|
||||||
)
|
)
|
||||||
pemfile.save()
|
pemfile.save()
|
||||||
print pemfile.value
|
print(pemfile.value)
|
||||||
|
|||||||
@@ -41,10 +41,9 @@ class Command(BaseCommand):
|
|||||||
run.open_fifo_write(ssh_key_path, settings.AWX_ISOLATED_PRIVATE_KEY)
|
run.open_fifo_write(ssh_key_path, settings.AWX_ISOLATED_PRIVATE_KEY)
|
||||||
args = run.wrap_args_with_ssh_agent(args, ssh_key_path, ssh_auth_sock)
|
args = run.wrap_args_with_ssh_agent(args, ssh_key_path, ssh_auth_sock)
|
||||||
try:
|
try:
|
||||||
print ' '.join(args)
|
print(' '.join(args))
|
||||||
subprocess.check_call(args)
|
subprocess.check_call(args)
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
sys.exit(e.returncode)
|
sys.exit(e.returncode)
|
||||||
finally:
|
finally:
|
||||||
shutil.rmtree(path)
|
shutil.rmtree(path)
|
||||||
|
|
||||||
|
|||||||
@@ -46,6 +46,6 @@ class CallbackQueueDispatcher(object):
|
|||||||
delivery_mode="persistent" if settings.PERSISTENT_CALLBACK_MESSAGES else "transient",
|
delivery_mode="persistent" if settings.PERSISTENT_CALLBACK_MESSAGES else "transient",
|
||||||
routing_key=self.connection_queue)
|
routing_key=self.connection_queue)
|
||||||
return
|
return
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
self.logger.info('Publish Job Event Exception: %r, retry=%d', e,
|
self.logger.info('Publish Job Event Exception: %r, retry=%d', e,
|
||||||
retry_count, exc_info=True)
|
retry_count, exc_info=True)
|
||||||
|
|||||||
@@ -577,5 +577,5 @@ def delete_inventory_for_org(sender, instance, **kwargs):
|
|||||||
for inventory in inventories:
|
for inventory in inventories:
|
||||||
try:
|
try:
|
||||||
inventory.schedule_deletion(user_id=getattr(user, 'id', None))
|
inventory.schedule_deletion(user_id=getattr(user, 'id', None))
|
||||||
except RuntimeError, e:
|
except RuntimeError as e:
|
||||||
logger.debug(e)
|
logger.debug(e)
|
||||||
|
|||||||
@@ -17,19 +17,19 @@ class Migration(DataMigration):
|
|||||||
obj1 = eval(obj_type + ".objects.get(id=" + str(activity_stream_object.object1_id) + ")")
|
obj1 = eval(obj_type + ".objects.get(id=" + str(activity_stream_object.object1_id) + ")")
|
||||||
if hasattr(activity_stream_object, activity_stream_object.object1):
|
if hasattr(activity_stream_object, activity_stream_object.object1):
|
||||||
getattr(activity_stream_object, activity_stream_object.object1).add(obj1)
|
getattr(activity_stream_object, activity_stream_object.object1).add(obj1)
|
||||||
except ObjectDoesNotExist, e:
|
except ObjectDoesNotExist as e:
|
||||||
print("Object 1 for AS id=%s does not exist. (Object Type: %s, id: %s" % (str(activity_stream_object.id),
|
print("Object 1 for AS id=%s does not exist. (Object Type: %s, id: %s" % (str(activity_stream_object.id),
|
||||||
activity_stream_object.object1_type,
|
activity_stream_object.object1_type,
|
||||||
str(activity_stream_object.object1_id)))
|
str(activity_stream_object.object1_id)))
|
||||||
continue
|
continue
|
||||||
if activity_stream_object.operation in ('associate', 'disassociate'):
|
if activity_stream_object.operation in ('associate', 'disassociate'):
|
||||||
try:
|
try:
|
||||||
obj_type = "orm." + activity_stream_object.object2_type.split(".")[-1]
|
obj_type = "orm." + activity_stream_object.object2_type.split(".")[-1]
|
||||||
if obj_type == 'orm.User':
|
if obj_type == 'orm.User':
|
||||||
obj_type = 'orm["auth.User"]'
|
obj_type = 'orm["auth.User"]'
|
||||||
obj2 = eval(obj_type + ".objects.get(id=" + str(activity_stream_object.object2_id) + ")")
|
obj2 = eval(obj_type + ".objects.get(id=" + str(activity_stream_object.object2_id) + ")")
|
||||||
getattr(activity_stream_object, activity_stream_object.object2).add(obj2)
|
getattr(activity_stream_object, activity_stream_object.object2).add(obj2)
|
||||||
except ObjectDoesNotExist, e:
|
except ObjectDoesNotExist as e:
|
||||||
print("Object 2 for AS id=%s does not exist. (Object Type: %s, id: %s" % (str(activity_stream_object.id),
|
print("Object 2 for AS id=%s does not exist. (Object Type: %s, id: %s" % (str(activity_stream_object.id),
|
||||||
activity_stream_object.object2_type,
|
activity_stream_object.object2_type,
|
||||||
str(activity_stream_object.object2_id)))
|
str(activity_stream_object.object2_id)))
|
||||||
|
|||||||
@@ -1135,7 +1135,7 @@ class RunJob(BaseTask):
|
|||||||
# job and visible inside the proot environment (when enabled).
|
# job and visible inside the proot environment (when enabled).
|
||||||
cp_dir = os.path.join(kwargs['private_data_dir'], 'cp')
|
cp_dir = os.path.join(kwargs['private_data_dir'], 'cp')
|
||||||
if not os.path.exists(cp_dir):
|
if not os.path.exists(cp_dir):
|
||||||
os.mkdir(cp_dir, 0700)
|
os.mkdir(cp_dir, 0o700)
|
||||||
env['ANSIBLE_SSH_CONTROL_PATH'] = os.path.join(cp_dir, '%%h%%p%%r')
|
env['ANSIBLE_SSH_CONTROL_PATH'] = os.path.join(cp_dir, '%%h%%p%%r')
|
||||||
|
|
||||||
# Allow the inventory script to include host variables inline via ['_meta']['hostvars'].
|
# Allow the inventory script to include host variables inline via ['_meta']['hostvars'].
|
||||||
|
|||||||
@@ -172,7 +172,7 @@ def test_utc_until_in_the_past(job_template):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
@mock.patch('awx.main.models.schedules.now', lambda: datetime(2030, 03, 05, tzinfo=pytz.utc))
|
@mock.patch('awx.main.models.schedules.now', lambda: datetime(2030, 3, 5, tzinfo=pytz.utc))
|
||||||
def test_dst_phantom_hour(job_template):
|
def test_dst_phantom_hour(job_template):
|
||||||
# The DST period in the United States begins at 02:00 (2 am) local time, so
|
# The DST period in the United States begins at 02:00 (2 am) local time, so
|
||||||
# the hour from 2:00:00 to 2:59:59 does not exist in the night of the
|
# the hour from 2:00:00 to 2:59:59 does not exist in the night of the
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ def parse_configuration():
|
|||||||
errors.append("Missing TOWER_INVENTORY in environment")
|
errors.append("Missing TOWER_INVENTORY in environment")
|
||||||
if errors:
|
if errors:
|
||||||
raise RuntimeError("\n".join(errors))
|
raise RuntimeError("\n".join(errors))
|
||||||
|
|
||||||
return dict(tower_host=host_name,
|
return dict(tower_host=host_name,
|
||||||
tower_user=username,
|
tower_user=username,
|
||||||
tower_pass=password,
|
tower_pass=password,
|
||||||
@@ -103,9 +103,9 @@ def read_tower_inventory(tower_host, tower_user, tower_pass, inventory, license_
|
|||||||
return response.json()
|
return response.json()
|
||||||
json_reason = response.json()
|
json_reason = response.json()
|
||||||
reason = json_reason.get('detail', 'Retrieving Tower Inventory Failed')
|
reason = json_reason.get('detail', 'Retrieving Tower Inventory Failed')
|
||||||
except requests.ConnectionError, e:
|
except requests.ConnectionError as e:
|
||||||
reason = "Connection to remote host failed: {}".format(e)
|
reason = "Connection to remote host failed: {}".format(e)
|
||||||
except json.JSONDecodeError, e:
|
except json.JSONDecodeError as e:
|
||||||
reason = "Failed to parse json from host: {}".format(e)
|
reason = "Failed to parse json from host: {}".format(e)
|
||||||
raise RuntimeError(reason)
|
raise RuntimeError(reason)
|
||||||
|
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ options = vars(options)
|
|||||||
|
|
||||||
|
|
||||||
if options['preset']:
|
if options['preset']:
|
||||||
print ' Using preset data numbers set ' + str(options['preset'])
|
print(' Using preset data numbers set ' + str(options['preset']))
|
||||||
# Read the numbers of resources from presets file, if provided
|
# Read the numbers of resources from presets file, if provided
|
||||||
presets_filename = os.path.abspath(os.path.join(
|
presets_filename = os.path.abspath(os.path.join(
|
||||||
os.path.dirname(os.path.abspath(__file__)), 'presets.tsv'))
|
os.path.dirname(os.path.abspath(__file__)), 'presets.tsv'))
|
||||||
|
|||||||
16
tools/rdb.py
16
tools/rdb.py
@@ -183,7 +183,7 @@ def listen():
|
|||||||
def _consume(queue):
|
def _consume(queue):
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
sock.bind(('0.0.0.0', 6899))
|
sock.bind(('0.0.0.0', 6899))
|
||||||
print 'listening for rdb notifications on :6899...'
|
print('listening for rdb notifications on :6899...')
|
||||||
while True:
|
while True:
|
||||||
r, w, x = select.select([sock], [], [])
|
r, w, x = select.select([sock], [], [])
|
||||||
for i in r:
|
for i in r:
|
||||||
@@ -201,13 +201,13 @@ def listen():
|
|||||||
if port == 'q':
|
if port == 'q':
|
||||||
break
|
break
|
||||||
port = int(port)
|
port = int(port)
|
||||||
print 'opening telnet session at localhost:%d...' % port
|
print('opening telnet session at localhost:%d...' % port)
|
||||||
telnet(port)
|
telnet(port)
|
||||||
print 'listening for rdb notifications on :6899...'
|
print('listening for rdb notifications on :6899...')
|
||||||
except Empty:
|
except Empty:
|
||||||
pass
|
pass
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print 'got Ctrl-C'
|
print('got Ctrl-C')
|
||||||
queue.put('q')
|
queue.put('q')
|
||||||
|
|
||||||
|
|
||||||
@@ -218,18 +218,18 @@ def telnet(port):
|
|||||||
try:
|
try:
|
||||||
s.connect(('0.0.0.0', port))
|
s.connect(('0.0.0.0', port))
|
||||||
except Exception:
|
except Exception:
|
||||||
print 'unable to connect'
|
print('unable to connect')
|
||||||
return
|
return
|
||||||
print 'connected to 0.0.0.0:%d' % port
|
print('connected to 0.0.0.0:%d' % port)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
socket_list = [sys.stdin, s]
|
socket_list = [sys.stdin, s]
|
||||||
r, w, e = select.select(socket_list , [], [])
|
r, w, e = select.select(socket_list, [], [])
|
||||||
for sock in r:
|
for sock in r:
|
||||||
if sock == s:
|
if sock == s:
|
||||||
data = sock.recv(4096)
|
data = sock.recv(4096)
|
||||||
if not data:
|
if not data:
|
||||||
print 'connection closed'
|
print('connection closed')
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
sys.stdout.write(data)
|
sys.stdout.write(data)
|
||||||
|
|||||||
Reference in New Issue
Block a user