Guest User

Untitled

a guest
Dec 7th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. # Upload Tectonicus changed.txt files to ftp host.
  2. # By LordMike91
  3. # V 0.5
  4.  
  5. # imports
  6. import os, sys
  7. from ftplib import FTP
  8. from sets import Set
  9.  
  10. print "imports loaded"
  11.  
  12. # Vars
  13. changeFile = 'x:\\tectonicus\\changed.txt'
  14.  
  15. localRoot = 'x:\\tectonicus\\'
  16.  
  17. remoteRoot = '/'
  18. remoteHost = '127.0.0.1'
  19. remoteUser = ''
  20. remotePass = ''
  21.  
  22. # Make a list of changed files
  23. fChanged = open(changeFile, 'r')
  24.  
  25. files = []
  26.  
  27. for line in fChanged:
  28.     # Tuple: localPath, remotePath
  29.     t = line.strip(), line.replace(localRoot, '').strip().replace('\\', '/')
  30.     files.append(t)
  31.     #print line
  32.  
  33. print "BLAH"
  34.    
  35. fChanged.close()
  36.  
  37. # Connect to FTP
  38. ftp = FTP(remoteHost, remoteUser, remotePass)
  39. ftp.cwd(remoteRoot)
  40.  
  41. # Upload files
  42. i = 0
  43. createdDirectories = Set()
  44.  
  45. print "FOR LOOP HAPPENING"
  46.  
  47. for f in files:
  48.     i += 1
  49.     print i
  50.     print "----"
  51.  
  52.     # Make directory
  53.     folders = f[1].split('/')
  54.     for v in range(0, len(folders)):
  55.         # Prepend previous folders
  56.         folder = ''
  57.         for p in range(0, v):
  58.             folder += folders[p] + '/'
  59.        
  60.         if folder not in createdDirectories:
  61.             # Folder has not been attempted created before
  62.             # Saves a lot of unecessary work
  63.             try:
  64.                 ftp.mkd(folder)
  65.                 # print 'Made folder ' + folder
  66.             except:
  67.                 pass
  68.        
  69.         createdDirectories.add(folder)
  70.  
  71.     print "Uploading file " + str(i) + " / " + str(len(files)) + " (" + str(f[1]) + ")"
  72.  
  73.     while True:
  74.         try:
  75.             toUpload = open(f[0], 'rb')
  76.             ftp.storbinary('STOR ' + f[1], toUpload)
  77.             toUpload.close()
  78.  
  79.             break
  80.         except:
  81.             # Error uploading
  82.             print 'Error uploading - trying again'
  83.  
  84. # Close connection
  85. ftp.quit()
Add Comment
Please, Sign In to add comment