Skip to content

Commit

Permalink
Python - Running black on files (awsdocs#5828)
Browse files Browse the repository at this point in the history
running black
  • Loading branch information
ford-at-aws authored Dec 22, 2023
1 parent 6c761f8 commit 4f25bcb
Show file tree
Hide file tree
Showing 12 changed files with 571 additions and 359 deletions.
264 changes: 186 additions & 78 deletions .github/extract-snippets/extract-snippets.py

Large diffs are not rendered by default.

117 changes: 68 additions & 49 deletions .github/sd-cpp-linter/sd_review.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@


class Commit:

def __init__(self, pr_repo: str, head_ref: str, token: str):
self.pr_repo = pr_repo # account/repository
self.head_ref = head_ref
Expand All @@ -64,12 +63,12 @@ def get_commit_diff(self) -> List[unidiff.PatchSet]:
"""Download the compare diff, return a list of PatchedFile"""
diffs = self.get_for_compare("diff", f"/main...{self.head_ref}")

# PatchSet is the easiest way to construct what we want, but the
# diff_line_no property on lines is counted from the top of the
# whole PatchSet, whereas GitHub is expecting the "position"
# property to be line count within each file's diff. So we need to
# do this bit of faff to get a list of file-diffs with
# their own diff_line_no range
# PatchSet is the easiest way to construct what we want, but the
# diff_line_no property on lines is counted from the top of the
# whole PatchSet, whereas GitHub is expecting the "position"
# property to be line count within each file's diff. So we need to
# do this bit of faff to get a list of file-diffs with
# their own diff_line_no range
diff = [unidiff.PatchSet(str(file))[0] for file in unidiff.PatchSet(diffs)]

return diff
Expand Down Expand Up @@ -172,13 +171,14 @@ def make_file_offset_lookup(filenames):


def get_diagnostic_file_path(clang_tidy_diagnostic, build_dir):

# Sometimes, clang-tidy gives us an absolute path, so everything is fine.
# Sometimes however it gives us a relative path that is relative to the
# build directory, so we prepend that.

# Modern clang-tidy
if ("DiagnosticMessage" in clang_tidy_diagnostic) and ("FilePath" in clang_tidy_diagnostic["DiagnosticMessage"]):
if ("DiagnosticMessage" in clang_tidy_diagnostic) and (
"FilePath" in clang_tidy_diagnostic["DiagnosticMessage"]
):
file_path = clang_tidy_diagnostic["DiagnosticMessage"]["FilePath"]
if file_path == "":
return ""
Expand All @@ -187,19 +187,22 @@ def get_diagnostic_file_path(clang_tidy_diagnostic, build_dir):
else:
# Make the relative path absolute with the build dir
if "BuildDirectory" in clang_tidy_diagnostic:
return os.path.normpath(os.path.abspath(os.path.join(clang_tidy_diagnostic["BuildDirectory"],
file_path)))
return os.path.normpath(
os.path.abspath(
os.path.join(clang_tidy_diagnostic["BuildDirectory"], file_path)
)
)
else:
return os.path.normpath(os.path.abspath(file_path))

# Pre-clang-tidy-9 format
elif "FilePath" in clang_tidy_diagnostic:
file_path = clang_tidy_diagnostic["FilePath"]
if file_path == "":
return ""
else:
return os.path.normpath(os.path.abspath(os.path.join(build_dir, file_path)))

else:
return ""

Expand Down Expand Up @@ -425,7 +428,9 @@ def format_notes(notes, offset_lookup):
return code_blocks


def make_comment_from_diagnostic(diagnostic_name, diagnostic, filename, offset_lookup, notes):
def make_comment_from_diagnostic(
diagnostic_name, diagnostic, filename, offset_lookup, notes
):
"""Create a comment from a diagnostic
Comment contains the diagnostic message, plus its name, along with
Expand All @@ -449,9 +454,7 @@ def make_comment_from_diagnostic(diagnostic_name, diagnostic, filename, offset_l
)

if diagnostic["Replacements"]:
code_blocks, end_line = format_diff_line(
diagnostic, offset_lookup, line_num
)
code_blocks, end_line = format_diff_line(diagnostic, offset_lookup, line_num)
else:
# No fixit, so just point at the problem
code_blocks = format_ordinary_line(source_line, line_offset)
Expand All @@ -466,14 +469,15 @@ def make_comment_from_diagnostic(diagnostic_name, diagnostic, filename, offset_l


def comment_diagnostic_to_log(diagnostic, source_line, log_messages, http_prefix):

if 'DiagnosticMessage' in diagnostic:
diagnostic_message = diagnostic['DiagnosticMessage']
message = diagnostic_message['Message'] + " (" + diagnostic['DiagnosticName'] + ")"
file_path = diagnostic_message['FilePath']
if "DiagnosticMessage" in diagnostic:
diagnostic_message = diagnostic["DiagnosticMessage"]
message = (
diagnostic_message["Message"] + " (" + diagnostic["DiagnosticName"] + ")"
)
file_path = diagnostic_message["FilePath"]
else:
message = diagnostic['Message']
file_path = diagnostic['FilePath']
message = diagnostic["Message"]
file_path = diagnostic["FilePath"]

try:
index = file_path.index("cpp/")
Expand All @@ -485,11 +489,18 @@ def comment_diagnostic_to_log(diagnostic, source_line, log_messages, http_prefix
http_path = file_path

log_messages.append(
f"::error ::{message} {http_path}#L{source_line} {file_path}:{source_line}")

f"::error ::{message} {http_path}#L{source_line} {file_path}:{source_line}"
)

def make_comments(diagnostics, diff_lookup, offset_lookup, build_dir, has_compile_commands, http_prefix):

def make_comments(
diagnostics,
diff_lookup,
offset_lookup,
build_dir,
has_compile_commands,
http_prefix,
):
ignored_diagnostics = []
if not has_compile_commands:
# Clang-tidy will not be able to find aws-sdk headers. Ignore the error generated for missing headers.
Expand Down Expand Up @@ -576,7 +587,7 @@ def get_clang_tidy_warnings(
"""Get the clang-tidy warnings"""

if config_file != "":
config = f"-config-file=\"{config_file}\""
config = f'-config-file="{config_file}"'
else:
config = f"-checks={clang_tidy_checks}"

Expand All @@ -597,7 +608,11 @@ def get_clang_tidy_warnings(
try:
with message_group(f"Running:\n\t{command}"):
subprocess.run(
command.split(), capture_output=True, shell=False, check=True, encoding="utf-8"
command.split(),
capture_output=True,
shell=False,
check=True,
encoding="utf-8",
)
except subprocess.CalledProcessError:
pass
Expand All @@ -616,35 +631,35 @@ def get_clang_tidy_warnings(


def main(
repo,
pr_number,
build_dir,
clang_tidy_checks,
clang_tidy_binary,
config_file,
token,
include_pattern,
exclude_pattern,
max_comments,
lgtm_comment_body,
ref, # set during workflow runs, not set for PR
head_ref, # set for PR, not set during workflows
repo,
pr_number,
build_dir,
clang_tidy_checks,
clang_tidy_binary,
config_file,
token,
include_pattern,
exclude_pattern,
max_comments,
lgtm_comment_body,
ref, # set during workflow runs, not set for PR
head_ref, # set for PR, not set during workflows
):
source_actor = os.getenv('GITHUB_ACTOR')
source_actor = os.getenv("GITHUB_ACTOR")
if pr_number is not None and pr_number != "":
pull_request = PullRequest(repo, int(pr_number), token)
diff = pull_request.get_pr_diff()
branch = head_ref.replace("'", "")
elif ref is not None:
branch = ref[ref.rindex("/") + 1:]
branch = ref[ref.rindex("/") + 1 :]
branch = branch.replace("'", "")
commit = Commit(repo, branch, token)
diff = commit.get_commit_diff()
else:
print("No pull request or workflow reference. Unable to review.")
return 0

source_repo = source_actor + repo[repo.find("/"):]
source_repo = source_actor + repo[repo.find("/") :]
http_prefix = f"https://github.com/{source_repo}/tree/{branch}"

changed_files = [filename.target_file[2:] for filename in diff]
Expand Down Expand Up @@ -683,8 +698,12 @@ def main(

with message_group("Creating annotations from warnings"):
log_messages = make_comments(
clang_tidy_warnings["Diagnostics"], diff_lookup, offset_lookup, build_dir,
clang_tidy_warnings[HAS_COMPILE_COMMANDS], http_prefix
clang_tidy_warnings["Diagnostics"],
diff_lookup,
offset_lookup,
build_dir,
clang_tidy_warnings[HAS_COMPILE_COMMANDS],
http_prefix,
)

if not log_messages:
Expand Down Expand Up @@ -847,7 +866,7 @@ def fix_absolute_paths(absolute_paths, base_dir):
lgtm_comment_body=strip_enclosing_quotes(args.lgtm_comment_body),
head_ref=args.head_ref,
ref=args.ref,
)
)

if result == 1:
exit(1)
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,34 @@
import os
import json

s3 = boto3.client('s3')
rekognition = boto3.client('rekognition')
dynamodb = boto3.client('dynamodb')
s3 = boto3.client("s3")
rekognition = boto3.client("rekognition")
dynamodb = boto3.client("dynamodb")


def handler(event, context):
bucket_name = (os.environ['BUCKET_NAME'])
key = event['Records'][0]['s3']['object']['key']
image = {
'S3Object': {
'Bucket': bucket_name,
'Name': key
}
}
bucket_name = os.environ["BUCKET_NAME"]
key = event["Records"][0]["s3"]["object"]["key"]
image = {"S3Object": {"Bucket": bucket_name, "Name": key}}

try:
# Calls Amazon Rekognition DetectLabels API to classify images in Amazon Simple Storage Service (Amazon S3).
response = rekognition.detect_labels(Image=image, MaxLabels=10, MinConfidence=70)
response = rekognition.detect_labels(
Image=image, MaxLabels=10, MinConfidence=70
)

# Print response to console, visible with Amazon CloudWatch logs.
print(key,response["Labels"])
print(key, response["Labels"])

# Write results to JSON file in bucket results folder.
json_labels = json.dumps(response["Labels"])
filename = os.path.basename(key)
filename_prefix = os.path.splitext(filename)[0]
obj = s3.put_object(Body=json_labels, Bucket=bucket_name, Key="results/"+filename_prefix+".json")
obj = s3.put_object(
Body=json_labels,
Bucket=bucket_name,
Key="results/" + filename_prefix + ".json",
)

# Parse the JSON for Amazon DynamoDB.
db_result = []
Expand All @@ -41,16 +43,14 @@ def handler(event, context):
db_result.append(label["Name"])

# Write results to DynamoDB.
dynamodb.put_item(TableName=(os.environ['TABLE_NAME']),
Item = {
'image_name':{'S': key},
'labels':{'S': str(db_result)}
}
dynamodb.put_item(
TableName=(os.environ["TABLE_NAME"]),
Item={"image_name": {"S": key}, "labels": {"S": str(db_result)}},
)

return response

except Exception as e:
print(e)
print("Error processing object {} from bucket {}. ".format(key, bucket_name))
raise e
raise e
11 changes: 6 additions & 5 deletions cpp/example_code/glue/flight_etl_job_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
be processed.
--output_bucket_url An S3 bucket that receives the transformed output data.
"""
args = getResolvedOptions(sys.argv, [
"JOB_NAME", "input_database", "input_table", "output_bucket_url"])
args = getResolvedOptions(
sys.argv, ["JOB_NAME", "input_database", "input_table", "output_bucket_url"]
)
sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
Expand All @@ -36,8 +37,8 @@

# Script generated for node S3 Flight Data.
S3FlightData_node1 = glueContext.create_dynamic_frame.from_catalog(
database=args['input_database'],
table_name=args['input_table'],
database=args["input_database"],
table_name=args["input_table"],
transformation_ctx="S3FlightData_node1",
)

Expand Down Expand Up @@ -71,7 +72,7 @@
frame=ApplyMapping_node2,
connection_type="s3",
format="json",
connection_options={"path": args['output_bucket_url'], "partitionKeys": []},
connection_options={"path": args["output_bucket_url"], "partitionKeys": []},
transformation_ctx="RevisedFlightData_node3",
)

Expand Down
12 changes: 7 additions & 5 deletions gov2/lambda/handlers/lambda_handler_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ def lambda_handler(event, context):
:return: The result of the action.
"""
result = None
action = event.get('action')
if action == 'increment':
result = event.get('number', 0) + 1
logger.info('Calculated result of %s', result)
action = event.get("action")
if action == "increment":
result = event.get("number", 0) + 1
logger.info("Calculated result of %s", result)
else:
logger.error("%s is not a valid action.", action)

response = {'result': result}
response = {"result": result}
return response


# snippet-end:[gov2.lambda.handlers.increment]
Loading

0 comments on commit 4f25bcb

Please sign in to comment.