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
- from time import gmtime, strftime
- import ftputil
- import os, os.path
- import re
- import zipfile
- from os import stat
- from pwd import getpwuid
- banned = ["modpacks.xml", "thirdparty.xml", "texturepack.xml"]
- ignored = ["^/log\.txt","^/listed","\.ignore$"]
- rootdir = "/home/ftp"
- remotedir = ""
- logmain = open("/home/ftp/log.txt","a")
- logbak = open("/root/log.txt","a")
- isDebug = False
- ftp = None
- if len(sys.argv) > 1:
- if sys.argv[1] == "-v":
- print("Starting in debug mode")
- isDebug = True
- def log(string):
- logFile(string, None)
- def logFile(string, local):
- if local != None:
- string = "[" + find_owner(local) + "] " + string
- string = string + "\n"
- time = strftime("[%m-%d %H:%M:%S]", gmtime())
- logmain.write(time+" "+string)
- logbak.write(time+" "+string)
- debug(" "+string)
- def debug(string):
- if isDebug:
- print(string)
- def find_owner(filename):
- return getpwuid(stat(filename).st_uid).pw_name
- def initFTP():
- try:
- detailsFile = open("details","r")
- details = detailsFile.read().split(":")
- except:
- log("Problem reading credentials file")
- return None
- if len(details) != 3:
- log("Invalid credentials file")
- return None
- try:
- ftp = ftputil.FTPHost(details[0], details[1], details[2])
- except Exception, e:
- log("Connection failed: "+str(e))
- return None
- debug("FTP initialized")
- return ftp
- def cleanPointers():
- logmain.close()
- logbak.close()
- ftp.close()
- def process(local):
- remote = getRemote(local)
- if remote == None:
- log("Incorrect input file, aborting: "+local)
- return
- debug(" Remote: "+remote)
- if canIgnore(remote):
- debug(" Aborting on ignore")
- return
- if not isLegal(remote):
- debug(" Aborting on legal")
- return
- if isPack(remote):
- if not checkValid(local):
- debug(" Aborting on valid")
- return
- if checkPresent(local, remote):
- logFile("File already exists in privatepacks folder, aborting: "+remote, local)
- os.rename(local, local+".ignore")
- return
- if not upload(local, remote):
- return
- clean(local)
- def getRemote(local):
- fromroot = local.find(rootdir)
- if fromroot != 0:
- return None
- else:
- return local[len(rootdir):]
- def canIgnore(remote):
- for path in ignored:
- if re.search(path, remote) != None:
- return True
- return False
- def isPack(remote):
- return re.search("^/privatepacks/.*\.zip$", remote) != None
- def isStatic(remote):
- return re.search("^/static/.*\.xml$", remote) != None
- def isLegal(remote):
- if not isPack(remote) and not isStatic(remote):
- return False
- global remotedir
- name = ""
- lastslash = remote.rfind("/")
- if lastslash == -1:
- name = remote # who knows?
- remotedir = "/"
- else:
- name = remote[lastslash+1:]
- remotedir = remote[:lastslash]
- if name in banned:
- return False
- else:
- return True
- def checkValid(local):
- try:
- return zipfile.ZipFile(local).testzip() == None
- except:
- return False
- def checkPresent(local, remote):
- return ftp.path.exists(remote)
- def upload(local, remote):
- logFile("Uploading: "+remote, local)
- time.sleep(1)
- try:
- if isPack(remote):
- debug(" Making folders: "+remotedir)
- ftp.makedirs(remotedir)
- ftp.upload(local, remote)
- except Exception, e:
- log("Upload failed: "+str(e))
- return False
- return True
- def clean(local):
- os.remove(local)
- for num in range(len(local)-1,len(rootdir),-1):
- if local[num] == "/":
- try:
- os.rmdir(local[:num])
- except OSError:
- break
- ftp = initFTP()
- if ftp == None:
- log("Aborting")
- sys.exit()
- for root, _, files in os.walk(rootdir):
- for f in files:
- fullpath = os.path.join(root, f)
- debug("Full: "+fullpath)
- process(fullpath)
- cleanPointers()
Advertisement
Add Comment
Please, Sign In to add comment