The embedded Web server is different from the traditional server, the web needs to convert the array format saved in the Flash, is convenient LWIP the network interface The call, recently because the business demand, needs frequently to revise the webpage, each time the compression and the conversion is a very tedious process, therefore I have the use knowledge which grasps, Use Python to write a script that can process Web page files in batches, compress and convert the array.
Script run background (later version compatible):
Python 3.5.1 (download, install, configure please refer to the online tutorial)
node. js v4.4.7, install UGLIFYJS Management Pack, support JS file non-text compression
Uglifyjs the engine used to compress JS files
The specific implementation code is as follows:
#/usr/bin/pythonimport osimport binasciiimport shutil from functools import partialimport reimport gzip# Create a new folder Def mkdir (path): Path=path.strip () isexists=os.path.exists (path) #判断文件夹是否存在, does not exist create if not IsExists:os.mak Edirs (path) print (path+ ' Create successful ') Else:pass return path# Delete a folder (contains all files inside) def deldir (path): Path = PA Th.strip () isexists=os.path.exists (path) #判断文件夹是否存在, exists delete if IsExists:shutil.rmtree (path) print (p Ath + "Delete succeeded") else:pass# webpage once compressed file def filereduce (Inpath, outpath): INFP = open (Inpath, "R", encoding= "utf- 8 ") OUTFP = open (Outpath," w ", encoding=" Utf-8 ") for Li in Infp.readlines (): If Li.split (): #去除多余的 \ r \ n li = li.replace (' \ n ', '). replace (' \ t ', '); #空格只保留一个 li = ". Join (Li.split ()) Outfp.writelines (LI) infp.close () Outfp.clo SE () print (outpath+ "compression succeeded") #shell命令行调用 (with Ugllifyjs to compress JS files) def shellreduce (Inpath, outpath): Command = "Uglifyjs" +inpath+ "-m-o" +outpath print (command) os.syste M (Command) #gzip压缩模块def filegzip (Inpath, Outpath): With open (Inpath, ' RB ') as Plain_file:with Gzip.open (Outpath, ' WB ') as Zip_file:zip_file.writelines (plain_file) print (outpath+ "gzip-compression succeeded") #将文件以二进制读取 and convert array to save Def Fileh Ex (Inpath, outpath): i = 0 Count = 0 A = ' inf = open (Inpath, ' RB '); Outf = open (Outpath, ' W ') records = ITER (partial (inf.read, 1), B ") for r in Records:r_int = Int.from_bytes ( R, byteorder= ' big ') A + = Strzfill (Hex (R_int), 2, 2) + ', ' i + = 1 count + 1 if i = = 16: A + = ' \ n ' i = 0 A = "Const static char" + outpath.split ('. ') [ -2].split ('/') [-1] + "[" + str (count) + "]={\n" + A + "\n}\n\n" Outf.write (a) inf.close () Outf.close () print (O Utpath + "Convert to array Success") #在指定位置填充0def Strzfill (ISTR, index, N): return Istr[:index] + isTr[index:].zfill (n) #去css注释/*.....*/def uncommentreduce (Inpath, outpath): INFP = open (Inpath, "R", encoding= "Utf-8") OUTFP = open (Outpath, "w", encoding= "Utf-8") Filebyte = Infp.read (); Replace_reg = Re.compile ('/\*[\s\s]*?\*/') Filebyte = Replace_reg.sub (", filebyte) Filebyte = filebyte.replace (' \ n ') , '). replace (' \ t ', '); Filebyte = ". Join (Filebyte.split ()) Outfp.write (Filebyte) infp.close () Outfp.close () print (outpath+" Go to comment compression succeeded !") #程序处理主函数def webprocess (path): #原网页. \basic\ #压缩网页. \reduce\ #gzip二次压缩. \gzip #编译完成. C Web page: \programe Basicpath = path + "\\basic" Reducepath = path + "\\reduce" Gzippath = path + "\\gzip" Programpath = path + "\\program" #删除原文件夹, create new Folder Deldir (Programpath) Deldir (Reducepath) del Dir (Gzippath) mkdir (programpath) for root, dirs, files in Os.walk (Basicpath): For item in FIL Es:ext = ITEM.SPlit ('. ') Infilepath = root + "/" + Item Outreducepath = mkdir (Root.replace ("Basic", "reduce") + "/" + Item Outgzippath = mkdir (Root.replace ("Basic", "gzip") + "/" + Item + '. Gz ' Out Programpath = Programpath + "/" + item.replace ('. ', ' _ ') + '. C ' #根据后缀不同进行相应处理 #html remove ' \ n ', ' \ t ', the space character retains 1 #css remove \*......*\ annotation data, ' \ n ' and ' \ t ', while the space character retains 1 # JS call uglifyjs2 to compress #gif jpg Direct copy #其它 direct copy #上述执行完 After completion of compression into a. gz file #除其它外, the remaining files are converted to 16 binary arrays, saved as. c files if ext[-1] = = ' html ': Filereduce (Infilepath, Outreducepath) filegzip (Outreducepath, Outgzippath) Filehex (Outgzippath, Outprogrampath) elif ext[-1] = = ' CSS ': Uncommentreduce (Infilepath, Outreducepath) filegzip (Outreducepath, Outgzippa TH) Filehex (Outgzippath, Outprogrampath) elif ext[-1] = = ' js ': Shellreduce (Infilepath, Outreducepath) filegzip (Outreducepath, Outgzippath) Filehex (Outgzippath, Outprogrampath) elif ext[-1] in ["GIF", "JPG", "ico" ]: Shutil.copy (Infilepath, Outreducepath) filegzip (Outreducepath, Ou Tgzippath) Filehex (Outgzippath, Outprogrampath) Else: Shutil.copy (Infilepath, outreducepath) #获得当前路径path = Os.path.split (Os.path.realpath (__file__)) [0]; Webprocess (PATH)
The principles of the above implementations mainly include:
1. Traverse the Pending folder (path: \basic, requires the user to create and copy the processing file into it, and puts the script to the top layer of the folder)--webprocess
2. Create a compressed Pages folder (.. \reduce, used to store compressed files), completed by the script, processing the action:
HTM: Remove extra space in text, line break
CSS: Delete extra spaces in text, line breaks, and comment files/*......*/
JS: Call UGLIFYJS for compression processing
GIF, JPG, ICO, and others: direct copy processing
3. Create the Gzip file processing folder (.. \gzip, which is used to save two compressed files), the Gzip module is called by the script to complete.
4. Create a Process page folder (.. \program, used to store compressed files), completed by the script, processing the action:
Reads the file in binary mode and converts it to a 16 binary string written to the file.
By enabling the Windows command line under the folder (shift+ the right mouse button) and entering the Python web.py, you can do all the processing of the files by repeating the three processes in a loop.
Special Note: All processed files need to be stored in utf-8 format, otherwise the read "GBK" read error is reported.
Implementation effects such as
HTML file:
To convert an array:
Also comes with a small script, to query the current directory and sub-folder selected lines of code and the number of empty lines (as written in this script test derived from):
#/usr/bin/pythonimport ostotal_count = 0; empty_count = 0;def CountLine (path) : Global Total_count Global Empty_count tempfile = open (path) for lines in Tempfile: Total_count + = 1 If Len (Lines.strip ()) = = 0:empty_count + 1 def totalline (Pat h): For Root, dirs, files in Os.walk (path): for item in Files:ext = ITEM.SPL It ('. ') ext = ext[-1] if (ext in ["CPP", "C", "H", "Java", "PHP"]): subpath = root + "/" + Item Countline (subpath) path = Os.path.split (Os.path.realpath (__file__)) [0]; Totalline (path) print ("Input path:", path) print ("Total lines:", Total_count) print ("Empty lines:", Empty_count) print (" Code lines: ", (Total_count-empty_count))