2017-11-01 11 views
1

저자가 책을 쓸 때 HTML 콘텐츠를 생성하기 위해 CKEditor를 사용하고 있습니다. 우리는 python-django를 사용하여 그 내용을 디스크의 별도 HTML 파일에 저장합니다.Python - 파일 또는 폴더 콘텐츠 버전 관리

그러나 이클립스하는 것처럼 지금, 우리는 파일 (저자가 ctrl+s을 누를 때마다 사이드 바에서 타임 스탬프의 목록)의 역사/개정을 보여주기 위해 클라이언트의 요구 사항을 가지고있다 :

enter image description here

나는 2 개의 다른 시간에 저장된 html 원본의 교회법을 가지고 가기에 diff를 사용할 것을 계획하고있다.

하지만 나는 이미지, 오디오 및 비디오의 diff를 취하는 방법에 대해 전혀 몰라요.

git, eclipse 또는 Vesrsion 제어 시스템이 어떻게하는지 생각해보십시오. SHA와 같은 인코딩을 사용하여 디스크에 저장합니까?

다른 방법으로이 작업을 수행 할 수 있는지 제안 해주십시오.

사용할 수있는 오픈 소스 파이썬 lib가 있으면 사용할 준비가되었습니다. 나는 봤지만 운이 없다.

+0

근본적으로, 당신은 자식을 재 구현하려고합니까? – DeepSpace

+0

@DeepSpace 일종의,하지만 우리는 최대 4-6 시간과 단 하나의 HTML 파일에 대해서만 수있는 것처럼 사용자 세션을 위해 그것을 저장해야합니다. 하지만 HTML 파일에는 이미지, 오디오, 비디오 등이 링크되어있을 수 있습니다. – Laxmikant

+1

나는 이진 파일을 diff 할 수 있다고 생각하지 않는다. 파일이 변경된 경우에만 * 확인 * 할 수 있습니다. Python에서 파일을 비교하려면 [filecmp] (https://docs.python.org/2/library/filecmp.html) 모듈을 참조하십시오. – xyres

답변

2

이것 봐 (난 당신을위한 클래스를 썼다) :

import os 
import time 
import hashlib 


class SimpleFileCheckSum(object): 

    def __init__(self, path): 

     self.path = path 
     self.output = {} 

    def check_path_error(self): 

     if os.path.exists(self.path) is True and os.path.isfile(self.path): 
      return True 
     else: 
      return False 

    def get_file_size(self): 

     try: 
      st_data = os.stat(self.path) 
      get_size = str(st_data.st_size) 

     except PermissionError: 

      try: 

       os_size_data = str(os.path.getsize(self.path)) 
       self.output["SIZE"] = os_size_data 

      except: 
       self.output["SIZE"] = "Error" 

     else: 
      self.output["SIZE"] = get_size 

    def get_file_times(self): 

     def convert_time_to_human_readable(get_time): 

      return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(get_time)) 

     try: 

      my_st_object = os.stat(self.path) 

      file_creation_time = convert_time_to_human_readable(my_st_object.st_ctime) 
      file_last_modified_time = convert_time_to_human_readable(my_st_object.st_mtime) 

     except: 
      self.output['TIMES'] = {"CREATION": "Error", "MODIFIED": "Error"} 

     else: 
      self.output['TIMES'] = {"CREATION": file_creation_time, "MODIFIED": file_last_modified_time} 

    def get_file_full_path(self): 

     try: 

      get_full_path = os.path._getfullpathname(self.path) 
      get_final_path = os.path._getfinalpathname(self.path) 

     except: 
      self.output['PATH'] = {"FULL": "Error", "FINAL": "Error"} 

     else: 
      self.output['PATH'] = {"FULL": get_full_path, "FINAL": get_final_path} 

    def get_file_hashes(self): 

     try: 

      hash_md5 = hashlib.md5() 
      hash_sha1 = hashlib.sha1() 
      hash_sha256 = hashlib.sha256() 
      hash_sha512 = hashlib.sha512() 

      with open(self.path, "rb") as f: 
       for chunk in iter(lambda: f.read(2 ** 20), b""): 
        hash_md5.update(chunk) 
        hash_sha1.update(chunk) 
        hash_sha256.update(chunk) 
        hash_sha512.update(chunk) 

     except: 
      self.output["HASH"] = {"MD5": "Error", "SHA1": "Error", "SHA256": "Error", "SHA512": "Error"} 

     else: 
      self.output["HASH"] = {"MD5": hash_md5.hexdigest(), "SHA1": hash_sha1.hexdigest(), 
            "SHA256": hash_sha256.hexdigest(), "SHA512": hash_sha512.hexdigest()} 

    def call_all(self): 

     if self.check_path_error() is True: 

      self.get_file_full_path() 
      self.get_file_size() 
      self.get_file_times() 
      self.get_file_hashes() 

      return self.output 

     else: 
      return {"Error": "Your Path is Not Valid"} 


if __name__ == '__main__': 

    file_info = SimpleFileCheckSum("Your_file_address") 
    get_last_data = file_info.call_all() 

    print("Your Raw Dict Output : ", get_last_data, "\n\n") 

참고 :은 그래서 당신은 요청할 수 있습니다; 내 파일 주소가있는 경우 왜 get_file_full_path() 하위 함수가 필요합니까? ... 동적 주소를 "./myfile"과 같은이 클래스에 넣을 수 있고 get_file_full_path()가 전체 주소 및 최종 주소를 다시 표시 할 수 있기 때문입니다.

+0

굉장, 많은 감사 ...! 'get_file_full_path'에 +1 – Laxmikant