File size: 8,920 Bytes
2ebd074
aeea18d
2ebd074
 
 
 
aeea18d
2ebd074
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aeea18d
2ebd074
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aeea18d
2ebd074
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aeea18d
2ebd074
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aeea18d
 
 
 
 
2ebd074
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-
##import sys;sys.path.append("N:\\afilen\\program\\python\\Mjmoji");from mw import *
import sys
import time
from ctypes import *
import subprocess as p
from mw import *

def scroll(*ww):
    import edit_
#
#


def ctockai( text ):
        import os
        logfile=[fr"{tfolder}/1.txt",fr"{tfolder}/2er.txt"]
##        for f in logfile: os.path.exists(f) and os.remove(f)
        ip = p.Popen( ['cmd'], stdin=p.PIPE, shell=True )
        #text="%s > %s 2>&1 \n"%(text,erfile)#write
        text="{} 1> {} 2> {} \n".format(text,*logfile)#write
##        orror(text);
        ip.stdin.write( text.encode("cp932"))
        shuttown(ip.stdin)
        retcode = ip.wait()
        #
        lir=list(open(f,"rb").read() for f in logfile)
##        lir=[te.replace(b"\r\r\n",b"\n").replace(b"\r\n",b"\n").decode("mbcs") for te in lir]
        lir=[te.replace(b"\r\r\n",b"\n").replace(b"\r\n",b"\n").decode("utf8") for te in lir]
        return (retcode,lir[0],lir[1])
def ctod( text ,sh=True,cp="cp932",si=None):
        ip = p.Popen( ['cmd']if(sh)else text, stdin=p.PIPE, stdout=p.PIPE,stderr=p.PIPE,shell=sh )
        if 0:#no
            ip.stdin.write("echo 1\n".encode())
            for x in iter(ip.stdout.readline, b''):x
        if 0:#no
            ip.stdin.write("chcp 65001\n".encode("cp932"))
            ip.stdin.flush()
            ip.stdout.flush()
            print(ip.stdout.read());
        if(sh):
            text+="\n";
            ip.stdin.write( text.encode("cp932"))
            shuttown(ip.stdin)
        elif(si):
            ip.stdin.write(si)
            shuttown(ip.stdin)

        retcode=None
        try:
##            retcode = ip.wait(timeout=0)
            retcode = ip.wait()
            res=(ip.stdout.read()[96:],
                ip.stderr.read())
##            for f in range(212): er=ip.stdout.read(2)
        except p.TimeoutExpired as e:
##            return ip.stdout.readline(21)
            res=(ip.stdout.read()[96:],
                ip.stderr.read())
            fds
##        cp="utf8"
##        try:
##            return retcode,r.decode(cp),er.decode(cp),ip
##        except:
##            cp="cp932"
##            return retcode,r.decode(cp),er.decode(cp),ip
        res=[te.replace(b"\r\r\n",b"\n").replace(b"\r\n",b"\n") for te in res]
        from mw import vcs
        res=[vcs(te) for te in res]
        return retcode,*res,ip
def Runw(li): ctod(li,sh=False)
##(ctockai(jw(" ",["node",r"C:\v\P\electron\electron-quick-start\or1.js"])))
##print(ctockai("dir ."))
##print(ctod(jw(" ",["node",r"C:\v\P\electron\electron-quick-start\or1.js"])))
import os
import th
class cltof():
 def __init__(zl):
    #zl.mutex=fwe
    zl.ev=th.Event()
    zl.lock=th.Lock()
    os.environ['PYTHONIOENCODING'] =  'utf-8'
 def ctof(zl,cb,to, text ,null=""):
    from mw import vcs, tp, wfd
    zl.ev.clear()
    if 0==to:to=3600*12
    time0=time.perf_counter()
    def fppc():
        return time.perf_counter()-time0
    def fpin(cb,text):
        date=tp()
        if isinstance(text,(list,tuple)): c=os.path.basename((text[0]))
        elif isinstance(text,(str)):  c= "C"
        logfile=zl.logfile=[fr"{tfolder}/ctof_{c}_{x}_{date}.txt" for x in ("1","2")]
##        print(text,cb)
        for f in logfile: os.path.exists(f) and os.remove(f)
##        ip = p.Popen( [("echo ff"),"1>",logfile[0],"2>",logfile[1]], shell=True )
        if isinstance(text,(list,tuple)): te= [*text,"1>",logfile[0],"2>",logfile[1]]
        elif isinstance(text,(str)):te= text+" 1> {} 2> {}".format(*logfile)
        if not null or 1:
            zl.ip=ip=p.Popen(text,shell=True, stdout=open(logfile[0],"w"), stderr=open(logfile[1],"w"), stdin=p.PIPE)
            #
        while any([False==os.path.exists(f) for f in logfile]):
            print("not ready;",[os.path.exists(f) for f in logfile],te)
            sleep(0.1)
        while 1:
##            try:
                #with open(logfile[0],"rb") as fo:
                lifo=[open(f,"rb") for f in logfile]
                while 1:
                    poll=zl.poll=ip.poll()
                    for x,fo in enumerate(lifo):
                        bit=fo.read()
##                        print("p;",poll,bit)#lwt
                        if bit and cb: cb(x+1, vcs(bit))
                    if poll!=None: break
                    sleep(0.2)
                    if poll!=None: break
                    if to<(d:=fppc()):return
##                    print(d)
##            except 2:                f
                if ip.poll()!=None:break
                if to<(d:=fppc()):break
    def fpin1(*w):
        cb,*_=w
        try:
            res=fpin(*w)
##        except Exception as e:
##            wfd("r:\\logEdit.txt").t(str(e))
        finally:
            zl.ev.set()
        return res
    t=th.run(fpin1,(cb,text))
    return t
 @property
 def te(zl):
    from mw import vcs
    if 1:
        logfile=zl.logfile
        lir=list(open(f,"rb").read() for f in logfile)
##        lir=[te.replace(b"\r\r\n",b"\n").replace(b"\r\n",b"\n").decode("mbcs") for te in lir]
##        print(lir[0].decode("cp932"),poll)
        lir=[vcs(te.replace(b"\r\r\n",b"\n").replace(b"\r\n",b"\n"))for te in lir]
        return (zl.poll,lir[0],lir[1])
 @property
 def wa(zl):
    try:zl.ev.wait()
    except KeyboardInterrupt as e:e
    return zl
 @staticmethod
 def cbpr(std,b): print(f"{std} cblen({len(b)})  {b}")
 def put(p,te):
    p.ip.stdin.write(te.encode())
    p.ip.stdin.flush()





def ctof(*w):
    p=cltof()
    p.ctof(*w)
    return p
##def watofin(*w):
##    p=ctof(*w).wa
##    return p
##def returnwat():
def watof(*w):
    p=ctof(*w)
    te=p.wa.te
    return te[1]+"\n"+te[2]
def tofu(*w):
    return ctof(*w).wa.te
import subprocess, shlex
from threading import Timer

def run(cmd, timeout_sec):
  proc = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE,
    stderr=subprocess.PIPE)
  kill_proc = lambda p: p.kill()
  timer = Timer(timeout_sec, kill_proc, [proc])
  try:
    timer.start()
    stdout,stderr = proc.communicate()
  finally:
    timer.cancel()
#
import time
import signal,os
def reloadFull(timeout=2.2, me=[sys.argv[0],"-r"]):
        ip=p.Popen(me,shell=True)
        try:
            ri=ip.wait(timeout=timeout)#idea
            print("eof. continue")
        except subprocess.TimeoutExpired as e:
            print("reload",e)
            sys.exit(0)
def r1reloadFull(dure,me):
    def handler(signum, frame):
        print("reload,handler",signum)
    def handlerth(signum, frame):
        print("reload",signum)
##        sys.exit(0)#no
        os.kill(os.getpid(),signal.SIGINT)
##        os.kill(os.getpid(),signal.SIGTERM)#ideaLk
    signal.signal(signal.SIGTERM, handler)
    signal.signal(signal.SIGINT, handler)
    timer=Timer(dure-0.2,handlerth,args=[1,2])
    #
    try:
        timer.start()
        ip=p.Popen( me,bufsize=2, stdin=p.PIPE, stdout=p.PIPE,stderr=p.PIPE,shell=True )
        while ip.poll() is None:
            sleep(0.2)
            print("while_poll",ip.poll())
            pass
##            print(ip.stdout.read())
        print("poll2",ip.poll())
        try:
##            ri=ip.wait(timeout=dure)#idea
            ri=ip.wait()
        except subprocess.TimeoutExpired as e:
            print(e)
##            sys.exit(0)
##        ri
    finally:
        timer.cancel()

if __name__ == '__main__':
    import edit_
    acs()
    fil=sys.argv
    c1=fil[1]if(len(fil)>=2)else None
    def reheasal():
        import multiprocessing as mlti
        sys.stdin.conn

    if(c1=="-out"):
        for j in fil[2]:
            sleep(0.3)
            sys.stdout.write(j)
    if(c1=="-c"):
        for f in ":苍天\n:":
            print(f)
##            sys.stdout.write(f)
    if(c1=="-e"): fwf
    if(c1=="-lf"):
        for f in "123\n234":
            print(f)
            print(" start ",end="")
##            input("pw")
            sys.stdout.flush()
            print("", flush=True)#,file=stdout
            sleep(1)
    if not c1:
        print("c1;")
##    reloadFull([r"dir"])
##    reloadFull(2,[r"N:\afilen\program\python\work\ClipArg.py"] + args)
##    r1reloadFull(5,[r"N:\afilen\program\python\qt\ClipProcess2.pyw"] + args)
##        r1reloadFull(5,[fil[0],"-out", jw(".",[str(j)for j in list(range(2))])] )
##        ctof(__file__,"-out","21212")
##        print(";;",ctof(f'{__file__} -out 21\n\n\n'))
        #? print(";;",ctof(f'{__file__}',"-out", '"21\n21"'))
##        print(";;",ctof(f'{__file__}',"-out", "21"))
#lwt
        def cb(std,b): print(f"{std} cblen({len(b)})  {b}")
##        print(";",ctof(cb,'ffmpeg',"-y","-i",r"r:\\p9.mkv","r:\\fw.mp4"))
##        print(";;",ctof(f'{__file__}',"-c",''))
##        print(";;",ctof(f'{__file__}',"-e"))
##        print(";;",watof(cb,10,(f'{__file__}',"-lf")))
        print(";;",watof(cb,10,(f'{__file__}'+" -lf")))
##        print(";;",watof(f'{__file__}',"-out",'21212苍天\nf'))#?