summaryrefslogtreecommitdiff
blob: 35d0a9e39fa23a0d05d4e36ed09f9d7a197d96be (plain)
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
#! /usr/bin/python
#
# repcacheman ver 0.44
#
# Cache Manager for Http-Replicator
# deletes duplicate files in PORTDIR.
# imports authenticated (checksum + listed in portage)
# files from PORTDIR to replicator's cache directory.
#
# Uses portage to perform checksum and database functions.
# All else, Copyright(C)2004-2007 Tom Poplawski (poplawtm@earthlink.net)
# Distributed under the terms of the GNU General Public License v2
#
# 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


import portage.manifest
import portage.checksum
import portage.exception
import portage
import string
import os
import pwd,sys,optparse

if os.getuid():
	print"Must be root"
	sys.exit(1)

# Parse Options

parser = optparse.OptionParser()
parser.add_option('-d', '--dir', type='string', default="/var/cache/http-replicator", help='http-replicators cache DIR')
parser.add_option('-u','--user', type='string', default="portage", help='http-replicator USER')
options, args = parser.parse_args() # parse command line

if options.user:
	try:
		uid=pwd.getpwnam(options.user)[2]
		gid=pwd.getpwnam(options.user)[3]
	except:
		print "User \'" + options.user + "\' Doesn't exist on system - edit config or add user to system."
		sys.exit(1)
else:
	print "Error\n\tunable to get USER from /etc/http-replicator.conf"
	sys.exit(1)

# dir is replicator's cache directory
dir=options.dir+"/"

if os.path.isdir(dir) :
	newdir=0
else :
	print"\n\nBegin Http-Replicator Setup...."
	try:
		os.makedirs(dir)
		print "\tcreated " + dir
		newdir=1
	except:
		print "\tcreate " + dir + " failed"
		print '\terror:', sys.exc_info()[1]
		sys.exit(1)
	try:
		os.chown(dir,uid,gid)
		print "\tchanged owner of " + dir + " to " + options.user 
	except:
		print "\tchange owner " + dir + " to " + options.user + " failed:"
		print '\terror:', sys.exc_info()[1]

print "\n\nReplicator's cache directory: " + dir

# Import Portage settings

distdir=portage.settings["DISTDIR"]+"/"
if distdir:
	print "Portage's DISTDIR: " + distdir
else:
	print"Unable to get Portage's DISTDIR"
	sys.exit(1)

# Start Work

print "\nComparing directories...."

# Create filecmp object
import filecmp
dc=filecmp.dircmp (distdir,dir,['cvs-src','git-src','hg-src','egit-src','.locks'])
print "Done!"

dupes=dc.common
deleted=0

if dupes:
	print "\nDeleting duplicate file(s) in " + distdir

	for s in dupes:
		print s
		try:
			os.remove(distdir + s )
			deleted +=1
		except:
			print "\tdelete " + distdir + s + " failed:"
			print '\terror:', sys.exc_info()[1]

	print "Done!"


newfiles=dc.left_only
nf=len(dc.left_only)

if nf:
	print "\nNew files in DISTDIR:"
	for s in newfiles:
		print s
	print"\nChecking authenticity and integrity of new files..."
	added=0
	errors=0
	badsum=0

# search all packages 

	for mycp in portage.db["/"]["porttree"].dbapi.cp_all():
		manifest = portage.manifest.Manifest("/usr/portage/" + mycp , distdir)
		if manifest == None:
			portage.writemsg("Missing manifest: %s\n" % mycpv)

		remove=[]
		for file in newfiles:
			if manifest.hasFile("DIST",file):
				try:
					myok, myreason = manifest.checkFileHashes("DIST",file)
					
					try:
						os.rename(distdir+file,dir+file)
						added += 1
					except:
						try:
							import shutil
							shutil.copyfile(distdir+file,dir+file)
							added += 1
							os.remove(distdir+file)
						except:
							print "\tmove/copy " + file + " failed:"
							print '\terror:', sys.exc_info()[1]
							errors+=1
							
					try:
						os.chown(dir+file,uid,gid)
					except:
						print "\tchown " + file + " failed:"
						print '\terror:', sys.exc_info()[1]
						errors +=1
						
					remove.append( file )
					
				except portage.exception.DigestException, e:
					print("\n!!! Digest verification failed:")
					print("!!! %s" % e.value[0])
					print("!!! Reason: %s" % e.value[1])
					print("!!! Got: %s" % e.value[2])
					print("!!! Expected: %s" % e.value[3])
					badsum+=1
		if remove:
			for rf in remove:
				newfiles.remove ( rf )


print "\nSUMMARY:"
print "Found " + str(len(dupes)) + " duplicate file(s)"
if deleted:
	print "\tDeleted " + str(deleted) + " dupe(s)"

if nf:
	print "Found " + str(nf) + " new file(s)"
	print "\tAdded " + str(added) + " of those file(s) to the cache"
	
	print "Rejected " +str(len(newfiles))  + " File(s) - ",
	print str(badsum) +  " failed checksum(s)"
	for s in newfiles:
		print "\t%s" %s
	if errors:
		print "Encountered " +str(errors) + " errors"
#	if badsum:
#		print str(badsum) + " partial/corrupted file(s)"

if newdir:
	print"\n\nexecute:\n/etc/init.d/http-replicator start"
	print"to run http-replicator.\n\nexecute:\nrc-update add http-replicator default"
	print"to make http-replicator start at boot"
	print"\n\nexecute:\n/usr/bin/repcacheman\nafter emerge's on the server to delete"
	print"dup files and add new files to the cache"

print "\n\nHTTP-Replicator requires you delete any partial downloads in " + distdir
print "run rm -f " + distdir +'*'