| 1 | #!/usr/bin/python |
| 2 | ''' |
| 3 | This script copies the files in one directory to another, expanding any SSI |
| 4 | <!-- #include --> statements it encounters along the way. |
| 5 | |
| 6 | Usage: |
| 7 | |
| 8 | ./ssi_expander.py [source_directory] destination_directory |
| 9 | |
| 10 | If source_directory is not specified, then the current directory is used. |
| 11 | ''' |
| 12 | |
| 13 | import os |
| 14 | import ssi |
| 15 | import sys |
| 16 | |
| 17 | def process(source, dest): |
| 18 | for dirpath, dirnames, filenames in os.walk(source): |
| 19 | dest_dir = os.path.realpath(os.path.join(dest, os.path.relpath(dirpath, source))) |
| 20 | if not os.path.exists(dest_dir): |
| 21 | os.mkdir(dest_dir) |
| 22 | assert os.path.isdir(dest_dir) |
| 23 | for filename in filenames: |
| 24 | src_path = os.path.abspath(os.path.join(source, dirpath, filename)) |
| 25 | dest_path = os.path.join(dest_dir, filename) |
| 26 | if not filename.endswith('.html'): |
| 27 | os.symlink(src_path, dest_path) |
| 28 | else: |
| 29 | file(dest_path, 'w').write(ssi.InlineIncludes(src_path)) |
| 30 | |
| 31 | # ignore hidden directories |
| 32 | for dirname in dirnames[:]: |
| 33 | if dirname.startswith('.'): |
| 34 | dirnames.remove(dirname) |
| 35 | |
| 36 | |
| 37 | if __name__ == '__main__': |
| 38 | if len(sys.argv) == 2: |
| 39 | source = '.' |
| 40 | dest = sys.argv[1] |
| 41 | elif len(sys.argv) == 3: |
| 42 | source, dest = sys.argv[1:] |
| 43 | else: |
| 44 | sys.stderr.write('Usage: %s [source_directory] destination_directory\n' % sys.argv[0]) |
| 45 | sys.exit(1) |
| 46 | |
| 47 | process(source, dest) |