summaryrefslogtreecommitdiff
path: root/comm_zip_py/interval_zip.py
blob: 4ea95783c084292895e5a6f6112da2cae313c562 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/python
# encoding: utf8

# Author: zhangxi
# Last modified: 2017/11/24

import os
import json
import time
import datetime
import pprint
import pdb
from optparse import OptionParser


def interval_zip(interval, conf_file):

    dest_path = ""
    tmp_path = ""
    # interval = 1

    with open(conf_file) as f:
        for line in f.readlines():
            if line.startswith("upload_log_path"):
                dest_path = line[line.find("=")+1:].strip()
                if not dest_path.endswith('/'):
                    dest_path+='/'
            elif line.startswith("tmp_log_path"):
                tmp_path = line[line.find("=")+1:].strip()
            else:
                pass

    while True:
        try:
            '''
            try:
                with open(upload_pz) as f:
                    for line in f.readlines():
                        start = line.find('interval":')+10
                        end = line.find(',"num')
                        interval = int(line[start:end])
            except Exception, e:
                pass
                #raise e
            # print interval
            '''

            time_remaining = interval-time.time()%interval
            # print time_remaining
            time.sleep(time_remaining)

            # today = time.strftime('%Y-%m-%d',time.localtime(time.time()))
            # now_path = dest_path+today
            # now_path+="/"
            # cmd = 'mkdir -p %s; cd %s;  gzip -q *; mv %s/* %s'%(now_path, tmp_path, tmp_path, now_path)
            # # print cmd
            # os.system(cmd)


            # l = os.listdir(tmp_path)
            l = tmp_path
            if len(l)>1:
                # st = l.sort(key=lambda fn: os.path.getmtime(tmp_path+"/"+fn) if not os.path.isdir(tmp_path+"/"+fn) else 0)
                latest = sorted([(x, os.path.getmtime(os.path.join(l,x))) for x in os.listdir(l) if os.path.isfile(os.path.join(l,x))], \
                    key=lambda i: i[1])[-1]
                # print ('last file is '+l[-1])

                today = time.strftime('%Y-%m-%d',time.localtime(time.time()))
                now_path = dest_path+today
                now_path+="/"
                now_time = time.strftime('%H%M%S',time.localtime(time.time()))
                # print now_time
                # cmd = 'mkdir -p %s; cd %s;  tar zcf %s/%s.tar.gz * --exclude=%s; rm `ls *|egrep -v %s`'\
                #     %(now_path, tmp_path, now_path, now_time, l[-1], l[-1])
		#pdb.set_trace()
                cmd = 'mkdir -p %s; cd %s; count=`ls|grep .txt|wc -l`; if [[ $count -gt 1 ]]; then tar zcf %s/%s.tar.gz * --exclude=%s; rm `ls *|egrep -v %s`;fi;'\
                    %(now_path, tmp_path, now_path, now_time, latest[0], latest[0])
                # print cmd
                os.system(cmd)

        except Exception, e:
            pass
            # print e


if __name__ == '__main__':
    usage_info ="python %prog [options arg] \n"
    parser     =OptionParser(usage=usage_info,version="version verifiy 1.0")
    parser.add_option("-i","--interval",dest="interval",default=5,help="interval of scanning upload file. Default is 5 seconds.")
    # parser.add_option("-u","--upload_pz",dest="upload_pz",default="/tmp/upload_pz/net_log_upload_pz.txt",help="upload_pz_path. Default file is /tmp/upload_pz/net_log_upload_pz.txt")
    parser.add_option("-c","--conf_file",dest="conf_file",default="/home/jcq/bin/sapp_se/djconf/comm_audit.conf",help="conf_path. Default file is /home/jcq/bin/sapp_se/djconf/comm_audit.conf")
    (options,args) =parser.parse_args()
    interval_zip(options.interval, options.conf_file)