File: WFUTCore.py

package info (click to toggle)
libwfut 0.2.1-2
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 2,596 kB
  • ctags: 1,927
  • sloc: cpp: 18,504; sh: 9,196; python: 630; makefile: 76
file content (152 lines) | stat: -rwxr-xr-x 4,481 bytes parent folder | download
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
import os
from libwfut import WFUT

#### Select a mirror
##wfut.getMirrorList(server_root + mirror_file, mirrors);
##### TODO: Select mirror
##if mirrors.empty() == False:
##	print mirrors.front().url;

## Set up event handlers
def onDownloadComplete(url, filename, local, updates, tmpfile):
	print "Downloaded", filename;
	fileobject = updates.getFiles()[filename];
	## Log to local list
	local.addFile(fileobject);
	## Record Update
	recordUpdate(fileobject, tmpfile);

def onDownloadFailure(url, filename, reason):
	print "Downloaded Failed", filename, "Reason:", reason;

def onUpdateReason(filename, reason):
	#print filename, reason, WFUT.WFUT_UPDATE_NO_LOCAL;
	pass;

def recordUpdate(fileobject, tmpfile):
	"""Records a file update in the temp file."""
	print(tmpfile)
	try:
		fp = None;
		if os.path.exists(tmpfile):
			## Append to existing file
			fp = open(tmpfile, "at");
		else:
			## Create a new file
			fp = open(tmp_wfut, "wt");
			fp.write("<?xml version=\"1.0\"?>\n");
			fp.write("<fileList dir=\"\">\n");

		fname = WFUT.Encoder_encodeString(fileobject.filename);
		print(fname)
		## TODO: Make sure crc is printed as unsigned.
		entry = "".join(["<file filename=\"", fname, "\" version=\"", str(fileobject.version), "\" crc32=\"",  str(fileobject.crc32), "\" size=\"", str(fileobject.size), "\" execute=\"", str(fileobject.execute), "\"/>\n"]);
		fp.write(entry);

		fp.close();
	except Exception, e:
		print e;

class Client:
	def __init__(self):
		self.mirror_file = "mirrors.xml";
		self.channel_file = "wfut.xml";
		self.tmpfile = "tempwfut.xml";

		self.wfut = WFUT.WFUTClient();
		self.wfut.init();

		self.channel = ".";
		self.local_path = "./";
		self.system_path = "";
		self.server_root = "";

	def setChannelName(self, channel):
		self.channel = channel;

	def setSystemPath(self, system_path):
		self.system_path = system_path;

	def setLocalPath(self, local_path):
		self.local_path = local_path;

	def setServerRoot(self, server_root):
		self.server_root = server_root;

	def startUpdates(self):
		self.mirrors = WFUT.MirrorList();
		self.updates = WFUT.ChannelFileList();
		self.local = WFUT.ChannelFileList();
		self.tmplist = WFUT.ChannelFileList();
		self.system = WFUT.ChannelFileList();
		self.server = WFUT.ChannelFileList();

		local_root = self.local_path + "/" + self.channel + "/";
		local_wfut = self.local_path + "/" + self.channel + "/" + self.channel_file;
		system_wfut = self.system_path + "/" + self.channel + "/" + self.channel_file;
		server_wfut = self.server_root + "/" + self.channel + "/" + self.channel_file;

		self.tmp_wfut = self.local_path + "/" + self.tmpfile;

		## Hook up callbacks
		#  Create a callback binding the updates list and tmp file loations.
		def dc(u,f): return onDownloadComplete(u, f, self.local, self.updates, self.tmp_wfut)
		self.dc = dc;
		self.wfut.DownloadCompleteCB(self.dc);

		self.wfut.DownloadFailedCB(onDownloadFailure);
		self.wfut.UpdateReasonCB(onUpdateReason);

		
		## Read local updates location
		if (os.path.exists(local_wfut)):
			if (self.wfut.getLocalList(local_wfut, self.local) > 0):
				print "Error reading local wfut.xml";
			else:
				if (self.channel == "."):
					 self.channel = self.local.getName();

		## Look for tmpwfut.xml file and pull in extra updates.
		if (os.path.exists(self.tmp_wfut)):
			if (self.wfut.getLocalList(self.tmp_wfut, self.tmplist) > 0):
				print "Error reading tempwfut.xml";
			else:
				## Copy tmp list into local list
				files = self.tmplist.getFiles();
				for filename, fileobject in files.items():
					self.local.addFile(fileobject)

		## Read system location
		if (os.path.exists(system_wfut)):
			if (self.wfut.getLocalList(system_wfut, self.system) > 0):
				print "Error reading system wfut.xml";
			else:
				pass	

		## Get server list
		if (self.wfut.getFileList(server_wfut, self.server) > 0):
			print "Error reading or downloading server wfut.xml";
		else:
			pass

		## Calculate updates
		self.wfut.calculateUpdates(self.server, self.system, self.local, self.updates, local_root);

		# Set channel name
		self.local.setName(self.server.getName());

		## Download updates
		self.wfut.updateChannel(self.updates, self.server_root + "/" + self.channel, local_root);

	def poll(self):
		return self.wfut.poll();
			
	def finishUpdates(self):	
		## Save download list
		self.wfut.saveLocalList(self.local, local_wfut);
		## Delete temp files.
		os.remove(self.tmp_wfut);

	def __deinit__(self):
		self.wfut.shutdown();