环境介绍:生产服务器开发人员需要给client下发数据,主要是图片及视频;图片服务器用fastdfs,下载由nginx 来提供;

java 程序来调用此脚本,传递参数来决定打包文件内容;

#!/usr/bin/env python
#coding:utf-8
"""修改一些模块,使用python 自带模块;增加日志记录;"""
import sys,subprocess,os,time,json,shutil,logging
logname = 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):
"""此脚本用来处理打包文件:传参来决定打包什么文件,并记录打包记录;
"""
def __init__(self):
pass def args_Counts(self): ##############检测参数的长度
counts = len(sys.argv)
if counts == 1 : ######windows test
sys.exit(10)
else:
if os.path.isfile('argv.txt'): ######检测这个存在这个文件
logging.warning("The argvs file is exist...,WARNING!----------- ")
os.remove('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.loads(i) #########解析json
key = argschange.keys()[0] #########获取字典的key,目录的名字
name = argschange.get(key) #########获取字典的values,需要下载的文件名字----> list
dest = '/home/fastdfs/storage/data/' #####新fastdfs 的文件存储路
except Exception,e:
logging.warning("Something is wrong %s----------"%e)
sys.exit(10)
#dest = '/home/fastdfs/storage/files-data/data' #旧fastdfs if os.path.isdir(key):
logging.warning("The directory %s is exist and will delete it-----------"%key)
shutil.rmtree(key)
logging.info("The directory %s is not exist and will create it++++++++++"%key)
os.mkdir(key)
else:
logging.info("The directory %s is not exist and will create it++++++++++"%key)
os.mkdir(key) if os.path.isfile(key):
logging.warning("Error,program is Error----------")
sys.exit(20)
for i,k in enumerate(name): ########拷贝部分
dest_dir = os.path.dirname(k).split("M00/")[1] #########切割fastdfs 的文件路径,减少查找时间,精确查找
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) : ###############################优化查找部分,并记录
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)) dirtory += 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(10)
shutil.move(zip_name,"/usr/local/nginx/html/download/")
print zip_name
file_con.seek(0)
for i in file_con:
argschange = json.loads(i) #########解析json
key = argschange.keys()[0] #########获取字典的key,目录的名字
if os.path.isdir(key):
shutil.rmtree(key) def zip_Images(self): #####结束部分,删除文件
if os.path.isfile('./argv.txt'):
os.remove('./argv.txt') system = Global()
if __name__ == "__main__":
logging.info("Begin" + "+" * 20)
system.args_Counts()
system.args_Change()
system.zip_Images()

  调用命令: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"]}'

  此前写过基本功能的,有些BUG,打包偶尔有错误;

最新文章

  1. linux 安装 apache2.2.31
  2. python模块之os
  3. Unity3d Shader
  4. 做HDU1010 带出来一个小问题
  5. Dapper的完整扩展(转)
  6. sqlplus乱码
  7. -_-#【Canvas】圆弧运动
  8. Objective C内存管理之理解autorelease------面试题
  9. java:转换时间格式为String
  10. Spark笔记——技术点汇总
  11. 在 Docker 中使用 flannel - 每天5分钟玩转 Docker 容器技术(60)
  12. ZooKeeper基础CRUD操作
  13. 039 DataFrame的理解
  14. Lintcode: Insert Node in a Binary Search Tree
  15. c++ 中的符号与关键字
  16. 无法启动此程序因为计算机中丢失 xxx.dll
  17. zend Studio10.6.2 下载
  18. python 处理抓取网页乱码
  19. windows平台tensorboard的配置及使用
  20. CentOS6.8编译安装LAMP

热门文章

  1. c++设计模式概述之访问者
  2. 【LeetCode】1111. Maximum Nesting Depth of Two Valid Parentheses Strings 有效括号的嵌套深度
  3. 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)
  4. 【LeetCode】623. Add One Row to Tree 解题报告(Python)
  5. light oj -1245 - Harmonic Number (II)
  6. 【jvm】07-偏向锁、轻量锁、重量锁到底是啥?
  7. 编写Java程序,使用Swing事件处理机制实现用户登录和英雄信息显示
  8. Centos8 设置中文
  9. Django admin实现TextField字段changelist页面换行、空格正常显示
  10. Java高效开发-SSH+Wireshark+tcpdump组合拳