Guest User

Untitled

a guest
Sep 22nd, 2024
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. import os
  2. import sys
  3. import time
  4. from wsmux.utils import calculate_file_hash
  5. import zipfile
  6. from datetime import datetime
  7.  
  8. class MyArg:  
  9.   def __init__(self, f: str) -> None:    
  10.     self.second = 60
  11.     self.minute = 0
  12.     self.hour = 0    
  13.     # 获取文件名
  14.     fn = os.path.basename(f)
  15.     for s in fn.split("."):
  16.       if s.endswith('s'):
  17.         try:
  18.           self.second = int(s[:-1])
  19.         except Exception:
  20.           pass
  21.       elif s.endswith('m'):
  22.         try:
  23.           self.minute = int(s[:-1])
  24.         except Exception:
  25.           pass
  26.       elif s.endswith('h'):
  27.         try:
  28.           self.hour = int(s[:-1])
  29.         except Exception:
  30.           pass
  31.   @property
  32.   def duration(self):
  33.     return self.hour*3600+self.minute*60+self.second
  34.  
  35. def back_file(fn : str):
  36.   fn_list = fn.split('.')
  37.   fn_list.pop()
  38.   fn_list.append( datetime.now().strftime("%m-%d-%H-%M-%S"))
  39.   fn_list.append('zip')
  40.   zip_file_path = '.'.join(fn_list)
  41.   # 创建 ZIP 文件并添加文件
  42.   with zipfile.ZipFile(zip_file_path, 'w', compresslevel=9) as zipf:
  43.     # 将文件添加到 ZIP 文件中
  44.     zipf.write(fn, os.path.basename(fn))
  45.   print(f"备份文件{os.path.basename(fn)}到{zip_file_path}")
  46.  
  47.  
  48. arg = MyArg(sys.argv[0])
  49. print(f"每间隔{arg.duration}秒检查一次")
  50. print(f"文件有变动时,将变动后的文件备份")
  51.  
  52.  
  53. hashmap = dict()
  54.  
  55. while True:
  56.   for fn in sys.argv[1:]:
  57.     if os.path.isdir(fn):
  58.       continue
  59.    
  60.     sha1sum = calculate_file_hash(fn)
  61.     if sha1sum != hashmap.get(fn):
  62.       back_file(fn)
  63.       hashmap[fn] = sha1sum
  64.   print(f"上次检查时间 { datetime.now().strftime("%y-%m-%d-%H-%M-%S") }")
  65.   time.sleep(arg.duration)
  66.  
Advertisement
Add Comment
Please, Sign In to add comment