Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 15th, 2012  |  syntax: Python  |  size: 2.75 KB  |  hits: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env python
  2.  
  3. import os
  4. common_curves = os.path.join(os.getcwd(), 'src', 'common')
  5.  
  6. def install_cremona():
  7.     from sqlite3 import connect
  8.  
  9.     if 'SAGE_DATA' not in os.environ:
  10.         raise RuntimeError("SAGE_DATA undefined, maybe run `sage -sh`?")
  11.  
  12.     cremona_root = os.path.join(os.environ['SAGE_DATA'], 'cremona')
  13.     if not os.path.exists(cremona_root):
  14.         os.makedirs(cremona_root)
  15.  
  16.     target = os.path.join(cremona_root, 'cremona_mini.db')
  17.  
  18.     if os.path.exists(target):
  19.         os.remove(target)
  20.  
  21.     con = connect(target)
  22.  
  23.     con.execute('CREATE TABLE t_class(rank INTEGER, class TEXT PRIMARY KEY,'
  24.             ' conductor INTEGER)')
  25.     con.execute('CREATE TABLE t_curve(curve TEXT PRIMARY KEY, class TEXT, tors'
  26.             ' INTEGER, eqn TEXT UNIQUE)')
  27.     con.execute('CREATE INDEX i_t_class_conductor ON t_class(conductor)')
  28.     con.execute('CREATE INDEX i_t_curve_class ON t_curve(class)')
  29.  
  30.     class_data = []
  31.     curve_data = []
  32.  
  33.     for line in open(os.path.join(common_curves, 'allcurves.00000-09999')):
  34.         N, iso, num, eqn, r, tors = line.split()
  35.         cls = N + iso
  36.         cur = cls + num
  37.         if num == "1":
  38.             class_data.append((N, cls, r))
  39.         curve_data.append((cur, cls, eqn, tors))
  40.  
  41.     con.executemany('INSERT INTO t_class(conductor,class,rank) VALUES'
  42.             ' (?,?,?)', class_data)
  43.     con.executemany('INSERT INTO t_curve(curve,class,eqn,tors) VALUES'
  44.             ' (?,?,?,?)', curve_data)
  45.  
  46.     con.commit()
  47.  
  48. def install_ellcurves():
  49.     import shutil, tempfile
  50.  
  51.     if 'SAGE_DATA' not in os.environ:
  52.         raise RuntimeError("SAGE_DATA undefined, maybe run `sage -sh`?")
  53.  
  54.     target = os.path.join(os.environ['SAGE_DATA'], 'ellcurves')
  55.     if os.path.exists(target):
  56.         try:
  57.             shutil.rmtree(target)
  58.         except OSError:
  59.             os.remove(target)
  60.  
  61.     shutil.move(os.path.join('src', 'ellcurves'), target)
  62.     rank = {}
  63.     for line in open(os.path.join(common_curves, 'allcurves.00000-09999')):
  64.         r = line.split()[4]
  65.         if r not in rank:
  66.             rank[r] = open(tempfile.mkstemp()[1], 'w')
  67.         rank[r].write(line)
  68.  
  69.     for r, f in rank.iteritems():
  70.         f.close()
  71.         endpath = os.path.join(target, 'rank' + r)
  72.         if os.path.exists(endpath):
  73.             old = tempfile.mkstemp()[1]
  74.             shutil.move(endpath, old)
  75.             shutil.move(f.name, endpath)
  76.             f = open(endpath, 'a')
  77.             tmp = open(old, 'r')
  78.             f.write(tmp.read())
  79.             tmp.close()
  80.             f.close()
  81.             os.remove(old)
  82.         else:
  83.             shutil.move(f.name, endpath)
  84.         os.chmod(endpath, 0644)
  85.  
  86. if __name__ == '__main__':
  87.     install_cremona()
  88.     install_ellcurves()