#! /usr/bin/python
# -*- coding: iso-8859-1 -*-

#####################################################
#    Copyright (C) 2006 by Cesar Kallas   	
#    cesarkallas@soulivre.org   
#    http://cesarkallas.soulivre.org
#                                                                          
#    This program 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 2 of the License, or     
#    (at your option) any later version.                                   
#                                                                          
#    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 platform, sys, socket, fcntl, string, sys, exceptions, array, struct
import Softwares


class Software(Softwares.RPM): pass
	
class Hardware(object):
	"""
		Retorna informacoes sobre o hardware
	"""
	def __init__(self):
		pass

	def arquitetura(self):
		return platform.machine()
		
	def kernel(self):
		return platform.release()
		
	def processador(self):
		return platform.processor()
		
	def sistemaOperacional(self):
		return platform.system()
	
	def distribuicao(self):
		return platform.dist()
	
	def hostname(self):
		return platform.node()

	# From: http://www.gpr.com.br/download/system_info/system_info_linux.tar.bz2
	#def network_support(self):
	def interfacesRede(self):
		"""
		Returns:
			- Interfaces (type: list)
			# - True if the OS has network support
			# - False otherwise
		"""
		n_ifaces = 8
		bytes = n_ifaces*32
		s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
		names = array.array('B', '\0' * bytes)

		outbytes = \
				 struct.unpack('iL',fcntl.ioctl(s.fileno(), 
												0x8912, # SIOCGIFCONF (get iface list)
												struct.pack('iL', bytes, names.buffer_info()[0])
												))[0]
		namestr = names.tostring()
		
		# Modified by Cesar Kallas
		# return interfaces, not True or False
		return [namestr[i:i+32].split('\0', 1)[0] for i in range(0, outbytes, 32)]
	#return len([namestr[i:i+32].split('\0', 1)[0] for i in range(0, outbytes, 32)]) > 0

	# from: http://forums.devshed.com/archive/t-51149/
	# Ethernet-card-address-Through-Python-or-C
	def __hex(self, n):
		return "%02x" % (ord(n))
		
	# from: http://forums.devshed.com/archive/t-51149/
	# Ethernet-card-address-Through-Python-or-C
	def mac_address(self, interface):
		SIOCGIFHWADDR =  0x8927
		skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		
		interface = string.strip(interface)
		interface = interface + '\0' * (32-len(interface))

		try:
			ret = fcntl.ioctl(skt .fileno(), SIOCGIFHWADDR, interface)
			addr = map(self.__hex, ret[18:24])
			mac = (':'.join(map(str, addr)))
		except IOError, e:
				mac = -1
		return mac
