Skip to content

Commit

Permalink
feat(patch_applicator): added return code
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasrothenberger committed Nov 6, 2023
1 parent bf7fc58 commit 2378bd0
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 15 deletions.
18 changes: 14 additions & 4 deletions discopop_library/PatchApplicator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def parse_args() -> PatchApplicatorArguments:

# fmt: off
parser.add_argument("-v", "--verbose", action="store_true",
help="Enable verbose output.")
help="Enable verbose output.")
parser.add_argument('-a', '--apply', nargs='+', default=[], help="Apply the parallelization suggestions with the "
"given ids.")
parser.add_argument('-r', '--rollback', nargs='+', default=[], help="Roll back the application of the "
Expand Down Expand Up @@ -52,9 +52,19 @@ def parse_args() -> PatchApplicatorArguments:
)


def main():
arguments = parse_args()
run(arguments)
def main() -> int:
"""Return values:"
"0: Applied successfully"
"1: Nothing applied"
"2: Some changes applied successfully
"""
retval = 0
try:
arguments = parse_args()
retval = run(arguments)
except FileNotFoundError:
retval = 1
return retval


if __name__ == "__main__":
Expand Down
21 changes: 20 additions & 1 deletion discopop_library/PatchApplicator/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ def apply_patches(
arguments: PatchApplicatorArguments,
applied_suggestions_file: str,
patch_generator_dir: str,
):
) -> int:
"""Return values:"
"0: Applied successfully"
"1: Nothing applied"
"2: Some changes applied successfully"""
retval = -1 # -1 -> nothing seen so far
# get list of applicable suggestions
applicable_suggestions = [name for name in os.listdir(patch_generator_dir)]

Expand All @@ -46,11 +51,25 @@ def apply_patches(
# write updated applied suggestions to file
with open(applied_suggestions_file, "w") as f:
f.write(json.dumps(applied_suggestions))
# update return code
if retval == -1:
retval = 0
if retval == 1:
# no update for retval = 0 necessary
retval = 2
else:
print("Applying suggestion", suggestion_id, "not successful.")
# update return code
if retval == -1:
retval = 1
if retval == 0:
retval = 2
else:
if arguments.verbose:
print("Nothing to apply for suggestion ", suggestion_id)
if retval == -1:
retval = 0
return retval


def __apply_file_patches(
Expand Down
8 changes: 6 additions & 2 deletions discopop_library/PatchApplicator/clear.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def clear_patches(
arguments: PatchApplicatorArguments,
applied_suggestions_file: str,
patch_generator_dir: str,
):
) -> int:
# save the currently applied suggestions. Overwrites old saves
shutil.copyfile(applied_suggestions_file, applied_suggestions_file + ".save")

Expand All @@ -30,4 +30,8 @@ def clear_patches(

# rollback all suggestions in inverse order
applied_suggestions.reverse()
rollback_patches(applied_suggestions, file_mapping, arguments, applied_suggestions_file, patch_generator_dir)
retval = rollback_patches(
applied_suggestions, file_mapping, arguments, applied_suggestions_file, patch_generator_dir
)

return retval
8 changes: 6 additions & 2 deletions discopop_library/PatchApplicator/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def load_patches(
arguments: PatchApplicatorArguments,
applied_suggestions_file: str,
patch_generator_dir: str,
):
) -> int:
# check if a saved configuration exists
if not os.path.exists(applied_suggestions_file + ".save"):
raise FileNotFoundError(
Expand All @@ -31,4 +31,8 @@ def load_patches(
suggestions_to_be_applied = json.loads(f.read())["applied"]

# apply all suggestions
apply_patches(suggestions_to_be_applied, file_mapping, arguments, applied_suggestions_file, patch_generator_dir)
retval = apply_patches(
suggestions_to_be_applied, file_mapping, arguments, applied_suggestions_file, patch_generator_dir
)

return retval
20 changes: 15 additions & 5 deletions discopop_library/PatchApplicator/patch_applicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
from discopop_library.PathManagement.PathManagement import load_file_mapping


def run(arguments: PatchApplicatorArguments):
def run(arguments: PatchApplicatorArguments) -> int:
"""Return values:"
"0: Applied successfully"
"1: Nothing applied"
"2: Some changes applied successfully"""

if arguments.verbose:
print("Started DiscoPoP Patch Applicator...")
print("Working directory: ", os.getcwd())
Expand Down Expand Up @@ -61,16 +66,21 @@ def run(arguments: PatchApplicatorArguments):
)

# handle arguments
retval = 0
if len(arguments.apply) > 0:
apply_patches(arguments.apply, file_mapping, arguments, applied_suggestions_file, patch_generator_dir)
retval = apply_patches(arguments.apply, file_mapping, arguments, applied_suggestions_file, patch_generator_dir)
elif len(arguments.rollback) > 0:
rollback_patches(arguments.rollback, file_mapping, arguments, applied_suggestions_file, patch_generator_dir)
retval = rollback_patches(
arguments.rollback, file_mapping, arguments, applied_suggestions_file, patch_generator_dir
)
elif arguments.clear:
clear_patches(file_mapping, arguments, applied_suggestions_file, patch_generator_dir)
retval = clear_patches(file_mapping, arguments, applied_suggestions_file, patch_generator_dir)
elif arguments.load:
load_patches(file_mapping, arguments, applied_suggestions_file, patch_generator_dir)
retval = load_patches(file_mapping, arguments, applied_suggestions_file, patch_generator_dir)
elif arguments.list:
print("Applied suggestions: ", list_applied_suggestions(arguments, applied_suggestions_file))

if arguments.verbose:
print("Done.")

return retval
21 changes: 20 additions & 1 deletion discopop_library/PatchApplicator/rollback.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ def rollback_patches(
arguments: PatchApplicatorArguments,
applied_suggestions_file: str,
patch_generator_dir: str,
):
) -> int:
"""Return values:"
"0: Applied successfully"
"1: Nothing applied"
"2: Some changes applied successfully"""
retval = -1 # -1 -> nothing seen so far
# get list of applicable suggestions
applicable_suggestions = [name for name in os.listdir(patch_generator_dir)]

Expand All @@ -46,11 +51,25 @@ def rollback_patches(
# write updated applied suggestions to file
with open(applied_suggestions_file, "w") as f:
f.write(json.dumps(applied_suggestions))
# update return code
if retval == -1:
retval = 0
if retval == 1:
# no update for retval = 0 necessary
retval = 2
else:
print("Rollback of suggestion", suggestion_id, "not successful.")
# update return code
if retval == -1:
retval = 1
if retval == 0:
retval = 2
else:
if arguments.verbose:
print("Nothing to rollback for suggestion ", suggestion_id)
if retval == -1:
retval = 0
return retval


def __rollback_file_patches(
Expand Down

0 comments on commit 2378bd0

Please sign in to comment.