o
    VAf/                     @   s4  d dl Z d dlZd dlZd dlZddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
 d Zd	Zd
ZdZdZG dd deZdd ZG dd deedZedkre Zeej eej eej eej eej eej eej eej edej d eej!e" ddd dS dS )    N   )	_fireinfo)bios)cpu)device)
hypervisor)networkz/sys/class/dmi/idz/etc/fireinfo-id)ZOEMzO.E.M.zo.e.m.zN/Azn/aZ12345Z54321Z202020ZChassisZchassiszDefault stringZEVALzNot ApplicableNoneemptyZSerialzSystem Serial NumberZSSNZXXXXXz$01010101-0101-0101-0101-010101010101z$00020003-0004-0005-0006-000700080009z$03000200-0400-0500-0006-000700080009z$11111111-1111-1111-1111-111111111111Z0000000Z00000000z$3b903780-4f79-1018-816e-aeb2724778a7)ZNAc                       s(   e Zd Z fddZ fddZ  ZS )	Singletonc                    s   t t| ||| d | _d S N)superr   __init__instance)clsnamebasesdict	__class__ 4/usr/lib/python3.10/site-packages/fireinfo/system.pyr   @   s   
zSingleton.__init__c                    s(   | j d u rtt| j|i || _ | j S r   )r   r   r   __call__)r   argskwr   r   r   r   D   s   
zSingleton.__call__)__name__
__module____qualname__r   r   __classcell__r   r   r   r   r   ?   s    r   c                 C   sb   t j| sdS zt| }|  W  d   W S 1 sw   Y  W dS  ty0   Y dS w )z!
		Read all data from filename.
	N)ospathexistsopenreadstripIOError)filenamefr   r   r   read_from_fileK   s   

(r(   c                   @   s  e Zd Zdd Zdd Zedd Zedd Zed	d
 Zedd Z	edd Z
edd Zedd Zedd Zdd Zedd Zedd Zedd Zedd Zedd  Zed!d" Zed#d$ Zed%d& Zd'd( Zed)d* Zed+d, Zd-S ).Systemc                 C   s>   t | | _ g | _|   t | _t | _| j | _	d S r   )
r   ZBIOSdevicesscanr   ZCPUr   Z
HypervisorZread_cpuinfo_System__cpuinfoselfr   r   r   r   Y   s   

zSystem.__init__c              
   C   s:  i }| j | j| j| j| j| j| j| jd|d< g |d< | jD ],}|j	
 |j
 |j 
 |j|jd}|d dkrC|j|d< |j|d< |d | q| j| jj| jj | jj| jj| jj| jj| jj| jjd		|d
< | jjrt| jj|d< | j | j | j | j d|d< | jrd| jji|d< t| j | j!|dS )N)modelvendorvirtuallanguagereleasekernel_releasememory	root_sizesystemr*   )	subsystemr0   r/   deviceclassdriverr8   Zpci	sub_model
sub_vendor)	archr0   r/   model_stringsteppingflagsspeedfamilycountr   bogomips)ZgreenZblueZorangeZredr   r0   r   )Zprofile_version	public_id
private_idprofile)"r/   r0   r1   r2   r3   r4   r5   r6   r*   r8   lowerr9   r:   r;   r<   appendr=   r   r>   r?   r@   rA   rB   rC   rD   r   Z	has_greenZhas_blueZ
has_orangeZhas_redr   PROFILE_VERSIONrE   rF   )r.   pr   dr   r   r   rG   e   s^   

	



zSystem.profilec                 C      t  d S )N   r   unamer-   r   r   r   r=      s   zSystem.archc                 C   s$   | j }|sdS t| }| S )zi
			This returns a globally (hopefully) ID to identify the host
			later (by request) in the database.
		(0000000000000000000000000000000000000000)	secret_idhashlibsha1encode	hexdigest)r.   rE   hr   r   r   rE      s
   zSystem.public_idc                 C   s:   d}t | jD ]}||7 }q|sdS t| }| S )z
			The private ID is built out of the _unique_id and used to
			permit a host to do changes on the database.

			No one could ever guess this without access to the host.
		 rQ   )reversedrR   rS   rT   rU   rV   )r.   rF   irW   r   r   r   rF      s   
zSystem.private_idc                 C   s,   t jtr
ttS t| j }|	 S )zX
			Read a "secret" ID from a file if available
			or calculate it from the hardware.
		)
r   r    r!   SECRET_ID_FILEr(   rS   rT   
_unique_idrU   rV   )r.   rW   r   r   r   rR      s   zSystem.secret_idc                 C   s   g }dD ]}t tjt|}|| qg }|D ]7}|du r qtD ]
}||kr,d} nq"|r<tD ]
}||v r;d} nq1|rItdd |D rId}|rP|| q|}|sh| j	}|rh|
dsh|tvrh|| |sst d}|| d|S )z
			This is a helper ID which is generated out of some hardware information
			that is considered to be constant over a PC's lifetime.

			None of the data here is ever sent to the server.
		)Zproduct_uuidZproduct_serialZchassis_serialNc                 s   s    | ]}|d kV  qdS )   ÿNr   ).0er   r   r   	<genexpr>  s    z$System._unique_id.<locals>.<genexpr>ZQM000z/opt/pakfire/db/uuid#)r(   r   r    joinSYS_CLASS_DMIrI   INVALID_ID_STRINGS_EXACT_MATCHINVALID_ID_STRINGSallroot_disk_serial
startswith)r.   ZidsfileidZ_idsrZ   rg   r   r   r   r\      sD   



zSystem._unique_idc                 C   s   d}t j|s
dS t|d)}| D ]}|dd\}}|dkr/|   W  d   S qW d   dS 1 s;w   Y  dS )zK
			Return the language code of IPFire or "unknown" if we cannot get it.
		z/var/ipfire/main/settingsunknownr=r   ZLANGUAGEN)r   r    r!   r"   	readlinessplitr$   )r.   r&   r'   linekeyvalr   r   r   r2     s   
"zSystem.languagec                 C   s   t dpdS )z(
			Return the system release string.
		z/etc/system-releaserk   r(   r-   r   r   r   r3   .     zSystem.releasec                 C   s   t dS )z#
			Return the bios vendor name.
		z/sys/class/dmi/id/bios_vendorrs   r-   r   r   r   bios_vendor5     zSystem.bios_vendorc                 C   s   z| j d }W n
 ty   Y dS w |drd|fS z|dd\}}W ||fS  tyG   |dr9d}|}n	d }|}Y ||fS Y ||fS w )	NHardware)NNzARM-VersatileZARM r   ZBCMZBroadcom)r,   KeyErrorrh   ro   
ValueError)r.   svmr   r   r   vendor_model_tuple<  s(   
	
zSystem.vendor_model_tuplec                 C   s    | du rdS d dd | D S )zE
			Will remove all non-printable characters from the given string
		NrX   c                 S   s   g | ]	}|t jv r|qS r   )stringZ	printable)r^   xr   r   r   
<listcomp>Y  s    z(System.escape_string.<locals>.<listcomp>)rb   )r{   r   r   r   escape_stringQ  s   zSystem.escape_stringc                 C   sd   d}dD ]}t tjt|}|r| |  S qtjdr'| jdd}n| 	 \}}| |S )z8
			Return the vendor string of this system (if any).
		N)Z
sys_vendorZboard_vendorZchassis_vendorz/proc/device-treerw   )
r(   r   r    rb   rc   r   r!   r,   getr~   )r.   retri   r}   r   r   r   r0   [  s   
zSystem.vendorc                 C   sd   d}dD ]}t tjt|}|r| |  S qt d}|r%|dd}|s-|  \}}| |S )z7
			Return the model string of this system (if any).
		N)Zproduct_nameZboard_modelZchassis_modelz/proc/device-tree/model rX   )r(   r   r    rb   rc   r   replacer~   )r.   r   ri   r|   r   r   r   r/   m  s   
zSystem.modelc                 C   sJ   t dd}|  }t| d W  d   S 1 sw   Y  dS )z0
			Return the amount of memory in kilobytes.
		z/proc/meminforl   r   N)r"   readliner$   intro   )r.   r'   	firstliner   r   r   r5     s   $zSystem.memoryc                 C   rM   )z(
			Return the kernel release string.
		   rO   r-   r   r   r   r4     rt   zSystem.kernel_releasec                 C   s   t dd_}| D ]Q}|sq
|dd\}}}}|dkr[|dks[|dd }|d	 d
kr<|dd	   W  d   S |d tjv rP|dd }|d tjv sC|  W  d   S q
W d   dS 1 sgw   Y  dS )z,
			Return the dev node of the root disk.
		z/proc/mountsrl   rx      /Zrootfs   NrK   )r"   rn   ro   r   Zdigits)r.   r'   rp   ZdevZ
mountpointZfsrestr   r   r   	root_disk  s$   "zSystem.root_diskc                 C   s\   d| j  }tj|sdS t|d}t| d d W  d   S 1 s'w   Y  dS )z5
			Return the size of the root disk in kilobytes.
		z/sys/block/%s/sizeNrl   i   i   )r   r   r    r!   r"   r   r   )r.   r    r'   r   r   r   r6     s   
$zSystem.root_sizec                 C   s:   z
t d| j }W n
 ty   Y dS w |r| S dS )z:
			Return the serial number of the root disk (if any).
		z/dev/%sN)r   Zget_harddisk_serialr   OSErrorr$   )r.   serialr   r   r   rg     s   zSystem.root_disk_serialc              	   C   sh   g | _ dtjfdtjff}|D ]"\}}tj|sqt|}|D ]}| j |tj	|| q!qdS )zS
			Scan for all devices (PCI/USB) in the system and append them
			to our list.
		z/sys/bus/pci/devicesz/sys/bus/usb/devicesN)
r*   r   Z	PCIDeviceZ	USBDevicer   r    r!   listdirrI   rb   )r.   Ztoscanr    r   Zdirlistdirr   r   r   r+     s   
zSystem.scanc                 C   s   | j jS )z;
			Say if the host is running in a virtual environment.
		)r   r1   r-   r   r   r   r1     rv   zSystem.virtualc                 C   s   t  S )z&
			Reference to the network class.
		)r   ZNetworkr-   r   r   r   r     rv   zSystem.networkN)r   r   r   r   rG   propertyr=   rE   rF   rR   r\   r2   r3   ru   r~   staticmethodr   r0   r/   r5   r4   r   r6   rg   r+   r1   r   r   r   r   r   r)   X   sR    P




9



	







r)   )	metaclass__main__z------------
z
------------
TrN   )Z	sort_keysindent)#rS   Zjsonr   r   rX   r   r   r   r   r   r   rJ   rc   r[   re   rd   typer   r(   objectr)   r   r{   printr=   r2   r3   ru   r5   Zkernelr   r6   r*   dumpsrG   r   r   r   r   <module>   sD      







