Snakefile 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # -*- coding: utf-8; -*-
  2. import locale
  3. import os.path
  4. import regex
  5. from collections import Iterable, Mapping # in Python 3 use from collections.abc
  6. from distutils.spawn import find_executable
  7. from fnmatch import fnmatch
  8. from kwonly import kwonly
  9. from subprocess import check_output, check_call
  10. from tempfile import NamedTemporaryFile
  11. try:
  12. from os import scandir, walk
  13. except ImportError:
  14. from scandir import scandir, walk
  15. def unnest(*args):
  16. """Un-nest list- and tuple-like elements in arguments.
  17. "List-like" means anything with a len() and whose elments can be
  18. accessed with numeric indexing, except for string-like elements. It
  19. must also be an instance of the collections.Iterable abstract class.
  20. Dict-like elements and iterators/generators are not affected.
  21. This function always returns a list, even if it is passed a single
  22. scalar argument.
  23. """
  24. result = []
  25. for arg in args:
  26. if isinstance(arg, str):
  27. # String
  28. result.append(arg)
  29. elif isinstance(arg, Mapping):
  30. # Dict-like
  31. result.append(arg)
  32. elif isinstance(arg, Iterable):
  33. try:
  34. # Duck-typing test for list-ness (a stricter condition
  35. # than just "iterable")
  36. for i in range(len(arg)):
  37. result.append(arg[i])
  38. except TypeError:
  39. # Iterable but not list-like
  40. result.append(arg)
  41. else:
  42. # Not iterable
  43. result.append(arg)
  44. return result
  45. def check_output_decode(*args, encoding=locale.getpreferredencoding(), **kwargs):
  46. """Shortcut for check.output + str.decode"""
  47. return check_output(*args, **kwargs).decode(encoding)
  48. def find_mac_app(name):
  49. try:
  50. return check_output_decode(
  51. ["mdfind",
  52. "kMDItemDisplayName=={name}&&kMDItemKind==Application".format(name=name)]).split("\n")[0]
  53. except Exception:
  54. return None
  55. def glob_recursive(pattern, top=".", include_hidden=False, *args, **kwargs):
  56. """Combination of glob.glob and os.walk.
  57. Reutrns the relative path to every file or directory matching the
  58. pattern anywhere in the specified directory hierarchy. Defaults to the
  59. current working directory. Any additional arguments are passed to
  60. os.walk."""
  61. for (path, dirs, files) in walk(top, *args, **kwargs):
  62. for f in dirs + files:
  63. if include_hidden or f.startswith("."):
  64. continue
  65. if fnmatch(f, pattern):
  66. yield os.path.normpath(os.path.join(path, f))
  67. LYXPATH = find_executable("lyx") or \
  68. os.path.join(find_mac_app("LyX"), "Contents/MacOS/lyx") or \
  69. '/bin/false'
  70. @kwonly(0)
  71. def rsync_list_files(extra_rsync_args=(), include_dirs=False, *paths):
  72. """Iterate over the files in path that rsync would copy.
  73. By default, only files are listed, not directories, since doit doesn't
  74. like dependencies on directories because it can't hash them.
  75. This uses "rsync --list-only" to make rsync directly indicate which
  76. files it would copy, so any exclusion/inclusion rules are taken into
  77. account.
  78. """
  79. rsync_list_cmd = [ 'rsync', '-r', "--list-only" ] + unnest(extra_rsync_args) + unnest(paths) + [ "." ]
  80. rsync_out = check_output_decode(rsync_list_cmd).splitlines()
  81. for line in rsync_out:
  82. s = regex.search("^(-|d)(?:\S+\s+){4}(.*)", line)
  83. if s is not None:
  84. if include_dirs or s.group(1) == '-':
  85. yield s.group(2)
  86. def lyx_image_deps(wildcards):
  87. lyxfile = wildcards.filename + ".lyx"
  88. def lyx_bib_deps(wildcards):
  89. # Cheat: Assume every bib file is a dependency of any LaTeX
  90. # operation
  91. return list(glob_recursive('*.bib'))
  92. readme_files = list(glob_recursive("README.mkdn", top="examples"))
  93. index_files = [ os.path.join(os.path.dirname(f), "index.html") for f in readme_files ]
  94. rsync_common_args = ["-rL", "--size-only", "--delete", "--exclude", ".DS_Store", "--delete-excluded",]
  95. all_example_files = set(rsync_list_files('examples', extra_rsync_args=rsync_common_args))
  96. all_example_files = all_example_files.union(index_files)
  97. rsync_dest = "mneme:public_html/resume/"
  98. rule build_all:
  99. input: "ryan_thompson_resume.pdf", index_files
  100. rule create_resume_pdf:
  101. input: lyxfile="ryan_thompson_resume.lyx", bibfile="citations.bib", headshot="headshot-crop.jpg"
  102. output: pdf="ryan_thompson_resume.pdf"
  103. shell: '{LYXPATH:q} --export-to pdf4 {output.pdf:q} {input.lyxfile:q}'
  104. rule create_resume_html:
  105. input: lyxfile="ryan_thompson_resume.lyx", bibfile="citations.bib", headshot="headshot-crop.jpg"
  106. output: html="ryan_thompson_resume.html"
  107. run:
  108. with NamedTemporaryFile() as tempf:
  109. shell('{LYXPATH:q} --export-to xhtml {tempf.name:q} {input.lyxfile:q}')
  110. shell('''cat {tempf.name:q} | perl -lape 's[<span class="flex_cv_image">(.*?)</span>][<span class="flex_cv_image"><img src="$1" width="100"></span>]g' > {output.html:q}''')
  111. rule link_resume_to_index_html:
  112. input: 'ryan_thompson_resume.html'
  113. output: 'index.html'
  114. shell: 'ln -s {input:q} {output:q}'
  115. rule readme_to_index_html:
  116. input: "{dirname}/README.mkdn"
  117. output: "{dirname}/index.html"
  118. shell: 'pandoc -t html -o {output[0]:q} {input[0]:q}'
  119. rule publish:
  120. input: roots=('ryan_thompson_resume.pdf', 'ryan_thompson_resume.html', 'index.html', 'examples', 'headshot-crop.jpg'),
  121. others=all_example_files
  122. shell: '''
  123. rsync --info=progress2 {rsync_common_args:q} \
  124. {input.roots:q} {rsync_dest:q}
  125. '''