Skip to content

Instantly share code, notes, and snippets.

@atmin
Forked from ianpreston/fabfile.py
Last active December 16, 2015 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atmin/5403359 to your computer and use it in GitHub Desktop.
Save atmin/5403359 to your computer and use it in GitHub Desktop.
from fabric.api import local
import sys, os, webbrowser, jinja2
BASE_PATH = sys.path[0]
SRC_PATH = os.path.join(BASE_PATH, 'src')
BUILD_PATH = os.path.join(BASE_PATH, 'build')
JINJA_ENV = jinja2.Environment(loader=jinja2.FileSystemLoader(SRC_PATH))
PROCESSORS = {
'.html': lambda in_file, out_file:\
local('mkdir -p {0}'.format(os.path.dirname(out_file))) or \
open(out_file, 'w').write(
JINJA_ENV.get_template(in_file.replace(SRC_PATH, '')).render()
),
'*' : lambda in_file, out_file:\
local('mkdir -p {0} && cp {1} {2}'.format(os.path.dirname(out_file), in_file, out_file))
}
def build():
for root, _, filenames in os.walk(SRC_PATH):
relative_root = root.replace(SRC_PATH, '')
if relative_root.find('/_') > -1: continue
for filename in filenames:
if filename.startswith('_'): continue
local('echo "processing {0}..."'.format(filename))
processor = PROCESSORS.get(os.path.splitext(filename)[1])
if not processor: processor = PROCESSORS['*']
processor(
os.path.join(root, filename),
os.path.join(root, filename).replace(SRC_PATH, BUILD_PATH)
)
def browse(port=4200):
build()
webbrowser.open('http://localhost:{0}/'.format(port))
local('cd {0} && python -m SimpleHTTPServer {1}'.format(BUILD_PATH, port))
def deploy(user='sissel', remote='noveria.0x-1.com', webroot='/var/www'):
build()
local('scp -r {0}/* {1}@{2}:{3}'.format(BUILD_PATH, user, remote, webroot))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment