-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
executable file
·202 lines (159 loc) · 5.22 KB
/
utils.py
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
import os
import sys
from typing import Optional, Any, List
import pathlib
import json
import platform
import tempfile
import shutil
_KB = 1024
_MB = 1024*1024
_GB = 1024*1024*1024
def to_human_addr(value: int) -> str:
return hex(value)
def to_human_size(value: int) -> str:
if value == 0:
return "0"
if value%_GB == 0:
return f'{value//_GB}*GB'
if value%_MB == 0:
return f'{value//_MB}*MB'
if value%_KB == 0:
return f'{value//_KB}*KB'
return str(value)
def from_human_num(s) -> Optional[int]:
_globals = {
'__builtins__':None,
'KB': 1024,
'MB': 1024*1024,
'GB': 1024*1024*1024,
}
try:
value = eval(s, _globals)
return int(value)
except:
return None
def get_template_path(name: str) -> str:
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
meipass = getattr(sys, '_MEIPASS')
return os.path.join(meipass, "template", name)
return os.path.join("assets", "template", name)
def get_cpio() -> str:
if platform.system() == "Windows":
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
meipass = getattr(sys, '_MEIPASS')
return os.path.join(meipass, "tools", "win32", "cpio.exe")
current = os.path.abspath(os.path.dirname(__file__))
return os.path.join(current, "tools", "win32", "cpio.exe")
return "cpio"
class CpioUtil():
def __init__(self, filename) -> None:
self._filename = filename
self._temp_dir = tempfile.mkdtemp(prefix='jh_')
self._temp_cpio = None
def __del__(self):
shutil.rmtree(self._temp_dir)
self._temp_dir = None
def append(self, name: str, data: bytes) -> bool:
if self._temp_cpio is None:
temp = os.path.join(self._temp_dir, "temp.cpio")
try:
shutil.copy(self._filename, temp)
except:
return False
self._temp_cpio = temp
cwd = os.getcwd()
cpio_exe = get_cpio()
path = os.path.join(self._temp_dir, name)
print("path: ", path)
if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
try:
with open(path, "wb") as f:
f.write(data)
except:
return False
os.chdir(self._temp_dir)
cmd = f"echo {name} | {cpio_exe} -oA -H newc -F temp.cpio"
print(cmd)
status = os.system(cmd)
os.chdir(cwd)
if status != 0:
return False
return True
def get_bytes(self) -> Optional[bytes]:
fn = self._filename
if self._temp_cpio:
fn = self._temp_cpio
try:
with open(fn, "rb") as f:
return f.read()
except:
return None
return None
def save_as(self, dst: str) -> bool:
fn = self._filename
if self._temp_cpio:
fn = self._temp_cpio
try:
shutil.copy(fn, dst)
except:
return False
return True
def profile_load() -> dict:
fn = os.path.join(pathlib.Path.home(), ".resource_tool")
try:
with open(fn, 'rt', encoding='utf8') as f:
profile = json.load(f)
if isinstance(profile, dict):
return profile
return {}
except:
return {}
class Profile(object):
profile = profile_load()
@classmethod
def profile_get(cls, name, def_value) -> Any:
value = cls.profile.get(name)
if isinstance(value, type(def_value)):
return value
return def_value
@classmethod
def profile_set(cls, name: str, value: Any):
cls.profile[name] = value
fn = os.path.join(pathlib.Path.home(), ".resource_tool")
try:
with open(fn, 'wt', encoding='utf8') as f:
json.dump(cls.profile, f)
except:
pass
class Item(object):
def __init__(self, name, def_value) -> None:
self._name = name
self._value = Profile.profile_get(name, def_value)
def get(self):
return self._value
def set(self, value):
if not isinstance(value, type(self._value)):
return
self._value = value
Profile.profile_set(self._name, self._value)
if __name__ == '__main__':
# cmd = f"{get_cpio()} -t -H newc -F test.cpio"
# print(cmd)
# os.system(cmd)
# test_cpio = os.path.join(os.getcwd(), "test.cpio")
# x = CpioUtil(test_cpio)
# x.append("AAA", b'AAA')
# x.save_as("test.cpio")
import fdt
import pyfdt
from pyfdt.pyfdt import FdtBlobParse
from pyfdt.pyfdt import FdtFsParse
dtb = open("F:/02_实施中项目/04_613虚拟化/99_临时文件/d2000-linux/d2000-guestos.dtb", "rb").read()
#x = fdt.parse_dtb(dtb)
dts = open("platform/d2000-guestos.dts", "rt").read()
x = fdt.parse_dts(dts)
print(x)
FdtBlobParse(dts)
print(x.to_dtb(version=17))