Watchful11

Untitled

Apr 22nd, 2015
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 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 pysftp
  8. import sys
  9. import time
  10. from time import gmtime, strftime
  11. import ftputil
  12. import os, os.path
  13. import re
  14. import zipfile
  15. from os import stat
  16. from pwd import getpwuid
  17.  
  18. banned = ["modpacks.xml", "thirdparty.xml", "texturepack.xml", "version.xml", "newversion.xml","balance.json"]
  19. ignored = ["^/log\.txt","^/listed","\.ignore$"]
  20. rootdir = "/home/ftp"
  21. remoterootdir = "/ftb/FTB2"
  22. remotedir = "" #"ftb/FTB2" #ftb/FTB2 on curse
  23.  
  24. sftp = 1
  25.  
  26. logmain = open("/home/ftp/log.txt","a")
  27. logbak = open("/root/log.txt","a")
  28.  
  29. isDebug = False
  30.  
  31. ftp = None
  32.  
  33. if len(sys.argv) > 1:
  34. if sys.argv[1] == "-v":
  35. print("Starting in debug mode")
  36. isDebug = True
  37.  
  38. def log(string):
  39. logFile(string, None)
  40.  
  41. def logFile(string, local):
  42. if local != None:
  43. string = "[" + find_owner(local) + "] " + string
  44. string = string + "\n"
  45. time = strftime("[%m-%d %H:%M:%S]", gmtime())
  46.  
  47. logmain.write(time+" "+string)
  48. logbak.write(time+" "+string)
  49. debug(" "+string)
  50.  
  51. def debug(string):
  52. if isDebug:
  53. print(string)
  54.  
  55. def find_owner(filename):
  56. return getpwuid(stat(filename).st_uid).pw_name
  57.  
  58. def initFTP():
  59. try:
  60. detailsFile = open("details","r")
  61. details = detailsFile.read().split(":")
  62. except:
  63. log("Problem reading credentials file")
  64. return None
  65. if len(details) != 3:
  66. log("Invalid credentials file")
  67. return None
  68.  
  69. try:
  70. ftp = ftputil.FTPHost(details[0], details[1], details[2])
  71. except Exception, e:
  72. log("Connection failed: "+str(e))
  73. return None
  74. debug("FTP initialized")
  75. return ftp
  76.  
  77. def initSFTP():
  78. try:
  79. detailsFile = open("details","r")
  80. details = detailsFile.read().split(":")
  81. except:
  82. log("Problem reading credentials file")
  83. return None
  84. if len(details) != 3:
  85. log("Invalid credentials file")
  86. return None
  87.  
  88. try:
  89. ftp = pysftp.Connection(details[0], username=details[1], password=details[2])
  90. except Exception, e:
  91. log("Connection failed: "+str(e))
  92. return None
  93. sftp = 1
  94. debug("SFTP initialized")
  95. return ftp
  96.  
  97. def cleanPointers():
  98. logmain.close()
  99. logbak.close()
  100. ftp.close()
  101.  
  102. def process(local):
  103. remote = getRemote(local)
  104. if remote == None:
  105. log("Incorrect input file, aborting: "+local)
  106. return
  107. debug(" Remote: "+remote)
  108. if canIgnore(remote):
  109. debug(" Aborting on ignore")
  110. return
  111. if not isLegal(remote):
  112. debug(" Aborting on legal")
  113. return
  114. if isPack(remote):
  115. if not checkValid(local):
  116. debug(" Aborting on valid")
  117. return
  118. if checkPresent(local, remote):
  119. logFile("File already exists in privatepacks folder, aborting: "+remote, local)
  120. os.rename(local, local+".ignore")
  121. return
  122. if not upload(local, remote):
  123. return
  124. clean(local)
  125.  
  126. def getRemote(local):
  127. fromroot = local.find(rootdir)
  128. if fromroot != 0:
  129. return None
  130. else:
  131. return local[len(rootdir):]
  132.  
  133. def canIgnore(remote):
  134. for path in ignored:
  135. if re.search(path, remote) != None:
  136. return True
  137. return False
  138.  
  139. def isPack(remote):
  140. return re.search("^/privatepacks/.*\.zip$", remote) != None
  141.  
  142. def isStatic(remote):
  143. return re.search("^/static/", remote) != None
  144.  
  145. def isLegal(remote):
  146. if not isPack(remote) and not isStatic(remote):
  147. return False
  148.  
  149. global remotedir
  150. name = ""
  151. lastslash = remote.rfind("/")
  152. if lastslash == -1:
  153. name = remote # who knows?
  154. remotedir = "/"
  155. else:
  156. name = remote[lastslash+1:]
  157. remotedir = remote[:lastslash]
  158.  
  159. if name in banned:
  160. return False
  161. else:
  162. return True
  163.  
  164. def checkValid(local):
  165. try:
  166. return zipfile.ZipFile(local).testzip() == None
  167. except:
  168. return False
  169.  
  170. def checkPresent(local, remote):
  171. if sftp == 0:
  172. return ftp.path.exists(remote)
  173. else:
  174. return ftp.exists(remote)
  175.  
  176. def upload(local, remote):
  177. actualremote = remoterootdir+remote
  178. actualremotedir = remoterootdir+remotedir
  179. logFile("Uploading: "+remote, local)
  180. debug(" Actual upload: "+actualremote)
  181. time.sleep(1)
  182. try:
  183. if isPack(remote) and sftp == 0:
  184. debug(" Making folders: "+actualremotedir)
  185. ftp.makedirs(actualremotedir)
  186. if isPack(remote) and sftp == 1:
  187. if not ftp.exists(actualremotedir):
  188. debug(" Making folders: "+actualremotedir)
  189. ftp.makedirs(actualremotedir)
  190. if sftp == 1:
  191. ftp.put(local, actualremote)
  192. else:
  193. ftp.upload(local, actualremote)
  194. except Exception, e:
  195. log("Upload failed: "+str(e)+":"+str(e.errno)+":"+str(e.strerror))
  196. return False
  197. return True
  198.  
  199. def clean(local):
  200. os.remove(local)
  201. for num in range(len(local)-1,len(rootdir),-1):
  202. if local[num] == "/":
  203. try:
  204. debug("cleaning " + local[:num])
  205. os.rmdir(local[:num])
  206. except OSError:
  207. break
  208.  
  209. ftp = initSFTP()
  210. if ftp == None:
  211. log("Aborting")
  212. sys.exit()
  213.  
  214. for root, _, files in os.walk(rootdir):
  215. for f in files:
  216. fullpath = os.path.join(root, f)
  217. debug("Full: "+fullpath)
  218. process(fullpath)
  219.  
  220. cleanPointers()
Advertisement
Add Comment
Please, Sign In to add comment