import subprocess as s import re import os.path import os class LliurexBerry: def __init__(self): self.excludepaht = ['/','/boot','/home'] self.pkg="lliurexberry-data" def get_devices(self): cmd = s.Popen(["lsblk","-o","RM,TYPE,NAME,SIZE,MODEL"],stdout=s.PIPE) list_output = cmd.stdout.readlines() list_output.pop(0) result = [] for item in list_output: aux = item.strip() aux_splited = re.match(r'\s*(\S*)\s*(\S*)\s*(\S*)\s*(\S*)\s*(.*)',aux) volume = {} if aux_splited.group(2) == 'disk': volume['name'] = aux_splited.group(3) volume['size'] = aux_splited.group(4) volume['model'] = aux_splited.group(5) cmd1 = s.Popen(["lsblk",os.path.join('/dev',volume['name']),"-o","MOUNTPOINT"],stdout=s.PIPE) mountpointsdevice = map(str.strip,cmd1.stdout.readlines()) found = False for point in self.excludepaht: if point in mountpointsdevice: found = True if not found: result.append(volume) return {'status':True,'msg':result} def burn_llxberry(self,device): """ record /usr/share/n4d-lliurexberry/llxberry.img on device """ token = "/var/run/remove_partitions" f = open(token,'w') f.close() cmd = s.Popen(['create_llxberry_sd',device,'--ignore-removable'],stdout=s.PIPE) output = cmd.stdout.readlines() if output[-1].strip() == "Ok": status = True else: status = False return {"status": status,"msg":"".join(output)} def download_package_data(self): try: s.check_call(['apt-cache','show',self.pkg]) except: try: s.check_call(['apt-get','update']) s.check_call(['apt-cache','show',self.pkg]) except: return {"status": False, "msg": "Package unavailable, check your sources.list !"} try: s.check_call(['apt-get','install',self.pkg]) return {"status": True, "msg": "Installed"} except Exception as e: return {"status": False, "msg": str(e)} #def download_package_data(self) def check_for_data_package(self): try: with open(os.devnull,'w') as devnull: output=s.check_output(['dpkg-query','-W','-f=${Status}',self.pkg],stderr=devnull) regexp='\s*install\s+ok\s+installed' reg1=re.compile(regexp) if reg1.match(output): return {"status": True, "msg": "True"} else: return {"status": False, "msg": "False, the status is: "+output} except Exception as e: return {"status": False, "msg": "False"} #def check_for_data_package(self) if __name__ == "__main__": x = LliurexBerry() for z in x.get_removable_devices(): print z