aboutsummaryrefslogtreecommitdiff
path: root/h-client/hlibrary.py
blob: a9e93dd688efbd1863511e93b595f3d387e5b192 (plain) (blame)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# -*- coding: utf-8 -*-
# h-client, a client for an h-source server (such as http://www.h-node.com)
# Copyright (C) 2011  Antonio Gallo
#
#
# h-client is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# h-client 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 h-client.  If not, see <http://www.gnu.org/licenses/>.

import os
import string
import re
import sys
import pycurl
import urllib
import htmlentitydefs
from xml.dom import minidom



class Device(object):

	#from codename to h-source distro code
	_distrosTable = {
		'deltah'			:	'gnewsense_2_3',
		'metad'				:	'gnewsense_3_0',
		'awen'				:	'trisquel_3_5',
		'taranis'			:	'trisquel_4_0',
		'slaine'			:	'trisquel_4_5'
	}

	#list of options for the howItWorks entry
	_howItWorksOptions = ['yes','no']

	#_allowedDistros = {
		#'blag_90001'		:	'BLAG 90001',
		#'blag_120000'		:	'BLAG 120000',
		#'dragora_1_1'		:	'Dragora 1.1',
		#'dragora_2_0'		:	'Dragora 2.0 Ardi',
		#'dynebolic_2_5_2'	:	'Dynebolic 2.5.2 DHORUBA',
		#'gnewsense_2_3'		:	'gNewSense 2.3 Deltah',
		#'gnewsense_3_0'		:	'gNewSense 3.0 Metad',
		#'musix_2_0'			:	'Musix GNU+Linux 2.0 R0',
		#'trisquel_3_5' 		:	'Trisquel 3.5 Awen',
		#'trisquel_4_0' 		:	'Trisquel 4.0 Taranis',
		#'trisquel_4_5' 		:	'Trisquel 4.5 Slaine',
		#'ututo_xs_2009'		:	'UTUTO XS 2009',
		#'ututo_xs_2010'		:	'UTUTO XS 2010',
		#'venenux_0_8'		:	'VENENUX 0.8',
		#'venenux_0_8_2'		:	'VENENUX-EC 0.8.2'
	#}

	#list of interfaces
	_interfaces = []

	_status = True

	errors = []

	def __init__(self):
		self._post = {}
		self._type = ''
		self._vendor = ''
		self._model = ''
		self._kernel = ''
		self._distributions = [self.userDistribution()]
		self._interface = 'not-specified'
		self._year = 'not-specified'
		self._vendorId = ''
		self._productId = ''
		self._howItWorks = ''
		self._driver = ''
		self._description = ''

	def setPost(self):
		self._post['model'] = self._model;
		self._post['kernel'] = self._kernel;
		self._post['distribution'] = self.createDistroEntry();
		self._post['comm_year'] = self._year;
		self._post['pci_id'] = self._vendorId + ':' + self._productId;
		self._post['interface'] = self._interface;
		self._post['description'] = self.htmlentitiesDecode(self._description.replace("\n","\r\n"));
		self._post['driver'] = self._driver;
		
	#replace the HTML entitites with utf-8 characters
	def htmlentitiesDecode(self,string):
		for entity,code in htmlentitydefs.name2codepoint.iteritems():
			string = string.replace("&"+entity+";",unichr(code))
		return string.encode('utf-8')

	#get the h-source distro code from the codename
	def getDistroCode(self,codenameString):
		codenames = self._distrosTable.keys()
		for codename in codenames:
			if codenameString.find(codename) != -1:
				return self._distrosTable[codename]
		return ''

	#create the distribution entry
	def createDistroEntry(self):
		cleanDistros = []
		for distro in self._distributions:
			cleanDistros.append(distro.lstrip().rstrip())

		#remove duplicates
		cleanDistros = list(set(cleanDistros))
		#sort the elements
		cleanDistros = sorted(cleanDistros)

		dis = ' , '.join(cleanDistros)
		#correct a strange python behaviour
		if dis[0] == ' , ':
			dis = dis[3:]
		return dis

	#add a distribution
	def addDistribution(self,distroName):
		self._distributions.append(distroName)

	#add many distributions
	#distroList: comma separated list of distros (type: string)
	def addDistributions(self,distroList):
		distros = distroList.split(',')
		for distro in distros:
			self.addDistribution(distro)

	#get the h-source distro code of the user
	def userDistribution(self):
		if not os.system('cat /etc/*-release | grep CODENAME > tmp/temp'):
			f = open('tmp/temp','r')
			row = f.readline().replace("\n","").lower()
			return self.getDistroCode(row)
			f.close();
		else:
			self._status = False
			self.errors.append('tmp folder not writable')

	def getHowItWorksOptions(self):
		return self._howItWorksOptions

	def getInterfaces(self):
		return self._interfaces

	def getType(self):
		return self._type
		
	def getVendor(self):
		return self._vendor

	def getModel(self):
		return self._model

	def getKernel(self):
		return self._kernel

	def getDistributions(self):
		return self._distributions
		
	def getInterface(self):
		return self._interface

	def getYear(self):
		return self._year

	def getVendorId(self):
		return self._vendorId

	def getProductId(self):
		return self._productId

	def getHowItWorks(self):
		return self._howItWorks

	def getDriver(self):
		return self._driver

	def getDescription(self):
		return self._description

	def setType(self,ttype):
		self._type = ttype
		
	def setVendor(self,vendor):
		self._vendor = vendor

	def setModel(self,model):
		self._model = model

	def setKernel(self,kernel):
		self._kernel = kernel

	def setDistributions(self,distributions):
		self._distributions = distributions

	def setInterface(self,interface):
		self._interface = interface

	def setYear(self,year):
		self._year = year

	def setVendorId(self,vendorId):
		self._vendorId = vendorId

	def setProductId(self,productId):
		self._productId = productId

	def setHowItWorks(self,howItWorks):
		self._howItWorks = howItWorks

	def setDriver(self,driver):
		self._driver = driver

	def setDescription(self,description):
		self._description = description

	def getStatus(self):
		return self._status

	def getPost(self):
		return self._post

class Videocard(Device):

	def __init__(self):
		super(Videocard, self).__init__()
		self._type = 'videocard'
		self._howItWorks = 'does_not_work'
		self._interfaces = ['not-specified','PCI','AGP','PCI-E','ISA','MCA','VLB']
		self._howItWorksOptions = ['works_with_3D','works_without_3D','does_not_work']

	def setPost(self):
		super(Videocard, self).setPost()
		self._post['video_card_works'] = self._howItWorks

class Wifi(Device):

	def __init__(self):
		super(Wifi, self).__init__()
		self._type = 'wifi'
		self._howItWorks = 'no'
		self._interfaces = ['not-specified','USB','PCI','PCI-E','mini-PCI','mini-PCI-E','ExpressCard','PC-Card']

	def setPost(self):
		super(Wifi, self).setPost()
		self._post['wifi_works'] = self._howItWorks

class Soundcard(Device):

	def __init__(self):
		super(Soundcard, self).__init__()
		self._type = 'soundcard'
		self._howItWorks = 'no'
		self._interfaces = ['not-specified','PCI','ISA','USB','Firewire','Parallel','PCI-E','PCMCIA']

	def setPost(self):
		super(Soundcard, self).setPost()
		self._post['sound_card_works'] = self._howItWorks


#class to carry out http requests by means of pycurl
class Mycurl:

	_post = None

	#set the domain
	def __init__(self,domain):
		self.contents = ''
		self.setDomain(domain)

	def setDomain(self,domain):
		self.domain = domain
		#check if the trailing slash is present
		if len(self.domain) > 1:
			if self.domain[len(self.domain)-1] != '/':
				self.domain += '/'

	def setPost(self,post):
		self._post = post

	def body_callback(self, buf):
		self.contents = self.contents + buf

	def perform(self,requestUri = ''):

		self.url = self.domain + requestUri;
		#print self.url
		self.contents = ''
		c = pycurl.Curl()
		c.setopt(c.URL, self.url)
		c.setopt(pycurl.COOKIEFILE, 'tmp/cookies.txt')
		c.setopt(pycurl.COOKIEJAR, 'tmp/cookies.txt')
		if self._post != None:
			c.setopt(c.POSTFIELDS, urllib.urlencode(self._post))
		c.setopt(c.WRITEFUNCTION, self.body_callback)
		c.perform()
		c.close()

class Client:

	devices = {}

	_status = True
	errors = []

	_types = {
		'0403'	: 	{
			'type'		: 'soundcard',
			'controller': 'soundcards'
			},
		'0280'	:	{
			'type'		: 'wifi',
			'controller': 'wifi'
			},
		'0300'	: 	{
			'type'		: 'videocard',
			'controller': 'videocards'
			}
	}

	def __init__(self,url = ''):
		self.request = Mycurl(url)

	def setNode(self,domain):
		self.request.setDomain(domain)
	
	#get the type from the Class id
	def getType(self, Class):
		Classes = self._types.keys()
		if Class in Classes:
			return self._types[Class]['type']
		return None

	#get the controller from the Class id
	def getController(self, Class):
		Classes = self._types.keys()
		if Class in Classes:
			return self._types[Class]['controller']
		return None

	#return a device object
	def getObject(self,Class):
		if Class == '0403':
			return Soundcard()
		elif Class == '0280':
			return Wifi()
		elif Class == '0300':
			return Videocard()
		else:
			return None

	#get the system kernel
	def getKernel(self):
		if not os.system('uname -r > tmp/temp'):
			f = open('tmp/temp','r')
			row = f.readline().replace("\n","")
			f.close();
			return row
		else:
			self._status = False
			self.errors.append('tmp folder not writable')

	#log in 
	def login(self, username, password):
		self.request.setPost({'username' : username, 'password' : password})
		self.request.perform('users/login/en')
		self.request.setPost(None)
		
	#log out
	def logout(self):
		self.request.perform('users/logout/en')

	#get info about the user logged
	def getUserInfo(self):
		self.request.perform('users/info/en')
		xmldoc = minidom.parseString(self.request.contents)
		status = xmldoc.getElementsByTagName("status")[0].childNodes[0].data
		
		username = ''
		token = ''
		groups = ''
		
		if status == 'logged':
			username = xmldoc.getElementsByTagName("username")[0].childNodes[0].data
			token = xmldoc.getElementsByTagName("token")[0].childNodes[0].data
			groups = xmldoc.getElementsByTagName("groups")[0].childNodes[0].data
			
		return {'status':status,'username':username,'token':token,'groups':groups}

	#return True if the user is logged, else return False
	def isLogged(self):
		info = self.getUserInfo()

		if info['status'] == 'logged':
			return True
		
		return False
	
	def createDevices(self):
		if not os.system('lspci -vmmnn > tmp/temp'):
			f = open('tmp/temp','r')

			while 1:
				row = f.readline()

				if not row:
					break

				#get the slot
				if row.find('Slot') != -1:

					#get the class
					row = f.readline().replace("\n","")
					if row:
						cl = re.match('Class\:(.*)\[(.*)\]',row,re.I)
						if cl:
							#get the object
							dev = self.getObject(cl.group(2))
							if dev:
								#set the type attribute of the device object
								dev.setType(self.getType(cl.group(2)))
								#get the vendorid
								row = f.readline().replace("\n","")
								if row:
									vn = re.match('Vendor\:(.*)\[(.*)\]',row,re.I)
									if vn:
										dev.setVendorId(vn.group(2).replace("\t",""))
										#get the productid
										row = f.readline().replace("\n","")
										if row:
											pr = re.match('Device\:(.*)\[(.*)\]',row,re.I)
											if pr:
												dev.setProductId(pr.group(2).replace("\t",""))
												dev.setModel(pr.group(1).replace("\t",""))

												dev.setKernel(self.getKernel())
												
												self.devices['p_' + dev.getVendorId() + ':' + dev.getProductId()] = [dev,cl.group(2),'insert','0']
												
											else:
												self._status = False
												self.errors.append('the lspci -vmmnn output is not a standard output, some products row not found')
									else:
										self._status = False
										self.errors.append('the lspci -vmmnn output is not a standard output, some vendors row not found')
						else:
							self._status = False
							self.errors.append('the lspci -vmmnn output is not a standard output, some class row not found')

			f.close();
		else:
			self._status = False
			self.errors.append('tmp folder not writable')

	#syncronize with the xml database
	def sync(self):
		#loop the found devices
		for key,dev in self.devices.iteritems():
			#find the class
			Class = dev[1]
			vendorid_productid = key[2:]
			#find the controller
			controller = self.getController(Class)

			#perform an http request
			self.request.contents = ''
			self.request.perform('download/' + controller + '/en')

			#parse the xml database
			xmldoc = minidom.parseString(self.request.contents)
			devices = xmldoc.getElementsByTagName("device")
			for device in devices:

				code = device.getElementsByTagName("vendorid_productid")[0]
				if code.hasChildNodes():
					if (code.childNodes[0].data == vendorid_productid):
						
						modelName = device.getElementsByTagName("model_name")[0].childNodes[0].data.encode('utf-8')
						interface = device.getElementsByTagName("interface")[0].childNodes[0].data.encode('utf-8')
						distribution = device.getElementsByTagName("distribution")[0].childNodes[0].data.encode('utf-8')
						idDevice = device.getElementsByTagName("id")[0].childNodes[0].data.encode('utf-8')
						works = device.getElementsByTagName("it_works")[0].childNodes[0].data.encode('utf-8')
						year = device.getElementsByTagName("year")[0].childNodes[0].data.encode('utf-8')

						if device.getElementsByTagName("description")[0].hasChildNodes():
							description = device.getElementsByTagName("description")[0].childNodes[0].data.encode('utf-8')
							dev[0].setDescription(description)
						
						#print modelName
						dev[0].setModel(modelName)
						dev[0].setInterface(interface)
						dev[0].addDistributions(distribution)
						dev[0].setHowItWorks(works)
						dev[0].setYear(year)
						dev[2] = 'update'
						dev[3] = idDevice

	def submit(self):
		for key,dev in self.devices.iteritems():
			dev[0].setPost()
			post = dev[0].getPost()

			#get the node controller
			controller = self.getController(dev[1])

			#get the user info
			info = self.getUserInfo()
			token = info['token']
				
			if dev[2] == 'update':
				
				post['id_hard'] = dev[3]
				post['updateAction'] = 'update'
				url = controller + '/update/en/' + token

			elif dev[2] == 'insert':

				post['insertAction'] = 'insert'
				url = controller + '/insert/en/' + token

			self.request.setPost(post)
			self.request.perform(url.encode('utf-8'))
			#print self.request.contents