From d4bfac572e335b0ff0b18998e6ac495cf331637d Mon Sep 17 00:00:00 2001 From: Ayane Yokoya Date: 2023年7月10日 13:37:05 -0400 Subject: [PATCH 01/11] replaced original script to begin from scratch --- get_game_data.py | 114 ++-------------------------------------------- get_game_data1.py | 109 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 109 deletions(-) create mode 100644 get_game_data1.py diff --git a/get_game_data.py b/get_game_data.py index a59e4a6..8a018de 100644 --- a/get_game_data.py +++ b/get_game_data.py @@ -1,109 +1,5 @@ -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) +import os +import json # to work w/ json files +import shutil # will allow copying & overriding operations +from subprocess import PIPE, run # will allow running almost any terminal command +import sys # access to command line arguments diff --git a/get_game_data1.py b/get_game_data1.py new file mode 100644 index 0000000..a59e4a6 --- /dev/null +++ b/get_game_data1.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) From c5329fe3c0e4f76708673a8c3dd051994e6a6f32 Mon Sep 17 00:00:00 2001 From: Ayane Yokoya Date: 2023年7月10日 13:41:43 -0400 Subject: [PATCH 02/11] added main --- get_game_data.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/get_game_data.py b/get_game_data.py index 8a018de..55b8b99 100644 --- a/get_game_data.py +++ b/get_game_data.py @@ -2,4 +2,14 @@ import json # to work w/ json files import shutil # will allow copying & overriding operations from subprocess import PIPE, run # will allow running almost any terminal command -import sys # access to command line arguments +import sys # access to command line arguments + +# checks that file was ran directly +# won't execute anything if a function or class were imported from this file +if __name__ == "__main__": + args = sys.argv + if len(args) != 3: + raise Exception("You must pass a source and target directory only.") + + # strip off name of python file & get two arguments to be stored into separate variables + source, target = args[1:] \ No newline at end of file From 1da9d2d34c709486e9d26186859b9734d73db487 Mon Sep 17 00:00:00 2001 From: Ayane Yokoya Date: 2023年7月10日 13:44:36 -0400 Subject: [PATCH 03/11] created main method which takes in source, target --- get_game_data.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/get_game_data.py b/get_game_data.py index 55b8b99..e1b303b 100644 --- a/get_game_data.py +++ b/get_game_data.py @@ -4,6 +4,14 @@ from subprocess import PIPE, run # will allow running almost any terminal command import sys # access to command line arguments +def main(source, target): + cwd = os.getcwd() + # will join the path based on operating system + source_path = os.path.join(cwd, source) + target_path = os.path.join(cwd, target) + + + # checks that file was ran directly # won't execute anything if a function or class were imported from this file if __name__ == "__main__": @@ -12,4 +20,5 @@ raise Exception("You must pass a source and target directory only.") # strip off name of python file & get two arguments to be stored into separate variables - source, target = args[1:] \ No newline at end of file + source, target = args[1:] + main(source, target) \ No newline at end of file From 6093ea43770861d9ea9241f07813c3a6f5933ab0 Mon Sep 17 00:00:00 2001 From: Ayane Yokoya Date: 2023年7月10日 13:47:57 -0400 Subject: [PATCH 04/11] created method to find game paths --- get_game_data.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/get_game_data.py b/get_game_data.py index e1b303b..8bce908 100644 --- a/get_game_data.py +++ b/get_game_data.py @@ -4,6 +4,22 @@ from subprocess import PIPE, run # will allow running almost any terminal command import sys # access to command line arguments +GAME_DIR_PATTERN = "game" + +def find_all_game_paths(source): + game_paths = [] + + # will walk recursively through source directory + 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 main(source, target): cwd = os.getcwd() # will join the path based on operating system From f4b3c3bb6ee0974891a25d84ff61e953faa61f64 Mon Sep 17 00:00:00 2001 From: Ayane Yokoya Date: 2023年7月10日 13:55:19 -0400 Subject: [PATCH 05/11] implemented a method to get filename & strip --- get_game_data.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/get_game_data.py b/get_game_data.py index 8bce908..0ee4f43 100644 --- a/get_game_data.py +++ b/get_game_data.py @@ -20,13 +20,31 @@ def find_all_game_paths(source): 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_dir_name + +def create_dir(path): + if not os.path.exisst(path): + os.mkdir(path) + def main(source, target): cwd = os.getcwd() # will join the path based on operating system source_path = os.path.join(cwd, source) target_path = os.path.join(cwd, target) + game_paths = find_all_game_paths(source_path) + + # will give just the directory name but removes "game" + new_game_dirs = get_name_from_paths(game_paths, "game") + create_dir(target_path) # checks that file was ran directly # won't execute anything if a function or class were imported from this file From 0d7be094a74511f1ac51425c79fd741f15ca7632 Mon Sep 17 00:00:00 2001 From: Ayane Yokoya Date: 2023年7月10日 13:59:51 -0400 Subject: [PATCH 06/11] implemented a recursive method: copy & overwrite --- get_game_data.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/get_game_data.py b/get_game_data.py index 0ee4f43..4220eaf 100644 --- a/get_game_data.py +++ b/get_game_data.py @@ -33,18 +33,30 @@ def create_dir(path): if not os.path.exisst(path): os.mkdir(path) +# recursive copy method +def copy_and_overwrite(source, destination): + if os.path.exists(destination): + shutil.rmtree(destination) + shutil.copytree(source, destination) + def main(source, target): cwd = os.getcwd() # will join the path based on operating system source_path = os.path.join(cwd, source) target_path = os.path.join(cwd, target) + # finds game paths (directories) from source directory game_paths = find_all_game_paths(source_path) - # will give just the directory name but removes "game" - new_game_dirs = get_name_from_paths(game_paths, "game") + new_game_dirs = get_name_from_paths(game_paths, "_game") create_dir(target_path) + + # zip takes matching elements from two arrays and combine them into a tuple + for src, dest in zip(game_paths, new_game_dirs): + dest_path = os.path.join(target_path, dest) + copy_and_overwrite(src, dest_path) + # checks that file was ran directly # won't execute anything if a function or class were imported from this file From ce2e11c36df16e7c82a940d793d676877160bdab Mon Sep 17 00:00:00 2001 From: Ayane Yokoya Date: 2023年7月10日 14:03:12 -0400 Subject: [PATCH 07/11] debugging code --- get_game_data.py | 4 ++-- target/hello_world/main.go | 7 +++++++ target/rock_paper_scissors/code.go | 7 +++++++ target/simon_says/file.go | 7 +++++++ 4 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 target/hello_world/main.go create mode 100644 target/rock_paper_scissors/code.go create mode 100644 target/simon_says/file.go diff --git a/get_game_data.py b/get_game_data.py index 4220eaf..b4452f5 100644 --- a/get_game_data.py +++ b/get_game_data.py @@ -27,10 +27,10 @@ def get_name_from_paths(paths, to_strip): new_dir_name = dir_name.replace(to_strip, '') new_names.append(new_dir_name) - return new_dir_name + return new_names def create_dir(path): - if not os.path.exisst(path): + if not os.path.exists(path): os.mkdir(path) # recursive copy method diff --git a/target/hello_world/main.go b/target/hello_world/main.go new file mode 100644 index 0000000..8c3a830 --- /dev/null +++ b/target/hello_world/main.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("Hello world") +} diff --git a/target/rock_paper_scissors/code.go b/target/rock_paper_scissors/code.go new file mode 100644 index 0000000..089d8e5 --- /dev/null +++ b/target/rock_paper_scissors/code.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("Rock paper scissors"); +} \ No newline at end of file diff --git a/target/simon_says/file.go b/target/simon_says/file.go new file mode 100644 index 0000000..84da72e --- /dev/null +++ b/target/simon_says/file.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("Simon says") +} From 7bd3573ad1567051cd058592e6017664fcbed455 Mon Sep 17 00:00:00 2001 From: Ayane Yokoya Date: 2023年7月10日 14:05:19 -0400 Subject: [PATCH 08/11] created method to make json metadata file --- get_game_data.py | 9 +++++++++ target/hello_world/main.go | 7 ------- target/rock_paper_scissors/code.go | 7 ------- target/simon_says/file.go | 7 ------- 4 files changed, 9 insertions(+), 21 deletions(-) delete mode 100644 target/hello_world/main.go delete mode 100644 target/rock_paper_scissors/code.go delete mode 100644 target/simon_says/file.go diff --git a/get_game_data.py b/get_game_data.py index b4452f5..ad77378 100644 --- a/get_game_data.py +++ b/get_game_data.py @@ -39,6 +39,15 @@ def copy_and_overwrite(source, destination): shutil.rmtree(destination) shutil.copytree(source, destination) +def make_json_metadata_file(path, game_dirs): + data = { + "gameNames": game_dirs, + "numberOfGames": len(game_dirs) + } + # using "with" will automatically close the file + with open(path, "w") as f: + json.dump(data, f) + def main(source, target): cwd = os.getcwd() # will join the path based on operating system diff --git a/target/hello_world/main.go b/target/hello_world/main.go deleted file mode 100644 index 8c3a830..0000000 --- a/target/hello_world/main.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import "fmt" - -func main() { - fmt.Println("Hello world") -} diff --git a/target/rock_paper_scissors/code.go b/target/rock_paper_scissors/code.go deleted file mode 100644 index 089d8e5..0000000 --- a/target/rock_paper_scissors/code.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import "fmt" - -func main() { - fmt.Println("Rock paper scissors"); -} \ No newline at end of file diff --git a/target/simon_says/file.go b/target/simon_says/file.go deleted file mode 100644 index 84da72e..0000000 --- a/target/simon_says/file.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import "fmt" - -func main() { - fmt.Println("Simon says") -} From d8ac5ecc0aee319517cf2e08e0a92577827a17e5 Mon Sep 17 00:00:00 2001 From: Ayane Yokoya Date: 2023年7月10日 14:06:43 -0400 Subject: [PATCH 09/11] added json metadata file method into main --- get_game_data.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/get_game_data.py b/get_game_data.py index ad77378..7793fb0 100644 --- a/get_game_data.py +++ b/get_game_data.py @@ -65,7 +65,9 @@ def main(source, target): for src, dest in zip(game_paths, new_game_dirs): dest_path = os.path.join(target_path, dest) copy_and_overwrite(src, dest_path) - + + json_path = os.path.join(target_path, "metadata.json") + make_json_metadata_file(json_path, new_game_dirs) # checks that file was ran directly # won't execute anything if a function or class were imported from this file From 9757b4d59e5075ae82449f679e28801cb6e17b65 Mon Sep 17 00:00:00 2001 From: Ayane Yokoya Date: 2023年7月10日 14:14:51 -0400 Subject: [PATCH 10/11] ran code to test --- target/hello_world/main.go | 7 +++++++ target/metadata.json | 1 + target/rock_paper_scissors/code.go | 7 +++++++ target/simon_says/file.go | 7 +++++++ 4 files changed, 22 insertions(+) create mode 100644 target/hello_world/main.go create mode 100644 target/metadata.json create mode 100644 target/rock_paper_scissors/code.go create mode 100644 target/simon_says/file.go diff --git a/target/hello_world/main.go b/target/hello_world/main.go new file mode 100644 index 0000000..8c3a830 --- /dev/null +++ b/target/hello_world/main.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("Hello world") +} diff --git a/target/metadata.json b/target/metadata.json new file mode 100644 index 0000000..7800cd7 --- /dev/null +++ b/target/metadata.json @@ -0,0 +1 @@ +{"gameNames": ["rock_paper_scissors", "hello_world", "simon_says"], "numberOfGames": 3} \ No newline at end of file diff --git a/target/rock_paper_scissors/code.go b/target/rock_paper_scissors/code.go new file mode 100644 index 0000000..089d8e5 --- /dev/null +++ b/target/rock_paper_scissors/code.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("Rock paper scissors"); +} \ No newline at end of file diff --git a/target/simon_says/file.go b/target/simon_says/file.go new file mode 100644 index 0000000..84da72e --- /dev/null +++ b/target/simon_says/file.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("Simon says") +} From 0982bf8b278fb675bd6b197558aabb229288fd09 Mon Sep 17 00:00:00 2001 From: Ayane Yokoya Date: 2023年7月10日 14:15:49 -0400 Subject: [PATCH 11/11] deleted original script file --- get_game_data.py | 27 ++++++++++++ get_game_data1.py | 109 ---------------------------------------------- 2 files changed, 27 insertions(+), 109 deletions(-) delete mode 100644 get_game_data1.py diff --git a/get_game_data.py b/get_game_data.py index 7793fb0..b472243 100644 --- a/get_game_data.py +++ b/get_game_data.py @@ -5,6 +5,8 @@ import sys # access to command line arguments GAME_DIR_PATTERN = "game" +GAME_CODE_EXTENSION = ".go" +GAME_COMPILE_COMMAND = ["go", "build"] def find_all_game_paths(source): game_paths = [] @@ -47,6 +49,30 @@ def make_json_metadata_file(path, game_dirs): # using "with" will automatically close the file with open(path, "w") as f: json.dump(data, f) + +# method to compile the code inside directory +def compile_game_code(path): + code_file_name = None + for root, dirs, files in os.walk(path): + for file in files: + # checks if file is a .go file + 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] + +def run_command(command, path): + cwd = os.getcwd() + os.chdir(path) + + # stdout & stdin: location where command is accepting input & dumping output + # PIPE makes a bridge between python code and process used to run command + result = run(command, stdout=PIPE, stdin=PIPE, universal_newlines=True) def main(source, target): cwd = os.getcwd() @@ -65,6 +91,7 @@ def main(source, target): 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) diff --git a/get_game_data1.py b/get_game_data1.py deleted file mode 100644 index a59e4a6..0000000 --- a/get_game_data1.py +++ /dev/null @@ -1,109 +0,0 @@ -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)

AltStyle によって変換されたページ (->オリジナル) /