def doPasteKey(self, arg): src = misc.getClipboardText() try: lines = src.split('\n') idurl = lines[0] keysrc = '\n'.join(lines[1:]) if idurl != nameurl.FilenameUrl(nameurl.UrlFilename(idurl)): idurl = '' keysrc = src except: lg.exc() idurl = '' keysrc = src if self.state not in self.output: self.output[self.state] = {'data': [('', 'black')]} self.output[self.state]['idurl'] = idurl self.output[self.state]['keysrc'] = keysrc if 'RECOVER' not in self.output: self.output['RECOVER'] = {'data': [('', 'black')]} if keysrc and idurl: self.output['RECOVER']['data'].append( ('private key and IDURL was loaded', 'green')) elif not idurl and keysrc: self.output['RECOVER']['data'].append( ('private key was loaded, provide correct IDURL now', 'blue'))
def clear(exclude_list=None): """ Clear the database, indexes and cached files from disk. """ global _Contact2IDURL global _IPPort2IDURL global _IDURL2Contacts global _IdentityCache global _IdentityCacheIDs global _IdentityCacheModifiedTime lg.out(4, "identitydb.clear") _IdentityCache.clear() _IdentityCacheIDs.clear() _IdentityCacheModifiedTime.clear() _Contact2IDURL.clear() _IPPort2IDURL.clear() _IDURL2Contacts.clear() iddir = settings.IdentityCacheDir() if not os.path.exists(iddir): return for file_name in os.listdir(iddir): path = os.path.join(iddir, file_name) if not os.access(path, os.W_OK): continue if exclude_list: idurl = nameurl.FilenameUrl(file_name) if idurl in exclude_list: continue os.remove(path) lg.out(6, 'identitydb.clear remove ' + path) fire_cache_updated_callbacks()
def doReadKey(self, arg): # keyfn = arg['keyfilename'] src = arg['keysrc'] lg.out(2, 'installer.doReadKey length=%s' % len(src)) # src = bpio.ReadBinaryFile(keyfn) if len(src) > 1024 * 10: self.doPrint(('file is too big for private key', 'red')) return try: lines = src.splitlines() idurl = lines[0].strip() keysrc = '\n'.join(lines[1:]) if idurl != nameurl.FilenameUrl(nameurl.UrlFilename(idurl)): idurl = '' keysrc = src except: lg.exc() idurl = '' keysrc = src if self.state not in self.output: self.output[self.state] = {'data': [('', 'black')]} self.output[self.state] = {'data': [('', 'black')]} self.output[self.state]['idurl'] = idurl self.output[self.state]['keysrc'] = keysrc if 'RECOVER' not in self.output: self.output['RECOVER'] = {'data': [('', 'black')]} if keysrc and idurl: self.output['RECOVER']['data'].append( ('private key and IDURL was loaded', 'green')) elif not idurl and keysrc: self.output['RECOVER']['data'].append( ('private key was loaded, provide correct IDURL now', 'blue'))
def init(): """ Need to call before all other methods. Check to exist and create a folder to keep all cached identities. """ lg.out(4, "identitydb.init") id_cache_dir = settings.IdentityCacheDir() if not os.path.exists(id_cache_dir): lg.out(8, 'identitydb.init create folder %r' % id_cache_dir) bpio._dir_make(id_cache_dir) # make sure to read and cache all known identities at startup for id_filename in os.listdir(id_cache_dir): idurl = nameurl.FilenameUrl(id_filename) get_ident(idurl)
def read_index(monthstr=None): global _IndexMonth global _IndexTotal #out(4, 'ratings.read_index') if monthstr is None: monthstr = time.strftime('%m%y') _IndexMonth.clear() _IndexTotal.clear() for idurl_filename in os.listdir(settings.RatingsDir()): idurl = nameurl.FilenameUrl(idurl_filename) if idurl is None: continue month = read_month_rating_dict(idurl, monthstr) total = read_total_rating_dict(idurl) _IndexMonth[idurl] = {'all': '0', 'alive': '0'} if month is None else month _IndexTotal[idurl] = {'all': '0', 'alive': '0'} if total is None else total
def UpdateCustomers(): """ Test packets after list of customers was changed. """ space = bpio._read_dict(settings.CustomersSpaceFile()) if space is None: printlog('UpdateCustomers ERROR space file can not be read') return customers_dir = settings.getCustomersFilesDir() if not os.path.exists(customers_dir): printlog('UpdateCustomers ERROR customers folder not exist') return remove_list = {} for customer_filename in os.listdir(customers_dir): onecustdir = os.path.join(customers_dir, customer_filename) if not os.path.isdir(onecustdir): remove_list[onecustdir] = 'is not a folder' continue idurl = nameurl.FilenameUrl(customer_filename) if idurl is None: remove_list[onecustdir] = 'wrong folder name' continue curspace = space.get(idurl, None) if curspace is None: remove_list[onecustdir] = 'is not a customer' continue for path in remove_list.keys(): if not os.path.exists(path): continue if os.path.isdir(path): try: bpio._dir_remove(path) printlog('UpdateCustomers ' + path + ' folder removed (%s)' % (remove_list[path])) except: printlog('UpdateCustomers ERROR removing ' + path) continue try: if not os.access(path, os.W_OK): os.chmod(path, 0o600) except: pass try: os.remove(path) printlog('UpdateCustomers ' + path + ' file removed (%s)' % (remove_list[path])) except: printlog('UpdateCustomers ERROR removing ' + path) printlog('UpdateCustomers ' + str(time.strftime("%a, %d %b %Y %H:%M:%S +0000")))
def SpaceTime(): """ Test all packets for each customer. Check if he use more space than we gave him and if packets is too old. """ printlog('SpaceTime ' + str(time.strftime("%a, %d %b %Y %H:%M:%S +0000"))) space = bpio._read_dict(settings.CustomersSpaceFile()) if space is None: printlog('SpaceTime ERROR can not read file ' + settings.CustomersSpaceFile()) return customers_dir = settings.getCustomersFilesDir() if not os.path.exists(customers_dir): printlog('SpaceTime ERROR customers folder not exist') return remove_list = {} used_space = {} for customer_filename in os.listdir(customers_dir): onecustdir = os.path.join(customers_dir, customer_filename) if not os.path.isdir(onecustdir): remove_list[onecustdir] = 'is not a folder' continue idurl = nameurl.FilenameUrl(customer_filename) if idurl is None: remove_list[onecustdir] = 'wrong folder name' continue curspace = space.get(idurl, None) if curspace is None: remove_list[onecustdir] = 'not found in space file' continue try: maxspaceV = int(curspace) except: remove_list[onecustdir] = 'wrong space value' continue timedict = {} sizedict = {} def cb(path, subpath, name): # if not os.access(path, os.R_OK | os.W_OK): # return False if not os.path.isfile(path): return True # if name in [settings.BackupIndexFileName(),]: # return False stats = os.stat(path) timedict[path] = stats.st_ctime sizedict[path] = stats.st_size bpio.traverse_dir_recursive(cb, onecustdir) currentV = 0 for path in sorted(timedict.keys(), key=lambda x: timedict[x], reverse=True): filesize = sizedict.get(path, 0) currentV += filesize if currentV < maxspaceV: continue try: os.remove(path) printlog('SpaceTime ' + path + ' file removed (cur:%s, max: %s)' % (str(currentV), str(maxspaceV))) except: printlog('SpaceTime ERROR removing ' + path) # time.sleep(0.01) used_space[idurl] = str(currentV) timedict.clear() sizedict.clear() for path in remove_list.keys(): if not os.path.exists(path): continue if os.path.isdir(path): try: bpio._dir_remove(path) printlog('SpaceTime ' + path + ' dir removed (%s)' % (remove_list[path])) except: printlog('SpaceTime ERROR removing ' + path) continue try: if not os.access(path, os.W_OK): os.chmod(path, 0o600) except: pass try: os.remove(path) printlog('SpaceTime ' + path + ' file removed (%s)' % (remove_list[path])) except: printlog('SpaceTime ERROR removing ' + path) del remove_list bpio._write_dict(settings.CustomersUsedSpaceFile(), used_space)