/[qa]/trunk/pts/www/bin/extract_watch.py
ViewVC logotype

Contents of /trunk/pts/www/bin/extract_watch.py

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:eol-style native
svn:executable *
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.5