+import os
+import datetime
+import tarfile
+import re
+import shutil
+
+_month = set()
+_other = set()
+
+os.chdir("example")
+
+def archives(name):
+ if(re.match(r'\d{4}.apk.tar.gz',name)):
+ return False
+ if(re.match(r'\d{6}.tar.gz',name)):
+ return False
+ if(name == 'apk.tmp'):
+ return False
+ return True
+
+#find all date groups and other files
+for file in filter(archives,os.listdir()):
+ _split = file.split("-")
+ # print(file + " " + str(len(_split)))
+ if(len(_split) > 2):
+ if(len(_split[-2]) == 8):
+ _month.add(_split[-2][0:6])
+ else:
+ _other.add(file)
+ else:
+ _other.add(file)
+
+#remove most recent from compressing
+_today = datetime.datetime.today()
+if _today.strftime("%Y%m") in _month:
+ _month.remove(_today.strftime("%Y%m"))
+if (_today+datetime.timedelta(days=20)).strftime("%Y%m") in _month:
+ _month.remove((_today-datetime.timedelta(days=15)).strftime("%Y%m"))
+
+
+
+#compress date groups
+
+for date in _month:
+ if os.path.isfile(date + ".tar.gz"):
+ os.system("tar -xzkf " + date + ".tar.gz")
+ os.system("tar -czf " + date + ".tar.gz " + "*-" + date + "*" )
+ #shutil.rmtree("*-" + date + "*")
+ os.system("del *-" + date + "*")
+#print(_month)
+#print(_other)
+
+
+
+#compress other
+if(len(_other)>0):
+ _otherjoined = ' '.join(_other)
+ if os.path.isfile(_today.strftime("%Y") + ".apk.tar.gz"):
+ if not os.path.isdir("apk.tmp"):
+ os.mkdir("apk.tmp")
+ for move in _other:
+ os.system("move " + move + ' apk.tmp/')
+ os.system("tar -xzkf" + _today.strftime("%Y") + ".apk.tar.gz" + " -C apk.tmp")
+ os.chdir("apk.tmp")
+ os.system("tar -czf ../" + _today.strftime("%Y") + ".apk.tar.gz *")
+ os.chdir("..")
+ shutil.rmtree("apk.tmp")
+ else:
+ os.system("tar -czf " + _today.strftime("%Y") + ".apk.tar.gz " + _otherjoined)
+ os.system("del " + _otherjoined)