botlabsdev / npkpy Goto Github PK
View Code? Open in Web Editor NEWnpkPy is an unpacker tool for MikroTiks custom NPK container format
Home Page: https://pypi.org/project/npkPy
License: GNU General Public License v3.0
npkPy is an unpacker tool for MikroTiks custom NPK container format
Home Page: https://pypi.org/project/npkPy
License: GNU General Public License v3.0
Hello!
Can you cover how i can create or modify npk using this brilliant tool?
i'm totally understand that at the end of file got invalid signature but that's not a problem.
The tools internal functions always tend to throw a RuntimeError
independent of the internal problem. This only allows very basic exception handling from the outside.
At least something like an NPKError
or similar would be helpful to not simply catch all errors the same.
(Also there is some commented code ๐)
Hello,
Npks from latest stable release are unpackable with npkpy
npkpy --files system-6.48.4.npk --show-container
Traceback (most recent call last):
File "/home/afx/.local/share/virtualenvs/tmp-XVr6zr33/bin/npkpy", line 8, in <module>
sys.exit(main())
File "/home/afx/.local/share/virtualenvs/tmp-XVr6zr33/lib/python3.9/site-packages/npkpy/main.py", line 47, in main
analyse_npk(opts, files)
File "/home/afx/.local/share/virtualenvs/tmp-XVr6zr33/lib/python3.9/site-packages/npkpy/analyse_npk.py", line 13, in analyse_npk
for file in npk_files:
File "/home/afx/.local/share/virtualenvs/tmp-XVr6zr33/lib/python3.9/site-packages/npkpy/main.py", line 46, in <genexpr>
files = (Npk(f) for f in (opts.files if opts.files else get_all_nkp_files(opts.src_folder, opts.glob)))
File "/home/afx/.local/share/virtualenvs/tmp-XVr6zr33/lib/python3.9/site-packages/npkpy/npk/npk.py", line 35, in __init__
self.pck_header = self.pck_cnt_list[0]
File "/home/afx/.local/share/virtualenvs/tmp-XVr6zr33/lib/python3.9/site-packages/npkpy/npk/npk.py", line 74, in pck_cnt_list
self.__cnt_list = self.__parse_all_cnt()
File "/home/afx/.local/share/virtualenvs/tmp-XVr6zr33/lib/python3.9/site-packages/npkpy/npk/npk.py", line 81, in __parse_all_cnt
lst.append(self.__get_cnt(offset))
File "/home/afx/.local/share/virtualenvs/tmp-XVr6zr33/lib/python3.9/site-packages/npkpy/npk/npk.py", line 94, in __get_cnt
return CNT_HANDLER[cnt_id](data, offset)
TypeError: 'str' object is not callable
(tmp) โ ๎ฐ afx@proton ๎ฐ /mnt/iso ๎ฐ
Am I doing something wrong?
Thank you!
The zlib compressed part is another compressed archive of files.
The following code is briefly copied from mikrotik-npk and adapted to python3, showing the archive parsing and dumping.
Missing zlib decompression (done using the zlib-flate tool when testing the code), and proper integration into the npkpy code base. Too rusty in Python object model to immediately figure out how this should be done proper.
import os
import argparse
import struct
from pathlib import Path
def parse_data(data):
res = []
while len(data)>30:
header = data[:30]
dsize = struct.unpack("I", header[24:28])[0]
fsize = struct.unpack("H", header[28:30])[0]
if len(data) - 30 - fsize - dsize < 0:
dsize = len(data) - 30 - fsize
fstuff = data[30:30+fsize]
dstuff = data[30+fsize:30+fsize+dsize]
res.append({"header": header, "file": fstuff, "data": dstuff})
data = data[30+fsize+dsize:]
return res
def dump_data(data):
print("Files in package:")
for k in data:
perm, type, z1, z2, tim = struct.unpack("BBHII", k["header"][:12])
if type == 65:
type = "dir"
if not os.access(k["file"], os.F_OK):
os.makedirs(k["file"])
if type == 129:
type = "fil"
f = open(k["file"], "wb")
f.write(k["data"])
f.close()
os.chmod(k["file"], int(perm))
if type == 161:
type = "sym"
os.symlink(k["data"],k["file"])
# os.chmod(k["file"], int(perm))
if perm == 164:
perm = "nx"
if perm == 237:
perm = "ex"
print(type, perm, k["file"], tim)
def parse_args():
parser = argparse.ArgumentParser()
input_group = parser.add_argument_group("input")
input_group.add_argument("--files",
action='append', type=Path, help="Select one or more files to process")
return parser.parse_args()
if __name__ == "__main__":
opts = parse_args()
for file in opts.files:
with open(file, "rb") as f:
data = f.read()
data = parse_data(data)
dump_data(data)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.