Norm Jacobs
2011-04-13 4158c02ccf09e2f646cf2e9e5599f186ec8c7302
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/python2.6
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
# Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
#
#
# userland-mangler - a file mangling utility
#
#  A simple program to mangle files to conform to Solaris WOS or Consoldation
#  requirements.
#
 
import os
import sys
import re
 
import pkg.fmri
import pkg.manifest
import pkg.actions
import pkg.elf as elf
 
attribute_table_header = """
.SH ATTRIBUTES
See
.BR attributes (5)
for descriptions of the following attributes:
.sp
.TS
box;
cbp-1 | cbp-1
l | l .
ATTRIBUTE TYPE    ATTRIBUTE VALUE """
 
attribute_table_availability = """
=
Availability    %s"""
 
attribute_table_stability = """
=
Stability    %s"""
 
attribute_table_footer = """
.TE 
.PP
"""
def write_attributes_section(ofp, availability, stability):
    # is there anything to do?
    if availability is None and stability is None:
        return
 
    # append the ATTRIBUTES section
    ofp.write(attribute_table_header)
    if availability is not None:
        ofp.write(attribute_table_availability % availability)
    if stability is not None:
        ofp.write(attribute_table_stability % stability.capitalize())
    ofp.write(attribute_table_footer)
 
 
notes_header = """
.SH NOTES
"""
 
notes_community = """
Further information about this software can be found on the open source community website at %s.
"""
notes_source = """
This software was built from source available at http://opensolaris.org/.  The original community source was downloaded from  %s
"""
 
def write_notes_section(ofp, header_seen, community, source):
    # is there anything to do?
    if community is None and source is None:
        return
 
    # append the NOTES section
    if header_seen == False:
        ofp.write(notes_header)
    if source is not None:
        ofp.write(notes_source % source)
    if community is not None:
        ofp.write(notes_community % community)
 
 
section_re = re.compile('\.SH "?([^"]+).*$', re.IGNORECASE)
#
# mangler.man.stability = (mangler.man.stability)
# mangler.man.availability = (pkg.fmri)
# mangler.man.source_url = (pkg.source_url)
# mangler.man.upstream_url = (pkg.upstream_url)
#
def mangle_manpage(manifest, action, src, dest):
    # manpages must have a taxonomy defined
    stability = action.attrs.pop('mangler.man.stability', None)
    if stability is None:
        sys.stderr.write("ERROR: manpage action missing mangler.man.stability: %s" % action)
        sys.exit(1)
 
    attributes_written = False
    notes_seen = False
 
    if 'pkg.fmri' in manifest.attributes:
        fmri = pkg.fmri.PkgFmri(manifest.attributes['pkg.fmri'])
        availability = fmri.pkg_name
 
    if 'info.upstream_url' in manifest.attributes:
        community = manifest.attributes['info.upstream_url']
 
    if 'info.source_url' in manifest.attributes:
        source = manifest.attributes['info.source_url']
 
    # create a directory to write to
    destdir = os.path.dirname(dest)
    if not os.path.exists(destdir):
        os.makedirs(destdir)
 
    # read the source document
    ifp = open(src, "r")
    lines = ifp.readlines()
    ifp.close()
 
    # skip reference only pages
    if lines[0].startswith(".so "):
        return
 
    # open a destination
    ofp = open(dest, "w+")
 
    # tell man that we want tables (and eqn)
    ofp.write("'\\\" te\n")
 
    # write the orginal data
    for line in lines:
        match = section_re.match(line)
        if match is not None:
            section = match.group(1)
            if section in ['SEE ALSO', 'NOTES']:
                if attributes_written == False:
                    write_attributes_section(ofp,
                                 availability,
                                 stability)
                    attributes_written = True
                if section == 'NOTES':
                    notes_seen = True
        ofp.write(line)
 
    if attributes_written == False:
        write_attributes_section(ofp, availability, stability)
 
    write_notes_section(ofp, notes_seen, community, source)
 
    ofp.close()
 
 
#
# mangler.elf.strip = (true|false)
#
def mangle_elf(manifest, action, src, dest):
    pass
 
#
# mangler.script.file-magic =
#
def mangle_script(manifest, action, src, dest):
    pass
 
def mangle_path(manifest, action, src, dest):
    if 'facet.doc.man' in action.attrs:
         mangle_manpage(manifest, action, src, dest)
    elif 'mode' in action.attrs and int(action.attrs['mode'], 8) & 0111 != 0:
        if elf.is_elf_object(src):
             mangle_elf(manifest, action, src, dest)
        else:
             mangle_script(manifest, action, src, dest)
 
#
# mangler.bypass = (true|false)
#
def mangle_paths(manifest, search_paths, destination):
    for action in manifest.gen_actions_by_type("file"):
        bypass = action.attrs.pop('mangler.bypass', 'false').lower()
        if bypass == 'true':
            continue
 
        path = None
        if 'path' in action.attrs:
            path = action.attrs['path']
        if action.hash and action.hash != 'NOHASH':
            path = action.hash
        if not path:
            continue
 
        dest = os.path.join(destination, path)
        for directory in search_paths:
            if directory != destination:
                src = os.path.join(directory, path)
                if os.path.exists(src):
                    mangle_path(manifest, action, src, dest)
                    break
 
def load_manifest(manifest_file):
    manifest = pkg.manifest.Manifest()
    manifest.set_content(pathname=manifest_file)
 
    return manifest
 
def usage():
    print "Usage: %s [-m|--manifest (file)] [-d|--search-directory (dir)] [-D|--destination (dir)] " % (sys.argv[0].split('/')[-1])
    sys.exit(1)
 
def main():
    import getopt
 
    # FLUSH STDOUT 
    sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
 
    search_paths = []
    destination = None
    manifests = []
 
    try:
        opts, args = getopt.getopt(sys.argv[1:], "D:d:m:",
            ["destination=", "search-directory=", "manifest="])
    except getopt.GetoptError, err:
        print str(err)
        usage()
 
    for opt, arg in opts:
        if opt in [ "-D", "--destination" ]:
            destination = arg
        elif opt in [ "-d", "--search-directory" ]:
            search_paths.append(arg)
        elif opt in [ "-m", "--manifest" ]:
            try:
                manifest = load_manifest(arg)
            except IOError, err:
                print "oops, %s: %s" % (arg, str(err))
                usage()
            else:
                manifests.append(manifest)
        else:
            usage()
 
    if destination == None:
        usage()
 
    for manifest in manifests:
        mangle_paths(manifest, search_paths, destination)
        print manifest
 
    sys.exit(0)
 
if __name__ == "__main__":
    main()