File: create_sql_from_desc.py

package info (click to toggle)
gaby 2.0.2-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 6,148 kB
  • ctags: 3,101
  • sloc: ansic: 48,660; sh: 8,710; python: 1,161; makefile: 951; perl: 265; sed: 93; xml: 89; sql: 25
file content (47 lines) | stat: -rwxr-xr-x 930 bytes parent folder | download | duplicates (4)
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
#! /usr/bin/env python

import sys
import string

try:
	f = open(sys.argv[1])
except:
	print 'Usage: create_sql_from_desc.py descfile'
	sys.exit(1)

lines = f.readlines()
f.close()

intables = 0
intable = 0

tables = []
t = ''
for l in lines:
	if l == 'Begin tables\n':
		intables = 1
		continue
	if intables == 1 and l == 'End\n':
		break
	if intables == 1 and l[0] == '\t' and l[1] != '\t':
		if intable == 1:
			t = t[:-2] + '\n);\n'
			tables.append(t)
		tname = string.split(l[1:], ':')[0]
		if tname[-1] == '\n': tname = tname[:-1]
		t = 'create table %s (\n' % tname
		t = t + '\tid\tinteger\t\tprimary key,\n'
		intable = 1
		continue
	if intable == 1 and l[0:2] == '\t\t' and l[2] != '\t':
		fname = string.split(l[2:], ':')[0]
		fname = string.replace(fname, ' ', '_')
		fname = string.replace(fname, '/', '_')
		t = t + '\t%s\tvarchar(50),\n' % fname

t = t[:-2] + '\n);\n'
tables.append(t)

for t in tables:
	print t