fabfile.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env python
  2. import os
  3. import glob
  4. import subprocess
  5. try:
  6. from xmlrpc.client import ServerProxy
  7. except ImportError:
  8. from xmlrpclib import ServerProxy
  9. import boto
  10. from fabric.api import task, local
  11. if os.environ.get('AWS_ACCESS_KEY_ID') is not None \
  12. and len(os.environ['AWS_ACCESS_KEY_ID']) > 0 \
  13. and os.environ.get('AWS_SECRET_ACCESS_KEY') is not None \
  14. and len(os.environ['AWS_SECRET_ACCESS_KEY']) > 0:
  15. AWS_ENABLED = True
  16. else:
  17. AWS_ENABLED = False
  18. print("NOTE: S3 uploading is disabled because of missing AWS key environment variables.")
  19. # The keys are the file names of the Pandoc source files.
  20. # e.g. '01-frontpage.md'
  21. # The values are the slugs of the WordPress pages
  22. # e.g. http://www.swaroopch.com/notes/Python_en-Table_of_Contents
  23. MARKDOWN_FILES = {
  24. '01-frontpage.md' : 'Python',
  25. '02-table-of-contents.md' : 'Python_en-Table_of_Contents',
  26. }
  27. def _markdown_to_html(source_text):
  28. from_format = 'markdown'
  29. to_format = 'html'
  30. args = ['pandoc',
  31. '-f', from_format,
  32. '-t', to_format,
  33. '-S']
  34. p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  35. output = p.communicate(source_text)[0]
  36. # TODO Replace image URLs with uploaded AWS S3 public URLs
  37. return output
  38. @task
  39. def wp():
  40. for m in MARKDOWN_FILES.keys():
  41. converted_text = _markdown_to_html(open(m).read())
  42. with open(output_file_name, 'w') as output:
  43. # TODO Replace with uploading to WordPress
  44. # https://github.com/rgrp/pywordpress/blob/master/pywordpress.py
  45. # file:///Users/swaroop/code/docs/python/library/xmlrpclib.html
  46. output.write(converted_text)
  47. local("open {}".format(output_file_name))
  48. @task
  49. def epub():
  50. from_format = 'markdown'
  51. to_format = 'epub'
  52. output_file_name = 'byte_of_python.epub'
  53. args = ['pandoc',
  54. '-f', from_format,
  55. '-t', to_format,
  56. '-o', output_file_name,
  57. '-S'] + MARKDOWN_FILES.keys()
  58. local(' '.join(args))
  59. # TODO Add pdf()
  60. # TODO Make epub and pdf upload to S3 directly if AWS_ENABLED