Skip to content

Commit

Permalink
feat(folder_structure): add proper utilities to setup and teardown
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasrothenberger committed Oct 23, 2024
1 parent 3a92d43 commit e5b7538
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 54 deletions.
23 changes: 3 additions & 20 deletions discopop_explorer/discopop_explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from discopop_explorer.functions.PEGraph.output.json import dump_to_pickled_json
from discopop_explorer.utilities.statistics.collect_statistics import collect_statistics
from discopop_library.ArgumentClasses.GeneralArguments import GeneralArguments # type: ignore
from discopop_library.FolderStructure.setup import setup_explorer
from discopop_library.FolderStructure.teardown import teardown_explorer
from discopop_library.HostpotLoader.HotspotLoaderArguments import HotspotLoaderArguments
from discopop_library.HostpotLoader.HotspotNodeType import HotspotNodeType
from discopop_library.HostpotLoader.HotspotType import HotspotType # type:ignore
Expand Down Expand Up @@ -180,23 +182,7 @@ def run(arguments: ExplorerArguments) -> None:
"""Run the discopop_explorer with the given arguments"""
logger = logging.getLogger("Explorer")

# reset environment, if previous results existed
if os.path.exists(os.path.join(arguments.project_path, "explorer")):
shutil.rmtree(os.path.join(arguments.project_path, "explorer"))
# reset file lock in case of prior crashes
if os.path.exists("next_free_pattern_id.txt.lock"):
os.remove("next_free_pattern_id.txt.lock")
if os.path.exists("next_free_pattern_id.txt"):
os.remove("next_free_pattern_id.txt")
delete_line_mapping(arguments.project_path)

# create explorer directory if not already present
if not os.path.exists(os.path.join(arguments.project_path, "explorer")):
os.mkdir(os.path.join(arguments.project_path, "explorer"))
# create file to store next free pattern ids if not already present
if not os.path.exists("next_free_pattern_id.txt"):
with open("next_free_pattern_id.txt", "w") as f:
f.write(str(0))
setup_explorer(arguments.project_path)

if arguments.enable_profiling_dump_file is not None:
profile = cProfile.Profile()
Expand Down Expand Up @@ -282,9 +268,6 @@ def run(arguments: ExplorerArguments) -> None:
with open(arguments.enable_json_file, "w+") as f:
json.dump(res, f, indent=2, cls=PatternBaseSerializer)

# initialize the line_mapping.json
initialize_line_mapping(load_file_mapping(arguments.file_mapping_file), arguments.project_path)

print("Time taken for pattern detection: {0}".format(end - start))

# demonstration of Microbenchmark possibilities
Expand Down
Empty file.
76 changes: 76 additions & 0 deletions discopop_library/FolderStructure/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# This file is part of the DiscoPoP software (http://www.discopop.tu-darmstadt.de)
#
# Copyright (c) 2020, Technische Universitaet Darmstadt, Germany
#
# This software may be modified and distributed under the terms of
# the 3-Clause BSD License. See the LICENSE file in the package base
# directory for details.

import json
import logging
import os

from discopop_library.FolderStructure.teardown import teardown_explorer
from discopop_library.LineMapping.initialize import initialize_line_mapping
from discopop_library.PathManagement.PathManagement import load_file_mapping

logger = logging.getLogger("FolderStructure").getChild("Setup")


def setup_explorer(path: str = "") -> None:
tmp_logger = logger.getChild("explorer")
tmp_logger.info("Start")

teardown_explorer(path)

# create explorer directory if not already present
if not os.path.exists(os.path.join(path, "explorer")):
os.mkdir(os.path.join(path, "explorer"))
# create file to store next free pattern ids if not already present
if not os.path.exists("next_free_pattern_id.txt"):
with open("next_free_pattern_id.txt", "w") as f:
f.write(str(0))

# initialize the line_mapping.json
initialize_line_mapping(load_file_mapping(os.path.join(path, "FileMapping.txt")), path)

tmp_logger.info("Done")


def setup_patch_generator(path: str = "") -> None:
tmp_logger = logger.getChild("patch_generator")
tmp_logger.info("Start")
patch_generator_dir = os.path.join(path, "patch_generator")
if not os.path.exists(patch_generator_dir):
os.mkdir(patch_generator_dir)

setup_patch_applicator()

tmp_logger.info("Done")


def setup_patch_applicator(path: str = "") -> None:
tmp_logger = logger.getChild("patch_applicator")
tmp_logger.info("Start")
# create a directory for the patch applicator
patch_applicator_dir = os.path.join(path, "patch_applicator")
if not os.path.exists(patch_applicator_dir):
os.mkdir(patch_applicator_dir)

# create a file to store applied suggestions
applied_suggestions_file = os.path.join(patch_applicator_dir, "applied_suggestions.json")
if not os.path.exists(applied_suggestions_file):
with open(applied_suggestions_file, "w+") as f:
f.write(json.dumps({"applied": []}))

tmp_logger.info("Done")


def setup_optimizer(path: str = "") -> None:
tmp_logger = logger.getChild("optimizer")
tmp_logger.info("Start")
optimizer_dir = os.path.join(path, "optimizer")
if not os.path.exists(optimizer_dir):
os.mkdir(optimizer_dir)

tmp_logger.info("Done")
64 changes: 64 additions & 0 deletions discopop_library/FolderStructure/teardown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# This file is part of the DiscoPoP software (http://www.discopop.tu-darmstadt.de)
#
# Copyright (c) 2020, Technische Universitaet Darmstadt, Germany
#
# This software may be modified and distributed under the terms of
# the 3-Clause BSD License. See the LICENSE file in the package base
# directory for details.

import os
import shutil
from discopop_library.LineMapping.delete import delete_line_mapping

import logging

logger = logging.getLogger("FolderStructure").getChild("Teardown")


def teardown_explorer(path: str = "") -> None:
tmp_logger = logger.getChild("explorer")
tmp_logger.info("Start")
# reset environment, if previous results existed
if os.path.exists(os.path.join(path, "explorer")):
shutil.rmtree(os.path.join(path, "explorer"))
# reset file lock in case of prior crashes
if os.path.exists("next_free_pattern_id.txt.lock"):
os.remove("next_free_pattern_id.txt.lock")
if os.path.exists("next_free_pattern_id.txt"):
os.remove("next_free_pattern_id.txt")
delete_line_mapping(path)

teardown_patch_generator(path)
teardown_patch_applicator(path)
teardown_optimizer(path)

tmp_logger.info("Done")


def teardown_patch_generator(path: str = "") -> None:
tmp_logger = logger.getChild("patch_generator")
tmp_logger.info("Start")
patch_generator_dir = os.path.join(path, "patch_generator")
if os.path.exists(patch_generator_dir):
shutil.rmtree(patch_generator_dir)
teardown_patch_applicator(path)
tmp_logger.info("Done")


def teardown_patch_applicator(path: str = "") -> None:
tmp_logger = logger.getChild("patch_applicator")
tmp_logger.info("Start")
patch_applicator_dir = os.path.join(path, "patch_applicator")
if os.path.exists(patch_applicator_dir):
shutil.rmtree(patch_applicator_dir)
tmp_logger.info("Done")


def teardown_optimizer(path: str = "") -> None:
tmp_logger = logger.getChild("optimizer")
tmp_logger.info("Start")
optimizer_dir = os.path.join(path, "optimizer")
if os.path.exists(optimizer_dir):
shutil.rmtree(optimizer_dir)

tmp_logger.info("Done")
14 changes: 2 additions & 12 deletions discopop_library/PatchApplicator/patch_applicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import json
import os

from discopop_library.FolderStructure.setup import setup_patch_applicator
from discopop_library.PatchApplicator.PatchApplicatorArguments import PatchApplicatorArguments
from discopop_library.PatchApplicator.apply import apply_patches
from discopop_library.PatchApplicator.clear import clear_patches
Expand All @@ -30,20 +31,9 @@ def run(arguments: PatchApplicatorArguments) -> int:
print("Working directory: ", os.getcwd())
print(arguments)

# create a directory for the patch applicator
setup_patch_applicator(os.getcwd())
patch_applicator_dir = os.path.join(os.getcwd(), "patch_applicator")
if not os.path.exists(patch_applicator_dir):
if arguments.verbose:
print("Creating patch_applicator directory...")
os.mkdir(patch_applicator_dir)

# create a file to store applied suggestions
applied_suggestions_file = os.path.join(patch_applicator_dir, "applied_suggestions.json")
if not os.path.exists(applied_suggestions_file):
if arguments.verbose:
print("Creating applied_suggestions.json file...")
with open(applied_suggestions_file, "w+") as f:
f.write(json.dumps({"applied": []}))

# load file mapping
file_mapping_path = os.path.join(os.getcwd(), "FileMapping.txt")
Expand Down
22 changes: 3 additions & 19 deletions discopop_library/PatchGenerator/patch_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from typing import Dict

from discopop_library.CodeGenerator.CodeGenerator import from_json_strings
from discopop_library.FolderStructure.setup import setup_patch_generator
from discopop_library.JSONHandler.JSONHandler import read_patterns_from_json_to_json
from discopop_library.PatchGenerator.PatchGeneratorArguments import PatchGeneratorArguments
from discopop_library.PatchGenerator.from_optimizer_output import from_optimizer_output
Expand All @@ -25,26 +26,9 @@ def run(arguments: PatchGeneratorArguments) -> None:

if arguments.verbose:
print("Started DiscoPoP Patch Generator...")
if arguments.verbose:
print("Creating patch_generator directory...")
patch_generator_dir = os.path.join(os.getcwd(), "patch_generator")
if not os.path.exists(patch_generator_dir):
os.mkdir(patch_generator_dir)

# for compatibility reasons, initialize the file to store applied patches if it doesn't exist already
# create a directory for the patch applicator
patch_applicator_dir = os.path.join(os.getcwd(), "patch_applicator")
if not os.path.exists(patch_applicator_dir):
if arguments.verbose:
print("Creating patch_applicator directory...")
os.mkdir(patch_applicator_dir)
# create a file to store applied suggestions
applied_suggestions_file = os.path.join(patch_applicator_dir, "applied_suggestions.json")
if not os.path.exists(applied_suggestions_file):
if arguments.verbose:
print("Creating applied_suggestions.json file...")
with open(applied_suggestions_file, "w+") as f:
f.write(json.dumps({"applied": []}))
setup_patch_generator(os.getcwd())
patch_generator_dir = os.path.join(os.getcwd(), "patch_generator")

# get pattern file to load
if arguments.add_from_json != "None":
Expand Down
7 changes: 4 additions & 3 deletions discopop_library/discopop_optimizer/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from sympy import Float, Symbol # type: ignore

from discopop_library.CodeGenerator.CodeGenerator import from_json_strings
from discopop_library.FolderStructure.setup import setup_optimizer
from discopop_library.HostpotLoader.HotspotLoaderArguments import HotspotLoaderArguments
from discopop_library.JSONHandler.JSONHandler import read_patterns_from_json_to_json
from discopop_library.PatchGenerator.PatchGeneratorArguments import PatchGeneratorArguments
Expand Down Expand Up @@ -90,10 +91,10 @@ def run_passive_optimizer(arguments: OptimizerArguments) -> None:
if arguments.verbose:
print("Started DiscoPoP Optimizer...")
print("Creating optimizer directory...")
optimizer_dir = os.path.join(os.getcwd(), "optimizer")
if not os.path.exists(optimizer_dir):
os.mkdir(optimizer_dir)

setup_optimizer(os.getcwd())

optimizer_dir = os.path.join(os.getcwd(), "optimizer")
explorer_dir = os.path.join(os.getcwd(), "explorer")
profiler_dir = os.path.join(os.getcwd(), "profiler")
pattern_file_path = os.path.join(explorer_dir, "patterns.json")
Expand Down

0 comments on commit e5b7538

Please sign in to comment.