asar 0.1.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- asar/__init__.py +1 -0
- asar/asar.py +284 -0
- asar/integrity.py +47 -0
- asar/limited_reader.py +42 -0
- asar/metadata.py +124 -0
- asar-0.1.1.dist-info/LICENSE +21 -0
- asar-0.1.1.dist-info/METADATA +86 -0
- asar-0.1.1.dist-info/RECORD +10 -0
- asar-0.1.1.dist-info/WHEEL +5 -0
- asar-0.1.1.dist-info/top_level.txt +1 -0
asar/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .asar import AsarArchive, create_archive, extract_archive
|
asar/asar.py
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Asar is a simple extensive archive format for Electron Archive, it works like tar that concatenates all files
|
|
3
|
+
together without compression, while having random access support.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import fnmatch
|
|
7
|
+
import json
|
|
8
|
+
import shutil
|
|
9
|
+
import struct
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import Dict, Any, BinaryIO, List
|
|
12
|
+
|
|
13
|
+
from .limited_reader import LimitedReader
|
|
14
|
+
from .metadata import Metadata, Type
|
|
15
|
+
|
|
16
|
+
UINT32_MAX = 2 ** 32 - 1
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def align_int(i: int, alignment: int) -> int:
|
|
20
|
+
"""
|
|
21
|
+
Align i to alignment, same as `i + (alignment - (i % alignment))`
|
|
22
|
+
"""
|
|
23
|
+
return (i + alignment - 1) & ~(alignment - 1)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
_modes = {"r": "rb", "w": "wb"}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class AsarArchive:
|
|
30
|
+
"""
|
|
31
|
+
Class with methods to open, read, write, close asar files.
|
|
32
|
+
"""
|
|
33
|
+
asar: Path
|
|
34
|
+
asar_unpacked: Path
|
|
35
|
+
_asar_io: BinaryIO
|
|
36
|
+
_mode: str
|
|
37
|
+
_header: Metadata
|
|
38
|
+
_offset: int
|
|
39
|
+
metas: List[Metadata]
|
|
40
|
+
|
|
41
|
+
def __init__(self, asar: Path, mode: str):
|
|
42
|
+
"""
|
|
43
|
+
Open the asar file with mode read 'r', write 'w'
|
|
44
|
+
:param asar: the asar file path
|
|
45
|
+
:param mode: read 'r' or write 'w'
|
|
46
|
+
"""
|
|
47
|
+
self.asar = asar
|
|
48
|
+
self.asar_unpacked = Path(f"{self.asar}.unpacked")
|
|
49
|
+
self._mode = mode
|
|
50
|
+
self._header = Metadata(path=Path(""), type=Type.DIRECTORY, files={})
|
|
51
|
+
self.metas = []
|
|
52
|
+
self._offset = 0
|
|
53
|
+
if self._mode not in _modes:
|
|
54
|
+
raise ValueError("AsarArchive requires mode 'r' or 'w'")
|
|
55
|
+
# noinspection PyTypeChecker
|
|
56
|
+
self._asar_io = self.asar.open(_modes[self._mode])
|
|
57
|
+
|
|
58
|
+
def __enter__(self):
|
|
59
|
+
if self._mode == "r":
|
|
60
|
+
self._read_from_asar()
|
|
61
|
+
return self
|
|
62
|
+
|
|
63
|
+
def __exit__(self, *args):
|
|
64
|
+
if self._mode == "w":
|
|
65
|
+
self._write_to_asar()
|
|
66
|
+
self._asar_io.close()
|
|
67
|
+
|
|
68
|
+
def list(self):
|
|
69
|
+
"""Return a list of file paths in the archive."""
|
|
70
|
+
return [meta.path for meta in self.metas]
|
|
71
|
+
|
|
72
|
+
def pack_other_asar(self, other: "AsarArchive"):
|
|
73
|
+
for meta in other.metas:
|
|
74
|
+
node = self._search_node_from_path(meta.path)
|
|
75
|
+
node.set_from_other(meta)
|
|
76
|
+
if meta.type == Type.FILE and not meta.unpacked:
|
|
77
|
+
node.offset = self._offset
|
|
78
|
+
self._offset += node.size
|
|
79
|
+
|
|
80
|
+
def pack(self, src: Path, unpack: str = None):
|
|
81
|
+
"""
|
|
82
|
+
Add all files from src path to the asar archive.
|
|
83
|
+
:param src: src path
|
|
84
|
+
:param unpack: do not pack files matching glob <expression>
|
|
85
|
+
"""
|
|
86
|
+
if not src:
|
|
87
|
+
raise ValueError("src path not provided")
|
|
88
|
+
if not src.is_absolute() or src.is_symlink():
|
|
89
|
+
src = src.resolve()
|
|
90
|
+
for path in src.iterdir():
|
|
91
|
+
self._pack(path, src, unpack)
|
|
92
|
+
|
|
93
|
+
def _pack(self, path: Path, src: Path, unpack: str):
|
|
94
|
+
path_in = path.relative_to(src)
|
|
95
|
+
if path.is_symlink():
|
|
96
|
+
node = self._search_node_from_path(path_in)
|
|
97
|
+
node.set_link(path.resolve().relative_to(src))
|
|
98
|
+
if node.link.parts[0] == "..":
|
|
99
|
+
raise ValueError(f'${path_in}: file "{node.link}" links out of the package')
|
|
100
|
+
elif path.is_dir():
|
|
101
|
+
node = self._search_node_from_path(path_in)
|
|
102
|
+
node.set_dir(unpacked=unpack and fnmatch.fnmatch(path.name, unpack))
|
|
103
|
+
for child in path.iterdir():
|
|
104
|
+
self._pack(child, src, unpack)
|
|
105
|
+
else:
|
|
106
|
+
self.pack_file(path_in, path, should_unpack=unpack and fnmatch.fnmatch(path.name, unpack))
|
|
107
|
+
|
|
108
|
+
def pack_stream(self, path_in: Path, file_reader: BinaryIO, should_unpack: bool = False):
|
|
109
|
+
"""
|
|
110
|
+
Add one file stream to the asar archive.
|
|
111
|
+
:param path_in: the path in asar archive.
|
|
112
|
+
:param file_reader: the file stream.
|
|
113
|
+
:param should_unpack: unpacked in asar? If not packed, it will be written to unpacked path
|
|
114
|
+
:return:
|
|
115
|
+
"""
|
|
116
|
+
dir_node = self._search_node_from_path(path_in.parent)
|
|
117
|
+
node = self._search_node_from_path(path_in)
|
|
118
|
+
node.set_stream(file_reader, unpacked=dir_node.unpacked or should_unpack)
|
|
119
|
+
if not node.unpacked:
|
|
120
|
+
if node.size > UINT32_MAX:
|
|
121
|
+
raise ValueError(f"{path_in}: file size can not be larger than 4.2GB")
|
|
122
|
+
node.offset = self._offset
|
|
123
|
+
self._offset += node.size
|
|
124
|
+
|
|
125
|
+
def pack_file(self, path_in: Path, file_path: Path, should_unpack: bool = False):
|
|
126
|
+
"""
|
|
127
|
+
Add one file to the asar archive.
|
|
128
|
+
:param path_in: the path in asar archive.
|
|
129
|
+
:param file_path: the actual file path.
|
|
130
|
+
:param should_unpack: unpacked in asar? If not packed, it will be written to unpacked path
|
|
131
|
+
"""
|
|
132
|
+
dir_node = self._search_node_from_path(path_in.parent)
|
|
133
|
+
node = self._search_node_from_path(path_in)
|
|
134
|
+
node.set_file(file_path, unpacked=dir_node.unpacked or should_unpack)
|
|
135
|
+
if not node.unpacked:
|
|
136
|
+
if node.size > UINT32_MAX:
|
|
137
|
+
raise ValueError(f"{path_in}: file size can not be larger than 4.2GB")
|
|
138
|
+
node.offset = self._offset
|
|
139
|
+
self._offset += node.size
|
|
140
|
+
|
|
141
|
+
def read(self, path_in: Path, follow_link: bool = True):
|
|
142
|
+
"""
|
|
143
|
+
Read one file from asar by random access
|
|
144
|
+
:param path_in: path in asar
|
|
145
|
+
:param follow_link: read the actual file if the path is a link
|
|
146
|
+
:return: file bytes
|
|
147
|
+
"""
|
|
148
|
+
node = self._search_node_from_path(path_in, create=False)
|
|
149
|
+
if node.type == Type.DIRECTORY:
|
|
150
|
+
return b""
|
|
151
|
+
elif node.type == Type.LINK:
|
|
152
|
+
return self.read(node.link) if follow_link else b""
|
|
153
|
+
elif node.unpacked:
|
|
154
|
+
if not node.file_path.exists():
|
|
155
|
+
raise FileNotFoundError(node.file_path)
|
|
156
|
+
with node.file_path.open("rb") as reader:
|
|
157
|
+
return reader.read()
|
|
158
|
+
else:
|
|
159
|
+
node.file_reader.seek(0)
|
|
160
|
+
return node.file_reader.read()
|
|
161
|
+
|
|
162
|
+
def extract(self, dst: Path = None):
|
|
163
|
+
"""
|
|
164
|
+
Extract the asar archive to the dst path.
|
|
165
|
+
:param dst: dst dir path
|
|
166
|
+
"""
|
|
167
|
+
dst = dst if dst else Path.cwd()
|
|
168
|
+
dst.mkdir(parents=True, exist_ok=True)
|
|
169
|
+
if not dst.is_absolute() or dst.is_symlink():
|
|
170
|
+
dst = dst.resolve()
|
|
171
|
+
for meta in self.metas:
|
|
172
|
+
cur_dst = dst / meta.path
|
|
173
|
+
if meta.type == Type.DIRECTORY:
|
|
174
|
+
cur_dst.mkdir(parents=True, exist_ok=True)
|
|
175
|
+
elif meta.type == Type.LINK:
|
|
176
|
+
src_link = dst / meta.link
|
|
177
|
+
try:
|
|
178
|
+
cur_dst.symlink_to(src_link)
|
|
179
|
+
except FileExistsError:
|
|
180
|
+
cur_dst.unlink()
|
|
181
|
+
cur_dst.symlink_to(src_link)
|
|
182
|
+
elif meta.unpacked:
|
|
183
|
+
if not meta.file_path.exists():
|
|
184
|
+
raise FileNotFoundError(meta.file_path)
|
|
185
|
+
shutil.copyfile(meta.file_path, cur_dst)
|
|
186
|
+
else:
|
|
187
|
+
with cur_dst.open(mode="wb") as writer:
|
|
188
|
+
meta.file_reader.seek(0)
|
|
189
|
+
shutil.copyfileobj(meta.file_reader, writer)
|
|
190
|
+
|
|
191
|
+
def _read_from_asar(self):
|
|
192
|
+
data_size, header_size, header_object_size, header_string_size = struct.unpack("<4I", self._asar_io.read(16))
|
|
193
|
+
header = json.loads(self._asar_io.read(header_string_size).decode("utf-8"))
|
|
194
|
+
self._offset = 8 + header_size
|
|
195
|
+
self._parse_metadata(header, Path(""))
|
|
196
|
+
|
|
197
|
+
def _parse_metadata(self, info: Dict[str, Any], path_in: Path):
|
|
198
|
+
for name, child in info["files"].items():
|
|
199
|
+
cur_path = path_in / name
|
|
200
|
+
node = self._search_node_from_path(cur_path)
|
|
201
|
+
if "files" in child:
|
|
202
|
+
node.set_dir("unpacked" in child and bool(child["unpacked"]))
|
|
203
|
+
self._parse_metadata(child, cur_path)
|
|
204
|
+
elif "link" in child:
|
|
205
|
+
node.set_link(Path(child["link"]))
|
|
206
|
+
else:
|
|
207
|
+
node.unpacked = "unpacked" in child and bool(child["unpacked"])
|
|
208
|
+
node.type = Type.FILE
|
|
209
|
+
node.integrity = child["integrity"]
|
|
210
|
+
node.size = child["size"]
|
|
211
|
+
if node.unpacked:
|
|
212
|
+
node.file_path = self.asar_unpacked / node.path
|
|
213
|
+
else:
|
|
214
|
+
node.offset = int(child["offset"])
|
|
215
|
+
node.file_reader = LimitedReader(self._asar_io, self._offset + node.offset, node.size)
|
|
216
|
+
|
|
217
|
+
def _write_to_asar(self):
|
|
218
|
+
header_json = json.dumps(self._header.to_dict(), sort_keys=True, separators=(",", ":"), ensure_ascii=False)
|
|
219
|
+
header_json = header_json.encode("utf-8")
|
|
220
|
+
data_size = 4
|
|
221
|
+
header_string_size = len(header_json)
|
|
222
|
+
aligned_size = align_int(header_string_size, data_size)
|
|
223
|
+
header_object_size = aligned_size + data_size
|
|
224
|
+
header_size = header_object_size + data_size
|
|
225
|
+
self._asar_io.write(struct.pack("<4I", data_size, header_size, header_object_size, header_string_size))
|
|
226
|
+
self._asar_io.write(header_json)
|
|
227
|
+
self._asar_io.write(b'\0' * (aligned_size - header_string_size))
|
|
228
|
+
for metadata in self.metas:
|
|
229
|
+
if metadata.type != Type.FILE:
|
|
230
|
+
continue
|
|
231
|
+
if metadata.unpacked:
|
|
232
|
+
dst = self.asar_unpacked / metadata.path
|
|
233
|
+
dst.parent.mkdir(parents=True, exist_ok=True)
|
|
234
|
+
if metadata.file_path:
|
|
235
|
+
shutil.copyfile(metadata.file_path, dst)
|
|
236
|
+
elif metadata.file_reader:
|
|
237
|
+
with dst.open("wb") as writer:
|
|
238
|
+
metadata.file_reader.seek(0)
|
|
239
|
+
shutil.copyfileobj(metadata.file_reader, writer)
|
|
240
|
+
else:
|
|
241
|
+
if metadata.file_path:
|
|
242
|
+
with open(metadata.file_path, "rb") as reader:
|
|
243
|
+
shutil.copyfileobj(reader, self._asar_io)
|
|
244
|
+
elif metadata.file_reader:
|
|
245
|
+
metadata.file_reader.seek(0)
|
|
246
|
+
shutil.copyfileobj(metadata.file_reader, self._asar_io)
|
|
247
|
+
|
|
248
|
+
def _search_node_from_path(self, path_in: Path, create: bool = True) -> Metadata:
|
|
249
|
+
node = self._header
|
|
250
|
+
name = path_in.name
|
|
251
|
+
if not name:
|
|
252
|
+
return node
|
|
253
|
+
for part in path_in.parent.parts:
|
|
254
|
+
if part != ".":
|
|
255
|
+
node = node.files[part]
|
|
256
|
+
if node.files.get(name) is None:
|
|
257
|
+
if create:
|
|
258
|
+
metadata = Metadata(path=path_in)
|
|
259
|
+
node.files[name] = metadata
|
|
260
|
+
self.metas.append(metadata)
|
|
261
|
+
else:
|
|
262
|
+
raise FileNotFoundError(path_in)
|
|
263
|
+
return node.files[name]
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
def create_archive(dir_path: Path, asar_path: Path, unpack: str = None):
|
|
267
|
+
"""
|
|
268
|
+
Pack the dir to create an asar archive.
|
|
269
|
+
:param dir_path: The dir to pack.
|
|
270
|
+
:param asar_path: The output asar path.
|
|
271
|
+
:param unpack: do not pack files matching glob <expression>
|
|
272
|
+
"""
|
|
273
|
+
with AsarArchive(asar_path, "w") as asar:
|
|
274
|
+
asar.pack(dir_path, unpack)
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def extract_archive(asar_path: Path, dir_path: Path):
|
|
278
|
+
"""
|
|
279
|
+
Extract the asar archive to the dir.
|
|
280
|
+
:param asar_path: The asar archive path to extract.
|
|
281
|
+
:param dir_path: The output dir path.
|
|
282
|
+
"""
|
|
283
|
+
with AsarArchive(asar_path, "r") as asar:
|
|
284
|
+
asar.extract(dir_path)
|
asar/integrity.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""
|
|
2
|
+
integrity is an object consisting of a few keys:
|
|
3
|
+
|
|
4
|
+
- A hashing algorithm, currently only SHA256 is supported.
|
|
5
|
+
- A hex encoded hash value representing the hash of the entire file.
|
|
6
|
+
- An array of hex encoded hashes for the blocks of the file. i.e. for a blockSize of 4KB this array contains the hash of every block if you split the file into N 4KB blocks.
|
|
7
|
+
- A integer value blockSize representing the size in bytes of each block in the blocks hashes above
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import hashlib
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from typing import BinaryIO, Dict, Any, Tuple
|
|
13
|
+
|
|
14
|
+
ALGORITHM = "SHA256"
|
|
15
|
+
BLOCK_SIZE = 4 * 1024 * 1024
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def get_file_integrity(file_path: Path) -> Dict[str, Any]:
|
|
19
|
+
"""
|
|
20
|
+
Calc the integrity of a file.
|
|
21
|
+
:param file_path: The file path.
|
|
22
|
+
:return: integrity
|
|
23
|
+
"""
|
|
24
|
+
with file_path.open("rb") as reader:
|
|
25
|
+
return get_reader_integrity(reader)[0]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def get_reader_integrity(reader: BinaryIO) -> Tuple[Dict[str, Any], int]:
|
|
29
|
+
"""
|
|
30
|
+
Calc the integrity of a file stream.
|
|
31
|
+
:param reader: Input file stream, it should be readable and seekable.
|
|
32
|
+
:return: (integrity, size)
|
|
33
|
+
"""
|
|
34
|
+
hasher = hashlib.sha256()
|
|
35
|
+
blocks = []
|
|
36
|
+
size = 0
|
|
37
|
+
reader.seek(0)
|
|
38
|
+
while chunk := reader.read(BLOCK_SIZE):
|
|
39
|
+
size += len(chunk)
|
|
40
|
+
hasher.update(chunk)
|
|
41
|
+
blocks.append(hashlib.sha256(chunk).hexdigest())
|
|
42
|
+
return {
|
|
43
|
+
"algorithm": ALGORITHM,
|
|
44
|
+
"hash": hasher.hexdigest(),
|
|
45
|
+
"blockSize": BLOCK_SIZE,
|
|
46
|
+
"blocks": blocks,
|
|
47
|
+
}, size
|
asar/limited_reader.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import io
|
|
2
|
+
from typing import BinaryIO
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class LimitedReader:
|
|
6
|
+
"""
|
|
7
|
+
LimitedReader wraps a part of a reader into a reader.
|
|
8
|
+
"""
|
|
9
|
+
_reader: BinaryIO
|
|
10
|
+
_offset: int
|
|
11
|
+
_size: int
|
|
12
|
+
_remaining: int
|
|
13
|
+
|
|
14
|
+
def __init__(self, reader: BinaryIO, offset: int, size: int):
|
|
15
|
+
self._reader = reader
|
|
16
|
+
self._offset = offset
|
|
17
|
+
self._size = size
|
|
18
|
+
self._remaining = size
|
|
19
|
+
|
|
20
|
+
def read(self, n=-1):
|
|
21
|
+
if n == -1 or n > self._remaining:
|
|
22
|
+
n = self._remaining
|
|
23
|
+
if n == 0:
|
|
24
|
+
return b''
|
|
25
|
+
chunk = self._reader.read(n)
|
|
26
|
+
self._remaining -= len(chunk)
|
|
27
|
+
return chunk
|
|
28
|
+
|
|
29
|
+
def seek(self, offset, whence=io.SEEK_SET):
|
|
30
|
+
end_offset = self._offset + self._size
|
|
31
|
+
if whence == io.SEEK_SET:
|
|
32
|
+
offset = self._offset + offset
|
|
33
|
+
elif whence == io.SEEK_CUR:
|
|
34
|
+
offset = self._reader.tell() + offset
|
|
35
|
+
elif whence == io.SEEK_END:
|
|
36
|
+
offset = end_offset + offset
|
|
37
|
+
else:
|
|
38
|
+
raise ValueError("invalid whence value")
|
|
39
|
+
if not (self._offset <= offset < end_offset):
|
|
40
|
+
raise ValueError("seek position is out of bounds")
|
|
41
|
+
self._remaining = end_offset - offset
|
|
42
|
+
return self._reader.seek(offset, whence)
|
asar/metadata.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from enum import Enum
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from typing import Dict, Any, Union, BinaryIO
|
|
6
|
+
|
|
7
|
+
from .integrity import get_file_integrity, get_reader_integrity
|
|
8
|
+
from .limited_reader import LimitedReader
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class Type(Enum):
|
|
12
|
+
"""
|
|
13
|
+
File Type
|
|
14
|
+
"""
|
|
15
|
+
DIRECTORY = 1
|
|
16
|
+
LINK = 2
|
|
17
|
+
FILE = 3
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass
|
|
21
|
+
class Metadata:
|
|
22
|
+
"""
|
|
23
|
+
File metadata stored in the header.
|
|
24
|
+
"""
|
|
25
|
+
# path in asar file
|
|
26
|
+
path: Path
|
|
27
|
+
type: Type = None
|
|
28
|
+
# directory
|
|
29
|
+
files: Dict[str, "Metadata"] = None
|
|
30
|
+
# link
|
|
31
|
+
link: Path = None
|
|
32
|
+
# file
|
|
33
|
+
unpacked: bool = False
|
|
34
|
+
size: int = 0
|
|
35
|
+
offset: int = 0
|
|
36
|
+
integrity: Dict[str, Any] = None
|
|
37
|
+
executable: bool = False
|
|
38
|
+
# real file path or stream to read
|
|
39
|
+
file_path: Path = None
|
|
40
|
+
file_reader: Union[BinaryIO, LimitedReader] = None
|
|
41
|
+
|
|
42
|
+
def set_link(self, link_in: Path):
|
|
43
|
+
"""
|
|
44
|
+
Set the node as a link node
|
|
45
|
+
:param link_in: the target path to which the symbolic link points
|
|
46
|
+
"""
|
|
47
|
+
self.type = Type.LINK
|
|
48
|
+
self.link = link_in
|
|
49
|
+
|
|
50
|
+
def set_dir(self, unpacked: bool):
|
|
51
|
+
"""
|
|
52
|
+
Set the node as a directory node
|
|
53
|
+
:param unpacked: Exclude from packaging?
|
|
54
|
+
"""
|
|
55
|
+
self.unpacked = unpacked
|
|
56
|
+
self.type = Type.DIRECTORY
|
|
57
|
+
self.files = {}
|
|
58
|
+
|
|
59
|
+
def set_file(self, file_path: Path, unpacked: bool):
|
|
60
|
+
"""
|
|
61
|
+
Set the node as a file node
|
|
62
|
+
:param file_path: The actual file path, through which we read file size and data
|
|
63
|
+
:param unpacked: Exclude from packaging?
|
|
64
|
+
"""
|
|
65
|
+
self.unpacked = unpacked
|
|
66
|
+
self.type = Type.FILE
|
|
67
|
+
stat = file_path.stat()
|
|
68
|
+
self.size = stat.st_size
|
|
69
|
+
self.integrity = get_file_integrity(file_path)
|
|
70
|
+
self.file_path = file_path
|
|
71
|
+
self.executable = os.name != "nt" and (stat.st_mode & 0o100)
|
|
72
|
+
|
|
73
|
+
def set_stream(self, file_reader: Union[BinaryIO, LimitedReader], unpacked: bool):
|
|
74
|
+
"""
|
|
75
|
+
Set the node as a file stream node
|
|
76
|
+
:param file_reader: The actual file stream, through which we read file size and data
|
|
77
|
+
:param unpacked: Exclude from packaging?
|
|
78
|
+
"""
|
|
79
|
+
self.unpacked = unpacked
|
|
80
|
+
self.type = Type.FILE
|
|
81
|
+
self.integrity, self.size = get_reader_integrity(file_reader)
|
|
82
|
+
self.file_reader = file_reader
|
|
83
|
+
|
|
84
|
+
def set_from_other(self, other: "Metadata"):
|
|
85
|
+
"""
|
|
86
|
+
Set the node from other metadata, but not including files and offset
|
|
87
|
+
:param other:
|
|
88
|
+
"""
|
|
89
|
+
self.path = other.path
|
|
90
|
+
self.type = other.type
|
|
91
|
+
if self.type == Type.LINK:
|
|
92
|
+
self.link = other.link
|
|
93
|
+
elif self.type == Type.DIRECTORY:
|
|
94
|
+
self.unpacked = other.unpacked
|
|
95
|
+
self.files = {}
|
|
96
|
+
else:
|
|
97
|
+
self.unpacked = other.unpacked
|
|
98
|
+
self.size = other.size
|
|
99
|
+
self.integrity = other.integrity
|
|
100
|
+
self.executable = other.executable
|
|
101
|
+
self.file_path = other.file_path
|
|
102
|
+
self.file_reader = other.file_reader
|
|
103
|
+
|
|
104
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
105
|
+
"""
|
|
106
|
+
Serialize to a formatted dict, usually the next step is to serialize to json
|
|
107
|
+
"""
|
|
108
|
+
if self.type == Type.DIRECTORY:
|
|
109
|
+
files = {}
|
|
110
|
+
for key, value in self.files.items():
|
|
111
|
+
files[key] = value.to_dict()
|
|
112
|
+
return {"files": files}
|
|
113
|
+
|
|
114
|
+
if self.type == Type.LINK:
|
|
115
|
+
return {"link": str(self.link)}
|
|
116
|
+
|
|
117
|
+
rst = {"size": self.size, "integrity": self.integrity}
|
|
118
|
+
if self.unpacked:
|
|
119
|
+
rst["unpacked"] = self.unpacked
|
|
120
|
+
else:
|
|
121
|
+
rst["offset"] = str(self.offset)
|
|
122
|
+
if self.executable:
|
|
123
|
+
rst["executable"] = self.executable
|
|
124
|
+
return rst
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 black-06
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: asar
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Library for working with Electron's ASAR archive files
|
|
5
|
+
Author-email: Black <hello.bug@foxmail.com>
|
|
6
|
+
Project-URL: Homepage, https://github.com/black-06/asar-py
|
|
7
|
+
Keywords: asar
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
|
|
12
|
+
# asar
|
|
13
|
+
|
|
14
|
+
Library for working with [Electron's ASAR](https://github.com/electron/asar) archive files.
|
|
15
|
+
|
|
16
|
+
# Installation
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
pip install asar
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
# Usage
|
|
23
|
+
|
|
24
|
+
- [X] pack and extract asar.
|
|
25
|
+
```python
|
|
26
|
+
from pathlib import Path
|
|
27
|
+
from asar import create_archive, extract_archive
|
|
28
|
+
|
|
29
|
+
create_archive(Path("src"), Path("app.asar"))
|
|
30
|
+
extract_archive(Path("app.asar"), Path("dst"))
|
|
31
|
+
```
|
|
32
|
+
- [X] with unpack expression
|
|
33
|
+
```python
|
|
34
|
+
create_archive(Path("src"), Path("app.asar"), unpack="*.exe")
|
|
35
|
+
```
|
|
36
|
+
then we got:
|
|
37
|
+
```
|
|
38
|
+
.
|
|
39
|
+
├── app.asar
|
|
40
|
+
└── app.asar.unpacked
|
|
41
|
+
└── f2.exe
|
|
42
|
+
```
|
|
43
|
+
- [X] list files of asar
|
|
44
|
+
```python
|
|
45
|
+
from pathlib import Path
|
|
46
|
+
from asar import AsarArchive
|
|
47
|
+
|
|
48
|
+
with AsarArchive(Path("app.asar"), mode="r") as archive:
|
|
49
|
+
print(archive.list())
|
|
50
|
+
```
|
|
51
|
+
then we got:
|
|
52
|
+
```
|
|
53
|
+
[Path('assets'), Path('assets/icon.png'), ...]
|
|
54
|
+
```
|
|
55
|
+
- [X] read one file from asar by random access
|
|
56
|
+
```python
|
|
57
|
+
from pathlib import Path
|
|
58
|
+
from asar import AsarArchive
|
|
59
|
+
|
|
60
|
+
with AsarArchive(Path("app.asar"), mode="r") as archive:
|
|
61
|
+
print(archive.read(Path("f1.txt"), follow_link=True))
|
|
62
|
+
```
|
|
63
|
+
then we got:
|
|
64
|
+
```
|
|
65
|
+
b'Hello, Asar'
|
|
66
|
+
```
|
|
67
|
+
- [X] create an asar from another asar, without extracting to filesystem.
|
|
68
|
+
```python
|
|
69
|
+
import io
|
|
70
|
+
from pathlib import Path
|
|
71
|
+
from asar import AsarArchive
|
|
72
|
+
|
|
73
|
+
with AsarArchive(Path("app.asar"), mode="r") as reader, AsarArchive(Path("app.new.asar"), "w") as writer:
|
|
74
|
+
writer.pack_other_asar(reader)
|
|
75
|
+
writer.pack_file(path_in=Path("assets/added1.txt"), file_path=Path("added.txt"))
|
|
76
|
+
writer.pack_stream(path_in=Path("assets/added2.txt"), file_reader=io.BytesIO(b"some text"))
|
|
77
|
+
```
|
|
78
|
+
then we got app.new.asar, extract it:
|
|
79
|
+
```
|
|
80
|
+
app_new_asar_extract
|
|
81
|
+
├── assets
|
|
82
|
+
│ ├── added1.txt
|
|
83
|
+
│ ├── added2.txt
|
|
84
|
+
│ └── ... other files from app.asar
|
|
85
|
+
└── ... other files from app.asar
|
|
86
|
+
```
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
asar/__init__.py,sha256=HiOtBER2SlvilBik6CHhX5XHMF-1g9t7TL3pKNJZKus,63
|
|
2
|
+
asar/asar.py,sha256=gcLeXx0QPVrhR5qzo0EkmezxxRqgG2At16Sj1kssANM,11141
|
|
3
|
+
asar/integrity.py,sha256=t2Yh3kz_7L9Es7NrLgLwh25jvhyvq72jsmc3Ty79CF8,1482
|
|
4
|
+
asar/limited_reader.py,sha256=i-S7SgL1KbE8bLQHaSyX7sa1kS8Sj1VnjSBxfyEgvdE,1248
|
|
5
|
+
asar/metadata.py,sha256=NhuFXY7RuEOWyCMQOlai8BgAYfDdOGi90yjuZu18vbE,3735
|
|
6
|
+
asar-0.1.1.dist-info/LICENSE,sha256=0cPZbIQWozYMgJUfGmq1JqzFxSKqVJs6Rs1pGyXKjK0,1065
|
|
7
|
+
asar-0.1.1.dist-info/METADATA,sha256=csSal3-wZO-7qV51990G8K8ps8O6OnJeYsI843XH5Nc,2361
|
|
8
|
+
asar-0.1.1.dist-info/WHEEL,sha256=cVxcB9AmuTcXqmwrtPhNK88dr7IR_b6qagTj0UvIEbY,91
|
|
9
|
+
asar-0.1.1.dist-info/top_level.txt,sha256=hpIVuuk59NiShfhSkWY_8wETcftWKLgSUnvy-YXQ3xg,5
|
|
10
|
+
asar-0.1.1.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
asar
|