Watchful11

Untitled

Mar 1st, 2015
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 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 ftputil
  11. import os, os.path
  12. import re
  13. import zipfile
  14.  
  15. banned = ["modpacks.xml", "thirdparty.xml", "texturepack.xml"]
  16. ignored = ["^/log\.txt","^/listed","\.ignore$"]
  17. rootdir = "/home/ftp"
  18. remotedir = ""
  19.  
  20. logmain = open("/home/ftp/log.txt","a")
  21. logbak = open("/root/log.txt","a")
  22.  
  23. isDebug = False
  24.  
  25. ftp = None
  26.  
  27. if len(sys.argv) > 0:
  28. if sys.argv[1] == "-v":
  29. print("Starting in debug mode")
  30. isDebug = True
  31.  
  32. def log(string):
  33. string = string + "\n"
  34. time = strftime("[%m-%d %H:%M:%S]", gmtime())
  35. logmain.write(time+" "+string)
  36. logbak.write(time+" "+string)
  37. debug(" "+string)
  38.  
  39. def debug(string):
  40. if isDebug:
  41. print(string)
  42.  
  43. def initFTP():
  44. try:
  45. detailsFile = open("details","r")
  46. details = detailsFile.read().split(":")
  47. except:
  48. log("Problem reading credentials file")
  49. return None
  50. if len(details) != 3:
  51. log("Invalid credentials file")
  52. return None
  53.  
  54. try:
  55. ftp = ftputil.FTPHost(details[0], details[1], details[2])
  56. except Exception, e:
  57. log("Connection failed: "+str(e))
  58. return None
  59. debug("FTP initialized")
  60. return ftp
  61.  
  62. def cleanPointers():
  63. logmain.close()
  64. logbak.close()
  65. ftp.close()
  66.  
  67. def process(local):
  68. remote = getRemote(local)
  69. if remote == None:
  70. log("Incorrect input file, aborting: "+local)
  71. return
  72. debug(" Remote: "+remote)
  73. if canIgnore(remote):
  74. debug(" Aborting on ignore")
  75. return
  76. if not isLegal(remote):
  77. debug(" Aborting on legal")
  78. return
  79. if isPack(remote):
  80. if not checkValid(local):
  81. debug(" Aborting on valid")
  82. return
  83. if checkPresent(local, remote):
  84. log("File already exists in privatepacks folder, aborting: "+remote)
  85. os.rename(local, local+".ignore")
  86. return
  87. if not upload(local, remote):
  88. return
  89. clean(local)
  90.  
  91. def getRemote(local):
  92. fromroot = local.find(rootdir)
  93. if fromroot != 0:
  94. return None
  95. else:
  96. return local[len(rootdir):]
  97.  
  98. def canIgnore(remote):
  99. for path in ignored:
  100. if re.search(path, remote) != None:
  101. return True
  102. return False
  103.  
  104. def isPack(remote):
  105. return re.search("^/privatepacks/.*\.zip$", remote) != None
  106.  
  107. def isStatic(remote):
  108. return re.search("^/static/.*\.xml$", remote) != None
  109.  
  110. def isLegal(remote):
  111. if not isPack(remote) and not isStatic(remote):
  112. return False
  113.  
  114. name = ""
  115. lastslash = remote.rfind("/")
  116. if lastslash == -1:
  117. name = remote # who knows?
  118. remotedir = "/"
  119. else:
  120. name = remote[lastslash+1:]
  121. remotedir = remote[:lastslash]
  122.  
  123. if name in banned:
  124. return False
  125. else:
  126. return True
  127.  
  128. def checkValid(local):
  129. try:
  130. return zipfile.ZipFile(local).testzip() == None
  131. except:
  132. return False
  133.  
  134. def checkPresent(local, remote):
  135. return ftp.path.exists(remote)
  136.  
  137. def upload(local, remote):
  138. log("Uploading: "+remote)
  139. try:
  140. if isPack(remote):
  141. ftp.makedirs(remote)
  142. ftp.upload(local, remote)
  143. except Exception, e:
  144. log("Upload failed: "+str(e))
  145. return False
  146. return True
  147.  
  148. def clean(local):
  149. os.remove(local)
  150. for num in range(len(local)-1,len(rootdir),-1):
  151. if local[num] == "/":
  152. try:
  153. os.rmdir(local[:num])
  154. except OSError:
  155. break
  156.  
  157. ftp = initFTP()
  158. if ftp == None:
  159. log("Aborting")
  160. sys.exit()
  161.  
  162. for root, _, files in os.walk(rootdir):
  163. for f in files:
  164. fullpath = os.path.join(root, f)
  165. debug("Full: "+fullpath)
  166. process(fullpath)
  167.  
  168. cleanPointers()
Advertisement
Add Comment
Please, Sign In to add comment