Guest User

Copy from CMIS to FTP - Python script

a guest
Jul 4th, 2020
42
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import configparser
  2. import ftplib
  3. import sys
  4. from ftplib import FTP_TLS
  5. import ftputil.session
  6. import importlib.util
  7. import os
  8. import urllib3
  9. from cmislib.model import CmisClient
  10. from datetime import datetime
  11.  
  12. pathname = os.path.dirname(sys.argv[0])
  13. spec = importlib.util.spec_from_file_location("MyBrowserBinding", pathname + "/MyBrowserBinding.py")
  14. foo = importlib.util.module_from_spec(spec)
  15. spec.loader.exec_module(foo)
  16. from MyBrowserBinding import BrowserBinding
  17.  
  18. urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
  19.  
  20. ################### PROPERTIES
  21. config = configparser.ConfigParser()
  22. config.read(pathname + '/Myconfig2.properties')
  23. FTP_HOST = config["FTP"]["FTP_HOST"]
  24. FTP_PORT = int(config["FTP"]["FTP_PORT"])
  25. FTP_USER = config["FTP"]["FTP_USER"]
  26. FTP_PASS = config["FTP"]["FTP_PASS"]
  27. FTP_START = config["FTP"]["FTP_START"]
  28.  
  29. CMIS_URL = config["CMIS"]["CMIS_URL"]
  30. CMIS_USER = config["CMIS"]["CMIS_USER"]
  31. CMIS_PASS = config["CMIS"]["CMIS_PASS"]
  32. START_SYNC_PATH = config["CMIS"]["START_SYNC_PATH"]
  33.  
  34. LAST_SYNC_DATE = datetime.strptime(config["DEFAULT"]["LAST_SYNC_DATE"], '%d/%m/%Y')
  35.  
  36.  
  37. ###################  FTP
  38. _old_makepasv = FTP_TLS.makepasv
  39.  
  40.  
  41. def _new_makepasv(self):
  42.     host, port = _old_makepasv(self)
  43.     host = self.sock.getpeername()[0]
  44.     return host, port
  45.  
  46.  
  47. def copyToFTP(source, targetPathName):
  48.     checkPath = FTP_START + "/" + targetPathName[:targetPathName.rfind('/')]  # cut name from path
  49.     try:
  50.         if not ftp_host.path.exists(checkPath):
  51.             ftp_host.makedirs(checkPath, exist_ok=True)
  52.         ftp_host.upload(source, FTP_START + "/" + targetPathName, callback=None)
  53.     except OSError:
  54.         print("Something went wrong while uploading or creating dirs!")
  55.         exit(1)
  56.  
  57.  
  58. ################### CMIS
  59.  
  60. def copyContentFromCMIS(content):  # download from CMIS and upload to FTP and delete
  61.     o = open(content.getName(), 'wb')
  62.     result = content.getContentStream()
  63.     o.write(result.read())
  64.     result.close()
  65.     o.close()
  66.     copyToFTP(content.getName(), content.getPaths()[0])
  67.     os.remove(content.getName())  # TODO check if file is deleted
  68.  
  69.  
  70. def traverseCmis(parent):  # DFS - traverse very deep, check if modDate is fresh, if so - copy content
  71.     children = parent.getChildren()
  72.     for child in children:
  73.         prop = child.getProperties()
  74.         if prop["cmis:objectTypeId"] == "cmis:folder":
  75.             print("Entering: " + child.getPaths()[0])
  76.             traverseCmis(child)
  77.         else:
  78.             lastModDate = prop["cmis:lastModificationDate"]
  79.             if LAST_SYNC_DATE < lastModDate:
  80.                 copyContentFromCMIS(child)
  81.                 print("{} is copied.".format(child.getName()))
  82.  
  83.  
  84. FTP_TLS.makepasv = _new_makepasv
  85. my_session_factory = ftputil.session.session_factory(
  86.     base_class=ftplib.FTP_TLS,
  87.     port=FTP_PORT,
  88.     encrypt_data_channel=True)
  89. ftp_host = ftputil.FTPHost(FTP_HOST, FTP_USER, FTP_PASS, session_factory=my_session_factory)
  90. ftp_host.use_list_a_option = True
  91. # ftps = FTP_TLS()
  92. # ftps.connect(FTP_HOST, FTP_PORT)
  93. # ftps.login(FTP_USER, FTP_PASS)
  94. # ftps.prot_p()
  95.  
  96.  
  97.  
  98. client = CmisClient(CMIS_URL, CMIS_USER, CMIS_PASS, binding=BrowserBinding())
  99. repo = client.defaultRepository
  100. rootFolder = repo.rootFolder
  101. children = rootFolder.getChildren()
  102.  
  103. startParent = repo.getObjectByPath(START_SYNC_PATH)
  104. traverseCmis(startParent)
  105.  
  106. ftp_host.close()
  107. # ftps.quit()
  108.  
  109. config["DEFAULT"]["LAST_SYNC_DATE"] = datetime.now().strftime("%m/%d/%Y")
  110. with open(pathname + '/Myconfig2.properties', 'w') as configfile:
  111.     config.write(configfile)
RAW Paste Data