An example of fabric commands against AlwaysData web hosting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import os | |
from cgi import escape | |
from datetime import date, datetime | |
import markdown | |
from fabric.api import run, env | |
USERNAME = 'larlet' | |
env.hosts = ['%s@ssh.alwaysdata.com' % USERNAME] | |
THOUGHTS_PATH = "larlet.fr/david/thoughts/" | |
FEED_PATH = "larlet.fr/david/log/" | |
TIMEZONE = "+09:00" # lazy me. | |
def __determine_season(date_instance): | |
""" | |
Determines the approximative season given a date instance. | |
""" | |
year = date_instance.year | |
if date(year, 3, 21) < date_instance < date(year, 6, 20): | |
return u'Spring' | |
elif date(year, 6, 21) < date_instance < date(year, 9, 22): | |
return u'Summer' | |
elif date(year, 9, 23) < date_instance < date(year, 12, 21): | |
return u'Autumn' | |
else: | |
return u'Winter' | |
def __retrieve_articles(): | |
""" | |
Retrieves articles' content from markdown files. | |
""" | |
articles = [] | |
# Generates the html for each md file in the thoughts directory | |
for writing in os.listdir(THOUGHTS_PATH): | |
if writing.endswith('.md'): | |
parser = markdown.Markdown(extensions = ['meta']) | |
source = open(THOUGHTS_PATH+writing).read().decode('utf-8') | |
content = parser.convert(source) | |
# Decorates with the unparsed date for the sorting part | |
# but if the date is not set, considered as draft and not published | |
if 'date' in parser.Meta: | |
articles.append((parser.Meta['date'][0], content, parser.Meta)) | |
articles.sort() # by date | |
return articles | |
def compile_thoughts(): | |
page_template = open(THOUGHTS_PATH+'template.html').read().decode('utf-8') | |
article_template = u""" | |
<article id="%(slug)s" typeof="schema:BlogPosting"> | |
<h2 property="schema:name"><a href="#%(slug)s" title="Link to that content" property="schema:url">%(title)s</a></h2> | |
<div property="schema:articleBody">%(content)s</div> | |
<footer>— | |
<span property="schema:datePublished">%(season)s %(year)s</span>, | |
<span property="schema:contentLocation">%(place)s</span> | |
</footer> | |
</article>""" | |
articles = __retrieve_articles() | |
rendered_articles = [] | |
# Deals with templating and metadata | |
for article_date, article_content, article_metadata in reversed(articles): | |
title = article_metadata['title'][0] | |
place = article_metadata['place'][0] | |
splited_date = article_date.split('/') | |
publication_date = date(int(splited_date[0]), | |
int(splited_date[1]), | |
int(splited_date[2])) | |
season = __determine_season(publication_date) | |
# Takes care of both 2011/12/23 and 2012/02/20 condidered as Winter 2011 | |
year = (season == u"Winter" and publication_date.month < 12) \ | |
and publication_date.year-1 or publication_date.year | |
# Generates automatically the slug if not specified | |
slug = 'slug' in article_metadata and article_metadata['slug'][0] or title.lower().replace(' ', '') | |
content = article_content | |
print "Compiling: %s (%s %s)" % (title, season, year) | |
rendered_articles.append(article_template % { | |
'title': title, | |
'slug': slug, | |
'content': content, | |
'season': season, | |
'year': year, | |
'place': place, | |
}) | |
# Puts articles in the general template | |
page = page_template.replace(u"{{articles}}", u"".join(rendered_articles)) | |
open(THOUGHTS_PATH+'index.html', 'w').write(page.encode('utf-8')) | |
def compile_feed(): | |
page_template = open(FEED_PATH+'template.xml').read().decode('utf-8') | |
feed_article_template = u""" | |
<entry xml:lang="en"> | |
<title>Thoughts: %(title)s</title> | |
<link href="https://larlet.fr/david/thoughts/#%(slug)s" rel="alternate" type="text/html" /> | |
<updated>%(normalized_date)s</updated> | |
<id>https://larlet.fr/david/thoughts/#%(slug)s</id> | |
<summary type="html">%(content)s</summary> | |
</entry>""" | |
articles = __retrieve_articles() | |
rendered_articles = [] | |
# Deals with templating and metadata | |
for article_date, article_content, article_metadata in reversed(articles[-5:]): | |
title = article_metadata['title'][0] | |
splited_date = article_date.split('/') | |
publication_date = date(int(splited_date[0]), | |
int(splited_date[1]), | |
int(splited_date[2])) | |
normalized_date = publication_date.strftime('%Y-%m-%dT%H:%M:%S' + TIMEZONE) | |
# Generates automatically the slug if not specified | |
slug = 'slug' in article_metadata and article_metadata['slug'][0] or title.lower().replace(' ', '') | |
content = escape(article_content) | |
print "Compiling Feed: %s (%s)" % (title, publication_date) | |
rendered_articles.append(feed_article_template % { | |
'title': title, | |
'slug': slug, | |
'content': content, | |
'normalized_date': normalized_date, | |
}) | |
current_datetime = datetime.now().strftime('%Y-%m-%dT%H:%M:%S' + TIMEZONE) | |
# Puts articles in the general template | |
page = page_template.replace(u"{{thoughts}}", u"".join(rendered_articles))\ | |
.replace(u"{{current_datetime}}", current_datetime) | |
open(FEED_PATH+'index.xml', 'w').write(page.encode('utf-8')) | |
def compile(): | |
compile_thoughts() | |
compile_feed() | |
def deploy(): | |
run('hg pull -u -R /home/%s/www' % USERNAME) | |
infos() | |
def log(filename="admin/log/access.log", backlog='F'): | |
run("tail -%s '%s'" % (backlog, filename)) | |
def errors(backlog=200): | |
return log("admin/log/error.log", backlog) | |
def infos(): | |
disk_usage = run('du -sh /home/%s/' % USERNAME) | |
disk_usage_mb = disk_usage.split('\t', 1)[0][:-1] | |
disk_usage_percent = float(disk_usage_mb)/10000.*100. | |
print '%sMo out of 10Go (%s%%)' % (disk_usage_mb, disk_usage_percent) | |
def top(): | |
run('top -U %s' % USERNAME) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment