Le site internet de CLI
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tasks.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. from pelican.settings import DEFAULT_CONFIG, get_settings_from_file
  10. SETTINGS_FILE_BASE = 'pelicanconf.py'
  11. SETTINGS = {}
  12. SETTINGS.update(DEFAULT_CONFIG)
  13. LOCAL_SETTINGS = get_settings_from_file(SETTINGS_FILE_BASE)
  14. SETTINGS.update(LOCAL_SETTINGS)
  15. CONFIG = {
  16. 'settings_base': SETTINGS_FILE_BASE,
  17. 'settings_publish': 'publishconf.py',
  18. # Output path. Can be absolute or relative to tasks.py. Default: 'output'
  19. 'deploy_path': SETTINGS['OUTPUT_PATH'],
  20. # Remote server configuration
  21. 'production': 'www-git@www2.frepoteries.fr:22',
  22. 'dest_path': '/home/wwww-git/scop-cli',
  23. # Port for `serve`
  24. 'port': 8000,
  25. }
  26. @task
  27. def clean(c):
  28. """Remove generated files"""
  29. if os.path.isdir(CONFIG['deploy_path']):
  30. shutil.rmtree(CONFIG['deploy_path'])
  31. os.makedirs(CONFIG['deploy_path'])
  32. @task
  33. def build(c):
  34. """Build local version of site"""
  35. c.run('pelican -s {settings_base}'.format(**CONFIG))
  36. @task
  37. def rebuild(c):
  38. """`build` with the delete switch"""
  39. c.run('pelican -d -s {settings_base}'.format(**CONFIG))
  40. @task
  41. def regenerate(c):
  42. """Automatically regenerate site upon file modification"""
  43. c.run('pelican -r -s {settings_base}'.format(**CONFIG))
  44. @task
  45. def serve(c):
  46. """Serve site at http://localhost:$PORT/ (default port is 8000)"""
  47. class AddressReuseTCPServer(RootedHTTPServer):
  48. allow_reuse_address = True
  49. server = AddressReuseTCPServer(
  50. CONFIG['deploy_path'],
  51. ('', CONFIG['port']),
  52. ComplexHTTPRequestHandler)
  53. sys.stderr.write('Serving on port {port} ...\n'.format(**CONFIG))
  54. server.serve_forever()
  55. @task
  56. def reserve(c):
  57. """`build`, then `serve`"""
  58. build(c)
  59. serve(c)
  60. @task
  61. def preview(c):
  62. """Build production version of site"""
  63. c.run('pelican -s {settings_publish}'.format(**CONFIG))
  64. @task
  65. def livereload(c):
  66. """Automatically reload browser tab upon file modification."""
  67. from livereload import Server
  68. build(c)
  69. server = Server()
  70. # Watch the base settings file
  71. server.watch(CONFIG['settings_base'], lambda: build(c))
  72. # Watch content source files
  73. content_file_extensions = ['.md', '.rst']
  74. for extension in content_file_extensions:
  75. content_blob = '{0}/**/*{1}'.format(SETTINGS['PATH'], extension)
  76. server.watch(content_blob, lambda: build(c))
  77. # Watch the theme's templates and static assets
  78. theme_path = SETTINGS['THEME']
  79. server.watch('{}/templates/*.html'.format(theme_path), lambda: build(c))
  80. static_file_extensions = ['.css', '.js']
  81. for extension in static_file_extensions:
  82. static_file = '{0}/static/**/*{1}'.format(theme_path, extension)
  83. server.watch(static_file, lambda: build(c))
  84. # Serve output path on configured port
  85. server.serve(port=CONFIG['port'], root=CONFIG['deploy_path'])
  86. @task
  87. def publish(c):
  88. """Publish to production via rsync"""
  89. c.run('pelican -s {settings_publish}'.format(**CONFIG))
  90. c.run(
  91. 'rsync --delete --exclude ".DS_Store" -pthrvz -c '
  92. '{} {production}:{dest_path}'.format(
  93. CONFIG['deploy_path'].rstrip('/') + '/',
  94. **CONFIG))