使用Python指令碼替換模板檔案中的版本號!

2021-10-24 16:54:39 字數 3811 閱讀 9820

後端模板檔案引用前端資源,發布時,使用python指令碼自動替換前端資源的版本。

前端使用webpack構建,每次構建完成時,會生成乙個buildfile.json

buildfile.json

複製**

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)):

with open(build_file_path,'r') as f:

return json.load(f)

else:

print('build file not exists.')

return ''

build_files = get_build_files();

複製**

def get_template_files():

template_path = '/users/pengjie/try/iseo2/tmpviews'

template_files =

for dirpath, dirnames, filenames in os.walk(template_path):

for filename in filenames:

if(filename[0] != '.'):

filepath = os.path.join(dirpath,filename)

return template_files

複製**

import os

import re

from bs4 import beautifulsoup

def get_replace_list(content):

replace_list =

soup = beautifulsoup(content,'html.parser')

# 獲取所有link

links = soup.find_all('link')

for item in links:

name = os.path.basename(item.get('href'))

matches = re.search(r'\-(.+)?(\.)',name,re.i)

key = name.replace(matches[0],'.')

# 獲取所有script

scripts = soup.find_all('script')

for item in scripts:

name = os.path.basename(item.get('src'))

matches = re.search(r'\-(.+)?(\.)',name,re.i)

key = name.replace(matches[0],'.')

return replace_list

複製**

def search_and_replace(tplpath):

content = ''

with open(tplpath,'r',encoding="utf-8") as f:

content = f.read()

replace_list = get_replace_list(content)

for item in replace_list:

if(item['old'] and item['new']):

content = content.replace(item['old'],item['new'])

with open(tplpath,"w",encoding="utf-8") as f:

f.write(content)

複製**

import os

import re

import json

from bs4 import beautifulsoup

build_files = {}

def run():

view_path = '/users/pengjie/try/iseo2/tmpviews'

for dirpath, dirnames, filenames in os.walk(view_path):

for filename in filenames:

if(filename[0] != '.'):

filepath = os.path.join(dirpath,filename)

# 讀取檔案,獲取替換列表

content = ''

with open(filepath,'r',encoding="utf-8") as f:

content = f.read()

replace_list = get_replace_list(content)

for item in replace_list:

if(item['old'] and item['new']):

content = content.replace(item['old'],item['new'])

with open(filepath,"w",encoding="utf-8") as f:

f.write(content)

print('----')

# 獲取替換列表

def get_replace_list(content):

replace_list =

soup = beautifulsoup(content,'html.parser')

# 獲取所有link

links = soup.find_all('link')

for item in links:

name = os.path.basename(item.get('href'))

matches = re.search(r'\-(.+)?(\.)',name,re.i)

key = name.replace(matches[0],'.')

# 獲取所有script

scripts = soup.find_all('script')

for item in scripts:

name = os.path.basename(item.get('src'))

matches = re.search(r'\-(.+)?(\.)',name,re.i)

key = name.replace(matches[0],'.')

return replace_list

# 獲取構建檔案

def get_build_file():

build_file_path = '/users/pengjie/try/iseo2/dist/buildfile.json'

if(os.path.exists(build_file_path)):

with open(build_file_path,'r') as f:

return json.load(f)

else:

print('build file not exists.')

return ''

if __name__ == '__main__':

build_files = get_build_file()

if(build_files):

run()

複製**

完整專案**獲取點這裡

Python指令碼模板

usr bin env python coding utf 8 import os 獲取指令碼所在路徑 realpath os.path.dirname os.path.realpath file main defmain print hello world start if name main m...

word 模板替換 poi tl簡單使用

工作中,需要給word 中的一些標籤替換為 指定的資料 暫時來記錄一下,並且網上找了一些 經典的 poi,itext 等,順便找了幾個比較好用的 嘗試使用了下並記錄下來poi tl 簡介 輕量,主要針對word的模板引擎,提供了對word的豐富操作。官網 其實官網很詳細了,自己就照搬一下吧。pomc...

使用Python指令碼批量替換專案中的資料庫位址

這幾天接手到了乙個很老很老的專案,php的,裡面的資料庫位址不是同一配置的。有很多子站點,每個字站點又有自己的配置檔案,這個時候,問題來了,要換資料庫位址了!初聞這個問題,我整個人都是懵逼的,這不是搞我嘛?這麼多改起來得多麻煩?但活兒還是得做啊,於是作為一名機智的大彩筆,我決定寫個python指令碼...