aboutsummaryrefslogtreecommitdiffstats
path: root/config-builder.py
blob: 7b388b6f5ac36f82e1b182043468e5620f8c1767 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# This simple script can be used to create package list 
# Copyright (c) 2011 Fabian Affolter <fabian@affolter-engineering.ch>
#
# 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 <http://www.gnu.org/licenses/>.
#

import os
import sys
import urllib2
import unicodedata
import codecs
from optparse import OptionParser
from BeautifulSoup import BeautifulSoup

def get_pkg(url):
    """Retrieve the data from the website."""
    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: <http://bugs.alpinelinux.org/>")

    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()