File: xdata2sql.pl

package info (click to toggle)
acl2 8.6%2Bdfsg-3
  • links: PTS
  • area: main
  • in suites: sid
  • size: 1,138,276 kB
  • sloc: lisp: 17,818,294; java: 125,359; python: 28,122; javascript: 23,458; cpp: 18,851; ansic: 11,569; perl: 7,678; xml: 5,591; sh: 3,978; makefile: 3,840; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (159 lines) | stat: -rwxr-xr-x 4,628 bytes parent folder | download | duplicates (3)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env perl

# XDOC Documentation System for ACL2
# Copyright (C) 2009-2013 Centaur Technology
#
# Contact:
#   Centaur Technology Formal Verification Group
#   7600-C N. Capital of Texas Highway, Suite 300, Austin, TX 78731, USA.
#   http://www.centtech.com/
#
# License: (An MIT/X11-style license)
#
#   Permission is hereby granted, free of charge, to any person obtaining a
#   copy of this software and associated documentation files (the "Software"),
#   to deal in the Software without restriction, including without limitation
#   the rights to use, copy, modify, merge, publish, distribute, sublicense,
#   and/or sell copies of the Software, and to permit persons to whom the
#   Software is furnished to do so, subject to the following conditions:
#
#   The above copyright notice and this permission notice shall be included in
#   all copies or substantial portions of the Software.
#
#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
#   DEALINGS IN THE SOFTWARE.
#
# Original author: Jared Davis <jared@centtech.com>

use warnings;
use strict;
use DBI;
use JSON::XS;
use File::Temp qw/ tempdir /;
use Cwd;
use File::Copy;

print <<END;

-------------------------------------------------------------------------------

xdata2sql.pl: Convert xdata.js into xdata.db (an sqlite3 database)

   NOTE: Most folks don\'t need to run this at all!
         If you just want to:
          - browse an XDOC manual on your local hard drive, or
          - share an XDOC manual on your fast intranet
         then ignore this script and just see index.html.

The main use for this script is to share XDOC manuals on the internet.  In this
scenario, just having the web browser load in the entire (generally 20+ MB)
xdata.js file is not very practical, because some users will have slow
connections and will take too long to load the file.

There are many ways to solve this.  Our approach is to convert xdata.js into an
sqlite3 database, and then use a server-side script that will allow us to
access topics only as they are requested.

-------------------------------------------------------------------------------

END

print "; Converting xdata.js\n\n";

sub read_whole_file {
  my $filename = shift;
  open (my $fh, "<", $filename) or die("Can't open $filename: $!\n");
  local $/ = undef;
  my $ret = <$fh>;
  close($fh);
  return $ret;
}

if (! -f "xdata.js") {
    print "Error: xdata.js not found.\n";
    exit(1);
}


print "; Reading file\n";

my $javascript = read_whole_file("xdata.js");


print "; Checking file\n";

my $json;
my $start = "var xdata = ";
if (length($start) < length($javascript)
    && substr($javascript, 0, length($start)) eq $start
    && substr($javascript, length($javascript)-1, 1) eq ";")
{
    my $stop = length($javascript) - length($start) - length(";");
    $json = substr($javascript, length($start), $stop);
}
else {
    print "Error: xdata.js does not have the expected format\n";
    exit(1);
}


print "; Parsing JSON data.\n";
my $xs = new JSON::XS;
my $xdata = $xs->decode($json);
if (!(ref($xdata) eq "HASH")) {
    print "Error: JSON object within xdata.js not a hash?\n";
    exit(1);
}

if (-f "xdata.db") {
    print "; Deleting old xdata.db.\n";
    unlink("xdata.db");
}


print "; Creating new xdata.db.\n";

my $curdir = cwd;
my $dir = tempdir( "xdata2sql_tmpXXXXXX", CLEANUP => 1, TMPDIR => 1);
print "; Using temporary dir $dir\n";

chdir($dir) or die("can't change to temp directory $dir\n");

my $dbh = DBI->connect("dbi:SQLite:dbname=xdata.db", "", "",
		       {RaiseError=>1, AutoCommit=>0})
    or die $DBI::errstr;

print "; Creating xdoc_data table.\n";

$dbh->do("CREATE TABLE XTABLE ("
	 . "XKEY TEXT PRIMARY KEY NOT NULL,"
	 . "XDATA TEXT)");


print "; Populating xdoc_data table.\n";
my $query = $dbh->prepare("INSERT INTO XTABLE (XKEY, XDATA) VALUES (?, ?)");

while(my ($key,$val) = each %$xdata)
{
    my $enc = $xs->encode($val);
    $query->bind_param(1, $key);
    $query->bind_param(2, $enc);
    $query->execute();
}

$dbh->commit();
$dbh->disconnect();

print "; Moving xdata.db back to $curdir\n";
move("xdata.db", "${curdir}/xdata.db");


print "; All done.\n\n";


print "To actually use the database, see xdataget.pl.\n\n";