-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
210 lines (169 loc) · 5.52 KB
/
tasks.py
File metadata and controls
210 lines (169 loc) · 5.52 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import os
import regex
import shutil
import stat
import invoke
import jinja2
import yaml
import util
USERNAME = os.getenv("USERNAME", "")
USERCODE_PATH = os.path.join(
"C:\\", "Users", USERNAME, "projects", "eco-mods-public", "Mods", "UserCode"
)
BUNWULF_CONSTRUCTION_PATH = os.path.join(
"C:\\",
"Users",
USERNAME,
"projects",
"eco-mods-public",
"Mods",
"UserCode",
"BunWulfConstruction",
"Recipes",
)
BUNWULF_EDUCATIONAL_PATH = os.path.join(
"C:\\",
"Users",
USERNAME,
"projects",
"eco-mods-public",
"Mods",
"UserCode",
"BunWulfEducational",
"Recipes",
)
LINUX_SERVER_PATH = os.path.join(
"/home",
"kai",
"Steam",
"steamapps",
"common",
"EcoServer",
).replace("\\", "/")
WINDOWS_SERVER_PATH = os.path.join(
"C:\\",
"Program Files (x86)",
"Steam",
"steamapps",
"common",
"Eco",
"Eco_Data",
"Server",
)
def server_path():
if "windows" in os.getenv("OS", "").lower():
return WINDOWS_SERVER_PATH
else:
return LINUX_SERVER_PATH
class RemovalException(Exception):
pass
def handleRemoveReadonly(func, path, _):
if not os.access(path, os.W_OK):
os.chmod(path, stat.S_IWUSR)
func(path)
else:
raise RemovalException("could not handle path")
def copy_paths(origin_path, target_path):
if not os.path.isdir(origin_path):
return
if os.path.exists(target_path) and os.path.isdir(target_path):
print(f"\tRemoving {target_path}")
shutil.rmtree(target_path, ignore_errors=False, onerror=handleRemoveReadonly)
if os.path.isdir(origin_path):
print(f"\tCopying {origin_path} to {target_path}")
shutil.copytree(origin_path, target_path)
@invoke.task
def copy_assets(ctx: invoke.Context, branch=""):
print("Cleaning out assets folder")
if os.path.exists("./eco-server/assets"):
shutil.rmtree(
"./eco-server/assets", ignore_errors=False, onerror=handleRemoveReadonly
)
# get assets from git
branch_flag = ""
if branch != "":
branch_flag = f"-b {branch}"
ctx.run(
f"git clone --depth 1 {branch_flag} -- git@github.com:coilysiren/eco-mods-assets.git ./eco-server/assets",
echo=True,
)
shutil.rmtree(
"./eco-server/assets/.git", ignore_errors=False, onerror=handleRemoveReadonly
)
for build in os.listdir("./eco-server/assets/Builds/Mods/UserCode/"):
origin_path = os.path.join(
"./eco-server/assets/Builds/Mods/UserCode", build, "Assets"
)
target_path = os.path.join("./Mods/UserCode", build, "Assets")
copy_paths(origin_path, target_path)
@invoke.task
def zip_assets(ctx: invoke.Context, mod):
if os.path.exists(f"{mod}.zip"):
os.remove(f"{mod}.zip")
if os.path.exists(os.path.join("./Mods/UserCode", mod, "bin")):
shutil.rmtree(
f"./Mods/UserCode/{mod}/bin",
ignore_errors=False,
onerror=handleRemoveReadonly,
)
if os.path.exists(os.path.join("./Mods/UserCode", mod, "obj")):
shutil.rmtree(
f"./Mods/UserCode/{mod}/obj",
ignore_errors=False,
onerror=handleRemoveReadonly,
)
ctx.run(f"zip -r {mod}.zip ./Mods/UserCode/{mod}")
@invoke.task
def push_asset(ctx: invoke.Context, mod):
zip_assets(ctx, mod)
# TODO: delete target folder first
ctx.run(f"scp {mod}.zip kai@kai-server:{LINUX_SERVER_PATH}")
ctx.run(f'ssh -t kai@kai-server "cd {LINUX_SERVER_PATH} && unzip -o {mod}.zip"')
#######################
# SPECIALITY SPECIFIC #
#######################
@invoke.task
def bunwulf_agricultural(_: invoke.Context):
plants = os.path.join(server_path(), "Mods", "__core__", "AutoGen", "Plant")
plant = os.listdir(plants)
plant_entity_pattern = r".*public partial class (\w+) : PlantEntity.*"
plant_species_pattern = r".*public partial class (\w+) : PlantSpecies.*"
tree_species_pattern = r".*public partial class (\w+) : TreeSpecies.*"
templates = jinja2.Environment(loader=jinja2.FileSystemLoader("templates/"))
template = templates.get_template("plant.template")
# Read in every plant file
for p in plant:
with open(os.path.join(plants, p), "r", encoding="utf-8") as f:
data = f.read()
# Skip anything produces plant fibers
if "PlantFibersItem" in data:
continue
# Skip trees
if regex.match(tree_species_pattern, data, regex.DOTALL):
continue
else:
# Pull out all the data we need
plant_entity = regex.search(plant_entity_pattern, data, regex.DOTALL).group(
1
)
plant_species = regex.search(
plant_species_pattern, data, regex.DOTALL
).group(1)
# Render and write the template
print(f"Writing {plant_entity} to BunWulfAgricultural")
content = template.render(
plant_entity=plant_entity, plant_species=plant_species
)
with open(
os.path.join(USERCODE_PATH, "BunWulfAgricultural", "Plant", p),
"w",
encoding="utf-8",
) as f:
f.write(content)
@invoke.task
def bunwulf_structural(_: invoke.Context):
with open("recipes.yml", "r", encoding="utf-8") as recipes:
recipe_data = yaml.safe_load(recipes)["BunWulfStructural"]
util.process_recipes(
recipe_data, os.path.join(USERCODE_PATH, "BunWulfStructural", "Recipes")
)