def pack_lin(dir): # Collect our files. file_list = sorted(list_all_files(dir)) txt = [filename for filename in file_list if os.path.splitext(filename)[1].lower() == ".txt"] wrd = [filename for filename in file_list if os.path.splitext(filename)[1].lower() == ".wrd"] py = [filename for filename in file_list if os.path.splitext(filename)[1].lower() == ".py"] # If there are more than one for whatever reason, just take the first. # We only have use for a single wrd or python file. wrd = wrd[0] if wrd else None py = py[0] if py else None # Prepare our temporary output directory. temp_dir = tempfile.mkdtemp(prefix = "sdse-") # Where we're outputting our wrd file, regardless of whether it's a python # file or a raw binary data file. wrd_dst = os.path.join(temp_dir, "0.wrd") if py: try: wrd_file = WrdFile(py) except: _LOGGER.warning("%s failed to compile. Parsing wrd file instead. Exception info:\n%s" % (py, traceback.format_exc())) shutil.copy(wrd, wrd_dst) else: # If we succeeded in loading the python file, compile it to binary. wrd_file.save_bin(wrd_dst) else: shutil.copy(wrd, wrd_dst) # Pack the text files in-place to save us a bunch of copying # and then move it to the tmp directory with the wrd file. if txt: data = pack_pak(dir, file_list = txt, eof = True) with open(os.path.join(temp_dir, "1.dat"), "wb") as f: data.tofile(f) # Then pack it like normal. data = pack_pak(temp_dir, eof = True) shutil.rmtree(temp_dir) return data