| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
import socket, sys, struct, os |
| 4 |
|
| 5 |
localpathcommands=["lcd"] |
| 6 |
|
| 7 |
def parseArgs(argsin): |
| 8 |
itochange = None |
| 9 |
totalRealArgs = 0 |
| 10 |
realargs = [] |
| 11 |
if argsin[0] in ["sync"]: |
| 12 |
#get first argument (local path) and append fullpath |
| 13 |
for i in range(1,len(argsin)): |
| 14 |
if not argsin[i].startswith("-"): |
| 15 |
if (itochange is None): itochange = i; |
| 16 |
totalRealArgs+=1 |
| 17 |
|
| 18 |
if argsin[0] in ["sync"]: |
| 19 |
if (itochange is not None and totalRealArgs>=2): |
| 20 |
argsin[itochange]=os.path.abspath(argsin[itochange]) |
| 21 |
|
| 22 |
if argsin[0] in localpathcommands: |
| 23 |
#get first argument (local path) and append fullpath |
| 24 |
for i in range(1,len(argsin)): |
| 25 |
if not argsin[i].startswith("-"): |
| 26 |
argsin[i]=os.path.abspath(argsin[i]) |
| 27 |
|
| 28 |
if argsin[0] in ["get", "preview", "thumbnail"]: |
| 29 |
#get second argument (local path) and append fullpath |
| 30 |
for i in range(1,len(argsin)): |
| 31 |
if not argsin[i].startswith("-"): |
| 32 |
totalRealArgs+=1 |
| 33 |
if (totalRealArgs>1): argsin[i]=os.path.abspath(argsin[i]) |
| 34 |
|
| 35 |
if argsin[0] in ["put"]: |
| 36 |
for i in range(1,len(argsin)): |
| 37 |
if not argsin[i].startswith("-"): |
| 38 |
totalRealArgs+=1 |
| 39 |
realargs.append(argsin[i]) |
| 40 |
|
| 41 |
totalRealArgs=0 |
| 42 |
for i in range(1,len(argsin)): |
| 43 |
if not argsin[i].startswith("-"): |
| 44 |
totalRealArgs+=1 |
| 45 |
if ((totalRealArgs == 1) or (totalRealArgs < (len(realargs))) ): |
| 46 |
argsin[i]=os.path.abspath(argsin[i]) |
| 47 |
|
| 48 |
|
| 49 |
for i in range(1,len(argsin)): |
| 50 |
if " " in argsin[i]: |
| 51 |
argsin[i]='"'+argsin[i]+'"' |
| 52 |
|
| 53 |
for i in range(1,len(argsin)): |
| 54 |
if "" == argsin[i]: |
| 55 |
argsin[i]='"'+argsin[i]+'"' |
| 56 |
|
| 57 |
return argsin; |
| 58 |
|
| 59 |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 60 |
try: |
| 61 |
sock.connect(('127.0.0.1',12300)) |
| 62 |
|
| 63 |
commandArgs=parseArgs(sys.argv[1:]) |
| 64 |
|
| 65 |
|
| 66 |
if (sys.version_info[0] == 3): |
| 67 |
sock.send(" ".join(commandArgs).encode(sys.stdin.encoding)) |
| 68 |
else: |
| 69 |
sock.send(" ".join(commandArgs)) |
| 70 |
|
| 71 |
data = sock.recv(100); |
| 72 |
sockOutId, = struct.unpack('H', data[:2]) |
| 73 |
|
| 74 |
sock.close() |
| 75 |
|
| 76 |
sockout = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 77 |
sockout.connect(('127.0.0.1',12300+sockOutId)) |
| 78 |
|
| 79 |
outCode, = struct.unpack('i', sockout.recv(4))#get out code |
| 80 |
|
| 81 |
data = sockout.recv(100); |
| 82 |
commandOutput="" |
| 83 |
while (len(data)): |
| 84 |
if (sys.version_info[0] == 3): |
| 85 |
commandOutput+=data.decode(sys.stdout.encoding) |
| 86 |
else: |
| 87 |
commandOutput+=data |
| 88 |
data = sockout.recv(100); |
| 89 |
|
| 90 |
sockout.close() |
| 91 |
|
| 92 |
if "" != commandOutput: |
| 93 |
sys.stdout.write(commandOutput) |
| 94 |
sys.stdout.flush() |
| 95 |
|
| 96 |
if (outCode <0): exit(-outCode) |
| 97 |
exit(outCode) |
| 98 |
except Exception as ex: |
| 99 |
sys.stderr.write("Unable to connect to service"+" "+str(ex)+"\n") |
| 100 |
sys.stderr.write("Please ensure MEGAcmd is running\n") |
| 101 |
exit(1) |