Watchful11

Untitled

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