Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- ## Rules:
- ## Don't overwrite modpacks.xml, thirdparty.xml or texturepack.xml
- ## Only upload files in the static and privatepacks folders
- ## Don't overwrite zips
- import ftplib
- import sys
- import time
- import os
- import ftputil
- rootdif = "/home/ftp"
- logmain = open("/home/ftp/log.txt","a")
- logbak = open("/root/log.txt","a")
- def log(string):
- logmain.write(string)
- logbak.write(string)
- if len(sys.argv) < 2:
- log("No file passed, aborting\n")
- sys.exit()
- local = sys.argv[1]
- fromroot = local.find(rootdif)
- if fromroot != 0:
- log("Incorrect input, aborting: "+local+"\n")
- sys.exit()
- if not os.access(local, os.R_OK):
- log("File does not exist or is not readable, aborting: "+remote+"\n")
- sys.exit()
- remote = local[len(rootdif):]
- isstatic = remote.find("static")
- isprivate = remote.find("privatepacks")
- if isstatic != 1 and isprivate != 1:
- log("Not a recognized dir, aborting: "+remote+"\n")
- sys.exit()
- banned = ["modpacks.xml", "thirdparty.xml", "texturepack.xml"]
- lastslash = remote.rfind("/")
- if lastslash == -1:
- name = remote # who knows?
- remotedir = "/"
- else:
- name = remote[lastslash+1:]
- remotedir = remote[:lastslash]
- if name in banned:
- log("File is banned from upload, aborting: "+remote+"\n")
- sys.exit()
- sizeFilePath = local+".size"
- if os.access(sizeFilePath, os.R_OK):
- #size file exists, so another instance is already uploading this file
- sys.exit()
- sizeFile = open(sizeFilePath,"w")
- size = os.path.getsize(local)
- sizeFile.write(size)
- time.sleep(2)
- while size != os.path.getsize(local):
- size = os.path.getsize(local)
- sizeFile.write(size)
- time.sleep(2)
- sizeFile.close()
- os.remove(sizeFilePath)
- try:
- ftp = ftputil.FTPHost("address", "user", "pass")
- except Exception, e:
- log("Connection failed: "+str(e)+"\n")
- log("Aborting: "+remote+"\n")
- sys.exit()
- if isprivate == 1:
- if ftp.path.exists(remote):
- log("File already exists in privatepacks folder, aborting: "+remote+"\n")
- sys.exit()
- ftp.makedirs(remotedir)
- log("Uploading: "+remote+"\n")
- try:
- ftp.upload(local, remote)
- except Exception, e:
- log("Upload failed: "+str(e)+"\n")
- sys.exit()
- os.remove(local)
- time.sleep(2)
- for num in range(len(local)-1,len(rootdif),-1):
- if local[num] == "/":
- try:
- os.rmdir(local[:num])
- except OSError:
- break
- logmain.close()
- logbak.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement