# HG changeset patch
# User Mitesh Patel <qed777@gmail.com>
# Date 1260758966 28800
# Node ID a5fe7b3281d407d75ca683402200b29c97939ea3
# Parent 2c17a7cee6e7b76fe67053f34c20ed7c6c33d7cb
#7650/doctest: Doctest option to force "library code" test mode
diff git a/sagedoctest b/sagedoctest
a

b

def is_64bit(): 
57  57  ###################################################### 
58  58  DOT_SAGE = os.environ['DOT_SAGE'] 
59  59  if 'SAGE_TESTDIR' not in os.environ or os.environ['SAGE_TESTDIR'] is "": 
60   SAGE_TESTDIR = DOT_SAGE + "/tmp" 
 60  SAGE_TESTDIR = os.path.join(DOT_SAGE, "tmp") 
61  61  else: 
62  62  SAGE_TESTDIR = os.environ['SAGE_TESTDIR'] 
63  63  
… 
… 
def delete_tmpfiles(): 
75  75  ###################################################### 
76  76  SAGE_ROOT = os.environ["SAGE_ROOT"] 
77  77  LD = os.environ["LD_LIBRARY_PATH"] 
78   os.environ["LD_LIBRARY_PATH"] = SAGE_ROOT + "/local/lib:" + LD 
79   os.environ["PYTHONPATH"]=SAGE_ROOT + "/local/lib/python/sitepackages" 
 78  os.environ["LD_LIBRARY_PATH"] = os.path.join(SAGE_ROOT, "local/lib") + ":" + LD 
 79  os.environ["PYTHONPATH"] = os.path.join(SAGE_ROOT, 
 80  "local/lib/python/sitepackages") 
80  81  if os.environ.has_key('SAGE_PATH'): 
81   os.environ["PYTHONPATH"]= os.environ["PYTHONPATH"]+ ':'+os.environ['SAGE_PATH'] 
 82  os.environ["PYTHONPATH"] = os.environ["PYTHONPATH"] + ':' + os.environ['SAGE_PATH'] 
82  83  
83  84  
84  85  ###################################################### 
85  86  # Custom flags for the valgrind modes 
86  87  ###################################################### 
87  88  try: 
88   SAGE_MEMCHECK_FLAGS=os.environ['SAGE_MEMCHECK_FLAGS'] 
 89  SAGE_MEMCHECK_FLAGS = os.environ['SAGE_MEMCHECK_FLAGS'] 
89  90  print SAGE_MEMCHECK_FLAGS 
90  91  except: 
91   SAGE_MEMCHECK_FLAGS=" leakresolution=high logfile=$HOME/.sage/valgrind/sagememcheck.%p leakcheck=full numcallers=25 suppressions=$SAGE_LOCAL/lib/valgrind/sage.supp " 
 92  SAGE_MEMCHECK_FLAGS = " leakresolution=high logfile=$HOME/.sage/valgrind/sagememcheck.%p leakcheck=full numcallers=25 suppressions=$SAGE_LOCAL/lib/valgrind/sage.supp " 
92  93  
93  94  try: 
94   SAGE_MASSIF_FLAGS=os.environ['SAGE_MASSIF_FLAGS'] 
 95  SAGE_MASSIF_FLAGS = os.environ['SAGE_MASSIF_FLAGS'] 
95  96  except: 
96   SAGE_MASSIF_FLAGS=" depth=6 logfile=$HOME/.sage/valgrind/sagemassif.%p " 
 97  SAGE_MASSIF_FLAGS = " depth=6 logfile=$HOME/.sage/valgrind/sagemassif.%p " 
97  98  
98  99  try: 
99   SAGE_CALLGRIND_FLAGS=os.environ['SAGE_CALLGRIND_FLAGS'] 
 100  SAGE_CALLGRIND_FLAGS = os.environ['SAGE_CALLGRIND_FLAGS'] 
100  101  except: 
101  102  SAGE_CALLGRIND_FLAGS = " logfile=$HOME/.sage/valgrind/sagecallgrind.%p " 
102  103  
103  104  try: 
104   SAGE_CACHEGRIND_FLAGS=os.environ['SAGE_CACHEGRIND_FLAGS'] 
 105  SAGE_CACHEGRIND_FLAGS = os.environ['SAGE_CACHEGRIND_FLAGS'] 
105  106  except: 
106   SAGE_CACHEGRIND_FLAGS=" logfile=$HOME/.sage/valgrind/sagecachegrind.%p " 
 107  SAGE_CACHEGRIND_FLAGS = " logfile=$HOME/.sage/valgrind/sagecachegrind.%p " 
107  108  
108  109  try: 
109   SAGE_OMEGA_FLAGS=os.environ['SAGE_OMEGA_FLAGS'] 
 110  SAGE_OMEGA_FLAGS = os.environ['SAGE_OMEGA_FLAGS'] 
110  111  except: 
111  112  SAGE_OMEGA_FLAGS = " logfile=$HOME/.sage/valgrind/sageomega.%p " 
112  113  
… 
… 
def new_index(n): 
148  149  return n 
149  150  
150  151  def test_code(filename): 
151   dict = { 'DIR' : repr('%s/local/bin' % SAGE_ROOT), 
 152  dict = { 'DIR' : repr(os.path.join(SAGE_ROOT, 'local/bin')), 
152  153  'FILENAME' : repr(filename), 
153  154  'OUTPUT_FILENAME' : repr(filename + '.timeit.sobj'), 
154  155  'TIMEIT' : do_timeit, # global 
… 
… 
def change_warning_output(file): 
404  405  if not library_code: 
405  406  if ext in ['.py', '.pyx','.spyx']: 
406  407  os.system('cp f %s %s' % (file_name, SAGE_TESTDIR)) 
407   tmpfiles.append('%s/%s%s' % (SAGE_TESTDIR, name, ext)) 
 408  tmpfiles.append(os.path.join(SAGE_TESTDIR, '%s%s' % (name, ext))) 
408  409  if ext == '.py': 
409  410  s += "from %s import *\n\n" % name 
410  411  else: 
… 
… 
def change_warning_output(file): 
412  413  elif ext == '.sage': 
413  414  os.system('sage preparse %s' % file_name) 
414  415  os.system('mv f %s.py %s' % (base, SAGE_TESTDIR)) 
415   tmpfiles.append('%s/%s.py' % (SAGE_TESTDIR, name)) 
 416  tmpfiles.append(os.path.join(SAGE_TESTDIR, name + '.py')) 
416  417  s += "from %s import *\n\n" % (name) 
417  418  if ext in ['.py', '.sage']: 
418   tmpfiles.append('%s/%s.pyc' % (SAGE_TESTDIR, name)) 
 419  tmpfiles.append(os.path.join(SAGE_TESTDIR, name + '.pyc')) 
419  420  
420  421  n = 0 
421  422  while True: 
… 
… 
def test_file(file, library_code): 
589  590  if len(s) == 0: 
590  591  sys.exit(0) 
591  592  
592   f = "%s/.doctest_%s.py"%(SAGE_TESTDIR,name) 
 593  f = os.path.join(SAGE_TESTDIR, ".doctest_%s.py" % name) 
593  594  
594  595  open(f,"w").write(s) 
595  596  tmpfiles.append(f) 
… 
… 
def test_file(file, library_code): 
610  611  if omega: 
611  612  cmd = "valgrind tool=expomega " + SAGE_OMEGA_FLAGS + cmd 
612  613  
613   VALGRIND = '%s/valgrind/'%DOT_SAGE 
 614  VALGRIND = os.path.join(DOT_SAGE, 'valgrind') 
614  615  if not os.path.exists(VALGRIND): 
615  616  os.makedirs(VALGRIND) 
616  617  
… 
… 
if __name__ == '__main__': 
742  743  massif = has_opt('massif') 
743  744  cachegrind = has_opt('cachegrind') 
744  745  omega = has_opt('omega') 
 746  force_lib = has_opt('force_lib') 
745  747  random_order = parse_rand() 
746  748  only_optional, only_optional_tags = parse_only_opt() 
747  749  if long_time: 
… 
… 
if __name__ == '__main__': 
751  753  if argv[1][0] == '': 
752  754  usage() 
753  755  
 756  ext = os.path.splitext(argv[1])[1] 
 757  
754  758  library_code = True 
755   ext = os.path.splitext(argv[1])[1] 
756   if ext in ['.spyx', '.sage'] or \ 
757   not (os.path.realpath(os.path.join(SAGE_ROOT, 'devel')) in os.path.realpath(argv[1])): 
 759  dev_path = os.path.realpath(os.path.join(SAGE_ROOT, 'devel')) 
 760  our_path = os.path.realpath(argv[1]) 
 761  
 762  if not force_lib and (ext in ['.spyx', '.sage'] or 
 763  not dev_path in our_path): 
758  764  library_code = False 
759  765  
760  766  try: 
diff git a/sageptest b/sageptest
a

b

except: 
17  17  numglobaliteration = 1 
18  18  
19  19  SAGE_ROOT = os.environ['SAGE_ROOT'] 
 20  SAGE_SITE = os.path.realpath(os.path.join(os.environ['SAGE_LOCAL'], 
 21  'lib/python/sitepackages')) 
20  22  BUILD_DIR = os.path.realpath(SAGE_ROOT + "/devel/sage/build") 
21  23  
22  24  print 'Global iterations: ' + str(numglobaliteration) 
… 
… 
def test_file(F): 
122  124  """ 
123  125  outfile = tempfile.NamedTemporaryFile() 
124  126  base, ext = os.path.splitext(F) 
 127  
125  128  if use_sage_only or ext == '.sage': 
126  129  cmd = 'doctest_tex ' + opts 
127  130  elif ext in ['.py', '.pyx', '.tex', '.pxi', '.rst']: 
128   cmd = 'doctest '+opts 
 131  cmd = 'doctest ' + opts 
 132  if SAGE_SITE in os.path.realpath(F): 
 133  cmd += ' force_lib' 
 134  
129  135  filestr = os.path.split(F)[1] 
130  136  for i in range(0,numiteration): 
131  137  os.chdir(os.path.dirname(F)) 
… 
… 
def populatefilelist(filelist): 
234  240  return 0 
235  241  
236  242  
237   
238   
239   
240   
241   
242   
243   
244   
245  243  for gr in range(0,numglobaliteration): 
246  244  
247  245  try: 
diff git a/sagetest b/sagetest
a

b

def strip_automount_prefix(filename): 
41  41  return filename 
42  42  
43  43  
44   SAGE_ROOT=os.environ['SAGE_ROOT'] 
 44  SAGE_ROOT = os.environ['SAGE_ROOT'] 
 45  SAGE_SITE = os.path.realpath(os.path.join(os.environ['SAGE_LOCAL'], 
 46  'lib/python/sitepackages')) 
 47  
45  48  if 'SAGE_TESTDIR' not in os.environ: 
46   os.environ['SAGE_TESTDIR'] = SAGE_ROOT + "/tmp" 
47   TMP=os.environ['SAGE_TESTDIR'] + "/tmp" 
 49  os.environ['SAGE_TESTDIR'] = os.path.join(SAGE_ROOT, "tmp") 
 50  TMP = os.path.join(os.environ['SAGE_TESTDIR'], "tmp") 
48  51  if not os.path.exists(TMP): 
49  52  os.makedirs(TMP) 
50  53  
… 
… 
def sage_test_command(f): 
56  59  
57  60  def skip(F): 
58  61  G = abspath(F) 
59   i = G.rfind('/') 
60   if os.path.exists('%s/nodoctest.py'%G[:i]): 
 62  i = G.rfind(os.sep) 
 63  if os.path.exists(os.path.join(G[:i], 'nodoctest.py')): 
61  64  print "%s (skipping)  nodoctest.py file in directory"%sage_test_command(F) 
62  65  return True 
63  66  
64  67  if 'nodoctest' in open(G).read()[:50]: 
65  68  return True 
66   if G.find('doc/output') != 1: 
 69  if G.find(os.path.join('doc', 'output')) != 1: 
67  70  return True 
68  71  
69  72  sys.stdout.write("%60s"%sage_test_command(F)+"\n") 
… 
… 
def test(F, cmd): 
77  80  if skip(F): 
78  81  return 0 
79  82  try: 
80   s = '%s/local/bin/sage%s "%s"'%(SAGE_ROOT, cmd, F) 
 83  s = os.path.join(SAGE_ROOT, 'local/bin/sage%s' % cmd) + ' "%s"' % F 
81  84  err = os.system(s) # need to catch control c by writing to a file and looking at it?? 
82  85  except KeyboardInterrupt: 
83  86  print "Keyboard Interrupt" 
… 
… 
def test(F, cmd): 
92  95  
93  96  def test_file(F): 
94  97  if not os.path.exists(F): 
95   if os.path.exists(os.environ["SAGE_ROOT"] + "/" + F): 
96   F = os.environ["SAGE_ROOT"] + "/" + F 
 98  if os.path.exists(os.path.join(SAGE_ROOT, F)): 
 99  F = os.path.join(SAGE_ROOT, F) 
97  100  if not os.path.exists(F): 
98  101  if F[:6] != "__test" and not F.endswith('.png'): 
99   print "ERROR: File %s/%s is missing"%(os.curdir,F) 
100   failed.append('%s/%s'%(os.curdir,F)) 
 102  print "ERROR: File %s is missing" % os.path.join(os.curdir, F) 
 103  failed.append(os.path.join(os.curdir, F)) 
101  104  return 1 
102  105  
 106  extra_opts = '' 
 107  if SAGE_SITE in os.path.realpath(F): 
 108  extra_opts = ' force_lib' 
 109  
103  110  base, ext = os.path.splitext(F) 
104  111  if ext in ['.py', '.spyx', '.pyx', '.tex', '.pxi', '.sage', '.rst']: 
105   return test(F, 'doctest '+opts) 
 112  return test(F, 'doctest ' + opts + extra_opts) 
106  113  elif os.path.isdir(F) and not (F[:1] == '.') \ 
107   and not '#' in F and not '/notes' in F: 
 114  and not '#' in F and not os.sep + 'notes' in F: 
108  115  ld = os.listdir(F) 
109  116  if not ('__nodoctest__' in ld): 
110  117  for L in ld: 
111   k = test_file('%s/%s'%(F,L)) 
 118  k = test_file(os.path.join(F, L)) 
112  119  return 0 
113  120  
114  121  files = argv[1:] 