Browse Source

Add tasks in fabfile

create a new_post task, which create a new_post with correct title
correct the serve task to add a livereload
Maxime Vidori 10 years ago
parent
commit
aba9c4daa1
3 changed files with 49 additions and 35 deletions
  1. 43 35
      fabfile.py
  2. 1 0
      requirements.txt
  3. 5 0
      templates/new_post.tplt

+ 43 - 35
fabfile.py

@@ -1,69 +1,77 @@
-from fabric.api import *
+from __future__ import unicode_literals
+
+import livereload
+import fabric.colors as colors
 import fabric.contrib.project as project
+import fabric.contrib.files as files
+import fabric.api as fabric
+import jinja2
+import pelican.utils as utils
+
+import datetime
 import os
+import os.path
 import sys
-import SimpleHTTPServer
-import SocketServer
 
 # Local path configuration (can be absolute or relative to fabfile)
-env.deploy_path = 'output'
-DEPLOY_PATH = env.deploy_path
+fabric.env.deploy_path = 'output'
+fabric.env.content_path = 'content'
+fabric.env.jinja = jinja2.Environment(
+    loader=jinja2.PackageLoader('fabfile', 'templates')
+)
+
+
+DEPLOY_PATH = fabric.env.deploy_path
 
 # Remote server configuration
 production = 'root@localhost:22'
 dest_path = '/var/www'
 
-# Rackspace Cloud Files configuration settings
-env.cloudfiles_username = 'my_rackspace_username'
-env.cloudfiles_api_key = 'my_rackspace_api_key'
-env.cloudfiles_container = 'my_cloudfiles_container'
-
-
 def clean():
     if os.path.isdir(DEPLOY_PATH):
-        local('rm -rf {deploy_path}'.format(**env))
-        local('mkdir {deploy_path}'.format(**env))
+        fabric.local('rm -rf {deploy_path}'.format(**fabric.env))
+        fabric.local('mkdir {deploy_path}'.format(**fabric.env))
 
 def build():
-    local('pelican -s pelicanconf.py')
+    fabric.local('pelican -s pelicanconf.py')
 
 def rebuild():
     clean()
     build()
 
 def regenerate():
-    local('pelican -r -s pelicanconf.py')
-
-def serve():
-    os.chdir(env.deploy_path)
-
-    PORT = 8000
-    class AddressReuseTCPServer(SocketServer.TCPServer):
-        allow_reuse_address = True
+    fabric.local('pelican -r -s pelicanconf.py')
 
-    server = AddressReuseTCPServer(('', PORT), SimpleHTTPServer.SimpleHTTPRequestHandler)
+def serve(*args):
+    port = args[0] if len(args) > 0 else 8000
+    server = livereload.Server()
+    server.watch(fabric.env.content_path, build)
+    server.serve(port=port, root=fabric.env.deploy_path)
 
-    sys.stderr.write('Serving on port {0} ...\n'.format(PORT))
-    server.serve_forever()
 
 def reserve():
     build()
     serve()
 
 def preview():
-    local('pelican -s publishconf.py')
+    fabric.local('pelican -s publishconf.py')
+
+def new_post(*args):
+    title = args[0] if len(args) > 0 else fabric.prompt('New post title?')
+    title = unicode(title, 'utf8')
+
+    date = datetime.date.today().isoformat()
+    filename = '.'.join([date, utils.slugify(title), 'md'])
+    filename = os.path.join(fabric.env.content_path, filename)
+    print(' '.join([colors.green('[create new post]'), filename]))
 
-def cf_upload():
-    rebuild()
-    local('cd {deploy_path} && '
-          'swift -v -A https://auth.api.rackspacecloud.com/v1.0 '
-          '-U {cloudfiles_username} '
-          '-K {cloudfiles_api_key} '
-          'upload -c {cloudfiles_container} .'.format(**env))
+    (fabric.env.jinja.get_template('new_post.tplt')
+     .stream(title=title)
+     .dump(filename, 'utf8'))
 
-@hosts(production)
+@fabric.hosts(production)
 def publish():
-    local('pelican -s publishconf.py')
+    fabric.local('pelican -s publishconf.py')
     project.rsync_project(
         remote_dir=dest_path,
         exclude=".DS_Store",

+ 1 - 0
requirements.txt

@@ -1,3 +1,4 @@
 pelican
 fabric
 markdown
+livereload

+ 5 - 0
templates/new_post.tplt

@@ -0,0 +1,5 @@
+Title: {{title}}
+Category:
+Tags:
+
+