Environment Introduction: Production server developers need to send data to the client, mainly pictures and video; Image server with Fastdfs, download by nginx to provide;
Java program to call this script, passing parameters to determine the contents of the package file;
#!/usr/bin/env python#coding:utf-8 "" "Modify some modules, use Python to bring the module; add log records;" " Import sys,subprocess,os,time,json,shutil,logginglogname = Time.strftime ("%y_%m_%d.log") Logging.basicconfig (level =logging. DEBUG, format= '% (asctime) s% (levelname) s% (message) s ', datefmt= '%a,%d%b%Y% H:%m:%s ', Filename=logname, filemode= ' W ') class Global (object): "" "This script is used to process packaged files: Decide what documents to pack and record packing records; "" "Def __init__ (self): Pass def args_counts (self): ############# #检测参数的长度 Counts = Len (SYS.ARGV) if counts = = 1: ##### #windows test sys.exit () else:if os.path.isfile (' Argv.txt '): ##### #检测这个存在这个文件 logging.warning ("The Argvs file is exist...,warning!-----------") OS.R Emove (' Argv.txt ') with open ("Argv.txt", ' A + ') as Fp:for I in Sys.argv[1:]: FP . write (i+ "\ n") def Args_change (self): File_con= Open ("Argv.txt", ' r ') Dirtory = ' for i in file_con:try:argschange = Json.load S (i) ######## #解析json key = Argschange.keys () [0] ######## #获取字典的key, the name of the directory name = Argschange.get (key) ######## #获取字典的values, need to download the file name----> List dest = '/home/fastdfs/storage/da ta/' # # # # # #新fastdfs file storage except exception,e:logging.warning ("Something is wrong%s------- ---"%e) sys.exit (#dest) = '/home/fastdfs/storage/files-data/data ' #旧fastdfs If Os.path.isdir (key): Logging.warning ("The directory%s is exist and would delete it-----------"%key) Shutil.rmtree (Key) Logging.info ("The directory%s is not exist and would create it++++++++++"%key ) Os.mkdir (key) Else:logging.info ("The directory%s is not exist and would create it++++++++++ "%key) Os.mkdir (Key) if Os.path.isfile (key): Logging.warning ("Error,program is Error-------- --") sys.exit () for i,k in Enumerate (name): ####### #拷贝部分 Dest_dir = Os.path.dirname (k). Split ("M00/") [1] ######## #切割fastdfs file path, reducing the time to find, and finding the exact img_name = Os.path.basename (k) ######## #合成路径 img_dest1 = dest+dest_dir+os.path.sep+img_name ######## #最近上传路径 img_dest2 = dest+ "data/" +dest_dir+os.path.sep+img_name ######## #以前上传的路径 If Os.path.isfile (img_dest1): ############################## #优化查找部分, and record Logging.info ("Find%s+++++ +++++ "%img_name) shutil.copy (Img_dest1,key) logging.info (" Copy%s%s++++++++++ "% (img _dest1,key)) else:logging.warning ("Not find%s and change directory to find------"%img _name) If Os.path.isfile (img_dest2): Logging.info ("Find%s++++++++++"% img_name) Shutil.copy (Img_dest2, key) Logging.info ("Copy%s%s++++++++++"% (Img_dest2, key)) dir Tory + = key + "# # # print Dirtory Bdass = Time.strftime ("%h%m%s ") Zip_name =" Bdass "+ Bdass + ". zip" Child_zip = subprocess. Popen (' zip-r ' + zip_name + "" + dirtory,shell=true) child_zip.wait () if Os.path.isfile (zip_name): Logging.info ("Compress successfuly++++++++++") logging.info ("End and clean workplace++++++++++") Else: Logging.warning ("Compress failed----------") Sys.exit Shutil.move (Zip_name, "/usr/local/n ginx/html/download/") Print Zip_name file_con.seek (0) for i in File_con:argschange = JSON . loads (i) ######## #解析json key = Argschange.keys () [0] ######## #获取字典的key, name of the directory If Os.path.isdir (key): Shutil.rmtree (Key) def zip_images (self): # # # # #结束部分, delete the file if os.path.is File ('./argv.txt '): Os.remove ('./argv.txt ') system = Global () if __name__ = = "__main__": Logging.info ("Begin" + "+" *) system.args_counts () System.args_change () system.zip_images ()
Invoke command: Python remote_zip_enhance.py ' {"2017092268": ["Group1/m00/00/00/rb95q1ncgd-ahb-faavtkbrr9gi895.png", "group1/ M00/00/0d/rb95rfm3iugayho2aajmfxpe4oi406.png "," Group1/m00/00/04/rb95rfmmja-avwspaax1vgabk-i426.png "]} ' {" 2017092274 ": [" Group1/m00/00/00/rb95q1ncgd-ahb-faavtkbrr9gi895.png "," group1/m00/00/0d/ Rb95rfm3iugayho2aajmfxpe4oi406.png "," Group1/m00/00/04/rb95rfmmja-avwspaax1vgabk-i426.png "]} '
Previously written basic functions, some bugs, packaging occasionally error;
Python+fastdfs+nginx for packaged Download functionality