~yeliabmas/sloecode/wiki-merge-proposal

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python

"""A simple script that takes source text files with reST markup, and spits
out HTML files in the correct directory.

This script depends on python-docutils.
"""

import sys
import os.path
import os
import shutil
import docutils.core

# stylesheet path effectively embedded in the resulting html pages
STYLESHEET_LOC = "sloecode/public/css/help/sloecode_rst.css"

USAGE = """
Usage:
    make_help.py source_dir dest_dir

source_dir will be scanned recursively for files with a .rst or .png file
extension. These files will be copied or compiled into dest_dir, according to
the file type. Directories under source_dir will be preserved in dest_dir.
dest_dir will be created if it does not exist.
"""

def process_file(source, target, time):
    """Process a source file into a destination one. Assumes that all path
    components of `target` have already been created, and we have permission
    to (over)write files in the destination directory.

    Destination files will have their modification and access time set to `time`.
    """

    if target[-5:].lower() == '.html':
        docutils.core.publish_file(source_path=source,
                                   destination_path=target,
                                   settings_overrides={
                                        "stylesheet_path": STYLESHEET_LOC,
                                        "embed_stylesheet": True,
                                       },
                                   writer_name='html')
    elif target[-4:].lower() == '.png':
        shutil.copy2(source, target)
    else:
        sys.stderr.write('Error: Unknown file: %s' % (target))

    os.utime(target, (time, time))


if __name__ == '__main__':
    if len(sys.argv) != 3:
        sys.stderr.write(USAGE)
        exit(1)

    # src and dst paths must end with trailing slash:
    source_dir = sys.argv[1]
    if source_dir[-1] != '/':
        source_dir += '/'
    dest_dir = sys.argv[2]
    if dest_dir[-1] != '/':
        dest_dir += '/'

    # make sure source_dir exists:
    if not os.path.exists(source_dir):
        sys.stderr.write(
            'ERROR: Source directory "%s" missing.\n' % (source_dir))
        exit(1)

    # Store all source files as keys in a dictionary:
    source_files = {}

    chop_amount = len(source_dir)
    for root, dirs, files in os.walk(source_dir):
        for fname in files:
            if fname[-4:].lower() == '.png' or fname[-4:].lower() == '.rst':
                path = os.path.join(root, fname)
                source_files[path[chop_amount:]] = int(os.path.getmtime(path))

    #print 'source files found:', source_files
    dest_files = {}
    if not os.path.exists(dest_dir):
        os.makedirs(dest_dir, 0755)
    else:
        chop_amount = len(dest_dir)
        for root, dirs, files in os.walk(dest_dir):
            for fname in files:
                if fname[-4:].lower() == '.png' or fname[-5:].lower() == '.html':
                    path = os.path.join(root, fname)
                    dest_files[path[chop_amount:]] = int(os.path.getmtime(path))


    # do the processing:
    for src_file, mtime in source_files.items():
        dst_file = src_file

        # mangle filename if we're dealing with a .rst source file:
        if src_file[-4:].lower() == '.rst':
            dst_file = dst_file[:-4] + '.html'
        # work out if we need to process this entry:
        if dst_file not in dest_files or dest_files[dst_file] != mtime:
            # yes: process this file:
            target_path = os.path.join(dest_dir, dst_file)
            target_dir = os.path.dirname(target_path)

            if not os.path.exists(target_dir):
                os.makedirs(target_dir, 0755)

            print 'processing file: %s' % (target_path)
            process_file(os.path.join(source_dir, src_file),
                         target_path, mtime)