/[gentoo-src]/portage/pym/sync/snapshot.py
Gentoo

Contents of /portage/pym/sync/snapshot.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations) (download) (as text)
Sun Nov 7 14:38:39 2004 UTC (13 years, 11 months ago) by ferringb
Branch: MAIN
CVS Tags: portage_041204, HEAD
Branch point for: portage_2_1
Changes since 1.2: +1 -1 lines
File MIME type: text/x-python
header fixes.

1 # snapshot.py; provides the capability of fetching a portage tree snapshot, and syncing a tree with it.
2 # Copyright 2004 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4 #$Header: /var/cvsroot/gentoo-src/portage/pym/sync/snapshot.py,v 1.2 2004/11/07 14:06:53 ferringb Exp $
5
6 import os
7 import time
8 import portage_checksum, portage_exec
9 import shutil
10 import sync.rsync
11
12 """snapshot-http://gentoo.chem.wisc.edu/gentoo"""
13 class SnapshotHost:
14 def __init__(self,host_uri,snapshots_dir,tmp_dir,fetcher=None,use_md5=True):
15 if fetcher==None:
16 import transports.bundled_lib
17 fetcher=transports.bundled_lib.BundledConnection()
18 self.__fetcher=fetcher
19 self.__host_uri=host_uri
20 self.__tmpdir = tmp_dir
21 self.__snapshots_dir = snapshots_dir
22 self.__use_md5 = use_md5
23
24 def sync(self, local_path,verbosity=1):
25 attempts=0
26 downloaded=False
27 while attempts < 40 and not downloaded:
28 file="portage-%s.tar.bz2" % time.strftime("%Y%m%d",
29 time.localtime(time.time() - (attempts*24*60*60)))
30 loc=self.__snapshots_dir+"/"+file
31 rem=self.__host_uri+"/"+file
32 downloaded=self.__fetch_snapshot(file,loc,rem,verbosity)
33 attempts += 1
34
35 if not downloaded:
36 # no snapshot, no syncy-poo.
37 return False
38
39 return self.__apply_snapshot(loc,local_path,verbosity)
40
41 def __apply_snapshot(self,snapshot,local_tree,verbosity):
42 """apply the actual snapshot. for different methods of this, inherit this class
43 and overload this function
44 current it untars to a temp location, and rsync's it over to local_path."""
45
46 #this should be checked
47 portage_exec.spawn(("tar","-jxf",snapshot,"-C",self.__tmpdir))
48 syncer=sync.rsync.RsyncHost("%s/portage/" % self.__tmpdir,local_host=True)
49 try:
50 ret = syncer.sync({},local_tree,excludes=("/distfiles","/local","/packages"),verbosity=verbosity)
51 except sync.rsync.RSyncSyntaxError,e:
52 print "caught rsync syntax exception:",e
53 return False
54 except IOError, ie:
55 print "issue: ",ie
56 return False
57 if verbosity:
58 print "cleaning tempoary snapshot directory- %s/portage" % self.__tmpdir
59 shutil.rmtree(self.__tmpdir+"/portage")
60
61 #nuke all other return codes.
62 if ret != True:
63 return False
64 return ret
65
66 def __fetch_snapshot(self,file,loc,rem,verbosity):
67 grab_md5=True
68 hash=None
69 md5=None
70 md5_existed=False
71 ret=False
72 if self.__use_md5 and os.path.exists(loc+".md5sum"):
73 hash=self.__read_md5sum(loc+".md5sum")
74 if hash==None:
75 os.remove(loc+".md5sum")
76 else:
77 md5_existed=True
78 grab_md5=False
79
80 if self.__use_md5 and grab_md5:
81 ret=self.__fetcher.fetch(rem+".md5sum",file_name=loc+".md5sum",verbose=(verbosity==1))
82 if not ret:
83 hash=self.__read_md5sum(loc+".md5sum")
84
85 if ret:
86 if verbosity:
87 print "!!! failed to fetch md5 for %s" % file
88 return False
89
90 # at this point we have the md5, and know the image *should* exist.
91 ret = False
92 if os.path.exists(loc):
93 if self.__use_md5:
94 md5=portage_checksum.perform_md5(loc)
95 if hash == md5 or not self.__use_md5:
96 if verbosity:
97 print ">>> reusing %s" % loc
98 return True
99 else:
100 if verbosity:
101 print ">>> resuming %s" % rem
102 ret=self.__fetcher.resume(rem,file_name=loc,verbose=(verbosity==1))
103 else:
104 if verbosity:
105 print ">>> fetching %s" % rem
106 ret=self.__fetcher.fetch(rem,file_name=loc,verbose=(verbosity==1))
107
108 if ret:
109 if verbosity:
110 print "!!! failed %s" % file
111 return False
112
113 if self.__use_md5 and md5==None:
114 md5=portage_checksum.perform_md5(loc)
115
116 if self.__use_md5 and md5_existed and md5!= hash:
117 print ">>> re-grabbing the hash"
118 # grab the md5 anew to be safe.
119 os.remove(loc+".md5sum")
120 if not self.__fetcher.fetch(rem+".md5sum",file_name=loc+".md5sum",verbose=True):
121 hash=self.__read_md5sum(loc+".md5sum")
122
123 if md5!=hash and self.__use_md5:
124 if verbosity:
125 print "!!! snapshots correct md5: %s" % hash
126 print "!!! snapshots actual md5 : %s" % md5
127 print "!!! cycling to next candidate."
128 print
129 return False
130 # if this point has been reached, things are sane.
131 return True
132
133
134 def __read_md5sum(self,file):
135 try:
136 myf=open(file,"r")
137 hash=myf.readline().split()[0]
138 if len(hash)!=32:
139 return None
140 return hash
141 except (OSError,IOError,IndexError),e:
142 print e
143 return None

  ViewVC Help
Powered by ViewVC 1.1.20