Watchful11

Untitled

Dec 27th, 2014
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. #!/usr/bin/python
  2. ## Rules:
  3. ## Don't overwrite modpacks.xml, thirdparty.xml or texturepack.xml
  4. ## Only upload files in the static and privatepacks folders
  5. ## Don't overwrite zips
  6. import ftplib
  7. import sys
  8. import time
  9. import os
  10. import ftputil
  11.  
  12. rootdif = "/home/ftp"
  13.  
  14. logmain = open("/home/ftp/log.txt","a")
  15. logbak = open("/root/log.txt","a")
  16.  
  17. def log(string):
  18. logmain.write(string)
  19. logbak.write(string)
  20.  
  21. if len(sys.argv) < 2:
  22. log("No file passed, aborting\n")
  23. sys.exit()
  24.  
  25.  
  26. local = sys.argv[1]
  27. fromroot = local.find(rootdif)
  28. if fromroot != 0:
  29. log("Incorrect input, aborting: "+local+"\n")
  30. sys.exit()
  31.  
  32. if not os.access(local, os.R_OK):
  33. log("File does not exist or is not readable, aborting: "+remote+"\n")
  34. sys.exit()
  35.  
  36. remote = local[len(rootdif):]
  37.  
  38. isstatic = remote.find("static")
  39. isprivate = remote.find("privatepacks")
  40. if isstatic != 1 and isprivate != 1:
  41. log("Not a recognized dir, aborting: "+remote+"\n")
  42. sys.exit()
  43.  
  44. banned = ["modpacks.xml", "thirdparty.xml", "texturepack.xml"]
  45. lastslash = remote.rfind("/")
  46. if lastslash == -1:
  47. name = remote # who knows?
  48. remotedir = "/"
  49. else:
  50. name = remote[lastslash+1:]
  51. remotedir = remote[:lastslash]
  52.  
  53. if name in banned:
  54. log("File is banned from upload, aborting: "+remote+"\n")
  55. sys.exit()
  56.  
  57. try:
  58. ftp = ftputil.FTPHost("address", "user", "pass")
  59. except Exception, e:
  60. log("Connection failed: "+str(e)+"\n")
  61. log("Aborting: "+remote+"\n")
  62. sys.exit()
  63.  
  64. if isprivate == 1:
  65. if ftp.path.exists(remote):
  66. log("File already exists in privatepacks folder, aborting: "+remote+"\n")
  67. sys.exit()
  68. ftp.makedirs(remotedir)
  69.  
  70. sizeFilePath = local+".size"
  71. if os.access(sizeFilePath, os.R_OK):
  72. #size file exists, so another instance is already uploading this file
  73. ftp.quit()
  74. sys.exit()
  75.  
  76. sizeFile = open(sizeFilePath,"w")
  77. size = os.path.getsize(local)
  78. sizeFile.write(size)
  79. time.sleep(2)
  80. while size != os.path.getsize(local):
  81. size = os.path.getsize(local)
  82. sizeFile.write(size)
  83. time.sleep(2)
  84.  
  85. sizeFile.close()
  86.  
  87. log("Uploading: "+remote+"\n")
  88.  
  89. try:
  90. ftp.upload(local, remote)
  91. except Exception, e:
  92. log("Upload failed: "+str(e)+"\n")
  93. ftp.quit()
  94. sys.exit()
  95.  
  96. os.remove(local)
  97. os.remove(sizeFilePath)
  98. time.sleep(2)
  99. for num in range(len(local)-1,len(rootdif),-1):
  100. if local[num] == "/":
  101. try:
  102. os.rmdir(local[:num])
  103. except OSError:
  104. break
  105.  
  106. logmain.close()
  107. logbak.close()
  108. ftp.quit()
Advertisement
Add Comment
Please, Sign In to add comment