summaryrefslogtreecommitdiffstats
path: root/build_tools/clarch/larch/run/getPackageServer
blob: 170ea737fa0310d3e7e161e22f33730190575108 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# getPackageServer - select a pacman package server
#
# Author: Michael Towers <gradgrind[at]online[dot]de>
#
# This file is part of the larch project.
#
#    larch 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.
#
#    larch 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 larch; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
#----------------------------------------------------------------------------
#2008.08.10

import getopt, sys, os
from subprocess import Popen, PIPE, STDOUT

# Build a list structure with areas, subareas and servers
# [ [area1, [ [subarea1, [server1, server2, ...]],
#             [subarea2, [server1, server2, ...]],
#             ...
#           ]
#   ],
#   [area2, [ [subarea1, [server1, server2, ...]],
#             [subarea2, [server1, server2, ...]],
#             ...
#           ]
#   ],
#   ...
# ]
def parseFile(path):
    areas = []
    header = True
    fh = open(path)
    for line in fh:
        sline = line.strip()
        if header:
            if not sline.startswith('#'):
                header = False
            continue

        if not sline:
            continue

        if sline.startswith('#'):
            if (sline.find('Server =') >= 0):
                # Commented out server
                continue
            if sline.startswith('##'):
                # Real comment
                continue
            # Otherwise assume this is an area name
            if sline.startswith('# -'):
                # New subarea
                servers = []
                subarealist = [sline.split('-', 1)[1].strip(), servers]
                subareas.append(subarealist)
            else:
                # New area (if no subarea, use 'all')
                servers = None
                subareas = []
                arealist = [sline[1:].strip(), subareas]
                areas.append(arealist)
            continue

        if sline.startswith('Server ='):
            server = sline.split('=')[1].strip()
            if (servers == None):
                servers = []
                subarealist = ['all', servers]
                subareas.append(subarealist)
            servers.append(server)

        else:
            report("Unexpected line:\n%s\n" % line)

    fh.close()
    return areas

#Menu using 'dialog'
#+++++++++++++++++++
# geometry:
WIDTH = '76'
HEIGHT = '20'
LINES = '8'
def menu_d(title, text, entries, default=0, header=None):
    # Note that dialog indexing starts at 1, not 0!
    i = 1
    mlist = []
    for m in entries:
        mlist += ["%d" % i, "%-60s" % m]
        i += 1
    if header:
        text += "\n       " + header
    cmd = ['dialog', '--title', title,
            '--default-item', "%d" % (default+1),
            '--menu', text, HEIGHT, WIDTH, LINES] + mlist
    #print cmd
    p = Popen(cmd, stderr=PIPE)
    output = p.communicate()[1]
    rc = p.returncode

    if (rc == 0):
        return int(output)
    elif (rc == 1):
        return 0
    else:
        assert False

#Menu using plain console
#++++++++++++++++++++++++
def menu_c(title, text, entries):
    t = "***** %s *****" % title
    print t
    print "=" * len(t)
    print text
    i = 0
    ilist = "123456789abcdefghijklmnopqrstuvwxyz"
    for m in entries:
        print "  %s - %-60s" % (ilist[i], m)
        i += 1
    print "-----------------------------------------"
    print "  0 - Go back"
    k = raw_input("Select: ")
    if (k == "0"):
        return 0
    n = -1
    if k in "123456789":
        n = (ord(k) - ord("0"))
    elif k in "abcdefghijklmnopqrstuvwxyz":
        n = (ord(k) - ord("a") + 10)
    assert ((n>0) and (n<=i))
    return n

def menu(title, text, entries, default=0, header=None):
    """General menu, uses 'dialog' if available, else plain console.
    """
    if console:
        return menu_c(title, text, entries)
    else:
        return menu_d(title, text, entries, default=0, header=None)

import re
re1 = re.compile(r"/\$repo/os/.*")
def repoDialog(servers):
    serverList = parseFile(servers)
    step = 0
    while True:
        if (step == 0):
            areaList = [a[0] for a in serverList]
            a1 = menu("Choose Arch Repository",
                    "Select a region",
                    areaList)
            if (a1 == 0):
                step = -1
                break
            step = 1

        list2 = serverList[a1-1][1]
        if (len(list2) == 1):
            a = 1
            step = 0
        else:
            subareaList = [a[0] for a in list2]
            a = menu("Choose Arch Repository",
                    "Select a sub-region",
                    subareaList)
            if (a == 0):
                step = 0
                continue

        mirrorList = list2[a-1][1]
        list3 = [re1.sub("", m) for m in mirrorList]
        a = menu("Choose Arch Repository",
                "Select a mirror",
                list3)
        if (a > 0):
            break

    if (step >= 0):
        return mirrorList[a-1]
    else:
        return None


def usage():
    print "Usage:"
    print "  getPackageServer [-h] [-i <repository list file>] [<pacman.conf>]"
    print
    print "Select a server for Arch Linux packages. The given pacman.conf"
    print "file will be updated to use the chosen server first. Other entries"
    print "will not be affected."
    print
    print "          -h       Print this message"
    print "          -i       Supply an alternative list of repositories."
    print "                   The default is /etc/pacman.d/mirrorlist"
    print "          -c       Use plain console for interaction even if"
    print "                   'dialog' is available"
    print
    print "If no pacman.conf file is supplied, the chosen repository URL will"
    print "be output to stdout - mainly for test purposes."

if __name__ == '__main__':
    def report(text):
        print text

    repolist = '/etc/pacman.d/mirrorlist'

    # determine whether 'dialog' utility is available
    p = Popen(["which", "dialog"], stdout=PIPE, stderr=STDOUT)
    output = p.communicate()[1]
    console = (p.returncode != 0)

    try:
        opts, args = getopt.getopt(sys.argv[1:], "i:hc")
    except getopt.GetoptError:
        # print help information and exit:
        usage()
        sys.exit(1)
    for o, a in opts:
        if (o == "-h"):
            usage()
            sys.exit()
        if (o == "-i"):
            repolist = a
        if (o == "-c"):
            console = True

    if (len(args) > 1):
        # print help information and exit:
        usage()
        sys.exit(1)

    mirror = repoDialog(repolist)
    if not mirror:
        print "Cancelled"
        sys.exit(1)

    if (len(args) == 0):
        print mirror
        sys.exit(0)

    # Generate pacman.conf file
    mirror = "Server = " + mirror
    # The value for @carch@ will be substituted when building the package
    mirror = mirror.replace("@carch@", "i686")
    pcfile = args[0]
    pcfh = open(pcfile, "r")
    pcf = pcfh.read()
    pcfh.close()
    os.remove(pcfile)
    pcfh = open(pcfile, "w")
    for line in pcf.splitlines(True):
        pcfh.write(line)
        if line.startswith("[core]"):
            pcfh.write(mirror.replace("$repo", "core") + "\n")
        elif line.startswith("[extra]"):
            pcfh.write(mirror.replace("$repo", "extra") + "\n")
        elif line.startswith("[community]"):
            pcfh.write(mirror.replace("$repo", "community") + "\n")

    pcfh.close()
    print "++ Updated %s" % pcfile