forked from techwithtim/Python-Scripting-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_game_data2.py
More file actions
112 lines (89 loc) · 3.49 KB
/
get_game_data2.py
File metadata and controls
112 lines (89 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import os
import json
import shutil
from subprocess import PIPE, run
import sys
GAME_DIR_PATTERN = "_game"
GAME_CODE_EXTENSION = ".go"
GAME_COMPILE_COMMANDS = ["go", "build"]
#Walks down through all the directories/Folders to find the ones matching
#GAME_DIR_PATTERN and creates a full path to that dirrectory and outputs
#them as a list
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
#Gets the directory name from the path and strips off the to_strip text
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
#Creates a directory if it does not exist
def create_dir(path):
if not os.path.exists(path):
os.mkdir(path)
#Deletes the directory if it exists in the destination
#Recursively copies the files and directories to the destination
def copy_and_overwrite(source, dest):
if os.path.exists(dest):
shutil.rmtree(dest)
shutil.copytree(source, dest)
#Creates and saves the json metadata file
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#Finds the first one and stops
break
if code_file_name is None:
return
command = GAME_COMPILE_COMMANDS + [code_file_name]
run_command(command, path)
def run_command(command, path):
cwd = os.getcwd()#gets the current working directory
os.chdir(path)#Changes the directory
result = run(command, stdout=PIPE, stdin=PIPE, universal_newlines=True, shell=True)
print("Compile result:", result)
os.chdir(cwd)#changes the directory back
def main(source, target):
#Don't get smart...just use these to get the file location instead fo trying to build them manually
cwd = os.getcwd()#Current Working Directory
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_DIR_PATTERN)
create_dir(target_path)
#Zipz the lists together then assigns the values to src and dest to loop through them
#a = [1, 2, 3]
#b = [a, b, c]
#zip(a,b) = [(1, a), (2, b), (3, c)]
for src, dest in zip(game_paths, new_game_dirs):#Zip creates a touple from equal length arrays
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)
#Prevents the script from running unless this it is run from command line with command line arguments
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)