From e483d0e90831949ef55406664138e4eb397ac75a Mon Sep 17 00:00:00 2001 From: Ryan Kelly Date: Sat, 30 Apr 2011 14:40:03 +1000 Subject: [PATCH] add basic support for publishing to a PyFS object --- hyde/ext/publishers/pyfs.py | 55 +++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 hyde/ext/publishers/pyfs.py diff --git a/hyde/ext/publishers/pyfs.py b/hyde/ext/publishers/pyfs.py new file mode 100644 index 0000000..bf64691 --- /dev/null +++ b/hyde/ext/publishers/pyfs.py @@ -0,0 +1,55 @@ +""" +Contains classes and utilities that help publishing a hyde website to +a filesystem using PyFilesystem FS objects. +""" + +import getpass + +from hyde.fs import File, Folder +from hyde.publisher import Publisher + +from hyde.util import getLoggerWithNullHandler +logger = getLoggerWithNullHandler('hyde.ext.publishers.pyfs') + + +from fs.osfs import OSFS +from fs.path import pathjoin +from fs.opener import fsopendir + + + +class PyFS(Publisher): + + def initialize(self, settings): + self.settings = settings + self.url = settings.url + self.prompt_for_credentials() + self.fs = fsopendir(self.url) + + def prompt_for_credentials(self): + credentials = {} + if "%(username)s" in self.url: + print "Username: ", + credentials["username"] = raw_input().strip() + if "%(password)s" in self.url: + credentials["password"] = getpass.getpass("Password: ") + if credentials: + self.url = self.url % credentials + + def publish(self): + super(PyFS, self).publish() + deploy_fs = OSFS(self.site.config.deploy_root_path.path) + for (dirnm,filenms) in deploy_fs.walk(): + logger.info("Making directory: %s",dirnm) + self.fs.makedir(dirnm,allow_recreate=True) + for filenm in filenms: + filepath = pathjoin(dirnm,filenm) + logger.info("Uploading file: %s",filepath) + with deploy_fs.open(filepath,"rb") as f: + self.fs.setcontents(filepath,f) + for filenm in self.fs.listdir(dirnm,files_only=True): + filepath = pathjoin(dirnm,filenm) + if filenm not in filenms: + logger.info("Removing file: %s",filepath) + self.fs.remove(filepath) +