-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsyrup.py
More file actions
138 lines (121 loc) · 4.49 KB
/
Copy pathsyrup.py
File metadata and controls
138 lines (121 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python3
import datetime
import json
import pathlib
import platform
import shutil
import subprocess
import sys
ascii = """
███████╗██╗ ██╗██████╗ ██╗ ██╗██████╗
██╔════╝╚██╗ ██╔╝██╔══██╗██║ ██║██╔══██╗
███████╗ ╚████╔╝ ██████╔╝██║ ██║██████╔╝
╚════██║ ╚██╔╝ ██╔══██╗██║ ██║██╔═══╝
███████║ ██║ ██║ ██║╚██████╔╝██║
╚══════╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝
"""
def pkg():
print(f"Welcome {pathlib.Path.home().name}, to syrup.")
print("Please enter the folder(s) to package. (eg.. ~/.config/)")
loadfolder = input("> ")
path = pathlib.Path(loadfolder).expanduser()
if not path.exists():
print("That path does not exist.")
raise FileNotFoundError
name = input("Enter a name: ")
dependencies = input("Please list all the dependencies, seperated by commas: ")
dependencies = [d.strip() for d in dependencies.split(",") if d.strip()]
desc = input("Enter a description: ")
try:
relpath = "~/" + str(path.relative_to(pathlib.Path.home()))
except ValueError:
relpath = str(path)
syrup = {
"name": name,
"creation": datetime.datetime.now().strftime("%d-%m-%Y - %H:%M"),
"author": pathlib.Path.home().name,
"folder": relpath,
"depends": dependencies,
"distro": platform.release(),
"desc": desc,
}
with open(f"{path.expanduser()}/.recipe", "w") as f:
json.dump(syrup, f)
shutil.make_archive(name, "zip", path)
tempzip = pathlib.Path(f"{name}.zip")
output = pathlib.Path(f"{name}.syp")
tempzip.rename(output)
pathlib.Path(f"{path.expanduser()}/.recipe").unlink()
print(f"Yaay! Done. You can find the file at {output.expanduser()}.")
def install(syrup):
syrup = pathlib.Path(syrup).expanduser()
if not syrup.exists():
print("File not found.")
return
tempdir = pathlib.Path("./temp_syrup")
tempdir.mkdir(exist_ok=True)
shutil.unpack_archive(str(syrup), extract_dir=tempdir, format="zip")
recipefile = tempdir / ".recipe"
with open(recipefile, "r") as f:
metadata = json.load(f)
deps = metadata.get("depends", [])
if deps:
print(f"Needed: {', '.join(deps)}")
if shutil.which("pacman"):
subprocess.run(["sudo", "pacman", "-S", "--needed"] + deps)
elif shutil.which("dnf"):
subprocess.run(["sudo", "dnf", "install"] + deps)
else:
print(
"Hey.. so like.. your system's main package manager isn't supported.. Sorry.."
)
destination = pathlib.Path(metadata["folder"]).expanduser()
if destination.exists():
if not any(destination.iterdir()):
pass
else:
yn = input(
f"[WARNING] {destination.name} is NOT empty. Are you sure you would like to pour the Syrup? [Y/n]"
).lower()
if yn == "y":
pass
elif yn == "n":
print("Bai!")
sys.exit(0)
else:
print("Huh?")
sys.exit(1)
destination.mkdir(parents=True, exist_ok=True)
for item in tempdir.iterdir():
if item.name == ".recipe":
continue
target = destination / item.name
if item.is_dir():
shutil.copytree(item, target, dirs_exist_ok=True)
else:
shutil.copy2(item, target)
shutil.rmtree(tempdir)
print(f"Syrup poured into {destination}!")
def main():
try:
if sys.argv[1] == "package":
pkg()
elif sys.argv[1] == "install":
if sys.argv[2]:
install(sys.argv[2])
else:
print(
"Syrup\ninstall - Installs a .syp file.\npackage - Packages a folder into a .syp file.\nExamples:\nsyrup install rice.syp\nsyrup package"
)
except IndexError:
print(ascii)
print("""
----------------[Commands]----------------
[install --- Installs a .syp file]
[package --- Package a folder into a .syp]
""")
except KeyboardInterrupt:
print("\n")
sys.exit()
if __name__ == "__main__":
main()