5 # The contents of this file are subject to the terms of the
6 # Common Development and Distribution License (the "License").
7 # You may not use this file except in compliance with the License.
9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 # or http://www.opensolaris.org/os/licensing.
11 # See the License for the specific language governing permissions
12 # and limitations under the License.
14 # When distributing Covered Code, include this CDDL HEADER in each
15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 # If applicable, add the following below this CDDL HEADER, with the
17 # fields enclosed by brackets "[]" replaced with your own identifying
18 # information: Portions Copyright [yyyy] [name of copyright owner]
22 # Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
25 # userland-mangler - a file mangling utility
27 # A simple program to mangle files to conform to Solaris WOS or Consoldation
40 attribute_oracle_table_header = """
41 .\\\" Oracle has added the ARC stability level to this manual page"""
43 attribute_table_header = """
47 for descriptions of the following attributes:
53 ATTRIBUTE TYPE ATTRIBUTE VALUE """
55 attribute_table_availability = """
59 attribute_table_stability = """
63 attribute_table_footer = """
67 def attributes_section_text(availability, stability, modified_date):
70 # is there anything to do?
71 if availability is not None or stability is not None:
72 result = attribute_oracle_table_header
73 if modified_date is not None:
74 result += ("\n.\\\" on %s" % modified_date)
75 result += attribute_table_header
77 if availability is not None:
78 result += (attribute_table_availability % availability)
79 if stability is not None:
80 result += (attribute_table_stability % stability.capitalize())
81 result += attribute_table_footer
85 notes_oracle_comment = """
86 .\\\" Oracle has added source availability information to this manual page"""
93 Further information about this software can be found on the open source community website at %s.
96 This software was built from source available at http://opensolaris.org/. The original community source was downloaded from %s
99 def notes_section_text(header_seen, community, source, modified_date):
102 # is there anything to do?
103 if community is not None or source is not None:
104 if header_seen == False:
105 result += notes_header
106 result += notes_oracle_comment
107 if modified_date is not None:
108 result += ("\n.\\\" on %s" % modified_date)
109 if source is not None:
110 result += (notes_source % source)
111 if community is not None:
112 result += (notes_community % community)
116 so_re = re.compile('^\.so.+$', re.MULTILINE)
117 section_re = re.compile('\.SH "?([^"]+).*$', re.IGNORECASE)
119 # mangler.man.stability = (mangler.man.stability)
120 # mangler.man.modified_date = (mangler.man.modified-date)
121 # mangler.man.availability = (pkg.fmri)
122 # mangler.man.source-url = (pkg.source-url)
123 # mangler.man.upstream-url = (pkg.upstream-url)
125 def mangle_manpage(manifest, action, text):
126 # manpages must have a taxonomy defined
127 stability = action.attrs.pop('mangler.man.stability', None)
128 if stability is None:
129 sys.stderr.write("ERROR: manpage action missing mangler.man.stability: %s" % action)
132 # manpages may have a 'modified date'
133 modified_date = action.attrs.pop('mangler.man.modified-date', None)
135 attributes_written = False
138 if 'pkg.fmri' in manifest.attributes:
139 fmri = pkg.fmri.PkgFmri(manifest.attributes['pkg.fmri'])
140 availability = fmri.pkg_name
143 if 'info.upstream-url' in manifest.attributes:
144 community = manifest.attributes['info.upstream-url']
147 if 'info.source-url' in manifest.attributes:
148 source = manifest.attributes['info.source-url']
149 elif 'info.repository-url' in manifest.attributes:
150 source = manifest.attributes['info.repository-url']
152 # skip reference only pages
153 if so_re.match(text) is not None:
156 # tell man that we want tables (and eqn)
157 result = "'\\\" te\n"
159 # write the orginal data
160 for line in text.split('\n'):
161 match = section_re.match(line)
162 if match is not None:
163 section = match.group(1)
164 if section in ['SEE ALSO', 'NOTES']:
165 if attributes_written == False:
166 result += attributes_section_text(
170 attributes_written = True
171 if section == 'NOTES':
173 result += ("%s\n" % line)
175 if attributes_written == False:
176 result += attributes_section_text(availability, stability,
179 result += notes_section_text(notes_seen, community, source,
186 # mangler.elf.strip = (true|false)
188 def mangle_elf(manifest, action, src, dest):
192 # mangler.script.file-magic =
194 def mangle_script(manifest, action, text):
198 # mangler.strip_cddl = false
200 def mangle_cddl(manifest, action, text):
201 strip_cddl = action.attrs.pop('mangler.strip_cddl', 'true')
202 if strip_cddl is 'false':
204 cddl_re = re.compile('^[^\n]*CDDL HEADER START.+CDDL HEADER END[^\n]*$',
205 re.MULTILINE|re.DOTALL)
206 return cddl_re.sub('', text)
208 def mangle_path(manifest, action, src, dest):
209 if elf.is_elf_object(src):
210 mangle_elf(manifest, action, src, dest)
212 # a 'text' document (script, man page, config file, ...
217 # remove the CDDL from files
218 result = mangle_cddl(manifest, action, text)
220 if 'facet.doc.man' in action.attrs:
221 result = mangle_manpage(manifest, action, result)
222 elif 'mode' in action.attrs and int(action.attrs['mode'], 8) & 0111 != 0:
223 result = mangle_script(manifest, action, result)
226 destdir = os.path.dirname(dest)
227 if not os.path.exists(destdir):
229 with open(dest, 'w') as ofp:
233 # mangler.bypass = (true|false)
235 def mangle_paths(manifest, search_paths, destination):
236 for action in manifest.gen_actions_by_type("file"):
237 bypass = action.attrs.pop('mangler.bypass', 'false').lower()
242 if 'path' in action.attrs:
243 path = action.attrs['path']
244 if action.hash and action.hash != 'NOHASH':
249 if not os.path.exists(destination):
250 os.makedirs(destination)
252 dest = os.path.join(destination, path)
253 for directory in search_paths:
254 if directory != destination:
255 src = os.path.join(directory, path)
256 if os.path.isfile(src):
257 mangle_path(manifest, action, src, dest)
260 def load_manifest(manifest_file):
261 manifest = pkg.manifest.Manifest()
262 manifest.set_content(pathname=manifest_file)
267 print "Usage: %s [-m|--manifest (file)] [-d|--search-directory (dir)] [-D|--destination (dir)] " % (sys.argv[0].split('/')[-1])
274 sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
281 opts, args = getopt.getopt(sys.argv[1:], "D:d:m:",
282 ["destination=", "search-directory=", "manifest="])
283 except getopt.GetoptError, err:
287 for opt, arg in opts:
288 if opt in [ "-D", "--destination" ]:
290 elif opt in [ "-d", "--search-directory" ]:
291 search_paths.append(arg)
292 elif opt in [ "-m", "--manifest" ]:
294 manifest = load_manifest(arg)
296 print "oops, %s: %s" % (arg, str(err))
299 manifests.append(manifest)
303 if destination == None:
306 for manifest in manifests:
307 mangle_paths(manifest, search_paths, destination)
312 if __name__ == "__main__":