From 7a75b0bbf4418b6a6cc14fd2263ded2d35d1e9e7 Mon Sep 17 00:00:00 2001 From: Snickerbones Date: Tue, 20 Feb 2024 19:09:46 -0500 Subject: [PATCH 1/3] Fixed a small typo within project.txt --- project.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project.txt b/project.txt index bd4fd24..053ef18 100644 --- a/project.txt +++ b/project.txt @@ -1,7 +1,7 @@ Assumptions: - data directory contains many files and directories -- you are only interested in the games contaiend in this directory +- you are only interested in the games contained in this directory - each game is stored in a directory that contains the word "game" - each game directory contains a single .go file that must be compiled before it can be run From f03586fec28b23ed34379974ba9dc0d9ac739efb Mon Sep 17 00:00:00 2001 From: Snickerbones Date: Tue, 20 Feb 2024 19:43:54 -0500 Subject: [PATCH 2/3] Starting point for this project, I've deleted the existing code so I can start from scratch --- ANSWER.py | 109 +++++++++++++++++++++++++++++++++++++++++++++++ get_game_data.py | 109 +++++------------------------------------------ 2 files changed, 119 insertions(+), 99 deletions(-) create mode 100644 ANSWER.py diff --git a/ANSWER.py b/ANSWER.py new file mode 100644 index 0000000..de56f0b --- /dev/null +++ b/ANSWER.py @@ -0,0 +1,109 @@ +import os +import json +import shutil +from subprocess import PIPE, run +import sys + + +GAME_DIR_PATTERN = "game" +GAME_CODE_EXTENSION = ".go" +GAME_COMPILE_COMMAND = ["go", "build"] + + +def find_all_game_paths(source): + game_paths = [] + + for root, dirs, files in os.walk(source): + for directory in dirs: + if GAME_DIR_PATTERN in directory.lower(): + path = os.path.join(source, directory) + game_paths.append(path) + + break + + return game_paths + + +def get_name_from_paths(paths, to_strip): + new_names = [] + for path in paths: + _, dir_name = os.path.split(path) + new_dir_name = dir_name.replace(to_strip, "") + new_names.append(new_dir_name) + + return new_names + + +def create_dir(path): + if not os.path.exists(path): + os.mkdir(path) + + +def copy_and_overwrite(source, dest): + if os.path.exists(dest): + shutil.rmtree(dest) + shutil.copytree(source, dest) + + +def make_json_metadata_file(path, game_dirs): + data = { + "gameNames": game_dirs, + "numberOfGames": len(game_dirs) + } + + with open(path, "w") as f: + json.dump(data, f) + + +def compile_game_code(path): + code_file_name = None + for root, dirs, files in os.walk(path): + for file in files: + if file.endswith(GAME_CODE_EXTENSION): + code_file_name = file + break + + break + + if code_file_name is None: + return + + command = GAME_COMPILE_COMMAND + [code_file_name] + run_command(command, path) + + +def run_command(command, path): + cwd = os.getcwd() + os.chdir(path) + + result = run(command, stdout=PIPE, stdin=PIPE, universal_newlines=True) + print("compile result", result) + + os.chdir(cwd) + +def main(source, target): + cwd = os.getcwd() + source_path = os.path.join(cwd, source) + target_path = os.path.join(cwd, target) + + game_paths = find_all_game_paths(source_path) + new_game_dirs = get_name_from_paths(game_paths, "_game") + + create_dir(target_path) + + for src, dest in zip(game_paths, new_game_dirs): + dest_path = os.path.join(target_path, dest) + copy_and_overwrite(src, dest_path) + compile_game_code(dest_path) + + json_path = os.path.join(target_path, "metadata.json") + make_json_metadata_file(json_path, new_game_dirs) + + +if __name__ == "__main__": + args = sys.argv + if len(args) != 3: + raise Exception("You must pass a source and target directory - only.") + + source, target = args[1:] + main(source, target) diff --git a/get_game_data.py b/get_game_data.py index a59e4a6..13e4887 100644 --- a/get_game_data.py +++ b/get_game_data.py @@ -1,109 +1,20 @@ +# importing a bunch of modules for what we're going to use import os import json import shutil from subprocess import PIPE, run import sys +''' +Info on the imported modules: -GAME_DIR_PATTERN = "game" -GAME_CODE_EXTENSION = ".go" -GAME_COMPILE_COMMAND = ["go", "build"] +os - operating system +json - how we're going to work with json files +shutil - using copy/overwrite operations +subprocess/PIPE , subprocess/run - allows up to run any terminal command that we want + which we can use to compile and run the GO code +sys - allows us access to the command line arguments +''' -def find_all_game_paths(source): - game_paths = [] - for root, dirs, files in os.walk(source): - for directory in dirs: - if GAME_DIR_PATTERN in directory.lower(): - path = os.path.join(source, directory) - game_paths.append(path) - - break - - return game_paths - - -def get_name_from_paths(paths, to_strip): - new_names = [] - for path in paths: - _, dir_name = os.path.split(path) - new_dir_name = dir_name.replace(to_strip, "") - new_names.append(new_dir_name) - - return new_names - - -def create_dir(path): - if not os.path.exists(path): - os.mkdir(path) - - -def copy_and_overwrite(source, dest): - if os.path.exists(dest): - shutil.rmtree(dest) - shutil.copytree(source, dest) - - -def make_json_metadata_file(path, game_dirs): - data = { - "gameNames": game_dirs, - "numberOfGames": len(game_dirs) - } - - with open(path, "w") as f: - json.dump(data, f) - - -def compile_game_code(path): - code_file_name = None - for root, dirs, files in os.walk(path): - for file in files: - if file.endswith(GAME_CODE_EXTENSION): - code_file_name = file - break - - break - - if code_file_name is None: - return - - command = GAME_COMPILE_COMMAND + [code_file_name] - run_command(command, path) - - -def run_command(command, path): - cwd = os.getcwd() - os.chdir(path) - - result = run(command, stdout=PIPE, stdin=PIPE, universal_newlines=True) - print("compile result", result) - - os.chdir(cwd) - -def main(source, target): - cwd = os.getcwd() - source_path = os.path.join(cwd, source) - target_path = os.path.join(cwd, target) - - game_paths = find_all_game_paths(source_path) - new_game_dirs = get_name_from_paths(game_paths, "_game") - - create_dir(target_path) - - for src, dest in zip(game_paths, new_game_dirs): - dest_path = os.path.join(target_path, dest) - copy_and_overwrite(src, dest_path) - compile_game_code(dest_path) - - json_path = os.path.join(target_path, "metadata.json") - make_json_metadata_file(json_path, new_game_dirs) - - -if __name__ == "__main__": - args = sys.argv - if len(args) != 3: - raise Exception("You must pass a source and target directory - only.") - - source, target = args[1:] - main(source, target) From 5f02544f6c6eaac9c0310668bc5ad83e8a0c3d7a Mon Sep 17 00:00:00 2001 From: Snickerbones Date: Thu, 22 Feb 2024 12:01:50 -0500 Subject: [PATCH 3/3] Added more functionality --- get_game_data.py | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/get_game_data.py b/get_game_data.py index 13e4887..9470dee 100644 --- a/get_game_data.py +++ b/get_game_data.py @@ -16,5 +16,86 @@ sys - allows us access to the command line arguments ''' +'''We'll be passing arguments for our final project result +python get_game_data.py /data /games_folder +'/data' will be our source directory argument +'/games_folder' will be our dest directory argument +''' +#We create this constant variable to specify the key we're +# looking for when searching directories containing games +GAME_DIR_PATTERN = "game" + +def find_all_game_paths(source): + game_paths = [] + + # os.walk(), recursively looks through root directory, directories, and files + # checkout https://docs.python.org/3/library/os.html for more info + for root, dirs, files in os.walk(source): + for directory in dirs: + if GAME_DIR_PATTERN in directory.lower(): + path = os.path.join(source, directory) + game_paths.append(path) + + # We only need this to loop once, so we include a break + break + return game_paths + +def get_name_from_paths(paths, to_strip): + new_names = [] + + # os.path.split within this for loop will return both + # the before path and the path we want. + # Since we don't care about the before path, we just use an _ here + for path in paths: + _, dir_name = os.path.split(path) + new_dir_name = dir_name.replace(to_strip, "") + new_names.append(new_dir_name) + + return new_names + +def create_dir(path): + if not os.path.exists(path): + os.mkdir(path) + +def clean_up(path): + if os.path.exists(path): + os.rmdir(path) + +def main(source, dest): + # Using the os module to get a pwd, then we join it + # with the destination directory + cwd = os.getcwd() + source_path = os.path.join(cwd, source) + dest_path = os.path.join(cwd, dest) + + game_paths = find_all_game_paths(source_path) + #print(game_paths) + new_game_dirs = get_name_from_paths(game_paths, "_game") + #new_game_dirs = get_name_from_paths(game_paths, GAME_DIR_PATTERN) + print("Game directories: ", new_game_dirs) + + create_dir(dest_path) + +def copy_and_overwrite(source, dest): + + +# Only want to execute the main script, if we're running this python file directly +# The following line checks if we ran the file directly +# Otherwise, Everything will be run, even if we did not want it to +if __name__ == "__main__": + args = sys.argv + print("arguments: ", args) + + #Make sure we have a valid amount of arguments + if len(args) != 3: + raise Exception('You only need to pass a source and destination directory as arguments') + + #Save the arguments + source, dest = args[1:] + clean_up(args[2]) + main(source, dest) + + +