Skip to content

Commit

Permalink
Centralized dirs module.
Browse files Browse the repository at this point in the history
  • Loading branch information
saurtron committed Jan 20, 2025
1 parent 956acb9 commit 6c23068
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 35 deletions.
25 changes: 9 additions & 16 deletions baker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import shutil

scriptdir = os.path.dirname(os.path.realpath(__file__))
datadir = os.path.dirname(scriptdir)
sys.path.append(scriptdir)

import unitbake
Expand Down Expand Up @@ -40,24 +39,18 @@ def compare_paths(path1, path2, path3):
progress_func(1.0, 'Done')

def bake_all():
unitbake.load_languages(os.path.join(datadir, 'games', 'BAR.sdd'))
work_dir = os.path.join(scriptdir, 'workdir')
dirs = unitbake.get_dirs(scriptdir)
unitbake.load_languages(dirs.game_dir)

path1 = os.path.join(work_dir, 'baked_defs.orig', 'units')
path2 = os.path.join(datadir, 'baked_defs', 'units')
path_units = os.path.join(datadir, 'games', 'BAR.sdd', 'units')
path_units_orig = os.path.join(work_dir, 'units.orig')
path3 = os.path.join(work_dir, 'units')
if os.path.exists(dirs.write_units):
shutil.rmtree(dirs.write_units)
shutil.copytree(dirs.backup_units, dirs.write_units)

if os.path.exists(path3):
shutil.rmtree(path3)
shutil.copytree(path_units_orig, path3)
compare_paths(dirs.backup_baked, dirs.new_baked, dirs.write_units)

compare_paths(path1, path2, path3)

if os.path.exists(path_units):
shutil.rmtree(path_units)
shutil.copytree(path3, path_units)
if os.path.exists(dirs.game_units):
shutil.rmtree(dirs.game_units)
shutil.copytree(dirs.write_units, dirs.game_units)

if __name__ == '__main__':
bake_all()
31 changes: 12 additions & 19 deletions prebake.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
# games/BAR.sdd/.git/FETCH_HEAD

script_dir = os.path.dirname(os.path.realpath(__file__))
data_dir = os.path.dirname(script_dir)
sys.path.append(script_dir)

import unitbake

def report_progress(progress, text=None):
pass

Expand All @@ -19,24 +20,16 @@ def set_progress_cb(cb):


def prebake():
work_dir = os.path.join(script_dir, 'workdir')
bar_dir = os.path.join(data_dir, 'games', 'BAR.sdd')

progress_func(0.1, "Create {}".format(work_dir))
os.makedirs(work_dir, exist_ok=True)

orig_units = os.path.join(bar_dir, 'units')
backup_units = os.path.join(work_dir, 'units.orig')
progress_func(0.33, "Copy {} to {}".format(orig_units, backup_units))
shutil.copytree(orig_units, backup_units, dirs_exist_ok=True)

if os.path.exists(os.path.join(data_dir, 'baked_defs.orig')):
orig_baked = os.path.join(data_dir, 'baked_defs.orig', 'units')
else:
orig_baked = os.path.join(data_dir, 'baked_defs', 'units')
backup_baked = os.path.join(work_dir, 'baked_defs.orig', 'units')
progress_func(0.66, "Copy {} to {}".format(orig_baked, backup_baked))
shutil.copytree(orig_baked, backup_baked, dirs_exist_ok=True)
dirs = unitbake.get_dirs(script_dir)

progress_func(0.1, "Create {}".format(dirs.work_dir))
os.makedirs(dirs.work_dir, exist_ok=True)

progress_func(0.33, "Copy {} to {}".format(dirs.game_units, dirs.backup_units))
shutil.copytree(dirs.game_units, dirs.backup_units, dirs_exist_ok=True)

progress_func(0.66, "Copy {} to {}".format(dirs.orig_baked, dirs.backup_baked))
shutil.copytree(dirs.orig_baked, dirs.backup_baked, dirs_exist_ok=True)
progress_func(1.0)

if __name__ == '__main__':
Expand Down
1 change: 1 addition & 0 deletions unitbake/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .unitbake import *
from .lang import load_languages
from .dirs import get_dirs
25 changes: 25 additions & 0 deletions unitbake/dirs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os

class DirsObject:
def __init__(self, script_dir):
self.data_dir = os.path.dirname(script_dir)
self.work_dir = os.path.join(script_dir, 'workdir')
self.game_dir = os.path.join(self.data_dir, 'games', 'BAR.sdd')

self.game_units = os.path.join(self.game_dir, 'units')
self.backup_units = os.path.join(self.work_dir, 'units.orig')
self.write_units = os.path.join(self.work_dir, 'units')

self.new_baked = os.path.join(self.data_dir, 'baked_defs', 'units')

if os.path.exists(os.path.join(self.data_dir, 'baked_defs.orig')):
self.orig_baked = os.path.join(self.data_dir, 'baked_defs.orig', 'units')
else:
self.orig_baked = self.new_baked

self.backup_baked = os.path.join(self.work_dir, 'baked_defs.orig', 'units')

def get_dirs(script_dir):
obj = DirsObject(script_dir)
return obj

0 comments on commit 6c23068

Please sign in to comment.