forked from rpm-software-management/yum-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepotrack.py
executable file
·247 lines (191 loc) · 8.31 KB
/
repotrack.py
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
#!/usr/bin/python -tt
# 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; either version 2 of the License, or
# (at your option) any later version.
#
# 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
# (c) 2005 seth vidal skvidal at phy.duke.edu
# helps partially track a repo. Let's you download a package + all of its
# deps from any set of repos.
# use: so you can keep current on any given pkg + its deps from another
# repo w/o enabling that repo in your yum configuration by default
# also for making partial mirrors that traverse dependencies.
import os
import sys
import shutil
from optparse import OptionParser
from urlparse import urljoin
import logging
import yum
import yum.Errors
import rpmUtils
from yum.misc import getCacheDir
from yum.constants import *
from yum.packages import parsePackages
from yum.packageSack import ListPackageSack
class RepoTrack(yum.YumBase):
def __init__(self, opts):
yum.YumBase.__init__(self)
self.logger = logging.getLogger("yum.verbose.repotrack")
self.opts = opts
def findDeps(self, po):
"""Return the dependencies for a given package, as well
possible solutions for those dependencies.
Returns the deps as a dict of:
dict[reqs] = [list of satisfying pkgs]"""
reqs = po.returnPrco('requires')
reqs.sort()
pkgresults = {}
for req in reqs:
(r,f,v) = req
if r.startswith('rpmlib('):
continue
pkgresults[req] = list(self.whatProvides(r, f, v))
return pkgresults
def more_to_check(unprocessed_pkgs):
for pkg in unprocessed_pkgs.keys():
if unprocessed_pkgs[pkg] is not None:
return True
return False
def parseArgs():
usage = """
Repotrack: keep current on any given pkg and its deps. It will download the package(s) you
want to track and all of their dependencies
%s [options] package1 [package2] [package..] """ % sys.argv[0]
parser = OptionParser(usage=usage)
parser.add_option("-c", "--config", default='/etc/yum.conf',
help='config file to use (defaults to /etc/yum.conf)')
parser.add_option("-a", "--arch", default=None,
help='check as if running the specified arch (default: current arch)')
parser.add_option("-r", "--repoid", default=[], action='append',
help="specify repo ids to query, can be specified multiple times (default is all enabled)")
parser.add_option("-t", "--tempcache", default=False, action="store_true",
help="Use a temp dir for storing/accessing yum-cache")
parser.add_option("-p", "--download_path", dest='destdir',
default=os.getcwd(), help="Path to download packages to")
parser.add_option("-u", "--urls", default=False, action="store_true",
help="Just list urls of what would be downloaded, don't download")
parser.add_option("-n", "--newest", default=True, action="store_false",
help="Toggle downloading only the newest packages(defaults to newest-only)")
parser.add_option("-q", "--quiet", default=False, action="store_true",
help="Output as little as possible")
(opts, args) = parser.parse_args()
return (opts, args)
def main():
# TODO/FIXME
# gpg/sha checksum them
(opts, user_pkg_list) = parseArgs()
if len(user_pkg_list) == 0:
print >> sys.stderr, "Error: no packages specified to parse"
sys.exit(1)
if not os.path.exists(opts.destdir) and not opts.urls:
try:
os.makedirs(opts.destdir)
except OSError, e:
print >> sys.stderr, "Error: Cannot create destination dir %s" % opts.destdir
sys.exit(1)
if not os.access(opts.destdir, os.W_OK) and not opts.urls:
print >> sys.stderr, "Error: Cannot write to destination dir %s" % opts.destdir
sys.exit(1)
my = RepoTrack(opts=opts)
my.doConfigSetup(fn=opts.config,init_plugins=False) # init yum, without plugins
if opts.arch:
archlist = []
archlist.extend(rpmUtils.arch.getArchList(opts.arch))
else:
archlist = rpmUtils.arch.getArchList()
# do the happy tmpdir thing if we're not root
if os.geteuid() != 0 or opts.tempcache:
cachedir = getCacheDir()
if cachedir is None:
print >> sys.stderr, "Error: Could not make cachedir, exiting"
sys.exit(50)
my.repos.setCacheDir(cachedir)
if len(opts.repoid) > 0:
myrepos = []
# find the ones we want
for glob in opts.repoid:
myrepos.extend(my.repos.findRepos(glob))
# disable them all
for repo in my.repos.repos.values():
repo.disable()
# enable the ones we like
for repo in myrepos:
repo.enable()
my._getSacks(archlist=archlist, thisrepo=repo.id)
my.doRepoSetup()
my._getSacks(archlist=archlist)
unprocessed_pkgs = {}
final_pkgs = {}
pkg_list = []
avail = my.pkgSack.returnPackages()
for item in user_pkg_list:
exactmatch, matched, unmatched = parsePackages(avail, [item])
pkg_list.extend(exactmatch)
pkg_list.extend(matched)
if opts.newest:
this_sack = ListPackageSack()
this_sack.addList(pkg_list)
pkg_list = this_sack.returnNewestByNameArch()
del this_sack
if len(pkg_list) == 0:
print >> sys.stderr, "Nothing found to download matching packages specified"
sys.exit(1)
for po in pkg_list:
unprocessed_pkgs[po.pkgtup] = po
while more_to_check(unprocessed_pkgs):
for pkgtup in unprocessed_pkgs.keys():
if unprocessed_pkgs[pkgtup] is None:
continue
po = unprocessed_pkgs[pkgtup]
final_pkgs[po.pkgtup] = po
deps_dict = my.findDeps(po)
unprocessed_pkgs[po.pkgtup] = None
for req in deps_dict.keys():
pkg_list = deps_dict[req]
if opts.newest:
this_sack = ListPackageSack()
this_sack.addList(pkg_list)
pkg_list = this_sack.returnNewestByNameArch()
del this_sack
for res in pkg_list:
if res is not None and res.pkgtup not in unprocessed_pkgs:
unprocessed_pkgs[res.pkgtup] = res
download_list = final_pkgs.values()
if opts.newest:
this_sack = ListPackageSack()
this_sack.addList(download_list)
download_list = this_sack.returnNewestByNameArch()
download_list.sort(key=lambda pkg: pkg.name)
for pkg in download_list:
repo = my.repos.getRepo(pkg.repoid)
remote = pkg.returnSimple('relativepath')
local = os.path.basename(remote)
local = os.path.join(opts.destdir, local)
if (os.path.exists(local) and
os.path.getsize(local) == int(pkg.returnSimple('packagesize'))):
if not opts.quiet:
my.logger.info("%s already exists and appears to be complete" % local)
continue
if opts.urls:
url = urljoin(repo.urls[0],remote)
print '%s' % url
continue
# Disable cache otherwise things won't download
repo.cache = 0
if not opts.quiet:
my.logger.info('Downloading %s' % os.path.basename(remote))
pkg.localpath = local # Hack: to set the localpath to what we want.
path = repo.getPackage(pkg)
if not os.path.exists(local) or not os.path.samefile(path, local):
shutil.copy2(path, local)
if __name__ == "__main__":
main()