fabfile.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 local
  11. assert os.environ.get('AWS_ACCESS_KEY_ID') is not None, "Set AWS_ACCESS_KEY_ID environment variable"
  12. assert len(os.environ['AWS_ACCESS_KEY_ID']) > 0, "Set AWS_ACCESS_KEY_ID environment variable"
  13. assert os.environ.get('AWS_SECRET_ACCESS_KEY') is not None, "Set AWS_SECRET_ACCESS_KEY environment variable"
  14. assert len(os.environ['AWS_SECRET_ACCESS_KEY']) > 0, "Set AWS_SECRET_ACCESS_KEY environment variable"
  15. # The keys are the file names of the Pandoc source files.
  16. # e.g. '01-frontpage.md'
  17. # The values are the slugs of the WordPress pages
  18. # e.g. http://www.swaroopch.com/notes/Python_en-Table_of_Contents
  19. MARKDOWN_FILES = {
  20. '01-frontpage.md' : 'Python',
  21. '02-table-of-contents.md' : 'Python_en-Table_of_Contents',
  22. }
  23. def _markdown_to_html(source_text):
  24. from_format = 'markdown'
  25. to_format = 'html'
  26. args = ['pandoc',
  27. '-f', from_format,
  28. '-t', to_format,
  29. '-S']
  30. p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  31. output = p.communicate(source_text)[0]
  32. # TODO Replace image URLs with uploaded AWS S3 public URLs
  33. return output
  34. def wp():
  35. for m in MARKDOWN_FILES.keys():
  36. converted_text = _markdown_to_html(open(m).read())
  37. with open(output_file_name, 'w') as output:
  38. # TODO Replace with uploading to WordPress
  39. # https://github.com/rgrp/pywordpress/blob/master/pywordpress.py
  40. # file:///Users/swaroop/code/docs/python/library/xmlrpclib.html
  41. output.write(converted_text)
  42. local("open {}".format(output_file_name))
  43. def epub():
  44. from_format = 'markdown'
  45. to_format = 'epub'
  46. output_file_name = 'byte_of_python.epub'
  47. args = ['pandoc',
  48. '-f', from_format,
  49. '-t', to_format,
  50. '-o', output_file_name,
  51. '-S'] + MARKDOWN_FILES.keys()
  52. local(' '.join(args))