File: test_buildexamples.py

package info (click to toggle)
dot2tex 2.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 968 kB
  • ctags: 337
  • sloc: python: 3,057; makefile: 4
file content (188 lines) | stat: -rwxr-xr-x 4,784 bytes parent folder | download | duplicates (3)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""
Script to build the the dot2tex examples.
"""


import re, os,shutil, glob,sys,logging

from os.path import join,basename,splitext,normpath,abspath

# intitalize logging module
log = logging.getLogger("test_graphparser")
console = logging.StreamHandler()
console.setLevel(logging.WARNING)
# set a format which is simpler for console use
formatter = logging.Formatter('%(levelname)-8s %(message)s')
# tell the handler to use this format
console.setFormatter(formatter)
log.addHandler(console)


BASE_DIR = join(abspath(os.path.dirname(__file__)),"")
DEST_DIR = join(BASE_DIR,'exrender/')
EXAMPLES_DIR = normpath(abspath(join(BASE_DIR,"../examples/")))


def copyifnewer(source, dest):
    """Copy source to dest if dest is older than source or doesn't exists"""
    copy = False
    # Check that dest exists. Create dir if necessary
    if os.path.exists(dest):
        # get source's and dest's timpestamps
        source_ts = os.path.getmtime(source)
        dest_ts = os.path.getmtime(dest)
        # compare timestamps
        if source_ts > dest_ts: copy = True
    else:
        copy = True
        dir = os.path.dirname(dest)
        if dir != '' and not os.path.exists(dir):
            os.makedirs(dir)
    # copy source to dest
    if copy:
        shutil.copy2(source, dest)


def runcmd(syscmd):
    #err = os.system(syscmd)
    sres = os.popen(syscmd)
    resdata =  sres.read()
    err = sres.close()
    if err:
        log.warning('Failed to run command:\n%s',syscmd)
        log.debug('Output:\n%s',resdata)
    return err

def meps(filename):
    fn = splitext(filename)[0]
    s = "latex -halt-on-error -interaction nonstopmode %s.tex" % fn
    err = runcmd(s)
    if err: return err
    if sys.platform=='win32':
        s = "dvips -Ppdf -G0 -D600 -E* -o%s.eps %s.dvi" % (fn,fn)
        err = runcmd(s)
        if err: return err
        s = "epstool --bbox --copy --output %s_tmp.eps %s.eps" % (fn,fn)
        err = runcmd(s)
        if err: return err
        try:
            os.remove("%s.eps" % fn)
            os.remove("%s.dvi" % fn)
            os.remove("%s.aux" % fn)
            os.remove("%s.log" % fn)
        #os.remove("%s.pgf" % fn)

        except:
            raise
        os.rename("%s_tmp.eps" % fn,"%s.eps" % fn)
        s = "epstopdf %s.eps" % fn
        err = runcmd(s)
    else:
        s = "dvips %s.dvi" % fn
        err = runcmd(s)
        s = "ps2eps -B -f %s.ps" % fn
        err = runcmd(s)
        if err: return err
        s = "epstopdf %s.eps" % fn
        err = runcmd(s)

    return err

def create_pdf(texfile,use_pdftex=True):
    if not splitext(texfile)[1]:
        fn = basename(texfile)+'.tex'
    else:
        fn = basename(texfile)
    if sys.platform=='win32':
        syscmd = 'texify --pdf --clean %s' % (fn)
    else:
        syscmd = 'pdflatex -halt-on-error -interaction nonstopmode %s' % (fn)
    err = runcmd(syscmd)
    return err


def find_command(data):
    commands = re.findall(r"\$ ((?:.*?) dot2tex (?:.*?))$", data,
    re.MULTILINE|re.VERBOSE)
    if commands:
        return commands[0].strip()
    else:
        return None



def make_img(c, filenm, name):
        filename = os.path.splitext(filenm)[0]
        err = runcmd(c)
        if err:
            return err

        if (c.find('--crop') > -1):
            #os.system('del %s.png %s.jpg' % (name,name))
            err = create_pdf(filename)
            #print "using mppdf"
        else:
            err = meps(filename)

        return err




def build_gallery(filelist):
    #os.chdir(EXAMPLES_DIR)
    entrylist = []
    failedfiles = []
    for file in filelist:
        f = open(file,'r')
        data = f.read()
        f.close()
        entry = {}
        dirname, filename = os.path.split(file)
        os.chdir(dirname)
        name = os.path.splitext(filename)[0]



        command = find_command(data)

        if command:
            c = command.replace("dot2tex.py","dot2tex")
        else:
            c = "dot2tex %s > %s.tex" % (file, name)
        print c

        err = make_img(c,filename,'')
        if err:
            log.warning('Failed to build %s',filename)
            failedfiles.append(filename)
    return failedfiles




if __name__ == "__main__":
    filelist = []
    exdir= EXAMPLES_DIR
    os.chdir(exdir)
    for f in glob.glob("*.dot"):
        if not f.find('dot2tex-fig') > 0:
            src = os.path.join(exdir,f)
            dst = normpath(join(DEST_DIR,basename(src)))
            filelist.append(dst)
            copyifnewer(src,dst)



    failedfiles = build_gallery(filelist)
    if failedfiles:
        log.warning('Failed files: %s',failedfiles);
        sys.exit(1)
    else:
        print "All tests passed!"
        sys.exit(0)

    #filelist=['distances.dot','tikzshapes.dot']