Skip to content

Commit 4745393

Browse files
authored
Merge pull request #3 from TheFlameFish/script-perms
Better cross platform support
2 parents 9ba62fa + 3c375e3 commit 4745393

8 files changed

Lines changed: 32 additions & 6 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
.vscode
55
**.sln
66
Packaged/
7-
Packaged.zip
7+
Packaged.zip
8+
Packaged.tar.gz

LockBot/LockBot.cmd

100644100755
File mode changed.

LockBot/LockBot.sh

100644100755
File mode changed.

MyFirstBot/MyFirstBot.cmd

100644100755
File mode changed.

MyFirstBot/MyFirstBot.sh

100644100755
File mode changed.

SensorBot/SensorBot.cmd

100644100755
File mode changed.

SensorBot/SensorBot.sh

100644100755
File mode changed.

package.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
import stat
12
import subprocess
23
import os
34
import shutil
5+
import tarfile
46

57
COPIED_FILES = [".json"]
6-
PACKAGED_DIR = "./Packaged"
8+
PACKAGED_DIR = "Packaged"
79

810
CMD_SCRIPT = "dotnet exec/{NAME}.dll > nul"
9-
SH_SCRIPT = "#!/bin/sh \n dotnet exec/{NAME}.dll"
11+
SH_SCRIPT = "dotnet exec/{NAME}.dll"
1012

1113
subdirs = []
1214

@@ -51,12 +53,35 @@
5153
print(f"Creating run scripts for {subdir}...")
5254

5355
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))
5659

5760
print(f"Finished packaging {subdir}.")
5861

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+
6084

6185
shutil.make_archive(PACKAGED_DIR, 'zip', PACKAGED_DIR)
86+
print("Removing.")
6287
shutil.rmtree(PACKAGED_DIR)

0 commit comments

Comments
 (0)