]> git.phdru.name Git - sqlconvert.git/commitdiff
Add script mysql-to-sql.py
authorOleg Broytman <phd@phdru.name>
Sat, 27 Aug 2016 14:39:20 +0000 (17:39 +0300)
committerOleg Broytman <phd@phdru.name>
Sat, 27 Aug 2016 14:39:20 +0000 (17:39 +0300)
Cannot be name mysql2sql.py to avoid collision with the package.

scripts/mysql-to-sql.py [new file with mode: 0755]
setup.py

diff --git a/scripts/mysql-to-sql.py b/scripts/mysql-to-sql.py
new file mode 100755 (executable)
index 0000000..add0a8c
--- /dev/null
@@ -0,0 +1,48 @@
+#! /usr/bin/env python
+
+import argparse
+import sys
+
+from mysql2sql.print_tokens import print_tokens
+from mysql2sql.process_tokens import requote_names, StatementGrouper
+
+
+def main(infile, outfile):
+    grouper = StatementGrouper()
+    for line in infile:
+        grouper.process_line(line)
+        if grouper.statements:
+            for statement in grouper.get_statements():
+                requote_names(statement)
+                print_tokens(statement, outfile=outfile)
+    tokens = grouper.close()
+    if tokens:
+        for token in tokens:
+            print_tokens(token, outfile=outfile)
+
+
+if __name__ == '__main__':
+    parser = argparse.ArgumentParser(description='Convert MySQL to SQL')
+    parser.add_argument('-i', '--infile', help='input file name')
+    parser.add_argument('-o', '--outfile', help='output file name')
+    args = parser.parse_args()
+
+    if args.infile:
+        infile = open(args.infile, 'rt')
+    else:
+        infile = sys.stdin
+        if infile.isatty():
+            parser.print_help()
+            sys.exit()
+
+    if args.outfile:
+        try:
+            outfile = open(args.outfile, 'wt')
+        except:
+            if infile is not sys.stdin:
+                infile.close()
+            raise
+    else:
+        outfile = sys.stdout
+
+    main(infile, outfile)
index fe59c28d003d489ff9d4990e26cfd5c5f504fbc0..5124f03fd5bc440b3368c46843de04288edd2728 100755 (executable)
--- a/setup.py
+++ b/setup.py
@@ -47,7 +47,7 @@ setup(name='mysql2sql',
           'Programming Language :: Python :: 3.4',
       ],
       packages=['mysql2sql'],
-      scripts=[],
       package_data={},
+      scripts=['scripts/mysql-to-sql.py'],
       requires=[],
       **kw)