summaryrefslogtreecommitdiff
path: root/src/backend/setmirror.py
blob: 90337cbc92228e4e47cdae72d2c03b354ba034da (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
#!/usr/bin/python3

import sisyphus.getfs


def getList():
    mirrorList = []

    with open(sisyphus.getfs.mirrorCfg) as mirrorFile:
        for line in mirrorFile.readlines():
            if 'PORTAGE_BINHOST=' in line:
                url = line.split("=")[1].replace('"', '').rstrip()
                mirror = {'isActive': True, 'Url': url}
                if line.startswith('#'):
                    mirror['isActive'] = False
                mirrorList.append(mirror)
        mirrorFile.close()

    return mirrorList


def printList():
    mirrorList = getList()

    for i, line in enumerate(mirrorList):
        if line['isActive']:
            print(i + 1, '*', line['Url'])
        else:
            print(i + 1, ' ', line['Url'])


def writeList(mirrorList):
    with open(sisyphus.getfs.mirrorCfg, 'w+') as mirrorFile:
        mirrorFile.write(
            "#######################################################\n")
        mirrorFile.write(
            "# Support for multiple mirrors is somewhat incomplete #\n")
        mirrorFile.write(
            "#######################################################\n")
        mirrorFile.write(
            "#       Please avoid using the Main Repository        #\n")
        mirrorFile.write(
            "#    http://mirrors.redcorelinux.org/redcorelinux     #\n")
        mirrorFile.write(
            "#  as the bandwidth is limited, use mirrors instead   #\n")
        mirrorFile.write(
            "#######################################################\n")
        mirrorFile.write(
            "#    Uncomment only one mirror from the list bellow   #\n")
        mirrorFile.write(
            "#######################################################\n")
        mirrorFile.write("\n")
        for line in mirrorList:
            mirror = 'PORTAGE_BINHOST=' + '"' + line['Url'] + '"'
            if not line['isActive']:
                mirror = '# ' + mirror
            mirrorFile.write(mirror + "\n")
            mirrorFile.write("\n")


def setActive(mirror):
    mirrorList = getList()
    if mirror not in range(1, len(mirrorList) + 1):
        print("\nSelected mirror index is incorrect. Please verify using 'sisyphus mirror list'.\n")
    else:
        for i in range(0, len(mirrorList)):
            indx = i + 1
            if indx == mirror:
                mirrorList[i]['isActive'] = True
            else:
                mirrorList[i]['isActive'] = False
        writeList(mirrorList)