File: stripdocs.py

package info (click to toggle)
freemat 4.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 141,800 kB
  • ctags: 14,082
  • sloc: ansic: 126,788; cpp: 62,046; python: 2,080; perl: 1,255; sh: 1,146; yacc: 1,019; lex: 239; makefile: 100
file content (114 lines) | stat: -rw-r--r-- 3,997 bytes parent folder | download | duplicates (2)
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
# python utility to strip documentation from source files.
# Creates links to the documentation using the "DOCLINK" mark.
import os
import sys
import re
import shutil
import glob

def makepath(path):

    """ creates missing directories for the given path and
        returns a normalized absolute version of the path.

    - if the given path already exists in the filesystem
      the filesystem is not modified.

    - otherwise makepath creates directories along the given path
      using the dirname() of the path. You may append
      a '/' to the path if you want it to be a directory path.

    from holger@trillke.net 2002/03/18
    """

    from os import makedirs
    from os.path import normpath,dirname,exists,abspath
    dpath = normpath(dirname(path))
    if not exists(dpath): makedirs(dpath)
    return normpath(abspath(path))


def matches(line,pattern):
    return re.search(pattern,line,re.DOTALL)

def getfirstgroup(line,pattern):
    return re.search(pattern,line,re.DOTALL).group(1)

def visit(arg, dirname, names):
    for name in names:
        if (re.search('\+octave',dirname)):
            return
        (root, ext) = os.path.splitext(name)
        if (ext == '.cpp'):
            return
            print('Stripping file %s/%s'%(dirname,name))
            iname = '%s/%s'%(dirname,name)
            h = open(iname,'rU')
            oname = '%s/%s.out'%(dirname,name)
            g = open(oname,'w')
            inblock = False
            insig = False
            repcount = 0
            for line in h:
                if (matches(line,r'^//!')):
                    inblock = not inblock
                    if (not inblock): 
                        insig = False
                        line = '//DOCBLOCK %s_%s\n'%(section.lower(),module.lower())
                        repcount = repcount+1
                if (inblock and matches(line,r'^//@@Signature')):
                    insig = True
                if (inblock and matches(line,r'^//@Module\s*(\w*)')):
                    module = getfirstgroup(line,r'^//@Module\s*(\w*)')
                if (inblock and matches(line,r'^//@@Section\s*(\w*)')):
                    section = getfirstgroup(line,r'^//@@Section\s*(\w*)')
                if (insig and inblock):
                    g.write(line)
                if (not inblock):
                    g.write(line)
            g.close()
            h.close()
            if (repcount == 0):
                os.unlink(oname)
            else:
                os.unlink(iname)
                os.rename(oname,iname)
        if (ext == '.m'):
            print('Stripping file %s/%s'%(dirname,name))
            found = False
            h = open('%s/%s'%(dirname,name),'rU')
            for line in h:
                line = line[:-1]
                if matches(line,r'@Module\s*(\w*)'):
                    module = getfirstgroup(line,r'@Module\s*(\w*)')
                    found = True
                if matches(line,r'@@Section\s*(\w*)'):
                    section = getfirstgroup(line,r'@@Section\s*(\w*)')
                    found = True
            h.close()
            if (found): 
                print('File %s --> DOCBLOCK %s_%s'%(name,section,module))
            else:
                print('Warning: %s is not doc-linked'%(name))
            state = 0
            oname = '%s/%s.out'%(dirname,name)
            iname = '%s/%s'%(dirname,name)
            g = open(oname,'w')
            h = open(iname,'rU')
            for line in h:
                if (matches(line,r'^%!')): 
                    state = state + 1
                    if (state == 2):
                        g.write(r'%% DOCBLOCK %s_%s'%(section.lower(),module.lower()))
                        g.write('\n')
                        line = ''
                if (state >=2): g.write(line)
            h.close()
            g.close()
            os.unlink(iname)
            os.rename(oname,iname)


basedir = sys.argv[1]
os.chdir(basedir)
os.path.walk('.', visit, '')