1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4import sys, os, subprocess, shutil
5from megacmd_tests_common import *
6
7GET="mega-get"
8PUT="mega-put"
9RM="mega-rm"
10CD="mega-cd"
11LCD="mega-lcd"
12MKDIR="mega-mkdir"
13EXPORT="mega-export -f"
14FIND="mega-find"
15WHOAMI="mega-whoami"
16LOGOUT="mega-logout"
17LOGIN="mega-login"
18ABSPWD=os.getcwd()
19currentTest=1
20
21
22try:
23    os.environ['VERBOSE']
24    VERBOSE=True
25except:
26    VERBOSE=False
27
28#VERBOSE=True
29
30
31try:
32    MEGA_EMAIL=os.environ["MEGA_EMAIL"]
33    MEGA_PWD=os.environ["MEGA_PWD"]
34except:
35    print >>sys.stderr, "You must define variables MEGA_EMAIL MEGA_PWD. WARNING: Use an empty account for $MEGA_EMAIL"
36    exit(1)
37
38
39try:
40    MEGACMDSHELL=os.environ['MEGACMDSHELL']
41    CMDSHELL=True
42    #~ FIND="executeinMEGASHELL find" #TODO
43
44except:
45    CMDSHELL=False
46
47def initialize_contents():
48    contents=" ".join(['"localtmp/'+x+'"' for x in os.listdir('localtmp/')])
49    cmd_ef(PUT+" "+contents+" /")
50    shutil.copytree('localtmp', 'localUPs')
51
52def clean_all():
53
54    if cmd_es(WHOAMI) != osvar("MEGA_EMAIL"):
55        cmd_ef(LOGOUT)
56        cmd_ef(LOGIN+" " +osvar("MEGA_EMAIL")+" "+osvar("MEGA_PWD"))
57
58    #~ rm pipe > /dev/null 2>/dev/null || :
59
60    cmd_ec(RM+' -rf "*"')
61    cmd_ec(RM+' -rf "//bin/*"')
62
63    rmfolderifexisting("localUPs")
64    rmfolderifexisting("localtmp")
65
66    rmfileifexisting("megafind.txt")
67    rmfileifexisting("localfind.txt")
68
69
70
71def clear_local_and_remote():
72    rmfolderifexisting("localUPs")
73    cmd_ec(RM+' -rf "/*"')
74    initialize_contents()
75
76def compare_and_clear() :
77    global currentTest
78    if VERBOSE:
79        print "test "+str(currentTest)
80
81    megafind=sort(cmd_ef(FIND))
82    localfind=sort(find('localUPs'))
83
84    #~ if diff --side-by-side megafind.txt localfind.txt 2>/dev/null >/dev/null; then
85    if (megafind == localfind):
86        if VERBOSE:
87            print "diff megafind vs localfind:"
88            #diff --side-by-side megafind.txt localfind.txt#TODO: do this
89            print "MEGAFIND:"
90            print megafind
91            print "LOCALFIND"
92            print localfind
93        print "test "+str(currentTest)+" succesful!"
94    else:
95        print "test "+str(currentTest)+" failed!"
96        print "diff megafind vs localfind:"
97        #~ diff --side-by-side megafind.txt localfind.txt #TODO: do this
98        print "MEGAFIND:"
99        print megafind
100        print "LOCALFIND"
101        print localfind
102
103        #cd $ABSPWD #TODO: consider this
104        exit(1)
105
106    clear_local_and_remote()
107    currentTest+=1
108    cmd_ef(CD+" /")
109
110def initialize():
111
112    if cmd_es(WHOAMI) != osvar("MEGA_EMAIL"):
113        cmd_es(LOGOUT)
114        cmd_ef(LOGIN+" " +osvar("MEGA_EMAIL")+" "+osvar("MEGA_PWD"))
115
116    if len(os.listdir(".")):
117        print >>sys.stderr, "initialization folder not empty!"
118        #~ cd $ABSPWD
119        exit(1)
120
121    if cmd_es(FIND+" /") != "/":
122        print >>sys.stderr, "REMOTE Not empty, please clear it before starting!"
123        #~ cd $ABSPWD
124        exit(1)
125
126
127    #initialize localtmp estructure:
128    makedir("localtmp")
129    touch("localtmp/file01.txt")
130    out('file01contents', 'localtmp/file01nonempty.txt')
131    #local empty folders structure
132    for f in ['localtmp/le01/'+a for a in ['les01/less01']+ ['les02/less0'+z for z in ['1','2']] ]: makedir(f)
133    #local filled folders structure
134    for f in ['localtmp/lf01/'+a for a in ['lfs01/lfss01']+ ['lfs02/lfss0'+z for z in ['1','2']] ]: makedir(f)
135    for f in ['localtmp/lf01/'+a for a in ['lfs01/lfss01']+ ['lfs02/lfss0'+z for z in ['1','2']] ]: touch(f+"/commonfile.txt")
136    #spaced structure
137    for f in ['localtmp/ls 01/'+a for a in ['ls s01/ls ss01']+ ['ls s02/ls ss0'+z for z in ['1','2']] ]: makedir(f)
138    for f in ['localtmp/ls 01/'+a for a in ['ls s01/ls ss01']+ ['ls s02/ls ss0'+z for z in ['1','2']] ]: touch(f+"/common file.txt")
139
140    # localtmp/
141    # ├── file01nonempty.txt
142    # ├── file01.txt
143    # ├── le01
144    # │   ├── les01
145    # │   │   └── less01
146    # │   └── les02
147    # │       ├── less01
148    # │       └── less02
149    # ├── lf01
150    # │   ├── lfs01
151    # │   │   └── lfss01
152    # │   │       └── commonfile.txt
153    # │   └── lfs02
154    # │       ├── lfss01
155    # │       │   └── commonfile.txt
156    # │       └── lfss02
157    # │           └── commonfile.txt
158    # └── ls 01
159        # ├── ls s01
160        # │   └── ls ss01
161        # │       └── common file.txt
162        # └── ls s02
163            # ├── ls ss01
164            # │   └── common file.txt
165            # └── ls ss02
166                # └── common file.txt
167
168    #initialize dynamic contents:
169    clear_local_and_remote()
170
171
172def compare_find(what, localFindPrefix='localUPs'):
173    global currentTest
174    if VERBOSE:
175        print "test "+str(currentTest)
176
177    if not isinstance(what, list):
178        what = [what]
179    megafind=""
180    localfind=""
181    for w in what:
182        megafind+=cmd_ef(FIND+" "+w)+"\n"
183        localfind+=find(localFindPrefix+'/'+w,w)+"\n"
184
185    megafind=sort(megafind).strip()
186    localfind=sort(localfind).strip()
187
188    #~ megafind=$FIND "$@"  | sort > $ABSPWD/megafind.txt
189    #~ (cd localUPs 2>/dev/null; find "$@" | sed "s#\./##g" | sort) > $ABSPWD/localfind.txt
190    if (megafind == localfind):
191        if VERBOSE:
192            print "diff megafind vs localfind:"
193            #diff --side-by-side megafind.txt localfind.txt#TODO: do this
194            print "MEGAFIND:"
195            print megafind
196            print "LOCALFIND"
197            print localfind
198        print "test "+str(currentTest)+" succesful!"
199    else:
200        print "test "+str(currentTest)+" failed!"
201        print "diff megafind vs localfind:"
202        #~ diff --side-by-side megafind.txt localfind.txt #TODO: do this
203        print "MEGAFIND:"
204        print megafind
205        print "LOCALFIND"
206        print localfind
207
208        #cd $ABSPWD #TODO: consider this
209        exit(1)
210
211    currentTest+=1
212
213def compare_remote_local(megafind, localfind):
214    global currentTest
215    if (megafind == localfind):
216        if VERBOSE:
217            print "diff megafind vs localfind:"
218            #diff --side-by-side megafind.txt localfind.txt#TODO: do this
219            print "MEGAFIND:"
220            print megafind
221            print "LOCALFIND"
222            print localfind
223        print "test "+str(currentTest)+" succesful!"
224    else:
225        print "test "+str(currentTest)+" failed!"
226        print "diff megafind vs localfind:"
227        #~ diff --side-by-side megafind.txt localfind.txt #TODO: do this
228        print "MEGAFIND:"
229        print megafind
230        print "LOCALFIND"
231        print localfind
232
233        exit(1)
234
235    currentTest+=1
236
237
238if VERBOSE: print "STARTING..."
239
240#INITIALIZATION
241clean_all()
242initialize()
243
244#Test 01 #destiny empty file
245compare_find('file01.txt')
246
247#Test 02  #entire empty folders
248compare_find('le01/les01/less01')
249
250#Test 03 #1 file folder
251compare_find('lf01/lfs01/lfss01')
252
253#Test 04 #entire non empty folders structure
254compare_find('lf01')
255
256#Test 05 #multiple
257compare_find(['lf01','le01/les01'])
258
259#Test 06 #.
260cmd_ef(CD+" le01")
261compare_find('.','localUPs/le01')
262cmd_ef(CD+" /")
263
264#Test 07 #. global
265compare_find('.')
266
267#Test 08 #spaced
268megafind=sort(cmd_ef(FIND+" "+"ls\ 01"))
269localfind=sort(find('localUPs/ls 01',"ls 01"))
270compare_remote_local(megafind,localfind)
271
272#Test 09 #XX/..
273currentTest=9
274megafind=sort(cmd_ef(FIND+" "+"ls\ 01/.."))
275localfind=sort(find('localUPs/',"/"))
276compare_remote_local(megafind,localfind)
277
278#Test 10 #..
279cmd_ef(CD+' le01')
280megafind=sort(cmd_ef(FIND+" "+".."))
281cmd_ef(CD+' /')
282localfind=sort(find('localUPs/',"/"))
283compare_remote_local(megafind,localfind)
284
285#Test 11 #complex stuff
286megafind=sort(cmd_ef(FIND+" "+"ls\ 01/../le01/les01" +" " +"lf01/../ls\ *01/ls\ s02"))
287localfind=sort(find('localUPs/le01/les01',"/le01/les01"))
288localfind+="\n"+sort(find('localUPs/ls 01/ls s02',"/ls 01/ls s02"))
289compare_remote_local(megafind,localfind)
290
291#Test 12 #folder/
292megafind=sort(cmd_ef(FIND+" "+"le01/"))
293localfind=sort(find('localUPs/le01',"le01"))
294compare_remote_local(megafind,localfind)
295
296if not CMDSHELL: #TODO: currently there is no way to know last CMSHELL status code
297
298    #Test 13 #file01.txt/non-existent
299    megafind,status=cmd_ec(FIND+" "+"file01.txt/non-existent")
300    if status == 0:
301        print "test "+str(currentTest)+" failed!"
302        exit(1)
303    else:
304        print "test "+str(currentTest)+" succesful!"
305
306currentTest=14
307
308#Test 14 #/
309compare_find('/')
310
311###TODO: do stuff in shared folders...
312
313###################
314
315# Clean all
316if not VERBOSE:
317    clean_all()
318