#!/usr/bin/python2 # Copyright (C) 2013-2016 Codethink Limited # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 2 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ''' A Morph configuration extension for removing gplv3 chunks from a system Using a hard-coded list of chunks, it will read the system's /baserock metadata to find the files created by that chunk, then remove them. ''' import os import re import subprocess import sys import writeexts import imp scriptslib = imp.load_source('scriptslib', 'scripts/scriptslib.py') class StripGPLv3ConfigureExtension(writeexts.Extension): gplv3_chunks = [ ['autoconf', ''], ['autoconf-tarball', ''], ['automake', ''], ['bash', ''], ['binutils', ''], ['bison', ''], ['ccache', ''], ['cmake', ''], ['flex', ''], ['gawk', ''], ['gcc', r'^.*lib.*\.so(\.\d+)*$'], ['gdbm', ''], ['gettext-tarball', ''], ['gperf', ''], ['groff', ''], ['libtool', r'^.*lib.*\.so(\.\d+)*$'], ['libtool-tarball', r'^.*lib.*\.so(\.\d+)*$'], ['m4', ''], ['make', ''], ['nano', ''], ['patch', ''], ['rsync', ''], ['texinfo-tarball', ''], ] def process_args(self, args): target_root = args[0] meta_dir = os.path.join(target_root, 'baserock') metadata = scriptslib.meta_load_from_dir(meta_dir) for chunk in self.gplv3_chunks: for meta in metadata.get_name(chunk[0]): self.remove_chunk( target_root, reversed(meta['contents']), chunk[1]) def remove_chunk(self, target_root, chunk_contents, pattern): updated_contents = [] for content_entry in chunk_contents: pat = re.compile(pattern) if len(pattern) == 0 or not pat.match(content_entry): self.remove_content_entry(target_root, content_entry) else: updated_contents.append(content_entry) def remove_content_entry(self, target_root, content_entry): entry_path = os.path.join(target_root, './' + content_entry) if not entry_path.startswith(target_root): raise writeexts.ExtensionError( '%s is not in %s' % (entry_path, target_root)) if os.path.exists(entry_path): if os.path.islink(entry_path): os.unlink(entry_path) elif os.path.isfile(entry_path): os.remove(entry_path) elif os.path.isdir(entry_path): if not os.listdir(entry_path): os.rmdir(entry_path) else: raise writeexts.ExtensionError( '%s is not a link, file or directory' % entry_path) StripGPLv3ConfigureExtension().run(sys.argv[1:])