-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoDist.py
194 lines (148 loc) · 7.13 KB
/
autoDist.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
#! /user/bin/python3
# Copyright (c) 2024. All rights reserved.
# This source code is licensed under the CC BY-NC-SA
# (Creative Commons Attribution-NonCommercial-NoDerivatives) License, By Xiao Songtao.
# This software is protected by copyright law. Reproduction, distribution, or use for commercial
# purposes is prohibited without the author's permission. If you have any questions or require
# permission, please contact the author: [email protected]
# -------------------------<edocsitahw>----------------------------
# 传建时间: 2024/7/16 下午2:11
# 当前项目名: vueOperation.py
# 编码模式: utf-8
# 注释:
# -------------------------<edocsitahw>----------------------------
from os import path, PathLike, listdir, walk, rename, remove
from typing import Literal, TypedDict
from shutil import move
from functools import cache, cached_property
from warnings import warn
from re import sub, compile, findall
from systemTools import instruct
ModeType = Literal['abs', 'dir']
class ModifyInfo(TypedDict):
suffix: str
subDirName: str
modifyPath: str | PathLike[str]
class NotAbsPathError(Exception):
pass
class autoDist:
def __init__(self, distPath: str | PathLike[str], toPath: str | PathLike[str], *, staticDirName: str = 'static'):
self._checkPath(distPath, mode=['abs', 'dir'])
self._checkPath(toPath, mode=['abs', 'dir'])
self._ins = instruct(ignore=True)
self._distPath = distPath
self._toPath = toPath
self._staticDirName = staticDirName
@cached_property
def assetsPath(self) -> str | PathLike[str]:
return self._checkPath(path.join(self._distPath, 'assets'))
@cached_property
def indexPath(self) -> str | PathLike[str]:
return self._checkPath(path.join(self._distPath, 'index.html'))
@cached_property
def jsPath(self) -> str | PathLike[str]:
return self._checkPath(self._filePath(self.assetsPath, suffix='js'))
@cached_property
def cssPath(self) -> str | PathLike[str]:
return self._checkPath(self._filePath(self.assetsPath, suffix='css'))
@cached_property
def staticPath(self):
return path.join(self._toPath, self._staticDirName)
@cache
def _filePath(self, basePath: str | PathLike[str], *, suffix: str) -> str | PathLike[str]:
for file in listdir(basePath):
if file.endswith(f".{suffix}"):
return path.join(basePath, file)
warn(
f"No {suffix} file found in {basePath}")
@staticmethod
def _rename(src: str | PathLike[str], dst: str | PathLike[str], *, force: bool = False):
if force and path.exists(p := path.join(dst)):
remove(p)
try:
rename(src, dst)
except FileExistsError:
pass
@staticmethod
def _checkPath(_path: str | PathLike[str], *, mode: ModeType | list[ModeType] = None) -> str | PathLike[str]:
if not path.exists(_path):
raise FileNotFoundError(
f"The path '{_path}' does not exist!")
match mode:
case None:
return _path
case 'abs':
if not path.isabs(_path):
raise FileNotFoundError() from NotAbsPathError(
f"The path '{_path}' is not an absolute path!")
case 'dir':
if not path.isdir(_path):
raise NotADirectoryError(
f"The path '{_path}' is not a directory!")
case _:
if isinstance(mode, list):
for m in mode:
autoDist._checkPath(_path, mode=m)
else:
raise ValueError(
f"The argument '{mode}' is not a valid mode!")
return _path
def _moveFile(self, filePath: str | PathLike[str], *, modeifyIndex: bool = False, subDirName: str = None):
_, suffix = path.basename(filePath).split('.')
if subDirName is None: subDirName = suffix
try:
self._rename(filePath, path.join(self.staticPath, subDirName, path.basename(filePath)))
except FileExistsError:
pass
if modeifyIndex:
with open(self.indexPath, 'r+', encoding='utf-8') as file:
content = file.read()
constent = sub(fr'(?<=")(/assets/)(index-.*\.{suffix})(?=")', fr'../static/{suffix}/\2', content)
file.seek(0)
file.write(constent)
def _autoMove(self, dirPath: str | PathLike[str], *modifyInfo: ModifyInfo):
files = listdir(dirPath)
for info in modifyInfo:
for file in files:
if file.endswith(info['suffix']):
self._moveFile(path.join(dirPath, file), subDirName=info['subDirName'])
if path.exists(info['modifyPath']):
with open(info['modifyPath'], 'r+', encoding='utf-8') as f:
content = f.read()
if findall(pattren := rf"(?<=url\()(/assets/)({file})(?=\))", content):
content = sub(pattren, fr'../{info["subDirName"]}/\2', content)
elif findall(pattren := rf"(?<=Zx=\")(/assets/)({file})(?=\")", content):
content = sub(pattren, fr'../static/{info["subDirName"]}/\2', content)
f.seek(0)
f.write(content)
def move(self):
self._moveFile(self.jsPath, modeifyIndex=True)
self._moveFile(self.cssPath, modeifyIndex=True)
self._autoMove(self.assetsPath,
{
'suffix': 'ttf',
'subDirName': 'font',
'modifyPath': path.join(self.staticPath, 'css', path.basename(self.cssPath))
},
{
'suffix': 'woff',
'subDirName': 'font',
'modifyPath': path.join(self.staticPath, 'css', path.basename(self.cssPath))
},
{
'suffix': 'woff2',
'subDirName': 'font',
'modifyPath': path.join(self.staticPath, 'css', path.basename(self.cssPath))
},
{
'suffix': 'png',
'subDirName': 'img',
'modifyPath': path.join(self.staticPath, 'js', path.basename(self.jsPath))
}
)
self._rename(self.indexPath, path.join(self._toPath, 'template', 'index.html'), force=True)
self._ins('rd /s /q ' + self._distPath)
if __name__ == '__main__':
# autoDist(r"E:\codeSpace\codeSet\web\project\examPlatform\dist", r"E:\codeSpace\codeSet\pyAndWeb\project\examPlatform").move()
autoDist(r"E:\codeSpace\codeSet\web\project\tinyWeb\dist", r"E:\codeSpace\codeSet\pyAndWeb\project\tinyWeb").move()
# autoDist(r"E:\codeSpace\codeSet\web\project\server\dist", r"E:\codeSpace\codeSet\pyAndWeb\project\server").move()