userland-mangler revision 5680
4134N/A#!/usr/bin/python2.7
4134N/A#
4134N/A# CDDL HEADER START
4134N/A#
4134N/A# The contents of this file are subject to the terms of the
4134N/A# Common Development and Distribution License (the "License").
4134N/A# You may not use this file except in compliance with the License.
4134N/A#
6982N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
6982N/A# or http://www.opensolaris.org/os/licensing.
4134N/A# See the License for the specific language governing permissions
4134N/A# and limitations under the License.
4134N/A#
4134N/A# When distributing Covered Code, include this CDDL HEADER in each
6982N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
6982N/A# If applicable, add the following below this CDDL HEADER, with the
6982N/A# fields enclosed by brackets "[]" replaced with your own identifying
6982N/A# information: Portions Copyright [yyyy] [name of copyright owner]
4134N/A#
4134N/A# CDDL HEADER END
4134N/A#
4134N/A
4134N/A#
4134N/A# Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
5897N/A#
4134N/A#
4134N/A# userland-mangler - a file mangling utility
4134N/A#
4134N/A# A simple program to mangle files to conform to Solaris WOS or Consoldation
4134N/A# requirements.
4134N/A#
4134N/A
4134N/Aimport os
4134N/Aimport sys
4134N/Aimport re
5897N/A
4134N/Aimport pkg.fmri
5897N/Aimport pkg.manifest
5897N/Aimport pkg.actions
4134N/Aimport pkg.elf as elf
4134N/A
4134N/Aattribute_oracle_table_header = """
4134N/A.\\\" Oracle has added the ARC stability level to this manual page"""
4134N/A
4134N/Aattribute_table_header = """
4134N/A.SH ATTRIBUTES
4134N/ASee
4134N/A.BR attributes (7)
4134N/Afor descriptions of the following attributes:
4134N/A.sp
4134N/A.TS
4134N/Abox;
4134N/Acbp-1 | cbp-1
4134N/Al | l .
4134N/AATTRIBUTE TYPE ATTRIBUTE VALUE """
4134N/A
4134N/Aattribute_table_availability = """
4134N/A=
4134N/AAvailability %s"""
4134N/A
4134N/Aattribute_table_stability = """
4134N/A=
4134N/AStability %s"""
4134N/A
4134N/Aattribute_table_footer = """
4134N/A.TE
4134N/A.PP
4134N/A"""
4134N/Adef attributes_section_text(availability, stability, modified_date):
4134N/A result = ''
4134N/A
4134N/A # is there anything to do?
4134N/A if availability is not None or stability is not None:
4134N/A result = attribute_oracle_table_header
4134N/A if modified_date is not None:
4134N/A result += ("\n.\\\" on %s" % modified_date)
4134N/A result += attribute_table_header
4134N/A
4134N/A if availability is not None:
4134N/A result += (attribute_table_availability % availability)
4134N/A if stability is not None:
4134N/A result += (attribute_table_stability % stability.capitalize())
4134N/A result += attribute_table_footer
4134N/A
4134N/A return result
4134N/A
4134N/Anotes_oracle_comment = """
4134N/A.\\\" Oracle has added source availability information to this manual page"""
4134N/A
4134N/Anotes_header = """
4134N/A.SH NOTES
4134N/A"""
4134N/A
4134N/Anotes_community = """
4134N/AFurther information about this software can be found on the open source community website at %s.
4134N/A"""
4134N/Anotes_source = """
4134N/AThis software was built from source available at https://java.net/projects/solaris-userland. The original community source was downloaded from %s
4134N/A"""
4134N/A
4134N/Adef notes_section_text(header_seen, community, source, modified_date):
4134N/A result = ''
4134N/A
4134N/A # is there anything to do?
4134N/A if community is not None or source is not None:
4134N/A if header_seen == False:
4134N/A result += notes_header
4134N/A result += notes_oracle_comment
4134N/A if modified_date is not None:
4134N/A result += ("\n.\\\" on %s" % modified_date)
4134N/A if source is not None:
4134N/A result += (notes_source % source)
4134N/A if community is not None:
4134N/A result += (notes_community % community)
4134N/A
4134N/A return result
4134N/A
4134N/Aso_re = re.compile('^\.so.+$', re.MULTILINE)
4134N/Asection_re = re.compile('\.SH "?([^"]+).*$', re.IGNORECASE)
4134N/ATH_re = re.compile('\.TH\s+(?:"[^"]+"|\S+)\s+(\S+)', re.IGNORECASE)
4134N/A#
4134N/A# mangler.man.stability = (mangler.man.stability)
4134N/A# mangler.man.modified_date = (mangler.man.modified-date)
4134N/A# mangler.man.availability = (pkg.fmri)
4134N/A# mangler.man.source-url = (pkg.source-url)
4134N/A# mangler.man.upstream-url = (pkg.upstream-url)
4134N/A# mangler.man.rewrite-section = ('true'/'false') default 'true'
4134N/A#
4134N/Adef mangle_manpage(manifest, action, text):
5897N/A # manpages must have a taxonomy defined
5897N/A stability = action.attrs.pop('mangler.man.stability', None)
5897N/A if stability is None:
5897N/A sys.stderr.write("ERROR: manpage action missing mangler.man.stability: %s" % action)
5897N/A sys.exit(1)
5897N/A
5897N/A # manpages may have a 'modified date'
5897N/A modified_date = action.attrs.pop('mangler.man.modified-date', None)
5897N/A
5897N/A
5897N/A # Rewrite the section in the .TH line to match the section in which
5897N/A # we're delivering it.
5897N/A rewrite_sect = action.attrs.pop('mangler.man.rewrite-section', 'true')
5897N/A
5897N/A attributes_written = False
5897N/A notes_seen = False
5897N/A
5897N/A if 'pkg.fmri' in manifest.attributes:
4134N/A fmri = pkg.fmri.PkgFmri(manifest.attributes['pkg.fmri'])
4134N/A availability = fmri.pkg_name
4134N/A
4134N/A community = None
4134N/A if 'info.upstream-url' in manifest.attributes:
4134N/A community = manifest.attributes['info.upstream-url']
4134N/A
4134N/A source = None
4134N/A if 'info.source-url' in manifest.attributes:
4134N/A source = manifest.attributes['info.source-url']
4134N/A elif 'info.repository-url' in manifest.attributes:
4134N/A source = manifest.attributes['info.repository-url']
4134N/A
4134N/A # skip reference only pages
4134N/A if so_re.match(text) is not None:
4134N/A return text
4134N/A
4134N/A # tell man that we want tables (and eqn)
4134N/A result = "'\\\" te\n"
4134N/A
4134N/A # write the orginal data
4134N/A for line in text.split('\n'):
4134N/A match = section_re.match(line)
4134N/A if match is not None:
4134N/A section = match.group(1)
4134N/A if section in ['SEE ALSO', 'NOTES']:
4134N/A if attributes_written == False:
4134N/A result += attributes_section_text(
4134N/A availability,
4134N/A stability,
4134N/A modified_date)
4134N/A attributes_written = True
4134N/A if section == 'NOTES':
4134N/A notes_seen = True
4134N/A
4134N/A match = TH_re.match(line)
4134N/A if match and rewrite_sect.lower() == "true":
4134N/A # Use the section defined by the filename, rather than
4134N/A # the directory in which it sits.
4134N/A sect = os.path.splitext(action.attrs["path"])[1][1:]
4134N/A line = line[:match.span(1)[0]] + sect + \
4134N/A line[match.span(1)[1]:]
4134N/A
4134N/A result += ("%s\n" % line)
4134N/A
4134N/A if attributes_written == False:
4134N/A result += attributes_section_text(availability, stability,
4134N/A modified_date)
4134N/A
4134N/A result += notes_section_text(notes_seen, community, source,
4134N/A modified_date)
4134N/A
4134N/A return result
4134N/A
4134N/A
4134N/A#
4134N/A# mangler.elf.strip = (true|false)
4134N/A#
4134N/Adef mangle_elf(manifest, action, src, dest):
4134N/A pass
4134N/A
4134N/A#
4134N/A# mangler.script.file-magic =
4134N/A#
4134N/Adef mangle_script(manifest, action, text):
4134N/A return text
4134N/A
4134N/A#
4134N/A# mangler.strip_cddl = false
4134N/A#
4134N/Adef mangle_cddl(manifest, action, text):
4134N/A strip_cddl = action.attrs.pop('mangler.strip_cddl', 'true')
4134N/A if strip_cddl is 'false':
4134N/A return text
4134N/A cddl_re = re.compile('^[^\n]*CDDL HEADER START.+CDDL HEADER END[^\n]*\n',
4134N/A re.MULTILINE|re.DOTALL)
4134N/A return cddl_re.sub('', text)
4134N/A
4134N/Adef mangle_path(manifest, action, src, dest):
4134N/A if elf.is_elf_object(src):
4134N/A mangle_elf(manifest, action, src, dest)
4134N/A else:
4134N/A # a 'text' document (script, man page, config file, ...
ifp = open(src, 'r')
text = ifp.read()
ifp.close()
# remove the CDDL from files
result = mangle_cddl(manifest, action, text)
if 'facet.doc.man' in action.attrs:
result = mangle_manpage(manifest, action, result)
elif 'mode' in action.attrs and int(action.attrs['mode'], 8) & 0111 != 0:
result = mangle_script(manifest, action, result)
if text != result:
destdir = os.path.dirname(dest)
if not os.path.exists(destdir):
os.makedirs(destdir)
with open(dest, 'w') as ofp:
ofp.write(result)
#
# 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
if not os.path.exists(destination):
os.makedirs(destination)
dest = os.path.join(destination, path)
for directory in search_paths:
if directory != destination:
src = os.path.join(directory, path)
if os.path.isfile(src):
mangle_path(manifest, action, src, dest)
break
def mangle_manifest(manifest):
# Check for file content and remove tpno data and license actions if
# there is no content in the package that can be licensed.
manifest_has_file_content = False
for action in manifest.gen_actions_by_type("file"):
manifest_has_file_content = True
break
if not manifest_has_file_content:
# search for and remove 'set name=com.oracle.info.tpno ...'
for action in manifest.gen_actions_by_type("set"):
if (action.attrs["name"] == "com.oracle.info.tpno"):
manifest.actions.remove(action)
for action in manifest.gen_actions_by_type("license"):
manifest.actions.remove(action)
# Check for pkg.obsolete and if found, remove any depend actions.
# Also remove any require dependency on the release/evalauation
# package for renamed packages.
manifest_is_obsolete = False
manifest_is_renamed = False
for action in manifest.gen_actions_by_type("set"):
if (action.attrs["name"] == "pkg.obsolete" and
action.attrs["value"] == "true"):
manifest_is_obsolete = True
if (action.attrs["name"] == "pkg.renamed" and
action.attrs["value"] == "true"):
manifest_is_renamed = True
if manifest_is_obsolete:
for action in manifest.gen_actions_by_type("depend"):
manifest.actions.remove(action)
if manifest_is_renamed:
for action in manifest.gen_actions_by_type("depend"):
if (action.attrs["type"] == "require" and
action.attrs["fmri"] == "release/evaluation"):
manifest.actions.remove(action)
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)
mangle_manifest(manifest)
print manifest
sys.exit(0)
if __name__ == "__main__":
main()