I have this idea of compressing and archiving log files created in the same calender week in one .7z file.
I have a dir that includes more than 100 files with a date in their name like this
- Code: Select all
#test_20141207.log
and the name includes the date the file is created. I want to zip the files according to their file name.
So this means every file that ends with 01.log - 07.log should be ziped into one archive.
I already have functioning script working in the command line if I give the file as argument.
So in Hazel:
Rule matches on extenstion being log and file starting with #.
This is what I find in Hazel log
- Code: Select all
2014-12-07 14:58:19.650 hazelworker[8825] DEBUG: Action changed file: #test_20140518.log
2014-12-07 14:58:19.655 hazelworker[8825] #test_20140519.log: Rule Weekly 7z matched.
2014-12-07 14:58:19.655 hazelworker[8825] DEBUG: Manual run. Forcing actions to be executed.
2014-12-07 14:58:19.727 hazelworker[8825] DEBUG: == script output ==
== End script output ==
- Code: Select all
#!/usr/bin/python
import datetime
import re
import subprocess
import sys
import os
pathwithfile = str(sys.argv[1])
filepath = str(os.path.dirname(pathwithfile)) + "/"
filenameregex = re.compile(r'([^\/]+)$', re.I)
filename = str(filenameregex.search(filewithpath).groups())
dateregex = re.compile(r'(\d{8})(?=\.log)', re.I)
filedate = str(dateregex.search(filename).groups()[0])
syear = int(filedate[0] + filedate[1] + filedate[2] + filedate[3])
smonth = int(filedate[4] + filedate[5])
sday = int(filedate[6] + filedate[7])
# scw is the calender week according to the pieces from the file name
scw = str(datetime.date(syear, smonth, sday).isocalendar()[1])
szip = "7z a -t7z " + filepath +"KW" + str(scw) + "_archive.7z " + filepath + "*" + str(filedate) + "* -mx9 -mmt > /dev/null 2>&1"
subprocess.call(szip, shell=True)
sys.exit(0)
It won't zip the files and I think I catched everything correctly..
I played around with builtin features but couldn't find a solution to zip files in a way like this.
Oh and please excuse my copypasta style of putting this script together.
Some things might not be super efficient I know.
Any ideas? Thank you!