From e93b062efa3c2dd6d7b00b8026afc8a0a14e93b1 Mon Sep 17 00:00:00 2001 From: a-Sansara Date: Thu, 13 Sep 2012 20:25:09 +0200 Subject: [PATCH] compat windows --- impra/cli.py | 8 +++++-- impra/core.py | 18 +++++++++------ impra/util.py | 61 ++++++++++++++++++++++++++++++++------------------- 3 files changed, 55 insertions(+), 32 deletions(-) diff --git a/impra/cli.py b/impra/cli.py index 3bf1efa..6a7426e 100644 --- a/impra/cli.py +++ b/impra/cli.py @@ -242,14 +242,15 @@ you can remove index but all presents files on the box %s will be unrecoverable print('bye') sys.exit(1) - if o.list : - + if o.list : if impst.index != None: impst.index.print(True,'-'*120+'\n -- INDEX '+impst.rootBox+'\n'+'-'*120) elif o.add : impst.addFile(o.add[0],o.add[1],o.user,o.category) + elif o.get : impst.getFile(o.get) + elif o.get_by_id : label = impst.index.searchById(o.get_by_id) if label !=None : @@ -258,11 +259,14 @@ you can remove index but all presents files on the box %s will be unrecoverable elif o.search : print(o.search) + elif o.remove : print(o.remove) + elif o.remove_by_id : print(o.remove_by_id) + #~ filePath = '/media/Data/dev/big_toph3.jpg' #~ lab = 'Meuf\'bonne aussi4' #~ print('\n -- ADD FILE -- ') diff --git a/impra/core.py b/impra/core.py index 0475100..4608b23 100644 --- a/impra/core.py +++ b/impra/core.py @@ -256,7 +256,7 @@ class ImpraIndex: if encdata =='' : data = encdata else : data = self.rsa.decrypt(encdata) data = data.replace(self.QUOTE_REPL, '\'') - ld = regsplit('\n? ?'+self.SEP_CATEGORY+' ?\n?',data) + ld = regsplit('\n?\r? ?'+self.SEP_CATEGORY+' ?\n\r??',data) l = regsplit(self.SEP_ITEM,ld[0]) for row in l: d = regsplit(self.SEP_TOKEN,row) @@ -265,11 +265,11 @@ class ImpraIndex: if len(d)>5 and d!='': self.dic[d[1]] = d if len(ld)>1: - l = regsplit(self.SEP_ITEM,ld[1].lstrip('\n')) + l = regsplit(self.SEP_ITEM,ld[1].lstrip('\n\r')) for row in l: d = regsplit(' ?= ?',row,1) if len(d)> 1 and len(d[0]) > 3 : - self.dic[d[0]] = d[1] + self.dic[d[0].lstrip('\n\r')] = d[1] else: for k in dicCategory : self.dic[self.SEP_KEY_INTERN+k] = dicCategory[k] @@ -306,17 +306,20 @@ class ImpraIndex: data = cdata = '' for k in sorted(self.dic): v = self.dic.get(k) + k = k.lstrip('\n\r') if k[0]==self.SEP_KEY_INTERN and len(k)>1: cdata += k+'='+v+self.SEP_ITEM else : if not idFirst : - for i in v: data += str(i)+self.SEP_TOKEN + for i in v: + data += str(i)+self.SEP_TOKEN else : data += str(v[6]).rjust(1+ceil(len(str(v[6]))/10),' ')+' ' - for i in v[:-1]: data += str(i)+self.SEP_TOKEN + for i in v[:-1]: + data += str(i)+self.SEP_TOKEN data = data.rstrip(self.SEP_TOKEN)+self.SEP_ITEM if not withoutCatg : - data += self.SEP_CATEGORY+'\n'+cdata + data += self.SEP_CATEGORY+'\n'+cdata return data; def encrypt(self): @@ -411,7 +414,8 @@ class ImpraStorage: """""" self._getIdIndex() if self.idx : - self.ih.delete(self.idx) + self.ih.delete(self.idx, True) + self.ih.deleteBin() def saveIndex(self): """""" diff --git a/impra/util.py b/impra/util.py index fba5c5a..67a703c 100755 --- a/impra/util.py +++ b/impra/util.py @@ -33,13 +33,13 @@ from hashlib import sha256 from math import log, floor, ceil from random import choice from os import urandom, popen, sep -from os.path import dirname, realpath, abspath +from os.path import dirname, realpath, abspath, join from time import time from re import split as regsplit from base64 import urlsafe_b64encode from inspect import stack from subprocess import PIPE, Popen -from sys import stderr +from sys import stderr, executable as pyexec # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -59,6 +59,12 @@ def quote_escape(data): """ return data.replace('\'', r'\'') +def linefeed_escape(data): + """Escape simple quote + :Returns: `str` + """ + return data.replace('\n', '\\n') + def get_file_content(fileName): """Get file content of `fileName` :Returns: `str` @@ -222,7 +228,7 @@ class RuTime: if DEBUG:self._stats() def _stats(self): - print(' <== '+self.label+(' [%.9f s]' % (self.ec - self.sc))+' <¤¤ ') + print(' <== '+self.label+(' [%.9f s]' % (self.ec - self.sc))) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -331,24 +337,22 @@ class Rsa: """""" def __init__(self, prvKey=None, pubKey=None, dpath='./', forceKeyGen=False): - """""" - self.cpath = dirname(realpath(__file__))+'/../desurveil/scripts/' + """""" + self.cpath = join(dirname(dirname(realpath(__file__))),'desurveil','scripts')+sep self.prvKey = prvKey self.pubKey = pubKey - self.dpath = dpath + self.dpath = realpath(dpath)+sep if prvKey == None or pubKey==None : self.key(forceKeyGen) def key(self,force=False): """""" - cmd = self.cpath+"desurveil key -a "+self.dpath+".impra_id_rsa -l "+self.dpath+".impra_id_rsa.pub" - #print(cmd) - + cmd = self.cpath+'desurveil key -a '+self.dpath+'.impra_id_rsa -l '+self.dpath+'.impra_id_rsa.pub' try : with open(self.dpath+'.impra_id_rsa','rt') as f: pass if force:d = popen(cmd).read() except IOError as e: - d = popen(cmd).read() - #print(d) + d = popen(pyexec+' '+cmd).read() + #print(pyexec+' '+cmd) self.prvKey = get_file_content(self.dpath+'.impra_id_rsa') self.pubKey = get_file_content(self.dpath+'.impra_id_rsa.pub') #~ print('pubKey : \n'+self.pubKey) @@ -357,22 +361,33 @@ class Rsa: def encrypt(self,data): """""" key = '' - if self.pubKey != None : key = " -CI '"+self.pubKey+"'" - #if self.pubKey != None : key = " -C '"+self.dpath+".impra_id_rsa.pub'" - cmd = self.cpath+"desurveil encrypt -i '"+data+"'"+key - #print(cmd) - return popen(cmd).read() + #if self.pubKey != None : key = ' -CI "'+self.pubKey+'"' + if self.pubKey != None : key = ' -C "'+self.dpath+'.impra_id_rsa.pub"' + with open(self.dpath+'.tmpdecd', mode='w', encoding='utf-8') as o: + o.write(data) + cmd = self.cpath+'desurveil encrypt "'+self.dpath+'.tmpdecd'+'" '+key + #print(pyexec+' '+cmd) + rs = run(pyexec+' '+cmd) + if rs[0]==1: + print(rs) + raise BadKeysException('bad key to encrypt') + else : + encData = str(rs[1],'utf-8') + return encData def decrypt(self,data): """""" key = '' - if self.prvKey != None : key = " -CI '"+self.prvKey+"'" - #if self.prvKey != None : key = " -C '"+self.dpath+".impra_id_rsa'" - cmd = self.cpath+"desurveil decrypt -i '"+data+"'"+key - - rs = run(cmd) + #if self.prvKey != None : key = ' -CI "'+self.prvKey+'"' + if self.prvKey != None : key = ' -C "'+self.dpath+'.impra_id_rsa"' + with open(self.dpath+'.tmpencd', mode='w', encoding='utf-8') as o: + o.write(data) + cmd = self.cpath+'desurveil decrypt "'+self.dpath+'.tmpencd'+'" '+key + #print(pyexec+' '+cmd) + rs = run(pyexec+' '+cmd) if rs[0]==1: + print(rs) raise BadKeysException('bad key to decrypt') else : - encData = str(rs[1],'utf-8') - return encData + decData = str(rs[1],'utf-8') + return decData