# test http filesystem updates.  
# On 2013-09-06 this showed:
#   * file updates are limited to once per 60 seconds.    
#   * file length() method will indicate a change in length, but the old file is returned.
#   * the website github always returns the current time, so it should always update, but instead
#     never updates

from java.io import File, FileOutputStream
from org.das2.util.filesystem import FileSystem
from java.util import Date
from java.lang import System

# stupid hash function, do not use elsewhere
def hashit( f ):
    f= f.toString()
    ff= open(f)
    s= ff.readline()
    i=1
    for ch in s:
        i= i+ord(ch)    
    ff.close()
    return i

print 'cacheDir=', FileSystem.settings().getLocalCacheDir()

f= File( '/home/jbf/public_html/autoplot/test/afile.txt' )
if ( f.exists() ):
    f.delete()

t0= System.currentTimeMillis()-1000;  # so relative times are always positive

FileSystem.reset()

print '-- test0 ----'

f= File('/home/jbf/public_html/autoplot/test/afile.txt')
fo= FileOutputStream(f)
fo.write('01234')
fo.close()
#print 'actual modified: ', f.lastModified()-t0, Date(f.lastModified())
print 'actual length: ', f.length()
# ----

fs= FileSystem.create('http://sarahandjeremy.net/~jbf/autoplot/test/')
fo= fs.getFileObject('afile.txt')
ff= fo.getFile( monitor )
print ff
#print 'fo modified: ', fo.lastModified().getTime()-t0, Date(fo.lastModified().getTime())
print 'fo length: ', fo.getSize()
print 'local length: ', ff.length()

# ----
w= 15
print 'wait %d seconds...' % w
from java.lang.Thread import sleep
sleep(w*1000)

print 'modify the length of the file on the server side'
f= File('/home/jbf/public_html/autoplot/test/afile.txt')
fo= FileOutputStream(f)
fo.write('0123456789')
fo.close()
print 'actual file: ', f
#print 'actual modified: ', f.lastModified()-t0, Date(f.lastModified())
print 'actual length: ', f.length()
print 'actual hashit: ', hashit( f )

print '-- test1.5 ----'
fs= FileSystem.create('http://sarahandjeremy.net/~jbf/autoplot/test/')
fo= fs.getFileObject('afile.txt')
#print 'fo modified: ', fo.lastModified().getTime()-t0, Date(fo.lastModified().getTime())
ff= fo.getFile( monitor )
print 'local file: ', ff
print 'fo length: ', fo.getSize()
print 'local length: ', ff.length()
print 'local hashit: ', hashit(ff )

w= 15 
print 'wait %d seconds...' % w
from java.lang.Thread import sleep
sleep(w*1000)

print '-- test2 ----'

fs= FileSystem.create('http://sarahandjeremy.net/~jbf/autoplot/test/')

fo= fs.getFileObject('afile.txt')
#print 'fo modified: ', fo.lastModified().getTime()-t0, Date(fo.lastModified().getTime())
ff= fo.getFile( monitor )
print 'local file: ', ff
print 'fo length: ', fo.getSize()
print 'local length: ', ff.length()
print 'local hashit: ', hashit(ff)

print '-- test3 ----'

fs= FileSystem.create('http://sarahandjeremy.net/~jbf/autoplot/test/')

fo= fs.getFileObject('afile.txt')
print 'local file: ', ff
#print 'fo modified: ', fo.lastModified().getTime()-t0, Date(fo.lastModified().getTime())
ff= fo.getFile( monitor )
print 'fo length: ', fo.getSize()
print 'local length: ', ff.length()
print 'local hashit: ', hashit(ff)

sleep(w*1000)

print '-- test4 ----'

print 'modify the contents of the file on the server side'
f= File('/home/jbf/public_html/autoplot/test/afile.txt')
fo= FileOutputStream(f)
fo.write('9123456789')
fo.close()
print 'actual file: ', f
#print 'actual modified: ', f.lastModified()-t0, Date(f.lastModified())
print 'actual length: ', f.length()
print 'actual hashit: ', hashit( f )

fs= FileSystem.create('http://sarahandjeremy.net/~jbf/autoplot/test/')

fo= fs.getFileObject('afile.txt')
print 'local file: ', ff
#print 'fo modified: ', fo.lastModified().getTime()-t0, Date(fo.lastModified().getTime())
ff= fo.getFile( monitor )
print 'fo length: ', fo.getSize()
print 'local length: ', ff.length()
print 'local hashit: ', hashit(ff)

