From: "Brian Dolbec" <brian.dolbec@gmail.com>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/mirrorselect:master commit in: mirrorselect/
Date: Thu, 17 Oct 2013 03:16:16 +0000 (UTC) [thread overview]
Message-ID: <1381979481.ae600d2d704209985cc50b914ab47982a842d87b.dol-sen@gentoo> (raw)
commit: ae600d2d704209985cc50b914ab47982a842d87b
Author: Brian Dolbec <dolsen <AT> gentoo <DOT> org>
AuthorDate: Thu Oct 17 03:11:21 2013 +0000
Commit: Brian Dolbec <brian.dolbec <AT> gmail <DOT> com>
CommitDate: Thu Oct 17 03:11:21 2013 +0000
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/mirrorselect.git;a=commit;h=ae600d2d
Add repos.conf support.
Move config file actions to their own file.
---
mirrorselect/configs.py | 177 ++++++++++++++++++++++++++++++++++++++++++++++++
mirrorselect/main.py | 168 ++++++++++++++-------------------------------
2 files changed, 226 insertions(+), 119 deletions(-)
diff --git a/mirrorselect/configs.py b/mirrorselect/configs.py
new file mode 100644
index 0000000..3534b72
--- /dev/null
+++ b/mirrorselect/configs.py
@@ -0,0 +1,177 @@
+#-*- coding:utf-8 -*-
+
+
+"""Mirrorselect 2.x
+ Tool for selecting Gentoo source and rsync mirrors.
+
+Copyright 2005-2012 Gentoo Foundation
+
+ 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.
+
+"""
+
+
+from __future__ import print_function
+
+
+import os
+import re
+import shlex
+import shutil
+import string
+import sys
+
+
+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 = []
+
+ regex = re.compile('^%s=.*' % var)
+ for line in lines:
+ if regex.match(line):
+ lines.remove(line)
+
+ 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')
+ sys.exit(0)
+
+
+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, 'wb') 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, sync=False):
+ """Read the current mirrors and retain mounted filesystems mirrors
+
+ @param config_path: string
+ @param sync: boolean, used to switch between SYNC and GENTOO_MIRRORS
+ make.conf variable target
+ @rtype list
+ """
+ fsmirrors = []
+
+ if sync:
+ var = 'SYNC'
+ else:
+ 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 """
+ try:
+ lex = shlex.shlex(f, posix=True)
+ lex.wordchars = string.digits+string.letters+"~!@#$%*_\:;?,./-+{}"
+ lex.quotes = "\"'"
+ while 1:
+ key = lex.get_token()
+ #output.write('get_filesystem_mirrors(): processing key = %s\n' % key, 2)
+
+ if key == var:
+ equ = lex.get_token()
+
+ if (equ == ''):
+ break
+ elif (equ != '='):
+ break
+
+ val = lex.get_token()
+ if val is None:
+ break
+
+ """ Look for mounted filesystem in value """
+ mirrorlist = val.rsplit()
+ output.write('get_filesystem_mirrors(): mirrorlist = %s\n' % mirrorlist, 2)
+ p = re.compile('rsync://|http://|ftp://', re.IGNORECASE)
+ 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
+ except Exception:
+ fsmirrors = []
+
+ output.write('get_filesystem_mirrors(): fsmirrors = %s\n' % fsmirrors, 2)
+ return fsmirrors
+
+
diff --git a/mirrorselect/main.py b/mirrorselect/main.py
index d309624..8a1bfca 100755
--- a/mirrorselect/main.py
+++ b/mirrorselect/main.py
@@ -34,17 +34,15 @@ from __future__ import print_function
import os
-import re
-import shlex
-import shutil
import socket
-import string
import sys
from optparse import OptionParser
from mirrorselect.mirrorparser3 import MIRRORS_3_XML, MIRRORS_RSYNC_DATA
from mirrorselect.output import Output, ColoredFormatter
from mirrorselect.selectors import Deep, Shallow, Interactive
from mirrorselect.extractor import Extractor
+from mirrorselect.configs import (get_make_conf_path, write_make_conf,
+ write_repos_conf, get_filesystem_mirrors)
from mirrorselect.version import version
# eprefix compatibility
@@ -93,123 +91,44 @@ class MirrorSelect(object):
return None
- def write_config(self, hosts, out, config_path, sync=False):
- """Writes the make.conf style string to the given file, or to stdout.
+ def change_config(self, hosts, out, config_path, sync=False):
+ """Writes the config changes to the given file, or to stdout.
@param hosts: list of host urls to write
@param out: boolean, used to redirect output to stdout
@param config_path; string
- @param sync: boolean, used to switch between SYNC and GENTOO_MIRRORS
- make.conf variable target
+ @param sync: boolean, used to switch between sync-uri repos.conf target,
+ SYNC and GENTOO_MIRRORS make.conf variable target
"""
if sync:
- var = 'SYNC'
+ if 'repos.conf' in config_path:
+ var = "sync-uri"
+ else:
+ var = 'SYNC'
else:
var = 'GENTOO_MIRRORS'
if hasattr(hosts[0], 'decode'):
hosts = [x.decode('utf-8') for x in hosts]
- mirror_string = '%s="%s"' % (var, ' '.join(hosts))
+ if var == "sync-uri" and out:
+ mirror_string = '%s = %s' % (var, ' '.join(hosts))
+ else:
+ mirror_string = '%s="%s"' % (var, ' '.join(hosts))
if out:
- print()
- print(mirror_string)
- sys.exit(0)
-
- self.output.write('\n')
- self.output.print_info('Modifying %s with new mirrors...\n' % config_path)
- try:
- config = open(config_path, 'r')
- self.output.write('\tReading make.conf\n')
- lines = config.readlines()
- config.close()
- self.output.write('\tMoving to %s.backup\n' % config_path)
- shutil.move(config_path, config_path + '.backup')
- except IOError:
- lines = []
-
- regex = re.compile('^%s=.*' % var)
- for line in lines:
- if regex.match(line):
- lines.remove(line)
-
- lines.append(mirror_string)
-
- self.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()
-
- self.output.print_info('Done.\n')
- sys.exit(0)
-
-
- def get_filesystem_mirrors(self, config_path, sync=False):
- """Read the current mirrors and retain mounted filesystems mirrors
-
- @param config_path: string
- @param sync: boolean, used to switch between SYNC and GENTOO_MIRRORS
- make.conf variable target
- @rtype list
- """
- fsmirrors = []
-
- if sync:
- var = 'SYNC'
+ self.write_to_output(mirror_string)
+ elif var == "sync-uri":
+ write_repos_conf(self.output, config_path, var, ' '.join(hosts))
else:
- var = 'GENTOO_MIRRORS'
+ write_make_conf(self.output, config_path, var, mirror_string)
- self.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 """
- try:
- lex = shlex.shlex(f, posix=True)
- lex.wordchars = string.digits+string.letters+"~!@#$%*_\:;?,./-+{}"
- lex.quotes = "\"'"
- while 1:
- key = lex.get_token()
- #self.output.write('get_filesystem_mirrors(): processing key = %s\n' % key, 2)
-
- if key == var:
- equ = lex.get_token()
-
- if (equ == ''):
- break
- elif (equ != '='):
- break
-
- val = lex.get_token()
- if val is None:
- break
-
- """ Look for mounted filesystem in value """
- mirrorlist = val.rsplit()
- self.output.write('get_filesystem_mirrors(): mirrorlist = %s\n' % mirrorlist, 2)
- p = re.compile('rsync://|http://|ftp://', re.IGNORECASE)
- for mirror in mirrorlist:
- if (p.match(mirror) == None):
- if os.access(mirror, os.F_OK):
- self.output.write('get_filesystem_mirrors(): found file system mirror = %s\n' % mirror, 2)
- fsmirrors.append(mirror)
- else:
- self.output.write('get_filesystem_mirrors(): ignoring non-accessible mirror = %s\n' % mirror, 2)
- break
- elif key is None:
- break
- except Exception:
- fsmirrors = []
-
- self.output.write('get_filesystem_mirrors(): fsmirrors = %s\n' % fsmirrors, 2)
- return fsmirrors
+
+ @staticmethod
+ def write_to_output(mirror_string):
+ print()
+ print(mirror_string)
+ sys.exit(0)
def _parse_args(self, argv, config_path):
@@ -362,8 +281,10 @@ class MirrorSelect(object):
@rtype: list
'''
if options.rsync:
+ self.output.write("using url: %s" % MIRRORS_RSYNC_DATA, 2)
hosts = Extractor(MIRRORS_RSYNC_DATA, options, self.output).hosts
else:
+ self.output.write("using url: %s" % MIRRORS_3_XML, 2)
hosts = Extractor(MIRRORS_3_XML, options, self.output).hosts
return hosts
@@ -388,21 +309,22 @@ class MirrorSelect(object):
return selector.urls
- @staticmethod
- def get_make_conf_path():
- '''Checks for the existance of make.conf in /etc/portage/
+ def get_conf_path(self, rsync=False):
+ '''Checks for the existance of repos.conf or make.conf in /etc/portage/
Failing that it checks for it in /etc/
Failing in /etc/ it defaults to /etc/portage/make.conf
@rtype: string
'''
- # start with the new 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
+ if rsync:
+ # startwith repos.conf
+ config_path = EPREFIX + '/etc/portage/repos.conf/gentoo.conf'
+ if not os.access(config_path, os.F_OK):
+ self.output.write("Failed access to gentoo.conf: "
+ "%s\n" % os.access(config_path, os.F_OK), 2)
+ return get_make_conf_path(EPREFIX)
+ return config_path
+ return get_make_conf_path(EPREFIX)
def main(self, argv):
@@ -410,12 +332,20 @@ class MirrorSelect(object):
@param argv: list of command line arguments to parse
"""
- config_path = self.get_make_conf_path()
-
+ config_path = self.get_conf_path()
options = self._parse_args(argv, config_path)
self.output.verbosity = options.verbosity
+ self.output.write("main(); config_path = %s\n" % config_path, 2)
+
+ # reset config_path to find repos.conf/gentoo.conf if it exists
+ if options.rsync:
+ config_path = self.get_conf_path(options.rsync)
+ self.output.write("main(); reset config_path = %s\n" % config_path, 2)
+ else:
+ self.output.write("main(); rsync = %s" % str(options.rsync),2)
- fsmirrors = self.get_filesystem_mirrors(config_path, options.rsync)
+ fsmirrors = get_filesystem_mirrors(self.output,
+ config_path, options.rsync)
hosts = self.get_available_hosts(options)
@@ -427,7 +357,7 @@ class MirrorSelect(object):
urls = self.select_urls(hosts, options)
if len(urls):
- self.write_config(fsmirrors + urls, options.output,
+ self.change_config(fsmirrors + urls, options.output,
config_path, options.rsync)
else:
self.output.write("No search results found. "
next reply other threads:[~2013-10-17 3:16 UTC|newest]
Thread overview: 86+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-10-17 3:16 Brian Dolbec [this message]
-- strict thread matches above, loose matches on Subject: below --
2024-05-28 3:48 [gentoo-commits] proj/mirrorselect:master commit in: mirrorselect/ Sam James
2023-08-07 0:23 Sam James
2023-08-07 0:14 Sam James
2023-08-07 0:14 Sam James
2023-08-07 0:14 Sam James
2023-08-06 23:30 Sam James
2023-08-06 23:30 Sam James
2023-07-06 8:05 Sam James
2022-05-31 18:39 Brian Dolbec
2022-05-31 18:39 Brian Dolbec
2022-05-31 18:39 Brian Dolbec
2022-05-31 18:39 Brian Dolbec
2022-05-31 4:08 Brian Dolbec
2022-05-31 4:08 Brian Dolbec
2022-05-31 2:22 Brian Dolbec
2022-05-31 2:22 Brian Dolbec
2022-05-30 23:12 Brian Dolbec
2022-05-30 23:12 Brian Dolbec
2020-06-03 19:05 Brian Dolbec
2019-07-17 5:06 Zac Medico
2019-05-27 17:52 Zac Medico
2019-05-27 17:22 Zac Medico
2019-02-13 8:22 Zac Medico
2019-02-13 8:09 Zac Medico
2019-02-13 5:51 Zac Medico
2018-05-26 15:43 Brian Dolbec
2017-02-21 4:44 Zac Medico
2017-02-21 3:19 Brian Dolbec
2017-02-21 3:19 Brian Dolbec
2016-11-15 1:16 Zac Medico
2015-01-27 18:20 Brian Dolbec
2015-01-27 4:38 Brian Dolbec
2015-01-27 4:38 Brian Dolbec
2014-05-28 21:08 Brian Dolbec
2014-05-28 19:44 Brian Dolbec
2014-05-05 2:04 Brian Dolbec
2014-05-05 2:04 Brian Dolbec
2014-05-05 2:04 Brian Dolbec
2014-03-02 7:44 [gentoo-commits] proj/mirrorselect:ssl " Brian Dolbec
2014-03-02 7:44 ` [gentoo-commits] proj/mirrorselect:master " Brian Dolbec
2014-03-02 7:44 Brian Dolbec
2014-03-02 7:44 Brian Dolbec
2014-03-02 7:44 Brian Dolbec
2014-03-02 7:44 Brian Dolbec
2014-03-02 7:44 Brian Dolbec
2014-03-02 7:44 Brian Dolbec
2014-03-02 7:44 Brian Dolbec
2014-01-31 15:44 [gentoo-commits] proj/mirrorselect:ssl " Brian Dolbec
2014-03-02 7:44 ` [gentoo-commits] proj/mirrorselect:master " Brian Dolbec
2013-10-20 18:19 [gentoo-commits] proj/mirrorselect:ssl " Brian Dolbec
2014-03-02 7:44 ` [gentoo-commits] proj/mirrorselect:master " Brian Dolbec
2013-10-19 9:06 Brian Dolbec
2013-10-19 9:06 Brian Dolbec
2013-10-19 9:06 Brian Dolbec
2013-10-18 14:26 Brian Dolbec
2013-10-18 6:46 Brian Dolbec
2013-10-18 6:46 Brian Dolbec
2013-10-18 6:46 Brian Dolbec
2013-10-18 6:46 Brian Dolbec
2013-10-17 14:26 Brian Dolbec
2013-10-17 6:57 Brian Dolbec
2013-10-17 3:16 Brian Dolbec
2013-10-17 3:16 Brian Dolbec
2013-10-16 9:17 Brian Dolbec
2013-10-16 8:36 Brian Dolbec
2013-10-16 8:36 Brian Dolbec
2013-10-16 8:36 Brian Dolbec
2013-10-16 8:36 Brian Dolbec
2013-10-15 22:43 Brian Dolbec
2013-10-15 14:39 Brian Dolbec
2013-10-15 14:39 Brian Dolbec
2013-10-15 14:39 Brian Dolbec
2013-10-15 7:27 Brian Dolbec
2013-10-15 3:51 Brian Dolbec
2013-10-15 3:51 Brian Dolbec
2013-03-10 13:07 Brian Dolbec
2012-12-16 2:38 Brian Dolbec
2012-12-15 21:25 Brian Dolbec
2012-11-15 3:53 Brian Dolbec
2012-11-15 3:44 Brian Dolbec
2012-11-14 20:28 Paul Varner
2012-11-12 21:41 Brian Dolbec
2012-11-12 20:37 Brian Dolbec
2012-11-12 15:56 Brian Dolbec
2012-11-12 7:46 Brian Dolbec
2012-11-12 7:46 Brian Dolbec
2012-11-12 7:46 Brian Dolbec
2012-11-12 7:46 Brian Dolbec
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1381979481.ae600d2d704209985cc50b914ab47982a842d87b.dol-sen@gentoo \
--to=brian.dolbec@gmail.com \
--cc=gentoo-commits@lists.gentoo.org \
--cc=gentoo-dev@lists.gentoo.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox