Watchful11

Untitled

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