demanda
El archivo de plantilla de back-end hace referencia a los recursos de front-end y, cuando se publica, la versión de los recursos de front-end se reemplaza automáticamente con una secuencia de comandos de Python.
Listo para trabajar
El front-end se construye usando Webpack, y cada vez que se completa la compilación, se generará un buildfile.json
buildfile.json
{ "about.css": "about-ca3f85cec35d3ab39ac0.css", "about.js": "about-613281148cb8885f2b3d.js", "home.css": "home-94093b44a5a85be01f38.css", "home.js": " home-6dd4e116818ee4945f65.js " } 复制 代码
Leer el archivo de compilación de front-end
import os import json BUILD_FILES = {} def get_build_files (): build_file_path = '/Users/pengjie/try/iseo2/dist/buildfile.json' if (os.path.exists (build_file_path)): con open (build_file_path, 'r ') como f: return json.load (f) else: print (' el archivo de compilación no existe. ') return' ' BUILD_FILES = get_build_files (); 复制 代码
Obtener todos los archivos de plantilla
def get_template_files (): template_path = '/ Users / pengjie / try / iseo2 / tmpviews' template_files = [] para ruta de directorio, nombres de directorio, nombres de archivo en os.walk (ruta de plantilla): para nombre de archivo en nombres de archivo: if (nombre de archivo [0]! = '.'): filepath = os.path.join (dirpath, filename) template_files.append (filepath) return template_files 复制 代码
Obtenga todos los scripts y enlaces para ser reemplazados
importar sistema operativo importar re de bs4 importar BeautifulSoup def get_replace_list (contenido): replace_list = [] soup = BeautifulSoup (contenido, 'html.parser') # 获取 所有 link links = soup.find_all ('link') para el elemento en los enlaces: nombre = os.path.basename (item.get ('href')) matches = re.search (r '\ - (. +)? (\.)', name, re.I) key = name.replace (matches [0], '.') Replace_list.append ({'old': name, 'new': BUILD_FILES.get (key, '')}) # 获取 所有 script scripts = soup.find_all ('script') para el elemento en scripts: nombre = os.path.basename (item.get ('src')) coincidencias = re.search (r '\ - (. +)? (\.)', nombre, re.I) key = name.replace (coincide con [0], '.') replace_list.append ({'old': name, 'new': BUILD_FILES.get (key, '')}) return replace_list 复制 代码
Encontrar y reemplazar
def search_and_replace (tplpath): content = '' with open (tplpath, 'r', encoding = "utf-8") as f: content = f.read () replace_list = get_replace_list (contenido) para el elemento en replace_list: if ( item ['old'] y item ['new']): content = content.replace (item ['old'], item ['new']) with open (tplpath, "w", encoding = "utf-8 ") como f: f.write (contenido) 复制 代码
Contenido completo del guión
import os import re import json from bs4 import BeautifulSoup BUILD_FILES = {} def run (): view_path = '/ Users / pengjie / try / iseo2 / tmpviews' for dirpath, dirnames, file names in os.walk (view_path): for filename in nombres de archivo: if (filename [0]! = '.'): filepath = os.path.join (dirpath, filename) # 读取 文件 , 获取 替换 列表 content = '' with open (filepath, 'r', encoding = "utf-8") como f: content = f.read () replace_list = get_replace_list (contenido) para el elemento en replace_list: if (elemento ['antiguo'] y elemento ['nuevo']): nombre = os.path.basename (item.get ('href')) coincide con = re.search (r '\ - (. +)? ( \.) ', nombre, re.I) clave = nombre.replace (coincide con [0],'. ') content = content.replace (item ['old'], item ['new']) con open (filepath, "w", encoding = "utf-8") como f: f.write (content) print ('- --- ') # 获取 替换 列表 def get_replace_list (contenido): replace_list = [] soup = BeautifulSoup (contenido,' html.parser ') # 获取 所有 link links = soup.find_all (' link ') para el elemento en los enlaces: replace_list.append ({'antiguo': nombre, 'nuevo': BUILD_FILES.get (clave, '')}) # 获取 所有 script scripts = soup.find_all ('script') para el elemento en scripts: nombre = os.path .basename (item.obtener ('src')) coincidencias = re.search (r '\ - (. +)? (\.)', nombre, re.I) clave = nombre.replace (coincide con [0], '.') replace_list.append ({'old' : name, 'new': BUILD_FILES.get (key, '')}) return replace_list # 获取 构建 文件 def get_build_file (): build_file_path = '/Users/pengjie/try/iseo2/dist/buildfile.json' if (os .path.exists (build_file_path)): con open (build_file_path, 'r') como f: return json.load (f) else: print ('el archivo de compilación no existe.') return '' if __name__ == '__main__' : BUILD_FILES = get_build_file () si (BUILD_FILES): ejecutar () copiar código
Haga clic aquí para obtener el código completo del proyecto