File size: 13,265 Bytes
aeea18d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# -*- coding: utf-8 -*-
##import zipOrZip as zo;
import gzip, bz2,  tarfile, zipfile, datetime, io
from io import BytesIO
import os
#
import mw;
from tofu_edit import ctod
#
def extgz(te):
    buf = BytesIO( te)
    f = gzip.GzipFile(fileobj=buf)
    data = f.read()
    return data;
def wbgz(te):#writeBuffer
    buf = BytesIO()
    f = gzip.GzipFile(fileobj=buf,mode="wb")
    f.write(te);
    f.close()#neccesery
    return buf.getvalue();
def wfgz(fn,te):#writeFile
    f = gzip.open(fn,"wb")
    data = f.write(te)
    f.close()#同じ名前のファイルがあるとRenameと同じ挙動に
def wfbz(fn,te):#writeFile
    f = bz2.BZ2File(fn,"wb")
    data = f.write(te)
    f.close()
def writebzb(te):#writeFilebz Buffer
    buf = BytesIO()
    f = bz2.BZ2File(fileobj=buf,mode="wb")#fileobj not work
    data = f.write(te)
    f.close()
    return buf.getvalue()
def tarlist(fn):
    with tf.open(fn,"r") as ta:
        return tf.list()
def tarprint(fn):
##    with tarfile.open(fn,"r") as ta:
##        print(ta.list())
    ta=tarfile.open(fn,"r")
    print(ta.list())
    for tarinfo in ta:
        print(tarinfo.name,tarinfo.size,  ##          datetime.fromtimestamp(tarinfo.mtime),
          tarinfo.mtime, oct(tarinfo.mode),            tarinfo.uname, tarinfo.gname)
    ta.close()
def tfread(tfn,fn):
    with tarfile.open(tfn,"r") as ta:
        ti = ta.getmember(fn)
        f = ta.extractfile(ti)
        te= f.read()
    return te ;
def writeExtract(dist,fns=()):
    if(type(fns)==str):
        with tarfile.open(fns) as ta:
            ta.extractall(dist)
def writexz(fn,li3,mode='w|xz'):codetar(li3=li3,mode=mode,fn=fn);
def codetar(li3,mode='w|',bio=BytesIO(),fn=""):
    with tarfile.open(name=fn, mode=mode, fileobj=(bio if not fn else None))as ta:
        for src,dist in li3:
            ta.add(src,dist)
    return bio
def zipread(zfn,fn="",pwd=None):
    with zipfile.ZipFile(zfn,"r") as zfo:
        if not fn:return zfo.infolist()
        #fo=zfo.open(fn,"r",pwd=pwd)
        data=zfo.read(fn,pwd=pwd)
        return data
def zipwrites_full(zfn,li,compress=zipfile.ZIP_DEFLATED, pwd=None):
    #passward not supported
    with zipfile.ZipFile(zfn,"w",compression=compress) as zfo:
        for rfn,wfn in li:
            zfo.write(rfn,wfn)
        return zfo.infolist()
def zipwrites(zfn,li,compress=zipfile.ZIP_DEFLATED, pwd=None):
    zipwrites_full(zfn,[(fn,fn) for fn in li],compress=compress, pwd=pwd)
def zipwrites_dir(zfn,dirn,*aw,**kw):
    import itertools
    imw=(itertools.chain.from_iterable(([jofn(r,fn) for fn in (ds+fs)] for r, ds, fs in os.walk(dirn) )   ))
##    dire=kah.split("\\")[-1]
    li=[(i,i[len(dirn):]) for i in imw]
    zipwrites_full(zfn,li,*aw,**kw)
#zipwrites_dir(r"r:\isshonisino.zip",r"r:\temp",compress=zipfile.ZIP_STORED)
def zipbufi(li):
    buf = BytesIO()
    with zipfile.ZipFile(buf,mode="w") as fo:
        for p,te in li:
            fo.writestr(p,te);
##            if type(te)==str: fo.writestr(p,te);
##            else: fo.write(p,te)
    #print(buf.getvalue())
    return buf;
def zipbuf():
    #buf=zipbufi([(r"v/w.txt","vw"),("new folder/",""),(r"ほのか","weffw")])
    buf=zipbufi([(r"nf/nf/0.txt","vw"),("cmczip.com.url",""),(r"nf/nf/cmczip.com.url","")])
    #buf=BytesIO(buflk)
    inf=zipread(buf)
    wr.wfdb(r"c:\v\t\ziptst.zip",buf.getbuffer())
    print(inf)
#zipbuf()
def ziplvlv():
    b1 = BytesIO()
    zipwrites_dir(b1,r"c:\v\t\lvlv")
    buf=zipbufi([(r"v1.zip",b1.getbuffer()),("v2.zip",b1.getbuffer())])
    zfn=r"c:\v\t\ziptst.zip"
    wr.wfdb(zfn,buf.getbuffer())
    print(zipread(zfn))
##ziplvlv()

#https://www.rarlab.com/rar_add.htm
os.environ["UNRAR_LIB_PATH"]=r"C:\dll\unrar64.dll"
try:
    from unrar import rarfile
except:0
def ur(fn):
        ro=rarfile.RarFile(fn)
        #ro.comment
##    print(ro.namelist())
##    ro.printdir()
        fs=ro.infolist()
        for fo in fs[:4]+fs[-2:]:
            print(fo.filename)
            ro.extract(fo,dstfolder)
#for fn in (r"C:\v\jd\Nagato Yuki Shoushitsu v0%d.rar"%d for d in range(6,10)): #exc
from mw import rfdb, jofn, lfnb
class zo:
    def ini1(self,zfn,tmpfolder=""):
        self.zfn=zfn
        self.tdn=tdn=jofn(tmpfolder,lfnb(zfn[0]))
        if not os.path.exists(tdn):
            os.makedirs(tdn)
        else: return self
        if 1:
            self.tfread=self.tfread
            for f in zfn: writeExtract(tdn,f)#4 bcmodified
        else:
            self.tfread=self.tfread
        return self
    def fpfn(self,fn): return jofn(self.tdn, fn)
    def tfread(self,zfn,fn):
        return rfdb(jofn(self.tdn, fn))
##    def __iter__(self):
##        for f in tempfolder
    def __enter__(self):
        return 1
    def __exit__(self, _type, value, traceback):
        self.tdn
    def rmtmp(self):
        import shutil
        from datetime import datetime, date, timedelta
        import time
        ctime=os.path.getctime(self.tdn)
        ctime=datetime.fromtimestamp(ctime)
        flnew=datetime.now()-timedelta(hours=2) < ctime
        print("remove;",ctime, self.tdn, f"{flnew=}")
        if flnew: shutil.rmtree(self.tdn)


#
import random
import string
import uuid
import hashlib
#hashlib.sha1(b"kanata").hexdigest()
def mkOneTime():return mkPas(uuid.uuid4())
def mk1p():return print(mkPas(uuid.uuid4(),l=12,letters="d!?-_#"))#keyboard friendly pass
def mkPas(k,l=24,letters="ad"):
    diLe={"a":string.ascii_letters, "d": string.digits,   "p":string.punctuation}
    diLe.update([(p,p)for p in string.punctuation])
    letters=jw("",[diLe[c]for c in letters])
    print(letters)
    #
    random.seed(k)
    return (jw('',[random.choice(letters) for i in range(l)]))
def mkKey(k,l=1234,l2=1):#initial vector, rn key len, range*256
    random.seed(k)
    rn=[random.randrange(0,l2*0x100) for i in range(l)]
    return rn
def visunel(k,te):
    t2=[(v+k[i%len(k)])%6 for i,v in enumerate(te)]
    return t2
def visuner(k,te):
    t2=[(v+256-k[i%len(k)])%6 for i,v in enumerate(te)]
    return t2
def visunex(k,te):return bytearray([(v^k[i%len(k)]) for i,v in enumerate(te)])#xor
import array
import struct
#http://docs.python.jp/3/library/struct.html
def pwdlen(pwd):
    if(type(pwd)==int):
        a
def imagineL(pwd,data,fn):
    datas=[bytes(v)for v in [fn,pwd,data]]
    lens=[len(i)for i in datas];#(len(pwd),len(data),len(fn),)
    ba=bytearray(4*len(lens)+ sum(lens))
##    p1=struct.pack("III"+jw("",[(str(i)+"s")for i in lens]), *lens)
    p1=struct.pack("III"+jw("",[(str(i)+"s")for i in lens]), *lens+datas)
    #struct.pack_into("2s",ba,0,b"j")
##    return p1
##def imagineL(pwd,data,fn):
    datas=[fn,pwd,data]
    lens=struct.unpack("III", p1[:12])
    p2r=struct.unpack("III"+jw("",[(str(i)+"s")for i in lens]), p1)
    print(p2r)
    return p2r
def main():
    hh=wr.hhww(mojibake=0)
    z="gz";
    fn="fn."+z;
    te=wr.rfdb(wr.sys.argv[0]);
    tfn=r"N:\dpDict\naist-jdic-0.4.3.tar.gz"
    tfn2=["unimj.txt","orror00.txt"]
    import os
    import re
    if 0:
        wr.inidir("testfldr\\")
        tarprint(tfn)
        ta=tarfile.open("testfldr\\ふぁいる.tar.gz","w:gz")
        for n in tfn2 : ta.add(n)
        ta.close()
        print(tfread(tfn,"naist-jdic-0.4.3/README").decode("cp932"))
    if 0:
        wfgz(fn,te);
        wfbz("fあみn.txt.bz2",te);#文字化け
##        wfbz(str("あいう.txt.bz2".encode("mbcs")),te);
        bf=wbgz(te);
        wr.wfdb("f2.txt.gz",bf);
        pass
    if 0:#NoWork
        bf=writebzb(te);
    def tes2():
        randkey=mkKey("key",22,1)
        print(randkey)
        randkey=mkKey("key2",22,1)
        print(randkey)
        coded=visunel(randkey, "amiあみ".encode("utf8"))
        raw=visuner(randkey,coded)
        print(coded,bytes(coded),raw, bytes(raw).decode())
        print("xor",visunex(randkey, visunex(randkey, "amiあみ".encode("utf8"))))
        #
        imagineL(randkey,coded,b"ami.c")
        #
        mk1p();print(mkOneTime())
##    class ca(io.m):
    class ca(io.BufferedIOBase):
##    class ca(io.BytesIO):
        def __init__(sl,ar):
            sl.ar=ar
            sl.p=0
        def read(sl,ln):
##            ri=io.BytesIO.read(sl,ln)
##            return ri
            ro=sl.ar[sl.p:sl.p+ln]
            sl.p=sl.p+ln
            return bytes(ro)
        def write(sl,te):
            sl.ar.extend(list(te))
##            ri=io.BufferedWriter.write(sl,te)
##            return ri
        def getvalue(sl):
            return bytes(sl.ar)
    def j1():#no
        return
        buf=ca([])
        with gzip.GzipFile(fileobj=buf,mode="wb") as go:
            go.write(b"fwefwe")
        wd=buf.getvalue()
        with gzip.GzipFile(fileobj=buf) as go:
            rd=go.read()
        print(wd,rd)
        #12
        with open(r"r:\j12.gz","wb") as fo:
            fo
    def j2():#print sorted punctuation
        "explorer '- !#$%&(),.;@[]^_`{}~+=0"#'- is ordered by following sign
        cant=r'"*./:<>?\|'#'unaviable'
        az="!#$%&'()+,-019;=@AZ[]^_`az{}~"#aviable
        #
        import os, time
        if not(wr.ifexist(r"r:\fn")):os.makedirs(r"r:\fn")
        #
        diLe={"a":string.ascii_letters, "d": string.digits,   "p":string.punctuation}
        li1=[i for j in diLe.values() for i in j]+list("わあ")
        li1.sort()
        print(jw("",li1))
        print(jw("",[ c for c in li1 if not c in cant]))
        #
        eFn=[]
        for i,c in enumerate(li1):
            if(c in r'"*/:<>?\|'):continue
            try:
                fn1=r"r:\fn\%s_d_x%x"%(c,i,ord(c))
                wr.write8(fn1+".exe","")#exe 4 sendto
                wr.write8(r"r:\fn\%c"%(c)+".exe","")
##                os.utime(fn1,(time.mktime((2000,1,1,5,55,55,1,1,1)),0.0))
            except Exception as e:# lkFileNotFoundError as e:
                eFn.append((c,e))
        print("eFn " + jw("",(c for c,e in eFn)))
    ap=r"N:\driveK\applic\7za920\7za.exe"
    def j4(fn=r"N:\afilen\program\python\Mjmoji",alias="files",pwd=""):#7z
        with wr.cldir(fn):
            fd=os.walk(fn)
            fn1=[jofn(r,f) for r,ds,fs in fd  for f in fs]
            fn2=[os.path.relpath(j) for j in fn1]
            if 1:
##                fnt=[(f,os.path.split(f)[1]) for f in fn1]
                fnt=[(f,os.path.relpath(f)) for f in fn1]
                buf=codetar(fnt,mode="w|")
                xz=r"r:\tar.tar.xz"
                codetar(fnt,mode="w|xz",fn=xz)#idea. encoding="utf-8"
                #
##                codetar([(fp,"alfa\\"+rp)for fp,rp in fnt],mode="a",fn=r"r:\alf.tar.gz")
                tarprint(xz)
                #open
##                with open(xz+"2","wb") as fo:
##                    fo
            fns=fn2
            j=fns[9]
            cline=[ap,'a', 'r:\\j4.7z','-si']
            print(cline)
            ri=ctod(cline,sh=False,si=buf.getvalue())
            print(jw(" ",[str(j)for j in ri]))
    def j3(fn=r"N:\afilen\program\python\file",alias="files",pwd=""):#7z
        with wr.cldir(fn):
            ap=r"N:\driveK\applic\7za920\7za.exe"
            #fns=os.listdir(fn);
            fd=os.walk(fn)
            fn1=[jofn(r,f) for r,ds,fs in fd  for f in fs]
            fn2=[os.path.relpath(j) for j in fn1]
            fns=fn2
##            jf=jw(" ",[jofn(fn,j)for j in fns])
####            print(jf)
##            cline=ap+" a "+("-p%s "%pwd if pwd else "")+r"r:\j.7z "+jf
    ##        ri=ctod(cline)
    ##        print(ri)
##            cline=ap+" a "+("-p%s "%pwd if pwd else "")+r"r:\stdin.7z -siSI/a "+jf
####            cline=ap+" a "+("-p%s "%pwd if pwd else "")+r'r:\stdin.7z -sir@list < '+jf
##            cline=ap+" a "+("-p%s "%pwd if pwd else "")+r'r:\stdin.7z kakomj/mj.py'#idea
            if 0:
                ri=ctod(cline)
            else:
                for j in fns:
                    cline=ap+" a "+("-p%s "%pwd if pwd else "")+r"r:\j.7z "+'"%s"'%j
                    cline=[ap," a "+("-p%s "%pwd if pwd else "")+r"r:\j.7z "+'%s'%j]
                    cline=[ap,'a', 'r:\\j32.7z',j]#idea
                    print(cline)
                    ri=ctod(cline,sh=False)
                    print(ri)
##                    wr.prexit()
            print(ri)
        #
        if 0:#no
            cline=ap+" a "+("-p%s "%pwd if 0 else "")+r"r:/j3.7z "+r"-ir!N:\afilen\program\python\file\*.txt"
            cline=ap+" a "+r" r:/j2.7z "+r" -ir!R:\temp\*.txt"#idea
            cline=ap+" a "+r" j2 "+r' -ir!"N:\afilen\program\python\file\*.txt"'
##            cline=ap+" a "+r" j2 "+r' -ir!"N:/afilen/program/python/file/*.txt"'
            print(cline)
            ri=ctod(cline)
            print(ri)
##    j4(fn=r"N:\afilen\program\python\file")
##    j3()
##    j2()
##    j1()
    if hh.mojibake:
        zfn=r"C:\v\jd\Pic201377.zip"
        info=zipread(zfn)
        print(info)
        #
        with zipfile.ZipFile(zfn,"r") as zfo:
            for im in zfo.infolist():#|namelist()
                fn=im.filename
                print(fn.encode('cp437').decode('cp932'))
def m0():
    te=wr.rfdb(r"N:\afilen\program\python\work\d.html")
##    buf = StringIO( te.decode("utf-8"))
    buf = BytesIO( te)
    f = gzip.GzipFile(fileobj=buf)
    data = f.read()

    f = open("tenkaiAfter"+".html","wb")
    f.write(data)
    f.close()


if __name__ == '__main__':
    main()