Skip to content

Commit

Permalink
Merge pull request #229 from Netflix/py3support_precommit_black
Browse files Browse the repository at this point in the history
Py3 support ; precommit; black
  • Loading branch information
castrapel committed Aug 27, 2019
2 parents 239da52 + 7dc3c57 commit 34a7c54
Show file tree
Hide file tree
Showing 25 changed files with 2,663 additions and 1,455 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -14,3 +14,4 @@ dist
.newt.yml
tox.ini
test-reports/*
config.json
28 changes: 28 additions & 0 deletions .pre-commit-config.yaml
@@ -0,0 +1,28 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v1.4.0 # Use the ref you want to point at
hooks:
- id: trailing-whitespace
- id: autopep8-wrapper
- id: check-ast
- id: check-case-conflict
- id: check-yaml
- id: flake8
args: []
- id: pretty-format-json
args: ["--autofix"]

- repo: https://github.com/ambv/black
rev: stable
hooks:
- id: black
language_version: python3.7

- repo: local
hooks:
- id: python-bandit-vulnerability-check
name: bandit
entry: bandit
args: ['--ini', 'tox.ini', '-r', 'consoleme']
language: system
pass_filenames: false
7 changes: 4 additions & 3 deletions .travis.yml
Expand Up @@ -2,7 +2,7 @@ language: python

matrix:
include:
- python: "2.7"
- python: "3.7"

# horrible thing to fix cloudaux/boto/google issue
before_install:
Expand All @@ -24,5 +24,6 @@ after_success:

notifications:
email:
tmcpeak@netflix.com
pkelley@netflix.com
- tmcpeak@netflix.com
- ccastrapel@netflix.com

22 changes: 13 additions & 9 deletions repokid/__init__.py
Expand Up @@ -19,7 +19,7 @@

import import_string

__version__ = '0.9.5'
__version__ = "0.10.0"


def init_config():
Expand All @@ -33,12 +33,14 @@ def init_config():
Returns:
None
"""
load_config_paths = [os.path.join(os.getcwd(), 'config.json'),
'/etc/repokid/config.json',
'/apps/repokid/config.json']
load_config_paths = [
os.path.join(os.getcwd(), "config.json"),
"/etc/repokid/config.json",
"/apps/repokid/config.json",
]
for path in load_config_paths:
try:
with open(path, 'r') as f:
with open(path, "r") as f:
print("Loaded config from {}".format(path))
return json.load(f)

Expand All @@ -59,12 +61,12 @@ def init_logging():
None
"""
if CONFIG:
logging.config.dictConfig(CONFIG['logging'])
logging.config.dictConfig(CONFIG["logging"])

# these loggers are very noisy
suppressed_loggers = [
'botocore.vendored.requests.packages.urllib3.connectionpool',
'urllib3'
"botocore.vendored.requests.packages.urllib3.connectionpool",
"urllib3",
]

for logger in suppressed_loggers:
Expand Down Expand Up @@ -94,7 +96,9 @@ def _get_hooks(hooks_list):
if hasattr(func, "_implements_hook"):
# append to the dictionary in whatever order we see them, we'll sort later. Dictionary value should be
# a list of tuples (priority, function)
hooks[func._implements_hook['hook_name']].append((func._implements_hook['priority'], func))
hooks[func._implements_hook["hook_name"]].append(
(func._implements_hook["priority"], func)
)

# sort by priority
for k in hooks.keys():
Expand Down
92 changes: 63 additions & 29 deletions repokid/cli/dispatcher_cli.py
Expand Up @@ -10,8 +10,17 @@


class Message(object):
def __init__(self, command, account, role_name, respond_channel, respond_user=None, requestor=None, reason=None,
selection=None):
def __init__(
self,
command,
account,
role_name,
respond_channel,
respond_user=None,
requestor=None,
reason=None,
selection=None,
):
self.command = command
self.account = account
self.role_name = role_name
Expand All @@ -38,32 +47,37 @@ def make_message(self, data):


def get_failure_message(channel=None, message=None):
return {'channel': channel,
'message': message,
'title': 'Repokid Failure'}
return {"channel": channel, "message": message, "title": "Repokid Failure"}


@sts_conn('sqs')
@sts_conn("sqs")
def delete_message(receipt_handle, client=None):
client.delete_message(QueueUrl=CONFIG['dispatcher']['to_rr_queue'], ReceiptHandle=receipt_handle)
client.delete_message(
QueueUrl=CONFIG["dispatcher"]["to_rr_queue"], ReceiptHandle=receipt_handle
)


@sts_conn('sqs')
@sts_conn("sqs")
def receive_message(client=None):
return client.receive_message(QueueUrl=CONFIG['dispatcher']['to_rr_queue'], MaxNumberOfMessages=1,
WaitTimeSeconds=10)
return client.receive_message(
QueueUrl=CONFIG["dispatcher"]["to_rr_queue"],
MaxNumberOfMessages=1,
WaitTimeSeconds=10,
)


@sts_conn('sns')
@sts_conn("sns")
def send_message(message_dict, client=None):
client.publish(TopicArn=CONFIG['dispatcher']['from_rr_sns'], Message=json.dumps(message_dict))
client.publish(
TopicArn=CONFIG["dispatcher"]["from_rr_sns"], Message=json.dumps(message_dict)
)


@contextlib.contextmanager
def message_context(message_object, connection):
try:
receipt_handle = message_object['Messages'][0]['ReceiptHandle']
yield json.loads(message_object['Messages'][0]['Body'])
receipt_handle = message_object["Messages"][0]["ReceiptHandle"]
yield json.loads(message_object["Messages"][0]["Body"])
except KeyError:
# we might not actually have a message
yield None
Expand All @@ -73,21 +87,26 @@ def message_context(message_object, connection):


all_funcs = inspect.getmembers(repokid.dispatcher, inspect.isfunction)
RESPONDER_FUNCTIONS = {func[1]._implements_command: func[1] for func in all_funcs if
hasattr(func[1], '_implements_command')}
RESPONDER_FUNCTIONS = {
func[1]._implements_command: func[1]
for func in all_funcs
if hasattr(func[1], "_implements_command")
}


def main():
dynamo_table = dynamo.dynamo_get_or_create_table(**CONFIG['dynamo_db'])
dynamo_table = dynamo.dynamo_get_or_create_table(**CONFIG["dynamo_db"])
message_schema = MessageSchema()

connection = {'assume_role': CONFIG['dispatcher'].get('assume_role', None),
'session_name': CONFIG['dispatcher'].get('session_name', 'Repokid'),
'region': CONFIG['dispatcher'].get('region', 'us-west-2')}
connection = {
"assume_role": CONFIG["dispatcher"].get("assume_role", None),
"session_name": CONFIG["dispatcher"].get("session_name", "Repokid"),
"region": CONFIG["dispatcher"].get("region", "us-west-2"),
}

while True:
message = receive_message(**connection)
if not message or 'Messages' not in message:
if not message or "Messages" not in message:
continue

with message_context(message, connection) as msg:
Expand All @@ -98,22 +117,37 @@ def main():
command_data = parsed_msg.data

if parsed_msg.errors:
failure_message = get_failure_message(channel=command_data.get('respond_channel', None),
message='Malformed message: {}'.format(parsed_msg.errors))
failure_message = get_failure_message(
channel=command_data.get("respond_channel", None),
message="Malformed message: {}".format(parsed_msg.errors),
)
send_message(failure_message, **connection)
continue

try:
return_val = RESPONDER_FUNCTIONS[command_data.command](dynamo_table, command_data)
return_val = RESPONDER_FUNCTIONS[command_data.command](
dynamo_table, command_data
)
except KeyError:
failure_message = get_failure_message(channel=command_data.respond_channel,
message='Unknown function {}'.format(command_data.command))
failure_message = get_failure_message(
channel=command_data.respond_channel,
message="Unknown function {}".format(command_data.command),
)
send_message(failure_message, **connection)
continue

send_message({'message': '@{} {}'.format(command_data.respond_user, return_val.return_message),
'channel': command_data.respond_channel,
'title': 'Repokid Success' if return_val.successful else 'Repokid Failure'}, **connection)
send_message(
{
"message": "@{} {}".format(
command_data.respond_user, return_val.return_message
),
"channel": command_data.respond_channel,
"title": "Repokid Success"
if return_val.successful
else "Repokid Failure",
},
**connection
)


if __name__ == "__main__":
Expand Down

0 comments on commit 34a7c54

Please sign in to comment.