File: ssi_expander.py

package info (click to toggle)
dygraphs 2.2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,340 kB
  • sloc: javascript: 24,842; sh: 800; python: 581; makefile: 45
file content (55 lines) | stat: -rwxr-xr-x 1,676 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/python3
'''
This script copies the files in one directory to another, expanding any SSI
<!-- #include --> statements it encounters along the way.
Only files that end in '.html' are processed, with the exceptions of
{header,footer}.html. Copy or symlink anything else manually.

Usage:

  python3 ssi_expander.py [source_directory] destination_directory

If source_directory is not specified, then the current directory is used.
'''

import os
import pathlib
import ssi
import sys

def _errorfn(msg, fn):
    sys.stderr.write('E: %s: %s\n' % (fn, msg))
    sys.exit(1)

def process(source, dest):
  for dirpath, dirnames, filenames in os.walk(source):
    dest_dir = os.path.realpath(os.path.join(dest, os.path.relpath(dirpath, source)))
    if not os.path.exists(dest_dir):
      os.mkdir(dest_dir)
    assert os.path.isdir(dest_dir)
    for filename in filenames:
      if not filename.endswith('.html'):
        continue
      if filename in ('header.html', 'footer.html'):
        continue
      src_path = os.path.abspath(os.path.join(source, dirpath, filename))
      dest_path = os.path.join(dest_dir, filename)
      pathlib.Path(dest_path).unlink(missing_ok=True)
      with open(dest_path, 'wb') as f:
        f.write(ssi.InlineIncludes(src_path, _errorfn))

    # ignore hidden directories
    for dirname in dirnames[:]:
      if dirname.startswith('.'):
        dirnames.remove(dirname)

if __name__ == '__main__':
  if len(sys.argv) == 2:
    source = '.'
    dest = sys.argv[1]
  elif len(sys.argv) == 3:
    source, dest = sys.argv[1:]
  else:
    _errorfn('Usage: %s [source_directory] destination_directory' % sys.argv[0])

  process(source, dest)