[yum-commits] 2 commits - yum-config-manager.py
skvidal at osuosl.org
skvidal at osuosl.org
Thu Jul 29 20:20:31 UTC 2010
yum-config-manager.py | 134 ++++++++++++++++++++++++++++++++++++++++++++------
1 file changed, 120 insertions(+), 14 deletions(-)
New commits:
commit 51226cd83d3903cc6f1162d92d792ec6a336333e
Merge: b381d85... f0e7e90...
Author: Seth Vidal <skvidal at fedoraproject.org>
Date: Thu Jul 29 16:20:12 2010 -0400
Merge branch 'master' of ssh://yum.baseurl.org/srv/projects/yum/git/yum-utils
* 'master' of ssh://yum.baseurl.org/srv/projects/yum/git/yum-utils:
Accept "*" to mean all arches.
commit b381d85873f49a25d4ec5c572d2c2e98131e8cc0
Author: Seth Vidal <skvidal at fedoraproject.org>
Date: Thu Jul 29 16:18:46 2010 -0400
add '--add-repo' option to yum-config-manager to allow adding repos from:
http://someserver/foo.repo
or
http://someplace/this/is/a/repo/
and setting them up, trivially.
diff --git a/yum-config-manager.py b/yum-config-manager.py
index 790cfe7..be6eee1 100755
--- a/yum-config-manager.py
+++ b/yum-config-manager.py
@@ -2,6 +2,7 @@
import os, os.path
import sys
+import re
import yum
sys.path.insert(0,'/usr/share/yum-cli')
from utils import YumUtilBase
@@ -9,6 +10,50 @@ import logging
from iniparse import INIConfig
+# Regular expressions to sanitise cache filenames
+re_url_scheme = re.compile(r'^\w+:/*(\w+:|www\.)?')
+re_slash = re.compile(r'[?/:&#|]+')
+re_initial_cruft = re.compile(r'^[,.]*')
+re_final_cruft = re.compile(r'[,.]*$')
+
+def sanitize_url_to_fs(url):
+ """Return a filename suitable for the filesystem
+
+ Strips dangerous and common characters to create a filename we
+ can use to store the cache in.
+ """
+
+ # code taken and modified from planet venus code base:
+ # http://intertwingly.net/code/venus/LICENCE
+
+ try:
+ if re_url_scheme.match(url):
+ if isinstance(url,str):
+ url=url.decode('utf-8').encode('idna')
+ else:
+ url=url.encode('idna')
+ except:
+ pass
+ if isinstance(url,unicode):
+ url=url.encode('utf-8')
+ url = re_url_scheme.sub("", url)
+ url = re_slash.sub("_", url)
+ url = re_initial_cruft.sub("", url)
+ url = re_final_cruft.sub("", url)
+
+ # limit length of url
+ if len(url)>250:
+ parts=url.split(',')
+ for i in range(len(parts),0,-1):
+ if len(','.join(parts[:i])) < 220:
+ url = ','.join(parts[:i]) + ',' + \
+ yum.misc.checksum('md5', (','.join(parts[i:])))
+ break
+
+ return url
+
+
+
def writeRawConfigFile(filename, sectionname, cfgoptions, items, optionobj,
only=None):
"""
@@ -47,13 +92,19 @@ group.add_option("--enable", default=False, action="store_true",
help='enable the specified repos (automatically saves)')
group.add_option("--disable", default=False, action="store_true",
help='disable the specified repos (automatically saves)')
-
+group.add_option("--add-repo", default=[], dest='addrepo', action='append',
+ help='add (and enable) the repo from the specified file or url')
try:
opts = yb.doUtilConfigSetup()
except yum.Errors.RepoError, e:
logger.error(str(e))
sys.exit(50)
+if opts.save or opts.enable or opts.disable or opts.addrepo:
+ if yb.conf.uid != 0:
+ logger.error("You must be root to change the yum configuration.")
+ sys.exit(50)
+
args = set(yb.cmds)
if opts.enable and opts.disable:
@@ -65,7 +116,7 @@ if opts.enable and not args:
only = None
-if not args or 'main' in args:
+if (not args and not opts.addrepo) or 'main' in args:
print yb.fmtSection('main')
print yb.conf.dump()
if opts.save and hasattr(yb, 'main_setopts') and yb.main_setopts:
@@ -88,15 +139,70 @@ if args:
else:
repos = yb.repos.listEnabled()
-for repo in sorted(repos):
- print yb.fmtSection('repo: ' + repo.id)
- if opts.enable and not repo.enabled:
- repo.enable()
- elif opts.disable and repo.enabled:
- repo.disable()
- print repo.dump()
- if (opts.save and
- (only or (hasattr(yb, 'repo_setopts') and repo.id in yb.repo_setopts))):
- writeRawConfigFile(repo.repofile, repo.id,
- repo.cfg.options, repo.iteritems, repo.optionobj,
- only)
+if not opts.addrepo:
+ for repo in sorted(repos):
+ print yb.fmtSection('repo: ' + repo.id)
+ if opts.enable and not repo.enabled:
+ repo.enable()
+ elif opts.disable and repo.enabled:
+ repo.disable()
+ print repo.dump()
+ if (opts.save and
+ (only or (hasattr(yb, 'repo_setopts') and repo.id in yb.repo_setopts))):
+ writeRawConfigFile(repo.repofile, repo.id,
+ repo.cfg.options, repo.iteritems, repo.optionobj,
+ only)
+
+if opts.addrepo:
+ # figure out the best reposdir by seeing which dirs exist
+ myrepodir = None
+ for rdir in yb.conf.reposdir:
+ if os.path.exists(rdir): # take the first one that exists
+ myrepodir = rdir
+ break
+
+ if not myrepodir:
+ myrepodir = yb.conf.reposdir[0]
+ os.makedirs(myrepodir)
+
+ for url in opts.addrepo:
+ print 'adding repo from: %s' % url
+ if url.endswith('.repo'): # this is a .repo file - fetch it, put it in our reposdir and enable it
+ destname = os.path.basename(url)
+ destname = myrepodir + '/' + destname
+ # this sucks - but take the first repo we come to that's enabled
+ # and steal it's grabber object - it could be proxy-laden but that's the risk we take
+ # grumbledy grumble
+ grabber = yb.repos.listEnabled()[0].grabfunc
+ print 'grabbing file %s to %s' % (url, destname)
+ try:
+ result = grabber.urlgrab(url, filename=destname, copy_local=True)
+ except (IOError, OSError, yum.Errors.YumBaseError), e:
+ logger.error('Could not fetch/save url %s to file %s: %s' % (url, destname, e))
+ continue
+ else:
+ print 'repo saved to %s' % result
+
+ else:
+ repoid = sanitize_url_to_fs(url)
+ reponame = 'added from: %s' % url
+ repofile = myrepodir + '/' + repoid + '.repo'
+ try:
+ thisrepo = yb.add_enable_repo(repoid, baseurl=[url], name=reponame)
+ except yum.Errors.DuplicateRepoError, e:
+ logger.error('Cannot add repo from %s as is a duplicate of an existing repo' % url)
+ continue
+ repoout = """\n[%s]\nname=%s\nbaseurl=%s\nenabled=1\n\n""" % (repoid, reponame, url)
+
+ try:
+ fo = open(repofile, 'w+')
+ fo.write(repoout)
+ print repoout
+ except (IOError, OSError), e:
+ logger.error('Could not save repo to repofile %s: %s' % (repofile, e))
+ continue
+ else:
+ fo.close()
+
+
+
More information about the Yum-commits
mailing list