def __init__(self, path, keyPath=None): """""" self.path = path self.dic = {} self.keyPath = path+'.key' if keyPath is None else keyPath if not Io.file_exists(self.keyPath) : kg = KeyGen() Io.set_data(self.keyPath, kg.key) if not Io.file_exists(path) : self.set('profile' , 'default', 'main') self.set('key' ,kg.key,'default.keys') self.set('mark' ,kg.mark,'default.keys') self.set('salt' ,'-*-ImpraStorage-*-','default.keys') self.save() self.read()
def on_proceed(self, btn): """""" if btn.get_label() == conf.GUI_LABEL_OK: btn.set_label(conf.GUI_LABEL_PROCEED) self.PROCEED = False self.pb.hide() self.show_log() else: if not self.PROCEED: self.PROCEED = True self.STOPPED = False btn.set_sensitive(False) self.app.setDestFile( self.get('filechooserbutton3').get_filename()) if not Io.file_exists(self.app.dst) or self.warnDialog( 'file ' + self.app.dst + ' already exists', 'Overwrite file ?'): self.pb = self.get('progressbar1') self.pb.set_fraction(0) self.pb.show() self.pb.pulse() btn.set_sensitive(True) btn.set_label(conf.GUI_LABEL_CANCEL) self.clear_log(self.get('checkbutton3')) self.show_log() self.launch_thread() else: self.on_proceed_end(True) else: self.halt_thread()
def on_proceed(self, btn): """""" if btn.get_label() == conf.GUI_LABEL_OK : btn.set_label(conf.GUI_LABEL_PROCEED) self.PROCEED = False self.pb.hide() self.show_log() else : if not self.PROCEED : self.PROCEED = True self.STOPPED = False btn.set_sensitive(False) self.app.setDestFile(self.get('filechooserbutton3').get_filename()) if not Io.file_exists(self.app.dst) or self.warnDialog('file '+self.app.dst+' already exists', 'Overwrite file ?'): self.pb = self.get('progressbar1') self.pb.set_fraction(0) self.pb.show() self.pb.pulse() btn.set_sensitive(True) btn.set_label(conf.GUI_LABEL_CANCEL) self.clear_log(self.get('checkbutton3')) self.show_log() self.launch_thread() else : self.on_proceed_end(True) else : self.halt_thread()
def onCommandKey(self): """""" if int(self.o.length) >= 128 and int(self.o.length) <= 4096: self.parser.print_header() if not self.o.outputfile: self.o.outputfile = self.home + '.kirmah' + Sys.sep + '.default.key' kg = KeyGen(int(self.o.length)) done = True if Io.file_exists(self.o.outputfile) and not self.o.force: Sys.pwarn((( 'the key file ', (self.o.outputfile, Sys.Clz.fgb3), ' already exists !' ), 'if you rewrite this file, all previous files encrypted with the corresponding key will be unrecoverable !' )) done = Sys.pask('Are you sure to rewrite this file') self.stime = Sys.datetime.now() if done: Io.set_data(self.o.outputfile, kg.key) Sys.pstep('Generate key file', self.stime, done) if done: Sys.print(' ' * 5 + Sys.realpath(self.o.outputfile), Sys.Clz.fgB1, True) else: self.parser.error_cmd( (('invalid option ', ('-l, --length', Sys.Clz.fgb3), ' value (', ('128', Sys.Clz.fgb3), ' to ', ('4096', Sys.Clz.fgb3), ')'), ))
def onCommandDec(self): """""" done = True if self.o.outputfile is None : self.o.outputfile = self.a[1][:-4] if self.a[1][-4:] == Kirmah.EXT else self.a[1] if not Sys.g.QUIET : self.parser.print_header() if Io.file_exists(self.o.outputfile) and not self.o.force: Sys.pwarn((('the file ',(self.o.outputfile, Sys.Clz.fgb3), ' already exists !'),)) done = Sys.pask('Are you sure to rewrite this file') self.stime = Sys.datetime.now() if done : try : if (self.o.multiprocess is not None and not represents_int(self.o.multiprocess)) or (not self.o.multiprocess is None and not(int(self.o.multiprocess)>=2 and int(self.o.multiprocess) <=8)) : self.parser.error_cmd((('invalid option ',('-j, --multiprocess', Sys.Clz.fgb3), ' value (', ('2',Sys.Clz.fgb3),' to ', ('8',Sys.Clz.fgb3),')'),)) nproc = int(self.o.multiprocess) if not self.o.multiprocess is None and int(self.o.multiprocess)>=2 and int(self.o.multiprocess) <=8 else 1 Sys.ptask() key = Io.get_data(self.o.keyfile) km = Kirmah(key) km.decrypt(self.a[1], self.o.outputfile, nproc) except BadKeyException: done = False Sys.pwarn((('BadKeyException : ',('wrong key ',Sys.CLZ_WARN_PARAM), ' !'),), False) if not Sys.g.QUIET : self.onend_cmd('Kirmah Decrypt', self.stime, done, self.o.outputfile)
def getBackupAddMap(self): """""" data = None if Io.file_exists(self.addmapPath + Kirmah.EXT): call = " ".join( [ Sys.executable, "kirmah-cli.py", "dec", "-qf", self.addmapPath + Kirmah.EXT, "-z", "-r", "-m", "-o", self.addmapPath, "-k", self.idxu.index.keyPath, ] ) print(call) Sys.sysCall(call) data = jloads(Io.get_data(self.addmapPath)) Io.removeFile(self.addmapPath) return data
def on_keylen_changed(self, spin): """""" filename = self.get('filechooserbutton1').get_filename() if Io.file_exists(filename): self.app.createNewKey(filename, int(self.get('spinbutton1').get_value())) if self.start: self.refreshProceed()
def selectKey(self, filename): """""" print('selectKey : ') print(filename) if not Io.file_exists(filename): raise FileNotFoundException(filename) self.kpath = filename
def getKeyInfos(self, filename=None): """""" if filename is None : filename = self.getDefaultKeyPath() if not Io.file_exists(filename): raise FileNotFoundException(filename) k = Io.get_data(filename) s = len(k) m = KeyGen(s).getMark(k) return k, s, m
def createDefaultKeyIfNone(self): """""" kpath = self.getDefaultKeyPath() if not Io.file_exists(kpath): if Sys.isUnix() : if not Sys.isdir(conf.DEFVAL_UKEY_PATH) : Sys.mkdir_p(conf.DEFVAL_UKEY_PATH) Io.set_data(kpath, KeyGen(conf.DEFVAL_UKEY_LENGHT).key) self.selectKey(kpath)
def createDefaultKeyIfNone(self): """""" kpath = self.getDefaultKeyPath() if not Io.file_exists(kpath): if Sys.isUnix(): if not Sys.isdir(conf.DEFVAL_UKEY_PATH): Sys.mkdir_p(conf.DEFVAL_UKEY_PATH) Io.set_data(kpath, KeyGen(conf.DEFVAL_UKEY_LENGHT).key) self.selectKey(kpath)
def get(self, forceRefresh=False): """""" self.switchFileAccount(self.conf.profile) index = None uid = self.conf.get('uid' ,'index') date = self.conf.get('date' ,'index') tstamp = self.conf.get('time' ,'index') refresh = forceRefresh delta = None if tstamp is None else Sys.datetime.now() - Sys.datetime.strptime(tstamp[:-7], '%Y-%m-%d %H:%M:%S') if not refresh and tstamp is not None and delta < Sys.timedelta(minutes = 3) : # getFromFile if uid != None and Io.file_exists(self.pathIdx): # int(self.idx) == int(uid) self.idx = uid Sys.pwlog([(' Get index from cache ' , Const.CLZ_7), ('(' , Const.CLZ_0), (str(int(self.idx)) , Const.CLZ_2), (')' , Const.CLZ_0, True)]) else: refresh = True else: refresh = True self.irefresh = refresh if refresh : Sys.pwlog([(' Checking index...', Const.CLZ_0, True)]) self._getId() if self.idx : if int(self.idx) != int(uid) or not Io.file_exists(self.pathIdx): Sys.pwlog([(' Refreshing index (local:', Const.CLZ_0), (str(int(uid)) , Const.CLZ_2), (' / remote:' , Const.CLZ_0), (str(int(self.idx)) , Const.CLZ_1), (')' , Const.CLZ_0, True)]) date = self.ih.headerField(self.idx, 'date', True) self.conf.sets((['uid' , str(int(self.idx)) , 'index'], ['date' , date , 'index'], ['time' , str(Sys.datetime.now()), 'index'])) self._saveLocalIndex() else : Sys.pwlog([(' Get index from cache ' , Const.CLZ_7), ('(' , Const.CLZ_0), (str(int(self.idx)) , Const.CLZ_2), (')' , Const.CLZ_0, True)]) self.conf.set('time',str(Sys.datetime.now()),'index') self.build()
def getKeyInfos(self, filename=None): """""" if filename is None: filename = self.getDefaultKeyPath() if not Io.file_exists(filename): raise FileNotFoundException(filename) k = Io.get_data(filename) s = len(k) m = KeyGen(s).getMark(k) return k, s, m
def onCommandDec(self): """""" done = True if self.o.outputfile is None: self.o.outputfile = self.a[1][:-4] if self.a[1][ -4:] == Kirmah.EXT else self.a[1] if not Sys.g.QUIET: self.parser.print_header() if Io.file_exists(self.o.outputfile) and not self.o.force: Sys.pwarn((('the file ', (self.o.outputfile, Sys.Clz.fgb3), ' already exists !'), )) done = Sys.pask('Are you sure to rewrite this file') self.stime = Sys.datetime.now() if done: try: if (self.o.multiprocess is not None and not represents_int(self.o.multiprocess)) or ( not self.o.multiprocess is None and not (int(self.o.multiprocess) >= 2 and int(self.o.multiprocess) <= 8)): self.parser.error_cmd( (('invalid option ', ('-j, --multiprocess', Sys.Clz.fgb3), ' value (', ('2', Sys.Clz.fgb3), ' to ', ('8', Sys.Clz.fgb3), ')'), )) nproc = int(self.o.multiprocess ) if not self.o.multiprocess is None and int( self.o.multiprocess) >= 2 and int( self.o.multiprocess) <= 8 else 1 Sys.ptask() key = Io.get_data(self.o.keyfile) km = Kirmah(key) km.decrypt(self.a[1], self.o.outputfile, nproc) except BadKeyException: done = False Sys.pwarn((('BadKeyException : ', ('wrong key ', Sys.CLZ_WARN_PARAM), ' !'), ), False) if not Sys.g.QUIET: self.onend_cmd('Kirmah Decrypt', self.stime, done, self.o.outputfile)
def createDefaultKeyIfNone(self): """""" kpath = self.getDefaultKeyPath() if not Io.file_exists(kpath): #if Sys.isUnix() : if not Sys.isdir(conf.DEFVAL_UKEY_PATH) : Sys.mkdir_p(conf.DEFVAL_UKEY_PATH) k = KeyGen(conf.DEFVAL_UKEY_LENGHT) print(k) content = k.key print('content') Io.set_data(kpath, content) print('set content') self.selectKey(kpath)
def onCommandEnc(self): """""" done = True if self.o.outputfile is None : self.o.outputfile = Sys.basename(self.a[1]) if self.o.outputfile[-len(Kirmah.EXT):] != Kirmah.EXT : print(self.o.outputfile[-len(Kirmah.EXT):]) self.o.outputfile += Kirmah.EXT print(self.o.outputfile) d = self.getDefaultOption((self.o.compress,self.o.fullcompress,self.o.nocompress)) compress = (KirmahHeader.COMP_END if d == 0 or (d is None and Io.is_binary(self.a[1])) else (KirmahHeader.COMP_ALL if d==1 or d is None else KirmahHeader.COMP_NONE)) random = True if (self.o.random is None and self.o.norandom is None) or self.o.random else False mix = True if (self.o.mix is None and self.o.nomix is None) or self.o.mix else False if (self.o.multiprocess is not None and not represents_int(self.o.multiprocess)) or (not self.o.multiprocess is None and not(int(self.o.multiprocess)>=2 and int(self.o.multiprocess) <=8)) : self.parser.error_cmd((('invalid option ',('-j, --multiprocess', Sys.Clz.fgb3), ' value (', ('2',Sys.Clz.fgb3),' to ', ('8',Sys.Clz.fgb3),')'),)) nproc = int(self.o.multiprocess) if not self.o.multiprocess is None and int(self.o.multiprocess)>=2 and int(self.o.multiprocess) <=8 else 1 if not Sys.g.QUIET : self.parser.print_header() if Io.file_exists(self.o.outputfile) and not self.o.force: Sys.pwarn((('the file ',(self.o.outputfile, Sys.Clz.fgb3), ' already exists !'),)) done = Sys.pask('Are you sure to rewrite this file') self.stime = Sys.datetime.now() if done : try : Sys.ptask() key = Io.get_data(self.o.keyfile) km = Kirmah(key, None, compress, random, mix) km.encrypt(self.a[1], self.o.outputfile, nproc) except Exception as e : done = False print(e) raise e pass if not Sys.g.QUIET : self.onend_cmd('Kirmah Encrypt', self.stime, done, self.o.outputfile)
def onCommandAdd(self): """""" if not len(self.a) > 1: self.parser.error_cmd((self.a[0] + " command need one argument",), True) else: vfile = self.a[1] if not Io.file_exists(vfile): if Sys.isdir(vfile): for f in Sys.listdir(vfile): if not Sys.isdir(f): label, ext = Sys.getFileExt(Sys.basename(f)) if self.o.category is None: self.o.category = "" done = self.impst.addFile(vfile + Sys.sep + f, label, self.o.category) if done: self.Cli.printLineSep(Const.LINE_SEP_CHAR, Const.LINE_SEP_LEN) Sys.dprint(" ", end="") Sys.echo(" == OK == ", Sys.Clz.bg2 + Sys.Clz.fgb7) Sys.dprint() else: self.parser.error_cmd((self.a[0] + " is not a file or a directory",), True) else: if not len(self.a) > 2: label = Sys.basename(vfile) else: label = self.a[2] if self.o.category is None: self.o.category = "" Sys.clear() self.pheader() done = self.impst.addFile(vfile, label, self.o.category) if done: self.Cli.printLineSep(Const.LINE_SEP_CHAR, Const.LINE_SEP_LEN) Sys.dprint(" ", end="") Sys.echo(" == OK == ", Sys.Clz.bg2 + Sys.Clz.fgb7) Sys.dprint() else: self.Cli.printLineSep(Const.LINE_SEP_CHAR, Const.LINE_SEP_LEN) Sys.dprint(" ", end="") Sys.echo(" == KO == ", Sys.Clz.bg1 + Sys.Clz.fgb7) Sys.dprint()
def decrypt(self, fromPath=None): """""" done = False try : if fromPath is None : fromPath = self.path toPath = fromPath[:-len(Kirmah.EXT)] if fromPath.endswith(Kirmah.EXT) else fromPath+'.dump' if Io.file_exists(fromPath) : Sys.pwlog([(' Decrypt Index... ' , Const.CLZ_0, True)]) call = ' '.join([Sys.executable, 'kirmah-cli.py', 'dec', '-qfj2' if Sys.isUnix() else '-qf', fromPath, '-z', '-r', '-m', '-o', toPath, '-k', self.keyPath ]) print(call) Sys.sysCall(call) data = jloads(Io.get_data(toPath)) Io.removeFile(toPath) else : data = {} done = True except ValueError as e: raise BadKeyException(e) Sys.pwlog([(' done'if done else ' ko' , Const.CLZ_2 if done else Const.CLZ_1, True)]) return data
def __init__(self, home, path, parser, Cli, a, o): """""" self.parser = parser self.Cli = Cli self.a = a self.o = o self.home = home self.stime = Sys.datetime.now() self.account = "default" self.rootBox = "__impra__2" self.uid = "0" self.date = "" if not Io.file_exists("impra2.ini" + Kirmah.EXT): print("ini file not exist") if len(self.a) > 0 and self.a[0] == "conf" and self.o.save: kg = KeyGen() Io.set_data("impra2.ini.key", kg.key) self.ini = KiniFile("impra2.ini") self.ini.set("key", kg.key, self.account + ".keys") self.ini.set("mark", kg.mark, self.account + ".keys") self.ini.set("salt", "-¤-ImpraStorage-¤-", self.account + ".keys") else: self.needConfig() else: if not (len(self.a) > 0 and self.a[0] == "conf"): self.ini = KiniFile("impra2.ini") self.impst = ImpraStorage(ImpraConf(self.ini), wkdir=path) self.uid = self.impst.idxu.conf.get("uid", "index") self.date = self.impst.idxu.conf.get("date", "index") self.account = self.impst.idxu.conf.get("user", "imap") self.rootBox = self.impst.rootBox if self.impst.idxu.index != None: noData = self.impst.idxu.index.isEmpty() if self.uid == None or noData: self.uid = "EMPTY" if self.date == None or noData: self.date = "" else: self.ini = KiniFile("impra2.ini")
def __init__(self, key, path, dicCategory={}, accountList={}, emit=False): """Initialize the index with rsa and encoded data :Parameters: `key` : str appropriate key to decrypt/encrypt data `mark` : str appropriate mark to check correct key `encdata` : str initial content of the index encrypted with Kirmah Algorythm and representing a dic index as json string """ self.pathPlain = path[:-len(Kirmah.EXT)] self.keyPath = self.pathPlain+self.KEY_EXT self.path = path Io.set_data(self.keyPath, key) self.dic = {} self.acclist = accountList encdata = Io.get_data(path, True) if Io.file_exists(path) else b'' if encdata == b'' : self.dic = {} self.id = 1 else : self.dic = self.decrypt(path) l = [self.dic[k][self.UID] for i, k in enumerate(self.dic) if not k.startswith(self.SEP_KEY_INTERN)] if len(l) > 0 : self.id = max(l)+1 else: self.id = 1 for k in dicCategory : if k == 'users' : for k1 in dicCategory[k]: if self.SEP_KEY_INTERN+k in self.dic: if k1 not in self.dic[self.SEP_KEY_INTERN+k]: self.dic[self.SEP_KEY_INTERN+k][k1] = dicCategory[k][k1] else : if not self.SEP_KEY_INTERN+k in self.dic: self.dic[self.SEP_KEY_INTERN+k] = dicCategory[k]
def onCommandKey(self): """""" if int(self.o.length) >= 128 and int(self.o.length) <= 4096 : self.parser.print_header() if not self.o.outputfile : self.o.outputfile = self.home+'.kirmah'+Sys.sep+'.default.key' kg = KeyGen(int(self.o.length)) done = True if Io.file_exists(self.o.outputfile) and not self.o.force : Sys.pwarn((('the key file ',(self.o.outputfile, Sys.Clz.fgb3), ' already exists !'), 'if you rewrite this file, all previous files encrypted with the corresponding key will be unrecoverable !')) done = Sys.pask('Are you sure to rewrite this file') self.stime = Sys.datetime.now() if done : Io.set_data(self.o.outputfile, kg.key) Sys.pstep('Generate key file', self.stime, done) if done : Sys.print(' '*5+Sys.realpath(self.o.outputfile), Sys.Clz.fgB1, True) else : self.parser.error_cmd((('invalid option ',('-l, --length', Sys.Clz.fgb3), ' value (', ('128',Sys.Clz.fgb3),' to ', ('4096',Sys.Clz.fgb3),')'),))
def __init__(self, path, remote=False, rwargs=None, thread=None, loglvl=Const.LOG_DEFAULT): """""" AbstractCli.__init__(self, conf, self) Cli.HOME = conf.DEFVAL_USER_PATH Cli.DIRKEY = Cli.HOME + '.' + conf.PRG_NAME.lower() + Sys.sep if not Sys.isUnix(): CHQ = '"' self.HOME = 'C:' + Sys.sep + conf.PRG_NAME.lower() + Sys.sep self.DIRKEY = self.HOME + 'keys' + Sys.sep Sys.mkdir_p(Cli.DIRKEY) gpData = OptionGroup(self.parser, '') gpData.add_option('-a', '--fullcompress', action='store_true') gpData.add_option('-z', '--compress', action='store_true') gpData.add_option('-Z', '--nocompress', action='store_true') gpData.add_option('-r', '--random', action='store_true') gpData.add_option('-R', '--norandom', action='store_true') gpData.add_option('-m', '--mix', action='store_true') gpData.add_option('-M', '--nomix', action='store_true') gpData.add_option('-j', '--multiprocess', action='store') gpData.add_option('-k', '--keyfile', action='store') gpData.add_option('-l', '--length', action='store', default=1024) gpData.add_option('-p', '--parts', action='store', default=22) gpData.add_option('-o', '--outputfile', action='store') self.parser.add_option_group(gpData) # rewrite argv sended by remote if rwargs is not None: import sys sys.argv = rwargs (o, a) = self.parser.parse_args() Sys.g.QUIET = o.quiet Sys.g.THREAD_CLI = thread Sys.g.GUI = thread is not None init(conf.PRG_NAME, o.debug, remote, not o.no_color, loglvl) if not a: try: if not o.help or not o.version: self.parser.error_cmd(('no command specified', ), True) else: Sys.clear() Cli.print_help() except: if not o.version: self.parser.error_cmd(('no command specified', ), True) else: Cli.print_header() else: if a[0] == 'help': Sys.clear() Cli.print_help() elif a[0] in ['key', 'enc', 'dec', 'split', 'merge']: app = CliApp(self.HOME, path, self, a, o) if a[0] == 'key': app.onCommandKey() else: if not len(a) > 1: self.parser.error_cmd( (('an ', ('inputFile', Sys.Clz.fgb3), ' is required !'), ), True) elif not Io.file_exists(a[1]): self.parser.error_cmd( (('the file ', (a[1], Sys.Clz.fgb3), ' doesn\'t exists !'), ), True) elif a[0] == 'enc': app.onCommandEnc() elif a[0] == 'dec': app.onCommandDec() elif a[0] == 'split': app.onCommandSplit() elif a[0] == 'merge': app.onCommandMerge() Sys.dprint('PUT END SIGNAL') if Sys.g.LOG_QUEUE is not None: Sys.g.LOG_QUEUE.put(Sys.g.SIGNAL_STOP) else: self.parser.error_cmd( (('unknow command ', (a[0], Sys.Clz.fgb3)), )) if not o.quiet: Sys.dprint()
def hasBackupAddMap(self): """""" return Io.file_exists(self.addmapPath + Kirmah.EXT)
def __init__(self, path, remote=False, rwargs=None, thread=None, loglvl=Const.LOG_DEFAULT): """""" AbstractCli.__init__(self, conf, self) Cli.HOME = conf.DEFVAL_USER_PATH Cli.DIRKEY = Cli.HOME + "." + conf.PRG_NAME.lower() + Sys.sep if not Sys.isUnix(): CHQ = '"' self.HOME = "C:" + Sys.sep + conf.PRG_NAME.lower() + Sys.sep self.DIRKEY = self.HOME + "keys" + Sys.sep Sys.mkdir_p(Cli.DIRKEY) gpData = OptionGroup(self.parser, "") gpData.add_option("-a", "--fullcompress", action="store_true") gpData.add_option("-z", "--compress", action="store_true") gpData.add_option("-Z", "--nocompress", action="store_true") gpData.add_option("-r", "--random", action="store_true") gpData.add_option("-R", "--norandom", action="store_true") gpData.add_option("-m", "--mix", action="store_true") gpData.add_option("-M", "--nomix", action="store_true") gpData.add_option("-j", "--multiprocess", action="store") gpData.add_option("-k", "--keyfile", action="store") gpData.add_option("-l", "--length", action="store", default=1024) gpData.add_option("-p", "--parts", action="store", default=22) gpData.add_option("-o", "--outputfile", action="store") self.parser.add_option_group(gpData) # rewrite argv sended by remote if rwargs is not None: import sys sys.argv = rwargs (o, a) = self.parser.parse_args() Sys.g.QUIET = o.quiet Sys.g.THREAD_CLI = thread Sys.g.GUI = thread is not None init(conf.PRG_NAME, o.debug, remote, not o.no_color, loglvl) if not a: try: if not o.help or not o.version: self.parser.error_cmd(("no command specified",), True) else: Sys.clear() Cli.print_help() except: if not o.version: self.parser.error_cmd(("no command specified",), True) else: Cli.print_header() else: if a[0] == "help": Sys.clear() Cli.print_help() elif a[0] in ["key", "enc", "dec", "split", "merge"]: app = CliApp(self.HOME, path, self, a, o) if a[0] == "key": app.onCommandKey() else: if not len(a) > 1: self.parser.error_cmd((("an ", ("inputFile", Sys.Clz.fgb3), " is required !"),), True) elif not Io.file_exists(a[1]): self.parser.error_cmd((("the file ", (a[1], Sys.Clz.fgb3), " doesn't exists !"),), True) elif a[0] == "enc": app.onCommandEnc() elif a[0] == "dec": app.onCommandDec() elif a[0] == "split": app.onCommandSplit() elif a[0] == "merge": app.onCommandMerge() Sys.dprint("PUT END SIGNAL") if Sys.g.LOG_QUEUE is not None: Sys.g.LOG_QUEUE.put(Sys.g.SIGNAL_STOP) else: self.parser.error_cmd((("unknow command ", (a[0], Sys.Clz.fgb3)),), True) if not o.quiet: Sys.dprint()
def onCommandSplit(self): """""" done = True Sys.cli_emit_progress(1) if not self.o.parts is None and not(int(self.o.parts)>=12 and int(self.o.parts) <=62) : self.parser.error_cmd((('invalid option ',('-p, --parts', Sys.Clz.fgb3), ' value (', ('12',Sys.Clz.fgb3),' to ', ('62',Sys.Clz.fgb3),')'),)) else : self.o.parts = int(self.o.parts) if not Sys.g.QUIET : self.parser.print_header() if self.o.outputfile is not None : if self.o.outputfile[-5:]!='.tark' : self.o.outputfile += '.tark' if Io.file_exists(self.o.outputfile) and not self.o.force: Sys.pwarn((('the file ',(self.o.outputfile, Sys.Clz.fgb3), ' already exists !'),)) done = Sys.pask('Are you sure to rewrite this file') self.stime = Sys.datetime.now() if done : try : Sys.ptask() Sys.cli_emit_progress(2) key = Io.get_data(self.o.keyfile) km = Kirmah(key) hlst = km.ck.getHashList(Sys.basename(self.a[1]), self.o.parts, True) Sys.cli_emit_progress(3) kcf = km.splitFile(self.a[1], hlst) t = int(Sys.time()) times = (t,t) p = 85 Sys.cli_emit_progress(p) Io.touch(kcf, times) frav = 0.24 for row in hlst['data']: p += frav Io.touch(row[1]+km.EXT,times) Sys.cli_emit_progress(p) if self.o.outputfile is not None : d = Sys.datetime.now() if Sys.g.DEBUG : Sys.wlog(Sys.dprint()) Sys.ptask('Preparing tark file') hlst['data'] = sorted(hlst['data'], key=lambda lst: lst[4]) with tarfile.open(self.o.outputfile, mode='w') as tar: tar.add(kcf, arcname=Sys.basename(kcf)) p = 90 for row in hlst['data']: tar.add(row[1]+km.EXT, arcname=Sys.basename(row[1]+km.EXT)) p += frav Sys.cli_emit_progress(p) Sys.pstep('Packing destination file', d, True) d = Sys.datetime.now() Sys.ptask('Finalize') for row in hlst['data']: Io.removeFile(row[1]+km.EXT) p += frav Sys.cli_emit_progress(p) Io.removeFile(kcf) Sys.pstep('Cleaning', d, True) except Exception as e : done = False if Sys.g.DEBUG : print('split exception') print(e) #~ raise e elif not Sys.g.QUIET : Sys.pwarn((str(e),)) if not Sys.g.QUIET: Sys.cli_emit_progress(100) self.onend_cmd('Kirmah Split', self.stime, done, self.o.outputfile)
def sendFile(self, data, retry=False): """""" done = None key = None if data is not None: key, label, ext, count, catg, hlst, usr, ownerHash, sha256, size = data self.idxu.index.addUser(usr, ownerHash) account = self.idxu.switchFileAccount() sendIds = [] cancel = False d = None Sys.cli_emit_progress(0) Sys.sleep(0.2) if not retry: Sys.pwlog( [ (" Sending... ", Const.CLZ_7), (" (", Const.CLZ_0), ( " ~" + Sys.readableBytes(Sys.getsize(self.outbox + hlst["data"][0][1] + Kirmah.EXT)), Const.CLZ_3, ), (" per msg ) ", Const.CLZ_0, True), ] ) else: Sys.pwlog( [ (" Retry sending last file... ", Const.CLZ_0), (label + ext, Const.CLZ_7), (" (" + catg + ")", Const.CLZ_3, True), ] ) ignore = False for i, row in enumerate(hlst["data"]): """""" if retry: if not Io.file_exists(self.outbox + row[1] + Kirmah.EXT): continue elif not ignore: Sys.pwlog([(" Ignoring file 1 to " + str(i), Const.CLZ_1, True)]) ignore = True d = Sys.datetime.now() msg = self.idxu.mb.build(usr, "all", hlst["head"][2], self.outbox + row[1] + Kirmah.EXT) try: mid = self.idxu.ih.send(msg.as_string(), self.rootBox) except Exception as e: Sys.pwarn((("addFile : ", (str(e), Sys.CLZ_WARN_PARAM), " !"),)) Sys.echo("waiting 5 s and retry") Sys.sleep(5) # force reconnect self.impst.idxu.switchFileAccount(account, True) # retry mid = self.idxu.ih.send(msg.as_string(), self.rootBox) finally: if not mid is None: status, resp = self.idxu.ih.fetch(mid[1], "(UID BODYSTRUCTURE)", True) if status == self.idxu.ih.OK: sendIds.append((mid[1], row)) Sys.pwlog( [ (" part ", Const.CLZ_0), (str(row[0]).rjust(2, "0"), Const.CLZ_2), (" sent as msg ", Const.CLZ_0), (str(mid[1]).rjust(5, "0"), Const.CLZ_1), (" (", Const.CLZ_7), (str(int(row[4]) + 1).rjust(2, "0"), Const.CLZ_2), ("/", Const.CLZ_7), (str(count), Const.CLZ_3), (") in ", Const.CLZ_7), (Sys.getDelta(d), Const.CLZ_4, True), ] ) Sys.cli_emit_progress(int((i + 1) * 100 / len(hlst["data"]))) Sys.removeFile(self.outbox + row[1] + Kirmah.EXT) else: Sys.pwarn((("error occured when sending part ", (row[0], Sys.Clz.fgb3), " !"),)) diff = self.checkSendIds(sendIds, hlst["head"][2]) if len(sendIds) == count or retry: self.idxu.get(True) self.idxu.index.add(key, label, hlst["head"][1], ext, ownerHash, catg, sha256, size, account) done = self.idxu.update() Io.removeFile(self.addmapPath + Kirmah.EXT) # resending missing parts else: Sys.pwarn((("TODO => must resending ", ("missing", Sys.CLZ_WARN_PARAM), " parts"),)) print(diff) # clean for mid, row in sendIds: if Io.file_exists(self.outbox + row[1] + Kirmah.EXT): Sys.removeFile(self.outbox + row[1] + Kirmah.EXT) if cancel: delids = [mid for mid, row in senids] print(delids) self.idxu.ih.delete(delids, True) return done, key
def selectKey(self, filename): """""" if not Io.file_exists(filename): raise FileNotFoundException(filename) self.kpath = filename
def onCommandEnc(self): """""" done = True if self.o.outputfile is None: self.o.outputfile = Sys.basename(self.a[1]) if self.o.outputfile[-len(Kirmah.EXT):] != Kirmah.EXT: print(self.o.outputfile[-len(Kirmah.EXT):]) self.o.outputfile += Kirmah.EXT print(self.o.outputfile) d = self.getDefaultOption( (self.o.compress, self.o.fullcompress, self.o.nocompress)) compress = (KirmahHeader.COMP_END if d == 0 or (d is None and Io.is_binary(self.a[1])) else (KirmahHeader.COMP_ALL if d == 1 or d is None else KirmahHeader.COMP_NONE)) random = True if (self.o.random is None and self.o.norandom is None) or self.o.random else False mix = True if (self.o.mix is None and self.o.nomix is None) or self.o.mix else False if (self.o.multiprocess is not None and not represents_int(self.o.multiprocess)) or ( not self.o.multiprocess is None and not (int(self.o.multiprocess) >= 2 and int(self.o.multiprocess) <= 8)): self.parser.error_cmd( (('invalid option ', ('-j, --multiprocess', Sys.Clz.fgb3), ' value (', ('2', Sys.Clz.fgb3), ' to ', ('8', Sys.Clz.fgb3), ')'), )) nproc = int( self.o.multiprocess) if not self.o.multiprocess is None and int( self.o.multiprocess) >= 2 and int( self.o.multiprocess) <= 8 else 1 if not Sys.g.QUIET: self.parser.print_header() if Io.file_exists(self.o.outputfile) and not self.o.force: Sys.pwarn((('the file ', (self.o.outputfile, Sys.Clz.fgb3), ' already exists !'), )) done = Sys.pask('Are you sure to rewrite this file') self.stime = Sys.datetime.now() if done: try: Sys.ptask() key = Io.get_data(self.o.keyfile) km = Kirmah(key, None, compress, random, mix) km.encrypt(self.a[1], self.o.outputfile, nproc) except Exception as e: done = False print(e) raise e pass if not Sys.g.QUIET: self.onend_cmd('Kirmah Encrypt', self.stime, done, self.o.outputfile)
def onCommandSplit(self): """""" done = True Sys.cli_emit_progress(1) if not self.o.parts is None and not (int(self.o.parts) >= 12 and int(self.o.parts) <= 62): self.parser.error_cmd( (('invalid option ', ('-p, --parts', Sys.Clz.fgb3), ' value (', ('12', Sys.Clz.fgb3), ' to ', ('62', Sys.Clz.fgb3), ')'), )) else: self.o.parts = int(self.o.parts) if not Sys.g.QUIET: self.parser.print_header() if self.o.outputfile is not None: if self.o.outputfile[-5:] != '.tark': self.o.outputfile += '.tark' if Io.file_exists(self.o.outputfile) and not self.o.force: Sys.pwarn((('the file ', (self.o.outputfile, Sys.Clz.fgb3), ' already exists !'), )) done = Sys.pask('Are you sure to rewrite this file') self.stime = Sys.datetime.now() if done: try: Sys.ptask() Sys.cli_emit_progress(2) key = Io.get_data(self.o.keyfile) km = Kirmah(key) hlst = km.ck.getHashList(Sys.basename(self.a[1]), self.o.parts, True) Sys.cli_emit_progress(3) kcf = km.splitFile(self.a[1], hlst) t = int(Sys.time()) times = (t, t) p = 85 Sys.cli_emit_progress(p) Io.touch(kcf, times) frav = 0.24 for row in hlst['data']: p += frav Io.touch(row[1] + km.EXT, times) Sys.cli_emit_progress(p) if self.o.outputfile is not None: d = Sys.datetime.now() if Sys.g.DEBUG: Sys.wlog(Sys.dprint()) Sys.ptask('Preparing tark file') hlst['data'] = sorted(hlst['data'], key=lambda lst: lst[4]) with tarfile.open(self.o.outputfile, mode='w') as tar: tar.add(kcf, arcname=Sys.basename(kcf)) p = 90 for row in hlst['data']: tar.add(row[1] + km.EXT, arcname=Sys.basename(row[1] + km.EXT)) p += frav Sys.cli_emit_progress(p) Sys.pstep('Packing destination file', d, True) d = Sys.datetime.now() Sys.ptask('Finalize') for row in hlst['data']: Io.removeFile(row[1] + km.EXT) p += frav Sys.cli_emit_progress(p) Io.removeFile(kcf) Sys.pstep('Cleaning', d, True) except Exception as e: done = False if Sys.g.DEBUG: print('split exception') print(e) #~ raise e elif not Sys.g.QUIET: Sys.pwarn((str(e), )) if not Sys.g.QUIET: Sys.cli_emit_progress(100) self.onend_cmd('Kirmah Split', self.stime, done, self.o.outputfile)
def setSourceFile(self, filename): """""" if not Io.file_exists(filename) : raise FileNotFoundException() else : self.src = filename
def hasSrcFile(self): """""" return Io.file_exists(self.src)