tools/userland-mangler
changeset 448 e07a264a6e3d
parent 379 c6a17bba1da3
child 452 ef9282f63da3
equal deleted inserted replaced
447:7ca7b95abd2f 448:e07a264a6e3d
    59 
    59 
    60 attribute_table_footer = """
    60 attribute_table_footer = """
    61 .TE 
    61 .TE 
    62 .PP
    62 .PP
    63 """
    63 """
    64 def write_attributes_section(ofp, availability, stability):
    64 def attributes_section_text(availability, stability):
       
    65 	result = ''
       
    66 
    65 	# is there anything to do?
    67 	# is there anything to do?
    66 	if availability is None and stability is None:
    68 	if availability is not None or stability is not None:
    67 		return
    69 		result = attribute_table_header
    68 
    70 
    69 	# append the ATTRIBUTES section
    71 		if availability is not None:
    70 	ofp.write(attribute_table_header)
    72 			result += (attribute_table_availability % availability)
    71 	if availability is not None:
    73 		if stability is not None:
    72 		ofp.write(attribute_table_availability % availability)
    74 			result += (attribute_table_stability % stability.capitalize())
    73 	if stability is not None:
    75 		result += attribute_table_footer
    74 		ofp.write(attribute_table_stability % stability.capitalize())
    76 
    75 	ofp.write(attribute_table_footer)
    77 	return result
    76 
       
    77 
    78 
    78 notes_header = """
    79 notes_header = """
    79 .SH NOTES
    80 .SH NOTES
    80 """
    81 """
    81 
    82 
    84 """
    85 """
    85 notes_source = """
    86 notes_source = """
    86 This software was built from source available at http://opensolaris.org/.  The original community source was downloaded from  %s
    87 This software was built from source available at http://opensolaris.org/.  The original community source was downloaded from  %s
    87 """
    88 """
    88 
    89 
    89 def write_notes_section(ofp, header_seen, community, source):
    90 def notes_section_text(header_seen, community, source):
       
    91 	result = ''
       
    92 
    90 	# is there anything to do?
    93 	# is there anything to do?
    91 	if community is None and source is None:
    94 	if community is not None or source is not None:
    92 		return
    95 		if header_seen == False:
    93 
    96 			result += notes_header
    94 	# append the NOTES section
    97 		if source is not None:
    95 	if header_seen == False:
    98 			result += (notes_source % source)
    96 		ofp.write(notes_header)
    99 		if community is not None:
    97 	if source is not None:
   100 			result += (notes_community % community)
    98 		ofp.write(notes_source % source)
   101 
    99 	if community is not None:
   102 	return result
   100 		ofp.write(notes_community % community)
   103 
   101 
   104 so_re = re.compile('^\.so.+$', re.MULTILINE)
   102 
       
   103 section_re = re.compile('\.SH "?([^"]+).*$', re.IGNORECASE)
   105 section_re = re.compile('\.SH "?([^"]+).*$', re.IGNORECASE)
   104 #
   106 #
   105 # mangler.man.stability = (mangler.man.stability)
   107 # mangler.man.stability = (mangler.man.stability)
   106 # mangler.man.availability = (pkg.fmri)
   108 # mangler.man.availability = (pkg.fmri)
   107 # mangler.man.source_url = (pkg.source_url)
   109 # mangler.man.source_url = (pkg.source_url)
   108 # mangler.man.upstream_url = (pkg.upstream_url)
   110 # mangler.man.upstream_url = (pkg.upstream_url)
   109 #
   111 #
   110 def mangle_manpage(manifest, action, src, dest):
   112 def mangle_manpage(manifest, action, text):
   111 	# manpages must have a taxonomy defined
   113 	# manpages must have a taxonomy defined
   112 	stability = action.attrs.pop('mangler.man.stability', None)
   114 	stability = action.attrs.pop('mangler.man.stability', None)
   113 	if stability is None:
   115 	if stability is None:
   114 		sys.stderr.write("ERROR: manpage action missing mangler.man.stability: %s" % action)
   116 		sys.stderr.write("ERROR: manpage action missing mangler.man.stability: %s" % action)
   115 		sys.exit(1)
   117 		sys.exit(1)
   129 	if 'info.source_url' in manifest.attributes:
   131 	if 'info.source_url' in manifest.attributes:
   130 		source = manifest.attributes['info.source_url']
   132 		source = manifest.attributes['info.source_url']
   131 	elif 'info.repository_url' in manifest.attributes:
   133 	elif 'info.repository_url' in manifest.attributes:
   132 		source = manifest.attributes['info.repository_url']
   134 		source = manifest.attributes['info.repository_url']
   133 
   135 
   134 	# create a directory to write to
       
   135 	destdir = os.path.dirname(dest)
       
   136 	if not os.path.exists(destdir):
       
   137 		os.makedirs(destdir)
       
   138 
       
   139 	# read the source document
       
   140 	ifp = open(src, "r")
       
   141 	lines = ifp.readlines()
       
   142 	ifp.close()
       
   143 
       
   144 	# skip reference only pages
   136 	# skip reference only pages
   145 	if lines[0].startswith(".so "):
   137 	if so_re.match(text) is not None:
   146 		return
   138 		return text
   147 
       
   148 	# open a destination
       
   149 	ofp = open(dest, "w+")
       
   150 
   139 
   151 	# tell man that we want tables (and eqn)
   140 	# tell man that we want tables (and eqn)
   152 	ofp.write("'\\\" te\n")
   141 	result = "'\\\" te\n"
   153 
   142 
   154 	# write the orginal data
   143 	# write the orginal data
   155 	for line in lines:
   144 	for line in text.split('\n'):
   156 		match = section_re.match(line)
   145 		match = section_re.match(line)
   157 		if match is not None:
   146 		if match is not None:
   158 			section = match.group(1)
   147 			section = match.group(1)
   159 			if section in ['SEE ALSO', 'NOTES']:
   148 			if section in ['SEE ALSO', 'NOTES']:
   160 				if attributes_written == False:
   149 				if attributes_written == False:
   161 					write_attributes_section(ofp,
   150 					result += attributes_section_text(
   162 								 availability,
   151 								 availability,
   163 								 stability)
   152 								 stability)
   164 					attributes_written = True
   153 					attributes_written = True
   165 				if section == 'NOTES':
   154 				if section == 'NOTES':
   166 					notes_seen = True
   155 					notes_seen = True
   167 		ofp.write(line)
   156 		result += ("%s\n" % line)
   168 
   157 
   169 	if attributes_written == False:
   158 	if attributes_written == False:
   170 		write_attributes_section(ofp, availability, stability)
   159 		result += attributes_section_text(availability, stability)
   171 
   160 
   172 	write_notes_section(ofp, notes_seen, community, source)
   161 	result += notes_section_text(notes_seen, community, source)
   173 
   162 
   174 	ofp.close()
   163 	return result
   175 
   164 
   176 
   165 
   177 #
   166 #
   178 # mangler.elf.strip = (true|false)
   167 # mangler.elf.strip = (true|false)
   179 #
   168 #
   181 	pass
   170 	pass
   182 
   171 
   183 #
   172 #
   184 # mangler.script.file-magic =
   173 # mangler.script.file-magic =
   185 #
   174 #
   186 def mangle_script(manifest, action, src, dest):
   175 def mangle_script(manifest, action, text):
   187 	pass
   176 	return text
       
   177 
       
   178 #
       
   179 # mangler.strip_cddl = false
       
   180 #
       
   181 def mangle_cddl(manifest, action, text):
       
   182 	strip_cddl = action.attrs.pop('mangler.strip_cddl', 'true')
       
   183 	if strip_cddl is 'false':
       
   184 		return text
       
   185 	cddl_re = re.compile('^[^\n]*CDDL HEADER START.+CDDL HEADER END[^\n]*$',
       
   186 			     re.MULTILINE|re.DOTALL)
       
   187 	return cddl_re.sub('', text)
   188 
   188 
   189 def mangle_path(manifest, action, src, dest):
   189 def mangle_path(manifest, action, src, dest):
   190 	if 'facet.doc.man' in action.attrs:
   190 	if elf.is_elf_object(src):
   191 		 mangle_manpage(manifest, action, src, dest)
   191 		mangle_elf(manifest, action, src, dest)
   192 	elif 'mode' in action.attrs and int(action.attrs['mode'], 8) & 0111 != 0:
   192 	else:
   193 		if elf.is_elf_object(src):
   193 		# a 'text' document (script, man page, config file, ...
   194 			 mangle_elf(manifest, action, src, dest)
   194 		ifp = open(src, 'r')
   195 		else:
   195 		text = ifp.read()
   196 			 mangle_script(manifest, action, src, dest)
   196 		ifp.close()
       
   197 
       
   198 		# remove the CDDL from files
       
   199 		result = mangle_cddl(manifest, action, text)
       
   200 
       
   201 		if 'facet.doc.man' in action.attrs:
       
   202 		 	result = mangle_manpage(manifest, action, result)
       
   203 		elif 'mode' in action.attrs and int(action.attrs['mode'], 8) & 0111 != 0:
       
   204 			result = mangle_script(manifest, action, result)
       
   205 
       
   206 		if text != result:
       
   207 			destdir = os.path.dirname(dest)
       
   208 			if not os.path.exists(destdir):
       
   209 				os.makedirs(destdir)
       
   210 			with open(dest, 'w') as ofp:
       
   211 			    ofp.write(result)
   197 
   212 
   198 #
   213 #
   199 # mangler.bypass = (true|false)
   214 # mangler.bypass = (true|false)
   200 #
   215 #
   201 def mangle_paths(manifest, search_paths, destination):
   216 def mangle_paths(manifest, search_paths, destination):
   209 			path = action.attrs['path']
   224 			path = action.attrs['path']
   210 		if action.hash and action.hash != 'NOHASH':
   225 		if action.hash and action.hash != 'NOHASH':
   211 			path = action.hash
   226 			path = action.hash
   212 		if not path:
   227 		if not path:
   213 			continue
   228 			continue
       
   229 
       
   230 		if not os.path.exists(destination):
       
   231 			os.makedirs(destination)
   214 
   232 
   215 		dest = os.path.join(destination, path)
   233 		dest = os.path.join(destination, path)
   216 		for directory in search_paths:
   234 		for directory in search_paths:
   217 			if directory != destination:
   235 			if directory != destination:
   218 				src = os.path.join(directory, path)
   236 				src = os.path.join(directory, path)