-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopkview.py
More file actions
executable file
·206 lines (192 loc) · 7.48 KB
/
opkview.py
File metadata and controls
executable file
·206 lines (192 loc) · 7.48 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/bin/env python3
import configparser
import re
import sys
import tempfile
import os
import magic
import io
from elftools.elf.elffile import ELFFile
from PySquashfsImage import SquashFsImage
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, GdkPixbuf
builder = Gtk.Builder()
builder.add_from_file("layout.glade")
window = builder.get_object("window1")
window.set_title("Tiny OPK Viewer")
window.show_all()
textview = builder.get_object("textview1")
textbuffer = textview.get_buffer()
image = builder.get_object("image1")
aboutdialog = builder.get_object('aboutdialog1')
def reset_view(msg):
global opk_path
opk_path = ""
textbuffer.set_text(msg)
# initialization
reset_view("Load an OPK file to see its description.")
def extract_node(node, destpath):
if node.isFolder():
if node.getName() == "":
dirpath = destpath
else:
dirpath = destpath + "/" + node.getName().decode("utf-8")
os.mkdir(dirpath)
for c in node.children:
extract_node(c, dirpath)
else:
with open(destpath + "/" + node.getName().decode("utf-8"), "wb") as f:
f.write(node.getContent())
def extract_opk(path, dest):
opk = SquashFsImage(path)
extract_node(opk.getRoot(), dest)
def load_opk(path):
global opk_path
opk_path = path
filename = path
opk = SquashFsImage(path)
image.clear()
platformset = set()
desktopno = 0
platforms = ""
desktops = dict()
manuals = dict()
execs = dict()
for i in opk.root.children:
iname = i.getName().decode("utf-8")
m = re.match(r"(?:[^.]*\.)?([^.]+)\.desktop", iname)
if m is not None:
desktopno = desktopno + 1
desktopfile = configparser.ConfigParser(allow_no_value=True, strict=False)
desktopfile.read_string(i.getContent().decode("utf-8"))
dset = dict()
dset['appname'] = desktopfile["Desktop Entry"].get("Name", "", raw=True)
dset['executable'] = os.path.basename(desktopfile["Desktop Entry"].get("Exec", "", raw=True).split()[0])
if dset['executable'] != "":
execs[dset['executable']] = dict()
else:
dset['executable'] = "<none>"
dset['version'] = desktopfile["Desktop Entry"].get("Version", "", raw=True)
dset['comment'] = desktopfile["Desktop Entry"].get("Comment", "", raw=True)
dset['manualpath'] = desktopfile["Desktop Entry"].get("X-OD-Manual", "", raw=True)
if dset['manualpath'] != "":
manuals[dset['manualpath']] = ""
desktops[iname] = dset
platformset.add(m.group(1))
m = re.match(r".+\.png", iname)
if m is not None:
loader = GdkPixbuf.PixbufLoader()
loader.write(i.getContent())
loader.close()
image.set_from_pixbuf(loader.get_pixbuf())
# convert platform set to string
for p in platformset:
if platforms == "":
platforms += p
else:
platforms += ", " + p
for i in opk.root.children:
iname = i.getName().decode("utf-8")
if iname in manuals:
encodings = ["utf-8", "latin_1", "iso8859_2", "cp1250", "cp1252", "utf-16", "utf-32"]
man = ""
for e in encodings:
try:
manuals[iname] = i.getContent().decode(e)
break
except:
manuals[iname] = "<reading error>"
if iname in execs:
execs[iname]['magic'] = magic.from_buffer(i.getContent())
execs[iname]['mime'] = magic.from_buffer(i.getContent(), mime=True)
execs[iname]['iself'] = execs[iname]['mime'] == 'application/x-executable' or execs[iname]['mime'] == 'application/x-pie-executable'
if execs[iname]['iself']:
bstream = io.BytesIO(i.getContent())
elf = ELFFile(bstream)
dynsec = elf.get_section_by_name(".dynamic")
execs[iname]['dynamic'] = dynsec is not None
if execs[iname]['dynamic']:
execs[iname]['deps'] = []
for tag in dynsec.iter_tags():
if tag.entry.d_tag == "DT_NEEDED":
execs[iname]['deps'].append(tag.needed)
opk.close()
if platforms == "":
platforms = "none"
content = "OPK filename: " + filename + "\n"
content += "Number of desktop files: " + str(desktopno) + "\n"
content += "Platforms: " + platforms + "\n\n"
for key in desktops:
content += "Desktop file: " + key + "\n"
if desktops[key]['appname'] == "":
desktops[key]['appname'] = "none"
content += "Appname: " + desktops[key]['appname'] + "\n"
content += "Executable: " + desktops[key]['executable'] + "\n"
if desktops[key]['version'] != "":
content += "Version: " + desktops[key]['version'] + "\n"
if desktops[key]['comment'] != "":
content += "Comment: " + desktops[key]['comment'] + "\n"
if desktops[key]['manualpath'] != "":
content += "Manual: " + desktops[key]['manualpath'] + "\n"
content += "\n"
for key in execs:
content += "Executable " + key + "\n"
content += "Identity: " + execs[key]['magic'] + "\n"
if execs[key]['iself']:
if execs[key]['dynamic']:
content += "The executable is dynamically linked.\n"
if len(execs[key]['deps']) == 0:
content += "No dynamic dependencies detected.\n"
else:
content += "Dynamic dependencies:\n"
for d in execs[key]['deps']:
content += " " + d + "\n"
else:
content += "The executable is statically linked.\n"
content += "\n"
for key in manuals:
if manuals[key] == "":
content += "Manual " + key + " is empty or nonexistent.\n"
else:
content += "Manual " + key + "\n"
content += "=== MANUAL START ===\n"
content += manuals[key] + "\n"
content += "=== MANUAL END ===\n"
content += "\n"
textbuffer.set_text(content)
class Handler:
def onDestroy(self, *args):
Gtk.main_quit()
def onOpen(self, *args):
dialog = Gtk.FileChooserDialog("Select an OPK file", window,
Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
response = dialog.run()
if response == Gtk.ResponseType.OK:
filepath = dialog.get_filename()
try:
load_opk(filepath)
except:
reset_view("OPK loading failed.")
dialog.destroy()
def onExtract(self, *args):
dialog = Gtk.FileChooserDialog("Select a destination folder", window,
Gtk.FileChooserAction.SELECT_FOLDER,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_SAVE, Gtk.ResponseType.OK))
response = dialog.run()
if response == Gtk.ResponseType.OK:
filepath = dialog.get_filename()
if opk_path != "":
extract_opk(opk_path, filepath)
dialog.destroy()
def onAbout(self, *args):
aboutdialog.show()
def onAboutDialogClose(self, *args):
aboutdialog.hide()
builder.connect_signals(Handler())
if len(sys.argv) == 2:
load_opk(sys.argv[1])
Gtk.main()