#!/usr/bin/env python # # config-builder is able to create a package list from a mediawiki # Copyright (c) 2011 Fabian Affolter # # All rights reserved. # # 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; either version 2 of the License, or # (at your option) any later version. # # 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, see . # import os import sys import urllib2 import unicodedata import codecs from optparse import OptionParser from BeautifulSoup import BeautifulSoup def get_pkg(url): """Retrieve the package names from a mediawiki page.""" page = urllib2.urlopen(url) soup = BeautifulSoup(page) completeList = [] outputList = [] for node in soup.findAll(attrs={'class':'wikitable'}): rows = node.findAll('tr') nameList = [] for tr in rows: cols = tr.findAll('td') if len(cols) == 0: continue toolDetails = [] for td in cols: entries = unicodedata.normalize('NFKD', ''.join(td.find(text=True))).encode('ascii', 'replace') toolDetails.append(entries.strip()) # Prints list with the tool's details #print toolDetails nameList.append(toolDetails[0]) outputList.append(toolDetails[0]) completeList.append(nameList) return outputList def get_git(volume): """Retrieve all packages from the Alpine Git repository.""" if volume == 'everything': urls = ['http://git.alpinelinux.org/cgit/aports.git/tree/main', 'http://git.alpinelinux.org/cgit/aports.git/tree/testing'] else: urls = ['http://git.alpinelinux.org/cgit/aports.git/tree/main'] outputList = [] for url in urls: page = urllib2.urlopen(url) soup = BeautifulSoup(page) for node in soup.findAll(attrs={'class':'ls-dir'}): outputList.append(node.contents[0]) return outputList def create_files(names, flavor): """Creates the configuration files.""" data = """ALPINE_NAME\t\t:= alpine-%s\nKERNEL_FLAVOR\t:= grsec\nMODLOOP_EXTRA\t:= """ % (flavor) if names != None: filename = 'alpine-' + flavor + '.packages' else: filename = 'alpine-' + flavor + '.conf.mk' if os.path.exists(os.getcwd() + '/' + filename): try: result = raw_input("\nWarning: %s already exists. " "Overwrite? [N/y]: " % (filename)) except NameError: result = input("\nWarning: %s already exists. " "Overwrite? [N/y]: " % (filename)) if result not in ['Y', 'y']: print "Leaving %s unchanged." % (filename) return try: if names != None: pkg_file = open(os.getcwd() + '/' + filename, "w") for entry in names: pkg_file.write(entry + u'\n') print "%s packages added to '%s'." % (len(names), filename) else: open(os.getcwd() + '/' + filename, "w").write(data) print "Configuration written to '%s'." % (filename) except Exception: e = sys.exc_info()[1] print("Error %s occured while trying to write the package list " "file to '%s'.\n" % (e, os.getcwd() + '/' + filename)) raise SystemExit(1) def main(): parser = OptionParser( usage = "usage: %prog [options]", version = "%prog 0.1", description = """This tool can help you to create configurations and package lists to use with the alpine-iso script.""", epilog = "Please report all bug: ") parser.add_option("-f", "--flavor", type = 'choice', action = 'store', dest = 'flavor', choices = ['security', 'rescue', 'main', 'everything', 'mini' ], help = "create a list of all available packages") parser.add_option("-u", "--URL", action = "store", dest = "url", help = "fetch the data from the given URL") parser.add_option("-n", "--Name", action = "store", dest = "name", help = "used as name for the custom ISO image") (options, args) = parser.parse_args() if options.url == None and options.flavor == None and options.name == None: parser.print_help() exit(-1) if options.url == None and options.name !=None: print "'-n, --Name' can only be used with the URL option." exit(-1) if options.flavor != None: if options.flavor == 'main': print """Package list of '%s' is created...""" % (options.flavor) create_files(get_git(options.flavor), options.flavor) create_files(None, options.flavor) return if options.flavor == 'everything': print """Package list of '%s' is created...""" % (options.flavor) create_files(get_git(options.flavor), options.flavor) create_files(None, options.flavor) return else: print """Package list for Alpine %s is created...""" \ % (options.flavor).capitalize() url = "http://wiki.alpinelinux.org/wiki/Alpine_" + options.flavor print "Source: %s" % url create_files(get_pkg(url), options.flavor) create_files(None, options.flavor.lower()) if options.url != None and options.flavor == None: url = options.url if options.name != None: flavor = (options.name).lower() else: flavor = 'custom' print """Package list for Alpine %s is created...""" \ % (flavor.capitalize()) print "Source: %s" % url create_files(get_pkg(url), flavor) create_files(None, flavor) if __name__ == '__main__': main()