-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpkg.py
More file actions
121 lines (92 loc) · 3.69 KB
/
pkg.py
File metadata and controls
121 lines (92 loc) · 3.69 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
#!/usr/bin/python3
from tarfile import open as topen, is_tarfile
from subprocess import Popen, PIPE, call, check_output, run
from os import popen, system, path
from glob import glob
from json import load, loads
from style.colours import colours
path.dirname(path.realpath(__file__))
# TODO(Kunal, Artur): Get install thing working better with parsed outputs and all
class packager():
"""Packs/depacks files in .spk format
Args:
file (string): File/ Folder to be packed/unpacked
pkg (bool, optional): If the file needs to be depacked. Defaults to True.
"""
def __init__(self, file, pkg=True, meta=False, longg=False):
pkgname = "{}.spk".format(file)
try:
# So this packages a folder into .spk
# folder --> .spk if pkg is true else .spk --> folder
if meta:
self.read_meta(pkgname, file, longg)
return
if not pkg:
self.dpkging(pkgname, file)
elif checkFolder(file) and pkg:
self.pkging(file, pkgname)
else:
print("ERR: No such file in directory")
except IOError as err:
print("ERR: Package not found", err)
def dpkging(self, pkgname, file):
result = run(f"tar xfOs {pkgname} {file}/metadata.json | cat".split(" "), stdout=PIPE)
result = result.stdout.decode('utf-8')
result = loads(result)
popen('tar -zxvf {}'.format(pkgname)).read()
# print("running the package installer")
# run_command(["bash", "install.sh"], file)
print("Running the package installer...")
run_command(["bash", (result['package-data'][0]['install'])], file)
def read_meta(self,pkgname,file,longg):
# tar xfO nano.spk nano/metadata.json | cat
result = run(f"tar xfOs {pkgname} {file}/metadata.json | cat".split(" "), stdout=PIPE)
result = result.stdout.decode('utf-8')
result = loads(result)
files = run(f"tar tf {pkgname} ".split(" "), stdout=PIPE)
files = files.stdout.decode('utf-8')
#print(check_output(f"tar xfO {pkgname} {file}/metadata.json | cat".split(" ")))
print(f"""
Package info:
\tName: {result['package-data'][0]['name']}
\tSize: {result['package-data'][0]['size']}
\tDepends on: {result['package-data'][0]['depends']}
\tBuild instructions: {result['package-data'][0]['build']}
\tInstall instructions: {result['package-data'][0]['install']}
""")
if longg:
print(f"Files in package: {files}")
def pkging(self, file, pkgname):
# Packaging (requires folder and validation)
for dir in ['fs', 'install.sh', 'metadata.json']:
if not checkFolder(f'{file}/{dir}'):
print("ERR: Invalid package")
quit()
print("packaging")
popen('tar -czf {} {}'.format(pkgname, file)).read()
def checkFolder(file):
return True if glob(file) else False
# runs install.sh
def run_command(command, dir):
"""A daemon, runs the command and gives output\n
Will parse stuff
Args:
command (string): What command to run
Returns:
String: Realtime output to the inputted command
"""
process = Popen(command, stdout=PIPE, cwd=dir)
alive = True
while alive:
output = process.stdout.readline()
print(parse(output.strip().decode("utf-8"))) if output else None
alive = process.poll() is None
rc = process.poll()
return rc
def parse(output):
if output == "$SUCCESS":
return colours.GREEN_SUCCESS + "SUCCESS" + colours.RESET
def LoadMeta(file):
with open(f'{file}/metadata.json') as f:
metadata = load(f)
return metadata