Advertisement
Guest User

Untitled

a guest
Dec 27th, 2014
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 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. sizeFilePath = local+".size"
  58. if os.access(sizeFilePath, os.R_OK):
  59. #size file exists, so another instance is already uploading this file
  60. sys.exit()
  61.  
  62. sizeFile = open(sizeFilePath,"w")
  63. size = os.path.getsize(local)
  64. sizeFile.write(size)
  65. time.sleep(2)
  66. while size != os.path.getsize(local):
  67. size = os.path.getsize(local)
  68. sizeFile.write(size)
  69. time.sleep(2)
  70.  
  71. sizeFile.close()
  72. os.remove(sizeFilePath)
  73.  
  74. try:
  75. ftp = ftputil.FTPHost("address", "user", "pass")
  76. except Exception, e:
  77. log("Connection failed: "+str(e)+"\n")
  78. log("Aborting: "+remote+"\n")
  79. sys.exit()
  80.  
  81. if isprivate == 1:
  82. if ftp.path.exists(remote):
  83. log("File already exists in privatepacks folder, aborting: "+remote+"\n")
  84. sys.exit()
  85. ftp.makedirs(remotedir)
  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. sys.exit()
  94.  
  95. os.remove(local)
  96. time.sleep(2)
  97. for num in range(len(local)-1,len(rootdif),-1):
  98. if local[num] == "/":
  99. try:
  100. os.rmdir(local[:num])
  101. except OSError:
  102. break
  103.  
  104. logmain.close()
  105. logbak.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement