|
2 | 2 | import subprocess |
3 | 3 | import os |
4 | 4 | import shutil |
| 5 | +import tarfile |
5 | 6 |
|
6 | 7 | COPIED_FILES = [".json"] |
7 | | -PACKAGED_DIR = "./Packaged" |
| 8 | +PACKAGED_DIR = "Packaged" |
8 | 9 |
|
9 | 10 | CMD_SCRIPT = "dotnet exec/{NAME}.dll > nul" |
10 | 11 | SH_SCRIPT = "#!/bin/sh \n dotnet exec/{NAME}.dll" |
|
53 | 54 |
|
54 | 55 | for script_data in [(CMD_SCRIPT, "cmd"), (SH_SCRIPT, "sh")]: |
55 | 56 | file_path = f"{path}/{subdir}.{script_data[1]}" |
56 | | - with open(file_path, "w") as file: |
57 | | - file.write(script_data[0].replace("{NAME}", subdir)) |
58 | | - try: |
59 | | - os.chmod(file_path, stat.S_IRWXO | stat.S_IRWXG | stat.S_IRWXU) |
60 | | - except Exception as e: |
61 | | - print(f"Failed to chmod with error: {e}") |
| 57 | + with open(file_path, "w") as archive: |
| 58 | + archive.write(script_data[0].replace("{NAME}", subdir)) |
62 | 59 |
|
63 | 60 | print(f"Finished packaging {subdir}.") |
64 | 61 |
|
65 | | -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=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=perms_filter) |
| 83 | + |
66 | 84 |
|
67 | 85 | shutil.make_archive(PACKAGED_DIR, 'zip', PACKAGED_DIR) |
68 | 86 | print("Removing.") |
|
0 commit comments