aboutsummaryrefslogtreecommitdiff
path: root/make_guides.py
blob: a59138ad5a24169741eddba069b3651b654cee61 (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python3

import argparse, os, shutil
import docutils.core

def basename(fname):
    '''Returns the name of the guide from either its name or its directory'''
    if os.path.isdir(os.path.join('guides',fname)):
        return fname
    elif os.path.isdir(fname):
        return os.path.basename(os.path.abspath(fname))
    else:
        msg = '%s is not a valid directory' % fname
        raise argparse.ArgumentTypeError(msg)

def build_latex(guide):
    '''Builds the LaTeX source for the guide'''
    src_dir = os.path.join('guides',guide)
    tools_dir = 'tools'
    build_dir = os.path.join('build',guide)
    tex_name = guide+'.tex'
    # Check if we need to (re)build
    rebuild = False
    try:
        tex_time = os.stat(os.path.join(build_dir,tex_name)).st_mtime
        # the source has changed?
        for dirpath, dirnames, filenames in os.walk(src_dir):
            for src_file in filenames:
                src_time = os.stat(os.path.join(dirpath,src_file)).st_mtime
                if src_time > tex_time:
                    rebuild = True
                    break
        # a template has changed?
        for dirpath, dirnames, filenames in os.walk(tools_dir):
            for src_file in filenames:
                src_time = os.stat(os.path.join(dirpath,src_file)).st_mtime
                if src_time > tex_time:
                    rebuild = True
                    break
    except (IOError,OSError):
        rebuild = True
    if not rebuild:
        return None
    # Build the LaTeX source
    s_files = []
    for dirpath, dirnames, filenames in os.walk(src_dir):
        s_files += [os.path.join(dirpath,fn) 
                for fn in filenames 
                if fn.endswith(('.txt','.rst'))]
    s_files.sort()
    source = ""
    for fname in s_files:
        fp = open(fname,'rt')
        try:
            source += fp.read()
        except IOError:
            pass
        finally:
            fp.close()
    if not os.path.isdir(build_dir):
        os.mkdir(build_dir)
    fp = open(os.path.join(build_dir,tex_name),'wb')
    fp.write(docutils.core.publish_string(source,
        writer_name='latex',
        settings_overrides={'language_code': 'it',
            'documentoptions': 'a6paper,twoside',
            'stylesheet': 'lmodern',
            'template': os.path.join(tools_dir,'booklet-A6.template.tex'),
            'hyperlink-color': '0'}))
    fp.close()

def build_pdf(guide):
    '''Builds the A6 PDF for the guide'''
    build_dir = os.path.join('build',guide)
    dist_dir = os.path.join('dist','PDF')
    tex_name = guide+'.tex'
    pdf_name = guide+'.pdf'
    # check for prerequisites
    build_latex(guide)
    # Check if we need to (re)build
    tex_time = os.stat(os.path.join(build_dir,tex_name)).st_mtime
    try:
        pdf_time = os.stat(os.path.join(dist_dir,pdf_name)).st_mtime
        if pdf_time > tex_time:
            return None
    except OSError:
        pass
    if not os.path.isdir(dist_dir):
        os.mkdir(dist_dir)
    os.chdir(build_dir)
    try:
        if os.system('pdflatex '+tex_name) == 0:
            if os.system('pdflatex '+tex_name) == 0:
                os.system('pdflatex '+tex_name)
    except OSError:
        return False
    finally:
        os.chdir('../../')
    try:
        shutil.move(os.path.join(build_dir,pdf_name),
                os.path.join(dist_dir,pdf_name))
    except IOError:
        return False

def build_nup_pdf(guide):
    '''Builds the nup-ed PDF for the guide'''
    dist_dir = os.path.join('dist','PDF')
    pdf_name = guide+'.pdf'
    nup_name = guide+'-A6suA4.pdf'
    # check for prerequisites
    build_pdf(guide)
    # Check if we need to (re)build
    pdf_time = os.stat(os.path.join(dist_dir,pdf_name)).st_mtime
    try:
        nup_time = os.stat(os.path.join(dist_dir,nup_name)).st_mtime
        if nup_time > pdf_time:
            return None
    except OSError:
        pass
    # DEBUG
    print('Building nup PDF...')
    if not os.path.isdir(dist_dir):
        os.mkdir(dist_dir)
    fp = open(os.path.join(dist_dir,nup_name),'w')
    fp.close()

def main():
    parser = argparse.ArgumentParser(description='Build a distribution format of a 1800 words article',
            epilog='Prerequisites of required formats are build automatically, but you may want to build the LaTeX source first, edit it by hand and then build the rest.')
    parser.add_argument('guide', nargs='+', type=basename,
            help='Source directory of the article')
    parser.add_argument('-l','--latex',action='store_true',
            help='Generate LaTeX source')
    parser.add_argument('-p','--pdf',action='store_true',
            help='Generate PDF')
    parser.add_argument('-n','--nup-pdf',action='store_true',
            help='Generate nup-ed A4 PDF')
    args = parser.parse_args()

    for guide in args.guide:
        if args.latex:
            build_latex(guide)
        if args.pdf:
            build_pdf(guide)
        if args.nup_pdf:
            build_nup_pdf(guide)


if __name__ == '__main__': main()