File: spkg-install

package info (click to toggle)
sagemath-database-elliptic-curves 0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,304 kB
  • ctags: 3
  • sloc: python: 69; makefile: 6
file content (88 lines) | stat: -rwxr-xr-x 2,731 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python

import os
common_curves = os.path.join(os.getcwd(), 'common')

def install_cremona():
    from sqlite3 import connect

    if 'SAGE_SHARE' not in os.environ:
        raise RuntimeError("SAGE_SHARE undefined, maybe run `sage -sh`?")

    cremona_root = os.path.join(os.environ['SAGE_SHARE'], 'cremona')
    if not os.path.exists(cremona_root):
        os.makedirs(cremona_root)

    target = os.path.join(cremona_root, 'cremona_mini.db')

    if os.path.exists(target):
        os.remove(target)

    con = connect(target)

    con.execute('CREATE TABLE t_class(rank INTEGER, class TEXT PRIMARY KEY,'
            ' conductor INTEGER)')
    con.execute('CREATE TABLE t_curve(curve TEXT PRIMARY KEY, class TEXT, tors'
            ' INTEGER, eqn TEXT UNIQUE)')
    con.execute('CREATE INDEX i_t_class_conductor ON t_class(conductor)')
    con.execute('CREATE INDEX i_t_curve_class ON t_curve(class)')

    class_data = []
    curve_data = []

    for line in open(os.path.join(common_curves, 'allcurves.00000-09999')):
        N, iso, num, eqn, r, tors = line.split()
        cls = N + iso
        cur = cls + num
        if num == "1":
            class_data.append((N, cls, r))
        curve_data.append((cur, cls, eqn, tors))

    con.executemany('INSERT INTO t_class(conductor,class,rank) VALUES'
            ' (?,?,?)', class_data)
    con.executemany('INSERT INTO t_curve(curve,class,eqn,tors) VALUES'
            ' (?,?,?,?)', curve_data)

    con.commit()

def install_ellcurves():
    import shutil, tempfile

    if 'SAGE_SHARE' not in os.environ:
        raise RuntimeError("SAGE_SHARE undefined, maybe run `sage -sh`?")

    target = os.path.join(os.environ['SAGE_SHARE'], 'ellcurves')
    if os.path.exists(target):
        try:
            shutil.rmtree(target)
        except OSError:
            os.remove(target)

    shutil.move(os.path.join(os.getcwd(), 'ellcurves'), target)
    rank = {}
    for line in open(os.path.join(common_curves, 'allcurves.00000-09999')):
        r = line.split()[4]
        if r not in rank:
            rank[r] = open(tempfile.mkstemp()[1], 'w')
        rank[r].write(line)

    for r, f in rank.items():
        f.close()
        endpath = os.path.join(target, 'rank' + r)
        if os.path.exists(endpath):
            old = tempfile.mkstemp()[1]
            shutil.move(endpath, old)
            shutil.move(f.name, endpath)
            f = open(endpath, 'a')
            tmp = open(old, 'r')
            f.write(tmp.read())
            tmp.close()
            f.close()
            os.remove(old)
        else:
            shutil.move(f.name, endpath)
        os.chmod(endpath, 0o644)

if __name__ == '__main__':
    install_cremona()
    install_ellcurves()