tasks.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import shutil
  4. import sys
  5. import datetime
  6. from invoke import task
  7. from invoke.util import cd
  8. from pelican.server import ComplexHTTPRequestHandler, RootedHTTPServer
  9. CONFIG = {
  10. # Local path configuration (can be absolute or relative to tasks.py)
  11. 'deploy_path': 'output',
  12. # Port for `serve`
  13. 'port': 8000,
  14. }
  15. @task
  16. def clean(c):
  17. """Remove generated files"""
  18. if os.path.isdir(CONFIG['deploy_path']):
  19. shutil.rmtree(CONFIG['deploy_path'])
  20. os.makedirs(CONFIG['deploy_path'])
  21. @task
  22. def build(c):
  23. """Build local version of site"""
  24. c.run('pelican -s pelicanconf.py')
  25. @task
  26. def rebuild(c):
  27. """`build` with the delete switch"""
  28. c.run('pelican -d -s pelicanconf.py')
  29. @task
  30. def regenerate(c):
  31. """Automatically regenerate site upon file modification"""
  32. c.run('pelican -r -s pelicanconf.py')
  33. @task
  34. def serve(c):
  35. """Serve site at http://localhost:8000/"""
  36. class AddressReuseTCPServer(RootedHTTPServer):
  37. allow_reuse_address = True
  38. server = AddressReuseTCPServer(
  39. CONFIG['deploy_path'],
  40. ('', CONFIG['port']),
  41. ComplexHTTPRequestHandler)
  42. sys.stderr.write('Serving on port {port} ...\n'.format(**CONFIG))
  43. server.serve_forever()
  44. @task
  45. def reserve(c):
  46. """`build`, then `serve`"""
  47. build(c)
  48. serve(c)
  49. @task
  50. def preview(c):
  51. """Build production version of site"""
  52. c.run('pelican -s publishconf.py')
  53. @task
  54. def publish(c):
  55. """Publish to production via rsync"""
  56. c.run('pelican -s publishconf.py')
  57. c.run(
  58. 'rsync --delete --exclude ".DS_Store" -pthrvz -c '
  59. '{} {production}:{dest_path}'.format(
  60. CONFIG['deploy_path'].rstrip('/') + '/',
  61. **CONFIG))