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
|
#-*- coding:utf-8 -*-
"""Mirrorselect 2.x
Tool for selecting Gentoo source and rsync mirrors.
Copyright 2005-2019 Gentoo Authors
Copyright (C) 2005 Colin Kingsley <tercel@gentoo.org>
Copyright (C) 2008 Zac Medico <zmedico@gentoo.org>
Copyright (C) 2009 Sebastian Pipping <sebastian@pipping.org>
Copyright (C) 2009 Christian Ruppert <idl0r@gentoo.org>
Copyright (C) 2012 Brian Dolbec <dolsen@gentoo.org>
Distributed under the terms of the GNU General Public License v2
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 St, Fifth Floor, Boston, MA 02110-1301, USA.
"""
import os
import re
import shlex
import shutil
import string
import sys
letters = string.ascii_letters
def get_make_conf_path(EPREFIX):
# try the newer make.conf location
config_path = EPREFIX + '/etc/portage/make.conf'
if not os.access(config_path, os.F_OK):
# check if the old location is what is used
if os.access(EPREFIX + '/etc/make.conf', os.F_OK):
config_path = EPREFIX + '/etc/make.conf'
return config_path
def write_make_conf(output, config_path, var, mirror_string):
"""Write the make.conf target changes
@param output: file, or output to print messages to
@param mirror_string: "var='hosts'" string to write
@param config_path; string
"""
output.write('\n')
output.print_info('Modifying %s with new mirrors...\n' % config_path)
try:
config = open(config_path, 'r')
output.write('\tReading make.conf\n')
lines = config.readlines()
config.close()
output.write('\tMoving to %s.backup\n' % config_path)
shutil.move(config_path, config_path + '.backup')
except IOError:
lines = []
with open(config_path + '.backup', 'r') as f:
lex = shlex.shlex(f, posix=True)
lex.wordchars = string.digits + letters + r"~!@#$%*_\:;?,./-+{}"
lex.quotes = "\"'"
while True:
key = lex.get_token()
if key is None:
break
if key == var:
begin_line = lex.lineno
equ = lex.get_token()
if equ is None:
break
if equ != '=':
continue
val = lex.get_token()
if val is None:
break
end_line = lex.lineno
new_lines = []
for index, line in enumerate(lines):
if index < begin_line - 1 or index >= end_line - 1:
new_lines.append(line)
lines = new_lines
break
lines.append(mirror_string)
output.write('\tWriting new %s\n' % config_path)
config = open(config_path, 'w')
for line in lines:
config.write(line)
config.write('\n')
config.close()
output.print_info('Done.\n')
def write_repos_conf(output, config_path, var, value):
"""Saves the new var value to a ConfigParser style file
@param output: file, or output to print messages to
@param config_path; string
@param var: string; the variable to save teh value to.
@param value: string, the value to set var to
"""
try:
from configparser import ConfigParser
except ImportError:
from ConfigParser import ConfigParser
config = ConfigParser()
config.read(config_path)
if config.has_option('gentoo', var):
config.set('gentoo', var, value)
with open(config_path, 'w') as configfile:
config.write(configfile)
else:
output.print_err("write_repos_conf(): Failed to find section 'gentoo',"
" variable: %s\nChanges NOT SAVED" %var)
def get_filesystem_mirrors(output, config_path):
"""Read the current mirrors and retain mounted filesystems mirrors
@param config_path: string
@rtype list
"""
def get_token(lex):
'''internal function for getting shlex tokens
'''
try:
val = lex.get_token()
except ValueError:
val = None
return val
fsmirrors = []
var = 'GENTOO_MIRRORS'
output.write('get_filesystem_mirrors(): config_path = %s\n' % config_path, 2)
try:
f = open(config_path,'r')
except IOError:
return fsmirrors
""" Search for 'var' in make.conf and extract value """
lex = shlex.shlex(f, posix=True)
lex.wordchars = string.digits + letters + r"~!@#$%*_\:;?,./-+{}"
lex.quotes = "\"'"
p = re.compile('rsync://|http://|https://|ftp://', re.IGNORECASE)
while 1:
key = get_token(lex)
#output.write('get_filesystem_mirrors(): processing key = %s\n' % key, 2)
if key == var:
equ = get_token(lex)
if (equ != '='):
break
val = get_token(lex)
if val is None:
break
""" Look for mounted filesystem in value """
mirrorlist = val.rsplit()
output.write('get_filesystem_mirrors(): mirrorlist = %s\n' % mirrorlist, 2)
for mirror in mirrorlist:
if (p.match(mirror) == None):
if os.access(mirror, os.F_OK):
output.write('get_filesystem_mirrors(): found file system mirror = %s\n' % mirror, 2)
fsmirrors.append(mirror)
else:
output.write('get_filesystem_mirrors(): ignoring non-accessible mirror = %s\n' % mirror, 2)
break
elif key is None:
break
output.write('get_filesystem_mirrors(): fsmirrors = %s\n' % fsmirrors, 2)
return fsmirrors
|