# run all the Autoplot jython scripts in this folder.
#
# tests should output test_NAME.txt, or test_NAME.png test_NAME.stdout.txt is created automatically.

import subprocess
import os 
import sys

def _runone( i ):
  #subprocess.call( ['echo', '%s' % i ] )
  JAVA='/usr/local/jdk-8/bin/java'   
  #JAR='/mynet/spot8/var/local/jenkins/jobs/autoplot-jar-all/lastSuccessful/archive/autoplot/Autoplot/dist/AutoplotAll.jar'
  JAR='../autoplot-jar-all/autoplot/Autoplot/dist/AutoplotAll.jar'

  if not ( i.startswith('https://') or i.startswith('http://') ):
      # make relative names absolute
      i= 'https://raw.githubusercontent.com/autoplot/dev/master/'+i
  else:
      print '***********************************************************'
      print '* scripts outside of autoplot/dev/master are not allowed. *'
      print '***********************************************************'
      return

  ID= i.rsplit('/',1)[1]

  #JAVA_VM_ARGS="-Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=12345,suspend=y"
  JAVA_VM_ARGS=""

  if len(JAVA_VM_ARGS)==0:
     cmd= [ JAVA, '-Djava.awt.headless=true', '-cp', JAR, 'org.autoplot.AutoplotUI', '--script', '%s' % i ]
  else:
     cmd= [ JAVA, '-Djava.awt.headless=true' ] + JAVA_VM_ARGS.split(' ')+['-cp', JAR, 'org.autoplot.AutoplotUI', '--script', '%s' % i ]
  print ' '.join(cmd)

  sys.stdout.flush()
  
  if ( not os.path.exists( JAVA ) ):
      print 'no java'
      raise Exception('no java: '+JAVA )
  print 'open test052_%s.stderr.txt' % ID
  STDERR_= open( 'test052_%s.stderr.txt' % ID, 'w' )
  
  print 'do subprocess.call...'
  proc= subprocess.Popen( cmd, stderr=STDERR_, stdout=subprocess.PIPE )

  STDOUT_= open( 'test052_%s.stdout.txt' % ID, 'w' )
  
  for line in iter(proc.stdout.readline,''):
     STDOUT_.write( line )
  STDOUT_.close()
  STDERR_.close()

  print '~~~~~'

  if len(JAVA_VM_ARGS)==0:
     cmd= [ JAVA, '-Djava.awt.headless=true', '-cp', JAR, 'org.autoplot.AutoplotUI', '--script', '/home/jbf/ct/hudson/script/test052/_copyScript.jy', 'src='+i, 'pwdd='+os.getcwd() ]
  else:
     cmd= [ JAVA, '-Djava.awt.headless=true' ] + JAVA_VM_ARGS.split(' ')+['-cp', JAR, 'org.autoplot.AutoplotUI', '--script','/home/jbf/ct/hudson/script/test052/_copyScript.jy',  'src='+i, 'pwdd='+os.getcwd() ]
  print ' '.join(cmd)

  sys.stdout.flush()
  
  STDERR_= open( 'copyScript_%s.err.txt' % ID, 'w' )

  print 'do subprocess.call...'
  proc= subprocess.Popen( cmd, stderr=STDERR_, stdout=subprocess.PIPE )
  print 'done'

  STDOUT_= open( 'copyScript_%s.txt' % ID, 'w' )
  for line in iter(proc.stdout.readline,''):
     STDOUT_.write( line )
  STDOUT_.close()
  STDERR_.close()


import urllib2

import sys, time
for i in urllib2.urlopen('https://raw.githubusercontent.com/autoplot/dev/master/test052.txt'):
   i= i.partition('#')[0]
   i= i.strip() 
   if ( len(i)==0 ): continue
   print '== %s ==' % i
   sys.stdout.flush()
   t0= time.time()
   print 'started test'
   _runone( i )
   print 'completed test %s in %.1f seconds' % ( i, ( time.time()-t0 ) )
   print ''

