fabfile.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. and os.environ.get('AWS_S3_BUCKET_NAME') is not None \
  16. and len(os.environ['AWS_S3_BUCKET_NAME']) > 0:
  17. AWS_ENABLED = True
  18. else:
  19. AWS_ENABLED = False
  20. print("NOTE: S3 uploading is disabled because of missing AWS key environment variables.")
  21. # The keys are the file names of the Pandoc source files.
  22. # e.g. '01-frontpage.md'
  23. # The values are the slugs of the WordPress pages
  24. # e.g. http://www.swaroopch.com/notes/Python_en-Table_of_Contents
  25. MARKDOWN_FILES = {
  26. '01-frontpage.md' : 'Python',
  27. '02-table-of-contents.md' : 'Python_en-Table_of_Contents',
  28. }
  29. def _markdown_to_html(source_text):
  30. args = ['pandoc',
  31. '-f', 'markdown',
  32. '-t', 'html',
  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. args = ['pandoc',
  51. '-f', 'markdown',
  52. '-t', 'epub',
  53. '-o', 'byte_of_python.epub',
  54. '-S'] + MARKDOWN_FILES.keys()
  55. local(' '.join(args))
  56. @task
  57. def pdf():
  58. args = ['pandoc',
  59. '-f', 'markdown',
  60. ##'-t', 'pdf', # Intentionally commented out due to https://github.com/jgm/pandoc/issues/571
  61. '-o', 'byte_of_python.pdf',
  62. '-S'] + MARKDOWN_FILES.keys()
  63. local(' '.join(args))
  64. POSSIBLE_OUTPUTS = (
  65. 'byte_of_python.epub',
  66. 'byte_of_python.pdf',
  67. )
  68. @task
  69. def clean():
  70. for filename in POSSIBLE_OUTPUTS:
  71. if os.path.exists(filename):
  72. os.remove(filename)
  73. print("Removed {}".format(filename))
  74. # TODO Make epub and pdf upload to S3 directly if AWS_ENABLED
  75. # TODO http://docs.pythonboto.org/en/latest/s3_tut.html