| 1 |
#!/usr/bin/python2.2
|
| 2 |
|
| 3 |
# TODO: move md5sum() and hashdir() to a common place?
|
| 4 |
|
| 5 |
from config import dir, odir, mirrors
|
| 6 |
import rfc822, string, md5, gzip, os, re
|
| 7 |
from shutil import copyfile
|
| 8 |
|
| 9 |
watch = {}
|
| 10 |
tmpdir = ''
|
| 11 |
|
| 12 |
# stolen from sources_to_xml.py
|
| 13 |
def treat_sources_file(f, dist):
|
| 14 |
"""Scan the given Sources file and treat each Package entry"""
|
| 15 |
while 1:
|
| 16 |
try:
|
| 17 |
m = rfc822.Message(f)
|
| 18 |
if len(m) == 0: #eof
|
| 19 |
break
|
| 20 |
update_watch_info(m, dist)
|
| 21 |
except EOFError:
|
| 22 |
break
|
| 23 |
|
| 24 |
|
| 25 |
def update_watch_info(m, dist):
|
| 26 |
global watch, tmpdir
|
| 27 |
|
| 28 |
pkg = m['Package']
|
| 29 |
ver = m['Version']
|
| 30 |
dir = m['Directory']
|
| 31 |
diff = ''
|
| 32 |
|
| 33 |
for line in string.split(m['Files'], "\n"):
|
| 34 |
line = line.strip()
|
| 35 |
if line[-8:] == ".diff.gz":
|
| 36 |
diff = line.split()[2]
|
| 37 |
md5sum = line.split()[0]
|
| 38 |
|
| 39 |
if not watch.has_key(pkg):
|
| 40 |
if diff: # don't consider debian native packages
|
| 41 |
watch[pkg] = (ver, md5sum, dir + '/' + diff)
|
| 42 |
else:
|
| 43 |
f = os.popen("/usr/bin/dpkg --compare-versions %s gt %s" % (ver, watch[pkg][0]) )
|
| 44 |
retval = f.close()
|
| 45 |
if retval == 0: # we have found a newer version
|
| 46 |
watch[pkg] = (ver, md5sum, dir + '/' + diff)
|
| 47 |
|
| 48 |
|
| 49 |
def download_diffs():
|
| 50 |
"""downloads all diffs"""
|
| 51 |
global mirrors, watch, tmpdir
|
| 52 |
wgetopts="-q -t2" # at most two tries, and quiet
|
| 53 |
|
| 54 |
watchtxt = open(dir + "/watch.txt", "w+")
|
| 55 |
|
| 56 |
tmpdir = dir + "/diffs"
|
| 57 |
if not os.path.exists(tmpdir): os.mkdir(tmpdir)
|
| 58 |
|
| 59 |
sorted = watch.keys()
|
| 60 |
sorted.sort()
|
| 61 |
|
| 62 |
for pkg in sorted:
|
| 63 |
#print watch[pkg]
|
| 64 |
fullpath = tmpdir + '/' + watch[pkg][2] # complete path with diff.gz
|
| 65 |
|
| 66 |
#first create dirs and cleanup old diffs
|
| 67 |
|
| 68 |
if not os.path.exists(os.path.dirname(fullpath)): os.makedirs(os.path.dirname(fullpath))
|
| 69 |
|
| 70 |
for file in os.listdir(os.path.dirname(fullpath)):
|
| 71 |
if file == os.path.split(fullpath)[1]: continue
|
| 72 |
os.remove(os.path.dirname(fullpath) + '/' + file)
|
| 73 |
|
| 74 |
for mirror in mirrors:
|
| 75 |
if os.path.exists(fullpath) and md5sum(fullpath) == watch[pkg][1]: break
|
| 76 |
|
| 77 |
if mirror.startswith("file://"): # we are local
|
| 78 |
localpath = mirror[8:] + watch[pkg][2]
|
| 79 |
if os.path.exists(localpath) and md5sum(localpath) == watch[pkg][1]: # file is sane
|
| 80 |
fullpath = localpath
|
| 81 |
break
|
| 82 |
elif mirror.startswith("http://") or mirror.startswith("ftp://"): # we are remote
|
| 83 |
f = os.popen("/usr/bin/wget %s -O %s %s" % (wgetopts, fullpath, mirror + watch[pkg][2]))
|
| 84 |
retval = f.close()
|
| 85 |
|
| 86 |
if retval:
|
| 87 |
# print "something weird with wget"
|
| 88 |
pass
|
| 89 |
else:
|
| 90 |
break
|
| 91 |
|
| 92 |
# sanity checks on file should be done here
|
| 93 |
has_watch = extract_watch(pkg, fullpath)
|
| 94 |
if has_watch:
|
| 95 |
watchtxt.write("%s %s\n" % (pkg, watch[pkg][0]))
|
| 96 |
watchtxt.flush()
|
| 97 |
|
| 98 |
watchtxt.close()
|
| 99 |
|
| 100 |
#os.popen("rm -rf " + tmpdir)
|
| 101 |
|
| 102 |
|
| 103 |
def extract_watch(pkg, path):
|
| 104 |
"""extract watch from given diff and write it to base/hash/package/watch
|
| 105 |
and return 1 if is found"""
|
| 106 |
f = gzip.open(path, "r")
|
| 107 |
if not f: return 0
|
| 108 |
|
| 109 |
in_watch = 0
|
| 110 |
has_watch = 0
|
| 111 |
filepath = ''
|
| 112 |
|
| 113 |
for line in f.readlines():
|
| 114 |
m = re.search("\+\+\+ \S+/debian/watch", line)
|
| 115 |
if m:
|
| 116 |
filepath = odir + '/' + hashdir(pkg) + '/' + pkg + '/watch'
|
| 117 |
watch_f = open(filepath,'w+')
|
| 118 |
in_watch = 1
|
| 119 |
has_watch = 1
|
| 120 |
continue
|
| 121 |
|
| 122 |
if in_watch:
|
| 123 |
if line[0:2] == "@@": continue # diff offset ignored
|
| 124 |
if line[0] == "+": line = line[1:] # plus sign stripped
|
| 125 |
m = re.search("--- \S+", line)
|
| 126 |
if m:
|
| 127 |
in_watch = 0
|
| 128 |
break # only one debian/watch per diff is possible, no need to continue!
|
| 129 |
watch_f.write(line)
|
| 130 |
|
| 131 |
if has_watch: watch_f.close()
|
| 132 |
|
| 133 |
if not has_watch and os.path.exists(filepath): os.remove(filepath) # purge old watch file
|
| 134 |
|
| 135 |
f.close()
|
| 136 |
|
| 137 |
return has_watch
|
| 138 |
|
| 139 |
|
| 140 |
# some utils
|
| 141 |
def md5sum(file):
|
| 142 |
"""returns md5sum hash in hex for the given file"""
|
| 143 |
f = open(file,"r")
|
| 144 |
if not f: return 0
|
| 145 |
|
| 146 |
hash = md5.new()
|
| 147 |
for line in f.readlines():
|
| 148 |
hash.update(line)
|
| 149 |
|
| 150 |
return hash.hexdigest()
|
| 151 |
|
| 152 |
def hashdir(pkg):
|
| 153 |
"""returns the hash for package name used in base/ directory"""
|
| 154 |
hash = pkg[0]
|
| 155 |
if pkg[0:3] == "lib":
|
| 156 |
hash = pkg[0:4]
|
| 157 |
|
| 158 |
return hash
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
for comp in ["main", "contrib", "non-free"]:
|
| 163 |
# only unstable and experimental to consider
|
| 164 |
f = open(dir + "/Sources_unstable_" + comp, "r")
|
| 165 |
treat_sources_file(f, "unstable")
|
| 166 |
f.close()
|
| 167 |
# Experimental
|
| 168 |
f = open(dir + "/Sources-experimental_" + comp, "r")
|
| 169 |
treat_sources_file(f, "experimental")
|
| 170 |
f.close()
|
| 171 |
|
| 172 |
download_diffs()
|