userland-mangler revision 181
181N/A#!/usr/bin/python2.6
181N/A#
181N/A# CDDL HEADER START
181N/A#
181N/A# The contents of this file are subject to the terms of the
181N/A# Common Development and Distribution License (the "License").
181N/A# You may not use this file except in compliance with the License.
181N/A#
181N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
181N/A# or http://www.opensolaris.org/os/licensing.
181N/A# See the License for the specific language governing permissions
181N/A# and limitations under the License.
181N/A#
181N/A# When distributing Covered Code, include this CDDL HEADER in each
181N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
181N/A# If applicable, add the following below this CDDL HEADER, with the
181N/A# fields enclosed by brackets "[]" replaced with your own identifying
181N/A# information: Portions Copyright [yyyy] [name of copyright owner]
181N/A#
181N/A# CDDL HEADER END
181N/A#
181N/A# Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
181N/A#
181N/A#
181N/A# userland-mangler - a file mangling utility
181N/A#
181N/A# A simple program to mangle files to conform to Solaris WOS or Consoldation
181N/A# requirements.
181N/A#
181N/A
181N/Aimport os
181N/Aimport sys
181N/Aimport re
181N/A
181N/Aimport pkg.fmri
181N/Aimport pkg.manifest
181N/Aimport pkg.actions
181N/Aimport pkg.elf as elf
181N/A
181N/Aattribute_table_header = """
181N/A.SH ATTRIBUTES
181N/ASee
181N/A.BR attributes (5)
181N/Afor descriptions of the following attributes:
181N/A.sp
181N/A.TS
181N/Abox;
181N/Acbp-1 | cbp-1
181N/Al | l .
181N/AATTRIBUTE TYPE ATTRIBUTE VALUE """
181N/A
181N/Aattribute_table_availability = """
181N/A=
181N/AAvailability %s"""
181N/A
181N/Aattribute_table_stability = """
181N/A=
181N/AStability %s"""
181N/A
181N/Aattribute_table_footer = """
181N/A.TE
181N/A.PP
181N/A"""
181N/Adef write_attributes_section(ofp, availability, stability):
181N/A # is there anything to do?
181N/A if availability is None and stability is None:
181N/A return
181N/A
181N/A # append the ATTRIBUTES section
181N/A ofp.write(attribute_table_header)
181N/A if availability is not None:
181N/A ofp.write(attribute_table_availability % availability)
181N/A if stability is not None:
181N/A ofp.write(attribute_table_stability % stability.capitalize())
181N/A ofp.write(attribute_table_footer)
181N/A
181N/A
181N/Anotes_header = """
181N/A.SH NOTES
181N/A"""
181N/A
181N/Anotes_community = """
181N/AFurther information about this software can be found on the open source community website at %s.
181N/A"""
181N/Anotes_source = """
181N/AThis software was built from source available at http://opensolaris.org/. The original community source was downloaded from %s
181N/A"""
181N/A
181N/Adef write_notes_section(ofp, header_seen, community, source):
181N/A # is there anything to do?
181N/A if community is None and source is None:
181N/A return
181N/A
181N/A # append the NOTES section
181N/A if header_seen == False:
181N/A ofp.write(notes_header)
181N/A if source is not None:
181N/A ofp.write(notes_source % source)
181N/A if community is not None:
181N/A ofp.write(notes_community % community)
181N/A
181N/A
181N/Asection_re = re.compile('\.SH "?([^"]+).*$', re.IGNORECASE)
181N/A#
181N/A# mangler.man.stability = (mangler.man.stability)
181N/A# mangler.man.availability = (pkg.fmri)
181N/A# mangler.man.source_url = (pkg.source_url)
181N/A# mangler.man.upstream_url = (pkg.upstream_url)
181N/A#
181N/Adef mangle_manpage(manifest, action, src, dest):
181N/A # manpages must have a taxonomy defined
181N/A stability = action.attrs.pop('mangler.man.stability', None)
181N/A if stability is None:
181N/A sys.stderr.write("ERROR: manpage action missing mangler.man.stability: %s" % action)
181N/A sys.exit(1)
181N/A
181N/A attributes_written = False
181N/A notes_seen = False
181N/A
181N/A if 'pkg.fmri' in manifest.attributes:
181N/A fmri = pkg.fmri.PkgFmri(manifest.attributes['pkg.fmri'])
181N/A availability = fmri.pkg_name
181N/A
181N/A if 'info.upstream_url' in manifest.attributes:
181N/A community = manifest.attributes['info.upstream_url']
181N/A
181N/A if 'info.source_url' in manifest.attributes:
181N/A source = manifest.attributes['info.source_url']
181N/A
181N/A # create a directory to write to
181N/A destdir = os.path.dirname(dest)
181N/A if not os.path.exists(destdir):
181N/A os.makedirs(destdir)
181N/A
181N/A # read the source document
181N/A ifp = open(src, "r")
181N/A lines = ifp.readlines()
181N/A ifp.close()
181N/A
181N/A # skip reference only pages
181N/A if lines[0].startswith(".so "):
181N/A return
181N/A
181N/A # open a destination
181N/A ofp = open(dest, "w+")
181N/A
181N/A # tell man that we want tables (and eqn)
181N/A ofp.write("'\\\" te\n")
181N/A
181N/A # write the orginal data
181N/A for line in lines:
181N/A match = section_re.match(line)
181N/A if match is not None:
181N/A section = match.group(1)
181N/A if section in ['SEE ALSO', 'NOTES']:
181N/A if attributes_written == False:
181N/A write_attributes_section(ofp,
181N/A availability,
181N/A stability)
181N/A attributes_written = True
181N/A if section == 'NOTES':
181N/A notes_seen = True
181N/A ofp.write(line)
181N/A
181N/A if attributes_written == False:
181N/A write_attributes_section(ofp, availability, stability)
181N/A
181N/A write_notes_section(ofp, notes_seen, community, source)
181N/A
181N/A ofp.close()
181N/A
181N/A
181N/A#
181N/A# mangler.elf.strip = (true|false)
181N/A#
181N/Adef mangle_elf(manifest, action, src, dest):
181N/A pass
181N/A
181N/A#
181N/A# mangler.script.file-magic =
181N/A#
181N/Adef mangle_script(manifest, action, src, dest):
181N/A pass
181N/A
181N/Adef mangle_path(manifest, action, src, dest):
181N/A if 'facet.doc.man' in action.attrs:
181N/A mangle_manpage(manifest, action, src, dest)
181N/A elif 'mode' in action.attrs and int(action.attrs['mode'], 8) & 0111 != 0:
181N/A if elf.is_elf_object(src):
181N/A mangle_elf(manifest, action, src, dest)
181N/A else:
181N/A mangle_script(manifest, action, src, dest)
181N/A
181N/A#
181N/A# mangler.bypass = (true|false)
181N/A#
181N/Adef mangle_paths(manifest, search_paths, destination):
181N/A for action in manifest.gen_actions_by_type("file"):
181N/A bypass = action.attrs.pop('mangler.bypass', 'false').lower()
181N/A if bypass == 'true':
181N/A continue
181N/A
181N/A path = None
181N/A if 'path' in action.attrs:
181N/A path = action.attrs['path']
181N/A if action.hash and action.hash != 'NOHASH':
181N/A path = action.hash
181N/A if not path:
181N/A continue
181N/A
181N/A dest = os.path.join(destination, path)
181N/A for directory in search_paths:
181N/A if directory != destination:
181N/A src = os.path.join(directory, path)
181N/A if os.path.exists(src):
181N/A mangle_path(manifest, action, src, dest)
181N/A break
181N/A
181N/Adef load_manifest(manifest_file):
181N/A manifest = pkg.manifest.Manifest()
181N/A manifest.set_content(pathname=manifest_file)
181N/A
181N/A return manifest
181N/A
181N/Adef usage():
181N/A print "Usage: %s [-m|--manifest (file)] [-d|--search-directory (dir)] [-D|--destination (dir)] " % (sys.argv[0].split('/')[-1])
181N/A sys.exit(1)
181N/A
181N/Adef main():
181N/A import getopt
181N/A
181N/A # FLUSH STDOUT
181N/A sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
181N/A
181N/A search_paths = []
181N/A destination = None
181N/A manifests = []
181N/A
181N/A try:
181N/A opts, args = getopt.getopt(sys.argv[1:], "D:d:m:",
181N/A ["destination=", "search-directory=", "manifest="])
181N/A except getopt.GetoptError, err:
181N/A print str(err)
181N/A usage()
181N/A
181N/A for opt, arg in opts:
181N/A if opt in [ "-D", "--destination" ]:
181N/A destination = arg
181N/A elif opt in [ "-d", "--search-directory" ]:
181N/A search_paths.append(arg)
181N/A elif opt in [ "-m", "--manifest" ]:
181N/A try:
181N/A manifest = load_manifest(arg)
181N/A except IOError, err:
181N/A print "oops, %s: %s" % (arg, str(err))
181N/A usage()
181N/A else:
181N/A manifests.append(manifest)
181N/A else:
181N/A usage()
181N/A
181N/A if destination == None:
181N/A usage()
181N/A
181N/A for manifest in manifests:
181N/A mangle_paths(manifest, search_paths, destination)
181N/A print manifest
181N/A
181N/A sys.exit(0)
181N/A
181N/Aif __name__ == "__main__":
181N/A main()