root/trunk/publishing/scripts/publish_toolbox.py @ 443

Revision 443, 22.7 KB (checked in by atorf, 5 years ago)

Update python publishing script to support windows (new command line to start matlab properly)

Line 
1
2__author__ = "Alexander Behrens (behrens@lfb.rwth-aachen.de)"
3__version__ = "$Revision: 0.1 $"
4__date__ = "$Date: 2008/10/01$"
5__copyright__ = "Copyright (c) 2008 Alexander Behrens"
6
7
8import os
9import sys
10import glob
11import shutil
12
13def publish_toolbox_mfiles(wg_publish_dir, input_dir, output_dir):
14    # start wg_publish_dir to create the html files from the toolbox mfiles
15    print
16    print "--------------------------------------------------------------"
17    print "Start MATLAB and publish_mfiles (wg_publish) to create html files..."
18    #message = [];
19    #message.append('matlab -nosplash -r')
20    #message.append(' "publish_mfiles(')
21    #message.append("'")
22    #message.append(wg_publish_dir)
23    #message.append("', '")
24    #message.append(input_dir)
25    #message.append("', '")
26    #message.append(output_dir)
27    #message.append("'")
28    #message.append('); exit;"')
29    #message = ''.join(message)
30    #print message
31    #os.system(message)
32    os.system(''.join(('matlab -automation -wait -r "cd(\'', sys.path[0], '\'); publish_mfiles(); exit;"')))
33
34
35def remove_styles(output_dir, depth, files):
36    # usage:
37    # python remove_sytles.py [output_dir] [html files (*.html)]
38    #
39    print
40    print "--------------------------------------------------------------"
41    print "Remove styles sheets from the html files..." 
42
43
44    # for each html-file do...
45    for i in files:
46
47        # set output file name
48        (filepath, filename) = os.path.split(i)
49        output_name = os.path.join(output_dir, filename)
50       
51        file1 = file(i,"r")
52        file2 = file(output_name,"w")
53       
54        print "Processing file: %s ..." % filename
55       
56       
57        # read original file
58        org_lines = file1.read().split("\n")
59        new_lines = []
60       
61       
62        # flags
63        key = True
64        style_start = False
65        style_end = False
66       
67       
68        for line in org_lines:
69       
70            s = line.find("<style>")
71           
72            if s != -1:
73                style_start = True
74                # add text before and after "<style>"
75                new_lines.append(line[0:s] + line[s+7:])
76                # add style sheet link
77                if depth == 2:
78                    new_lines.append('<link type="text/css" rel="stylesheet" href="../../style.css">')
79                elif depth == 1:
80                    new_lines.append('<link type="text/css" rel="stylesheet" href="../style.css">')
81                continue
82           
83            s = line.find("</style>")
84            if s != -1:
85                style_end = True
86                # add text before and after "</style>"
87                new_lines.append(line[0:s] + line[s+8:])
88                continue
89           
90            if style_start == True and style_end == False:
91                #skip
92                continue
93           
94            if style_start == False or style_end == True:
95                if line.find("<body>") != -1:
96                    new_lines.append(line)
97                    # add header banner
98                    new_lines.append('<p class="header">RWTH - Mindstorms NXT Toolbox</p>')
99           
100                else:
101                    new_lines.append(line)
102           
103           
104        # write new lines to output file
105        file2.write("\n".join(new_lines))
106       
107        file1.close()
108        file2.close()
109       
110       
111       
112def remove_formats(output_dir, m_files):
113    # usage:
114    # python remove_formats.py output_dir *.m
115    # remove ; and || and %+ formats in the header description of each m-file
116
117    print
118    print "--------------------------------------------------------------"
119    print "Remove formats from the m-files..."
120
121
122    # for each m-file do...
123    for i in m_files:
124       
125        # set output file name
126        (filepath, filename) = os.path.split(i)
127        output_name = os.path.join(output_dir, filename)
128       
129        file1 = file(i,"r")
130        file2 = file(output_name,"w")
131       
132        print "Processing file: %s ..." % filename
133       
134        # read original file
135        org_lines = file1.read().split("\n")
136        new_lines = []
137       
138        # flags
139        key = True
140       
141       
142        # for each line do...
143        for line in org_lines:
144           
145            # check if line is empty
146            if line.strip() != "":
147               
148                # check is ";" is occured
149                if line.strip()[0] == ";":
150                    key = False
151                    #  new_lines.append("") (not necessary since m2html is obsolete)
152                    continue
153                   
154                # remove | characters from %-lines     
155                if line.strip()[0] == '%' and key == True:
156                    line_cut = line.replace("|","")
157                   
158                    # replace %+ with %
159                    if len(line.strip()) > 1:
160                        if line_cut.strip()[1] == '+':
161                            line_cut = '%' + line_cut.strip()[2:]
162                       
163                    new_lines.append(line_cut)
164                    # new_lines.append(line.replace("|",""))
165                   
166                else:
167                    new_lines.append(line)
168                   
169           
170            else:
171                new_lines.append(line)
172       
173       
174        # write new lines to output file       
175        file2.write("\n".join(new_lines))
176           
177        file1.close()
178        file2.close()
179
180
181def createABCList(content_file, output_dir):
182    # create abc list of mfiles
183    print
184    print "--------------------------------------------------------------"
185    print "Create ABC list of m files..."
186   
187   
188    # set input and output files
189    file1 = file(content_file, "r")
190    file2 = file(os.path.join(output_dir, "abc.html"), "w")
191
192    print "Create Alphabetical List of file: %s ..." % content_file
193
194    # read original file
195    org_lines = file1.read().split("\n")
196    new_lines = []
197   
198    # add html header
199    new_lines.append('<html>')
200    new_lines.append('<head>')
201    new_lines.append('<title>Functions - Alphabetical List</title>')
202    new_lines.append('<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">')
203    new_lines.append('<link type="text/css" rel="stylesheet" href="../style.css">')
204    new_lines.append('</head>')
205    new_lines.append('')
206    new_lines.append('<body bgcolor="#FFFFFF" text="#000001">')
207    new_lines.append('<p class="header">RWTH - Mindstorms NXT Toolbox</p>')
208    new_lines.append('<p align="center">')
209    new_lines.append('<h1>Functions - Alphabetical List</h1>')
210    new_lines.append('</p>')
211    new_lines.append('')
212    new_lines.append('<table>')
213   
214    i = 0
215   
216    for line in org_lines:
217        i  = i + 1
218       
219        if (i > 3) & (len(line) > 0):
220       
221            words = line.lstrip("%").strip().split("-",1)
222       
223            if len(words) >= 2:
224                command     = words[0].strip()
225                description = words[1].strip()
226                words = [command, description]
227               
228                #print words
229                newline = '<tr><td><a href="./help/' + command + '.html">' + command + '</a></td><td>' + description + '</td></tr>'
230                new_lines.append(newline)
231   
232   
233   
234    # add html footer
235    new_lines.append('</table>')
236    new_lines.append('')
237    new_lines.append('</body>')
238    new_lines.append('</html>')
239   
240    # writ e new lines to output file   
241    file2.write("\n".join(new_lines))
242   
243    # close files
244    file1.close()
245    file2.close()
246   
247
248def createCategories(content_file, output_dir):
249    # create category list of mfiles
250    print
251    print "--------------------------------------------------------------"
252    print "Create category list of m files..."
253   
254   
255    # set input and output files
256    file1 = file(content_file, "r")
257    file2 = file(os.path.join(output_dir, "categories.html"), "w")
258   
259    print "Create Category List of file: %s ..." % content_file
260   
261   
262    # read original file
263    org_lines = file1.read().split("\n")
264    new_lines = []
265   
266    # add html header
267    new_lines.append('<html>')
268    new_lines.append('<head>')
269    new_lines.append('<title>Functions by Category</title>')
270    new_lines.append('<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">')
271    new_lines.append('<link type="text/css" rel="stylesheet" href="../style.css">')
272    new_lines.append('</head>')
273    new_lines.append('')
274    new_lines.append('<body bgcolor="#FFFFFF" text="#000001">')
275    new_lines.append('<p class="header">RWTH - Mindstorms NXT Toolbox</p>')
276    new_lines.append('<p align="center">')
277    new_lines.append('<h1>Functions by Category</h1>')
278    new_lines.append('</p>')
279    new_lines.append('')
280    new_lines.append('<table>')
281   
282    i = 0
283   
284    COM_Functions = []
285    NXT_Functions = []
286    MAP_Functions = []
287    General_Functions = []
288    General = ["OptimizeToolboxPerformance"]
289   
290    Sensor_Functions = []
291    Sensors = ["SENSOR_1", "SENSOR_2", "SENSOR_3", "SENSOR_4", "OpenLight", "OpenSound", "OpenSwitch", "OpenUltrasonic", "CloseSensor", "GetLight", "GetSound", "GetSwitch", "GetUltrasonic", "USGetSnapshotResults", "USMakeSnapshot", "OpenAccelerator", "GetAccelerator", "OpenInfrared", "GetInfrared", "OpenCompass", "GetCompass", "CalibrateCompass"]
292    Motor_Functions = []
293    Motors = ["GetMotor", "GetMotorSettings", "SetAngleLimit", "SetMotor", "SetPower", "SetRampMode", "SetTurnRatio", "SyncToMotor", "SpeedRegulation", "SendMotorSettings", "WaitForMotor", "StopMotor", "ResetMotorAngle", "SwitchLamp", "MOTOR_A", "MOTOR_B", "MOTOR_C", "MotorRotateAbs", "GetMemoryCount", "SetMemoryCount"]
294    Debug_Functions = []
295    Debug = ["DebugMode"]
296   
297    for line in org_lines:
298        i  = i + 1
299       
300        if (i > 3) & (len(line) > 0):
301       
302            words = line.lstrip("%").strip().split("-",1)
303           
304            if len(words) == 2:
305                command     = words[0].strip()
306                description = words[1].strip()
307                words = [command, description]
308               
309                #print command[0].islower()
310                if command.find("COM") == 0:
311                    COM_Functions.append(words)
312                   
313                elif command.find("NXT") == 0:
314                    NXT_Functions.append(words)
315               
316                elif command.find("MAP") == 0:
317                    MAP_Functions.append(words)
318                   
319                elif command[0].islower() or command in General:
320                    General_Functions.append(words)
321                   
322                elif command in Sensors:
323                    Sensor_Functions.append(words)
324               
325                elif command in Motors:
326                    Motor_Functions.append(words)
327               
328                elif command in Debug:
329                    Debug_Functions.append(words)
330               
331                else:
332                    print command
333                    sys.stderr.write('Error, command can not be categorized\n')
334                    sys.exit()
335   
336    categories = [[COM_Functions, 'NXT Communication'], [Sensor_Functions, 'NXT Sensors'], [Motor_Functions, 'NXT Motors'], [NXT_Functions, 'Direct NXT Functions'], [MAP_Functions, 'NXT Module Map Functions'], [General_Functions, 'General Functions'], [Debug_Functions, 'Debug Functions']]
337   
338   
339    for item in categories:
340        newline = '<tr><td colspan="2"><h2>' + item[1] + '</h2></td></tr>'
341        new_lines.append(newline)
342        for words in item[0]:
343            command     = words[0]
344            description = words[1]
345            newline = '<tr><td><a href="./help/' + command + '.html">' + command + '</a></td><td>' + description + '</td></tr>'
346            new_lines.append(newline)
347        newline = '<tr><td><br><br></td></tr>'
348        new_lines.append(newline)
349        new_lines.append('')
350        new_lines.append('')
351   
352   
353    # add html footer
354    new_lines.append('</table>')
355    new_lines.append('')
356    new_lines.append('</body>')
357    new_lines.append('</html>')
358   
359    # write new lines to output file   
360    file2.write("\n".join(new_lines))
361   
362    # close files
363    file1.close()
364    file2.close()
365   
366   
367
368
369def publish_m2html_files(output_dir, input_dir, m_files):
370    # create html from m-files in /doc/m2html/ --> doc/functions/ (e.g. Functions Overview etc.)
371    print
372    print "--------------------------------------------------------------"
373    print "Create html files from /doc/m2html/*.m files..."
374    message = [];
375    message.append('matlab -nosplash -r')
376    message.append(' "publish_mfiles2html(')
377    message.append("'")
378    message.append(output_dir)
379    message.append("', '")
380    message.append(input_dir)
381    message.append("'")
382    for file in m_files:
383        message.append(", '")
384        message.append(file)
385        message.append("'")
386    message.append('); exit;"')
387    message = ''.join(message)
388    print message
389    os.system(message)
390    #os.sytem('matlab -nosplash -r "publish_mfile2html(%s, %s); exit;"' % test)
391   
392   
393
394# # #############################
395# remove style sheets from the html files and add header banner
396#echo
397#echo "--------------------------------------------------------------"
398#echo "Remove styles sheets from the html files..."
399#mkdir $output_dir/doc/programming
400#python remove_styles2.py $output_dir/doc/programming $doc_dir/m2html/out/*.html#
401#
402#rm -Rf $doc_dir/m2html/out
403
404
405    # ##############################
406
407def createExamples(output_dir, m_files):
408    # create abc list of mfiles
409    print
410    print "--------------------------------------------------------------"
411    print "Create Examples list of demo files..."
412   
413   
414   
415    file2 = file(os.path.join(output_dir, "examples.html"), "w")
416   
417    new_lines = []
418   
419    # add html header
420    new_lines.append('<html>')
421    new_lines.append('<head>')
422    new_lines.append('<title>Examples</title>')
423    new_lines.append('<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">')
424    new_lines.append('<link type="text/css" rel="stylesheet" href="../style.css">')
425    new_lines.append('</head>')
426    new_lines.append('')
427    new_lines.append('<body bgcolor="#FFFFFF" text="#000001">')
428    new_lines.append('<p class="header">RWTH - Mindstorms NXT Toolbox</p>')
429    new_lines.append('<p align="center">')
430    new_lines.append('<h1>Examples</h1>')
431    new_lines.append('</p>')
432    new_lines.append('')
433    new_lines.append('<ul>')
434   
435   
436    for input_name in m_files:
437        name = os.path.basename(input_name)
438       
439        if name.count('GUI') < 1:
440            file1 = file(input_name, "r")
441            org_line = file1.readline()
442            org_line = file1.readline()
443           
444            new_lines.append('<li>' + '<a href="'+ name.rstrip('m').rstrip('.') + '.html' + '">' + name + ' : ' + org_line.lstrip('%') + '</a>' + '</li>')
445            new_lines.append('</br>')
446           
447            # close file
448            file1.close()
449   
450    new_lines.append('</ul>')
451   
452    new_lines.append('')
453    new_lines.append('</body>')
454    new_lines.append('</html>')
455   
456    # write new lines to output file   
457    file2.write("\n".join(new_lines))
458   
459    # close file
460    file2.close()
461   
462   
463def createHelpIndex(output_dir, html_files):
464    # create help index of html files
465    print
466    print "--------------------------------------------------------------"
467    print "Create Help Index of html files..."
468   
469   
470    # output file
471    file2 = file(os.path.join(output_dir, "helpindex.xml"), "w")
472   
473    new_lines = []
474   
475    # add xml header
476    new_lines.append("<?xml version='1.0' encoding='ISO-8859-1'  ?>")
477    new_lines.append('')
478    new_lines.append('')
479    new_lines.append('<index version="1.0">')
480   
481   
482    for input_name in html_files:
483        name = os.path.basename(input_name)
484       
485        new_lines.append('<indexitem target="./functions/help/' + name + '">'  + name.rstrip("html").rstrip(".") +  '</indexitem>')
486   
487   
488    new_lines.append('</index>')
489   
490    # write new lines to output file
491    file2.write("\n".join(new_lines))
492   
493    # close file
494    file2.close()
495
496def copyRecursive(input_dir, output_dir):
497    #
498    # get list
499    list = os.listdir(input_dir)
500    print list
501    for element in list:
502        # if element does not contain a '.' at the beginning
503        if element[0] != '.':
504       
505            if os.path.isdir(os.path.join(input_dir, element)):
506                try:
507                    os.mkdir(os.path.join(output_dir, element))
508                    print "create directory: %s" % element
509                except OSError:
510                    print "can't create directory: %s , exist already?" % element
511                copyRecursive(os.path.join(input_dir, element), os.path.join(output_dir, element))                   
512            elif os.path.isfile(os.path.join(input_dir, element)):
513                shutil.copy(os.path.join(input_dir, element), output_dir)
514            # mache
515       
516   
517# directory structure
518
519# /output
520#    /doc
521#        /functions
522#            /help
523
524if __name__ == "__main__":
525   
526    # set parameter / paths
527   
528    mfiles_dir     = os.path.join(os.getcwd(), "..", "..", "mfiles")
529    doc_dir        = os.path.join(os.getcwd(), "..", "..", "doc")
530    doc_html_dir   = os.path.join(os.getcwd(), "..", "..", "doc", "html")
531    doc_m2html_dir = os.path.join(os.getcwd(), "..", "..", "doc", "m2html")
532    demos_dir      = os.path.join(os.getcwd(), "..", "..", "demos")
533    tools_dir      = os.path.join(os.getcwd(), "..", "..", "tools")
534    root_dir       = os.path.join(os.getcwd(), "..", "..", "root")
535   
536    wg_publish_dir = os.path.join(os.getcwd(), "..", "tools", "3rdParties", "wg_publish")
537   
538   
539    output_dir                 = os.path.join(os.getcwd(), "..", "output")
540   
541    out_private_dir            = os.path.join(output_dir, "private")
542    out_demos_dir              = os.path.join(output_dir, "demos")
543   
544    out_doc_dir                = os.path.join(output_dir, "doc")
545    out_doc_examples_dir       = os.path.join(output_dir, "doc", "examples")   
546    out_doc_programming_dir    = os.path.join(output_dir, "doc", "programming")
547    out_doc_tools_dir          = os.path.join(output_dir, "doc", "tools")
548    out_doc_functions_dir      = os.path.join(output_dir, "doc", "functions")
549    out_doc_functions_help_dir = os.path.join(output_dir, "doc", "functions", "help")
550   
551    out_tmp                    = os.path.join(output_dir, "tmp")
552    out_tmp_mfileshtml         = os.path.join(output_dir, "tmp", "mfileshtml")
553    out_tmp_m2html             = os.path.join(output_dir, "tmp", "m2html")
554    out_tmp_examples           = os.path.join(output_dir, "tmp", "examples")
555    out_tmp_tools              = os.path.join(output_dir, "tmp", "tools")
556   
557   
558    dir_list = [out_private_dir, out_demos_dir, 
559                out_doc_dir, out_doc_examples_dir, out_doc_programming_dir, out_doc_tools_dir,
560                out_doc_functions_dir, out_doc_functions_help_dir,
561                out_tmp, out_tmp_mfileshtml, out_tmp_m2html, out_tmp_examples, out_tmp_tools
562                ]
563   
564   
565    # create directories
566    for directory in dir_list:
567        try:
568            os.mkdir(directory)
569            print "create directory: %s" % directory
570        except OSError:
571            print "can't create directory: %s , exist already?" % directory
572   
573   
574   
575   
576    # copy doc/html dircetory (recursive)
577    copyRecursive(doc_html_dir, out_doc_dir)
578   
579   
580   
581    # copy doc directory
582    files = glob.glob(os.path.join(doc_dir, "*.*"))
583    for in_file in files:
584        shutil.copy(in_file, out_doc_dir)
585   
586   
587   
588    # copy root directory
589    files = glob.glob(os.path.join(root_dir, "*"))
590    for in_file in files:
591        shutil.copy(in_file, output_dir)
592   
593   
594   
595    # publish toolbox mfiles
596    # TODO: Funktionsübergabe nach MATLAB funktioniert nicht wirklich
597    publish_toolbox_mfiles(wg_publish_dir, mfiles_dir, out_tmp_mfileshtml)
598   
599   
600   
601    # remove_styles of html files of m-files
602    files = glob.glob(os.path.join(out_tmp_mfileshtml, "*.html"))
603    remove_styles(out_doc_functions_help_dir, 2, files)
604   
605   
606   
607    # remove_formats of m-files (also copy)
608    files1 = glob.glob(os.path.join(mfiles_dir, "*.m*"))
609    files2 = glob.glob(os.path.join(mfiles_dir, "private", "*.m*"))
610    remove_formats(output_dir, files1)
611    remove_formats(out_private_dir, files2)
612   
613   
614   
615    # create ABC list & create Category List
616    in_file = os.path.join(mfiles_dir, "Contents.m")
617    createABCList(in_file, out_doc_functions_dir)
618    createCategories(in_file, out_doc_functions_dir)   
619   
620   
621   
622    # create Help Index files
623    files = glob.glob(os.path.join(out_doc_functions_help_dir, "*.html"))
624    createHelpIndex(out_doc_dir, files)
625   
626   
627   
628    # publish m2html files
629    files = glob.glob(os.path.join(doc_m2html_dir, "*.m"))
630    filenames = []
631    for in_file in files:
632        (filepath, filename) = os.path.split(in_file)
633        filenames.append(filename) 
634    #publish_m2html_files(out_tmp_m2html, doc_m2html_dir, filenames)
635   
636    # remove styles
637    files = glob.glob(os.path.join(out_tmp_m2html, "*.html"))
638    remove_styles(out_doc_programming_dir, 1, files)
639   
640   
641   
642    # publish demos
643    files = glob.glob(os.path.join(demos_dir, "*.m"))
644    filenames = []
645    for in_file in files:
646        (filepath, filename) = os.path.split(in_file)
647        filenames.append(filename) 
648    #publish_m2html_files(out_tmp_examples, demos_dir, filenames)
649   
650    # remove styles
651    files = glob.glob(os.path.join(out_tmp_examples, "*.html"))
652    remove_styles(out_doc_examples_dir, 1, files)
653   
654    # create example index
655    files = glob.glob(os.path.join(demos_dir, "*.m"))
656    createExamples(out_doc_examples_dir, files)
657   
658    # copy demos directory
659    files = glob.glob(os.path.join(demos_dir, "*.m"))
660    for in_file in files:
661        shutil.copy(in_file, out_demos_dir)
662   
663   
664   
665    # publish tools
666    files = glob.glob(os.path.join(tools_dir, "*.m"))
667    filenames = []
668    for in_file in files:
669        (filepath, filename) = os.path.split(in_file)
670        filenames.append(filename) 
671    #publish_m2html_files(out_tmp_tools, tools_dir, filenames)
672   
673    # remove styles
674    files = glob.glob(os.path.join(out_tmp_tools, "*.html"))
675    remove_styles(out_doc_tools_dir, 1, files)
676   
677
678   
Note: See TracBrowser for help on using the browser.