1+ from pathlib import PosixPath
2+
13from beet import Context , run_beet
24
35from .data_pack import gen_dp_overlays
@@ -17,12 +19,52 @@ def beet_default(ctx: Context):
1719 if ctx .data :
1820 dp_path = cache .get_path (f"{ ctx .directory } saved_data_pack" )
1921 if dp_path .is_dir ():
20- ctx .data .load (f"{ dp_path } " )
22+ # get files that were moved to an overlay
23+ with open (f"{ dp_path } /deleted.txt" , mode = "r" ) as del_list :
24+ deleted = del_list .read ().splitlines ()
25+
26+ # delete files that were moved to an overlay
27+ for target in deleted :
28+ # get file location
29+ target_path = PosixPath (target )
30+ folders = target_path .parts
31+ ext = "." + folders [- 1 ].split ("." )[- 1 ]
32+ loc = f"{ folders [1 ]} :{ folders [- 1 ].removesuffix (ext )} "
33+ # get resource location
34+ for location , resource in ctx .data .all (loc ):
35+ p = str (resource .source_path ) # type: ignore
36+ resource_path = PosixPath ((p [p .find ("/data/" ) + 1 :]))
37+ # delete resource from pack
38+ if target_path == resource_path :
39+ del ctx .data [type (resource )][location ]
40+
41+ # add overlays to pack
42+ ctx .data .load (f"{ dp_path } /pack" )
2143 cached_dp = True
2244 if ctx .assets :
2345 rp_path = cache .get_path (f"{ ctx .directory } saved_resource_pack" )
2446 if rp_path .is_dir ():
25- ctx .assets .load (f"{ rp_path } " )
47+ # get files that were moved to an overlay
48+ with open (f"{ rp_path } /deleted.txt" , mode = "r" ) as del_list :
49+ deleted = del_list .read ().splitlines ()
50+
51+ # delete files that were moved to an overlay
52+ for target in deleted :
53+ # get file location
54+ target_path = PosixPath (target )
55+ folders = target_path .parts
56+ ext = "." + folders [- 1 ].split ("." )[- 1 ]
57+ loc = f"{ folders [1 ]} :{ folders [- 1 ].removesuffix (ext )} "
58+ # get resource location
59+ for location , resource in ctx .assets .all (loc ):
60+ p = str (resource .source_path ) # type: ignore
61+ resource_path = PosixPath ((p [p .find ("/assets/" ) + 1 :]))
62+ # delete resource from pack
63+ if target_path == resource_path :
64+ del ctx .assets [type (resource )][location ]
65+
66+ # add overlays to pack
67+ ctx .assets .load (f"{ rp_path } /pack" )
2668 cached_rp = True
2769 if cached_dp and cached_rp :
2870 return
@@ -52,6 +94,8 @@ def beet_default(ctx: Context):
5294 for overlay in ctx .assets .overlays :
5395 save_rp .append (overlay )
5496 # loop through all overlays
97+ deleted_dp : list [PosixPath ] = []
98+ deleted_rp : list [PosixPath ] = []
5599 for overlay in ctx .meta ["observer" ]["overlays" ]:
56100 # get pack
57101 if overlay ["process" ].startswith ("https://" ):
@@ -70,12 +114,18 @@ def beet_default(ctx: Context):
70114 rp_dir = overlay ["directory" ]
71115 # compare build pack and overlay pack
72116 if not cached_dp and ctx .data :
73- gen_dp_overlays (ctx , ctx_overlay , dp_dir , save_dp )
117+ deleted_dp = gen_dp_overlays (ctx , ctx_overlay , dp_dir , save_dp )
74118 if not cached_rp and ctx .assets :
75- gen_rp_overlays (ctx , ctx_overlay , rp_dir , save_rp )
119+ deleted_rp = gen_rp_overlays (ctx , ctx_overlay , rp_dir , save_rp )
76120
77121 # save to cache
78122 if not cached_dp and ctx .data :
79- ctx .data .save (path = dp_path )
123+ ctx .data .save (path = f"{ dp_path } /pack" )
124+ with open (f"{ dp_path } /deleted.txt" , mode = "x" ) as file :
125+ for s in deleted_dp :
126+ file .write (str (s )[str (s ).find ("/data/" ) + 1 :] + "\n " )
80127 if not cached_rp and ctx .assets :
81- ctx .assets .save (path = rp_path )
128+ ctx .assets .save (path = f"{ rp_path } /pack" )
129+ with open (f"{ rp_path } /deleted.txt" , mode = "x" ) as file :
130+ for s in deleted_rp :
131+ file .write (str (s )[str (s ).find ("/assets/" ) + 1 :] + "\n " )
0 commit comments