dodo.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env doit -f
  2. import regex
  3. from fnmatch import fnmatch
  4. import os.path
  5. from subprocess import check_output
  6. from doit import create_after
  7. from distutils.spawn import find_executable
  8. try:
  9. from os import scandir, walk
  10. except ImportError:
  11. from scandir import scandir, walk
  12. DOIT_CONFIG = {
  13. 'default_tasks': ['publish_to_mneme'],
  14. }
  15. def glob_recursive(pattern, top=".", include_hidden=False, *args, **kwargs):
  16. """Combination of glob.glob and os.walk.
  17. Reutrns the relative path to every file or directory matching the
  18. pattern anywhere in the specified directory hierarchy. Defaults to the
  19. current working directory. Any additional arguments are passed to
  20. os.walk."""
  21. for (path, dirs, files) in walk(top, *args, **kwargs):
  22. for f in dirs + files:
  23. if include_hidden or f.startswith("."):
  24. continue
  25. if fnmatch(f, pattern):
  26. yield os.path.normpath(os.path.join(path, f))
  27. LYXPATH = find_executable("lyx") or "/Applications/LyX.app/Contents/MacOS/lyx"
  28. def task_lyx2pdf():
  29. yield {
  30. 'name': None,
  31. 'doc': "Convert LyX file to PDF."
  32. }
  33. for lyxfile in glob_recursive("*.lyx"):
  34. pdffile = lyxfile[:-3] + "pdf"
  35. lyx_cmd = [LYXPATH, "--export-to", "pdf4" , pdffile, lyxfile]
  36. yield {
  37. 'name': lyxfile,
  38. 'actions': [lyx_cmd],
  39. 'file_dep': [lyxfile],
  40. 'targets': [pdffile],
  41. 'verbosity': 0,
  42. }
  43. def task_readme2index():
  44. yield {
  45. 'name': None,
  46. 'doc': "Convert README.mkdn file to index.html."
  47. }
  48. for mkdnfile in glob_recursive("README.mkdn", top="examples"):
  49. htmlfile = os.path.join(os.path.dirname(mkdnfile), "index.html")
  50. yield {
  51. 'name': mkdnfile,
  52. 'actions': [["pandoc", "-t", "html", "-o", htmlfile, mkdnfile]],
  53. 'file_dep': [mkdnfile],
  54. 'targets': [htmlfile],
  55. }
  56. @create_after(executed='readme2index')
  57. def task_publish_to_mneme():
  58. rsync_common_args = [
  59. "rsync", "-vrL", "--size-only", "--delete",
  60. "--exclude", ".DS_Store", "--delete-excluded",
  61. ]
  62. rsync_srcs = ["ryan_thompson_resume.pdf", "examples"]
  63. rsync_dest = "mneme:public_html/resume/"
  64. # Compute file dependencies by running "rsync --list-only" and
  65. # parsing the output.
  66. rsync_list_cmd = rsync_common_args + rsync_srcs + [".", "--list-only"]
  67. rsync_out = check_output(rsync_list_cmd).splitlines()
  68. file_deps = []
  69. for line in rsync_out:
  70. s = regex.search("^-(?:\S+\s+){4}(.*)", line)
  71. if s is not None:
  72. file_deps.append(s.group(1))
  73. rsync_xfer_cmd = rsync_common_args + rsync_srcs + [rsync_dest]
  74. return {
  75. 'actions': [rsync_xfer_cmd],
  76. 'file_dep': file_deps,
  77. 'doc': "Sync resume and supporting files to mneme.",
  78. 'verbosity': 2,
  79. }