|
| 1 | +import stat |
1 | 2 | import subprocess |
2 | 3 | import os |
3 | 4 | import shutil |
| 5 | +import tarfile |
4 | 6 |
|
5 | 7 | COPIED_FILES = [".json"] |
6 | | -PACKAGED_DIR = "./Packaged" |
| 8 | +PACKAGED_DIR = "Packaged" |
7 | 9 |
|
8 | 10 | CMD_SCRIPT = "dotnet exec/{NAME}.dll > nul" |
9 | | -SH_SCRIPT = "#!/bin/sh \n dotnet exec/{NAME}.dll" |
| 11 | +SH_SCRIPT = "dotnet exec/{NAME}.dll" |
10 | 12 |
|
11 | 13 | subdirs = [] |
12 | 14 |
|
|
51 | 53 | print(f"Creating run scripts for {subdir}...") |
52 | 54 |
|
53 | 55 | for script_data in [(CMD_SCRIPT, "cmd"), (SH_SCRIPT, "sh")]: |
54 | | - with open(f"{path}/{subdir}.{script_data[1]}", "w") as file: |
55 | | - file.write(script_data[0].replace("{NAME}", subdir)) |
| 56 | + file_path = f"{path}/{subdir}.{script_data[1]}" |
| 57 | + with open(file_path, "w") as archive: |
| 58 | + archive.write(script_data[0].replace("{NAME}", subdir)) |
56 | 59 |
|
57 | 60 | print(f"Finished packaging {subdir}.") |
58 | 61 |
|
59 | | -print("Packaged all bots. Now zipping and removing directory...") |
| 62 | +print("Packaged all bots. Now archiving and removing directory...") |
| 63 | + |
| 64 | +def tar_perms_filter(tarinfo: tarfile.TarInfo): |
| 65 | + if tarinfo.name.endswith(".sh") or tarinfo.isdir(): |
| 66 | + tarinfo.mode = 0o755 |
| 67 | + else: |
| 68 | + tarinfo.mode = 0o644 |
| 69 | + |
| 70 | + return tarinfo |
| 71 | + |
| 72 | +with tarfile.open(f"{PACKAGED_DIR}.tar.gz", "w:gz") as tar: |
| 73 | + for dir_path, dir_names, file_names in os.walk(PACKAGED_DIR): |
| 74 | + dir_path_relative = os.path.relpath(dir_path, PACKAGED_DIR) # Don't nest everything inside a 'packaged' dir in the archive |
| 75 | + dir_path_archive = dir_path_relative.replace(os.path.sep, "/") |
| 76 | + if dir_path_relative != ".": |
| 77 | + tar.add(dir_path, dir_path_archive, recursive=False, filter=tar_perms_filter) |
| 78 | + |
| 79 | + for file_name in file_names: |
| 80 | + file_path = os.path.join(dir_path, file_name) |
| 81 | + file_path_archive = f"{dir_path_archive}/{file_name}" |
| 82 | + tar.add(file_path, file_path_archive, filter=tar_perms_filter) |
| 83 | + |
60 | 84 |
|
61 | 85 | shutil.make_archive(PACKAGED_DIR, 'zip', PACKAGED_DIR) |
| 86 | +print("Removing.") |
62 | 87 | shutil.rmtree(PACKAGED_DIR) |
0 commit comments