From: Oleg Broytman Date: Sat, 27 Aug 2016 14:39:20 +0000 (+0300) Subject: Add script mysql-to-sql.py X-Git-Tag: 0.0.1~15 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=979d3300ae140d6a07c662c899b00a5de84f8b38;p=sqlconvert.git Add script mysql-to-sql.py Cannot be name mysql2sql.py to avoid collision with the package. --- diff --git a/scripts/mysql-to-sql.py b/scripts/mysql-to-sql.py new file mode 100755 index 0000000..add0a8c --- /dev/null +++ b/scripts/mysql-to-sql.py @@ -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) diff --git a/setup.py b/setup.py index fe59c28..5124f03 100755 --- 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)