Commits

Miki Tebeka committed 8628534

0.2.0 with multi line support

Comments (0)

Files changed (5)

+2013-01-10 version 0.2.0
+    * Multi line statements, need ; for end
+
 2013-01-10 version 0.1.0
     * Initial release

hiver/__init__.py

-__version__ = '0.1.0'
+__version__ = '0.2.0'
 
 DEFAULT_HOST = 'localhost'
 DEFAULT_PORT = 10000

hiver/__main__.py

 import re
 from os import environ
 from os.path import expanduser, isfile
+from sys import stdin
+import atexit
 
 HOST = environ.get('HIVE_HOST', DEFAULT_HOST)
 PORT = int(environ.get('HIVE_PORT', DEFAULT_PORT))
 
 
 def fix_hql(hql):
-    # 'SHOW TABLES;' -> 'SHOW TABLES'
-    return re.sub('\s*;+\s*$', '', hql, re.M|re.S)
+    return hql.replace(';', '').strip()
 
 
-def repl(client):
-    import readline
-    if isfile(HISTFILE):
-        readline.read_history_file(HISTFILE)
+def iterhql(getline):
+    buf = ''
 
     while True:
         try:
-            hql = raw_input('[hive] ').strip()
-        except (KeyboardInterrupt, EOFError):
-            readline.write_history_file(HISTFILE)
-            return
+            line = getline()
+        except EOFError:
+            break
 
-        hql = fix_hql(hql)
-        try:
-            client.execute(hql)
-            for line in client.fetchAll():
-                print(line)
-        except ThriftHive.HiveServerException as e:
-            print('ERROR: {}'.format(e))
+        if not line:
+            break
 
+        buf += line
+        while True:
+            idx = buf.find(';')
+            if idx == -1:
+                break
+            hql = fix_hql(buf[:idx])
+            if hql:
+                yield hql
+            buf = buf[idx+1:]
 
-def script(client, filename):
-    with open(filename) as fo:
-        hql = fo.read()
+    hql = fix_hql(buf)
+    if hql:
+        yield hql
 
-    hql = fix_hql(hql)
 
-    if hql.endswith(';'):
-        hql = hql[:-1]
+def execute(client, hql):
+    if not hql.strip():
+        return
 
-    client.execute(hql)
-    for line in client.fetchAll():
-        print(line)
+    try:
+        client.execute(hql)
+        lnum = 0
+        for lnum, line in enumerate(client.fetchAll(), 1):
+            print('  {}'.format(line))
+        print('({} rows)\n'.format(lnum))
+    except ThriftHive.HiveServerException as e:
+        print('ERROR: {}'.format(e))
+        return
+
+
+def repl_line():
+    try:
+        return raw_input('[hiver] ').strip()
+    except (KeyboardInterrupt, EOFError):
+        return ''
 
 
 def main(argv=None):
     parser.add_argument('--host', default=HOST)
     parser.add_argument('--port', default=PORT, type=int)
     parser.add_argument('script', help='script file', nargs='?')
-    parser.add_argument('--version', action='version', version=__version__)
+    parser.add_argument('--version', action='version',
+                        version='hiver {}'.format(__version__))
     args = parser.parse_args(argv[1:])
 
     client = connect(args.host, args.port)
 
     if args.script:
-        script(client, args.script)
+        if args.script == '-':
+            fo = stdin
+        else:
+            try:
+                fo = open(args.script)
+            except IOError as e:
+                raise SystemExit(
+                    'error: cannot open {} - {}'.format(args.script, e))
+        getline = fo.readline
     else:
-        repl(client)
+        import readline
+        if isfile(HISTFILE):
+            readline.read_history_file(HISTFILE)
+        atexit.register(lambda: readline.write_history_file(HISTFILE))
+        getline = repl_line
+
+    for hql in iterhql(getline):
+        execute(client, hql)
+
 
 if __name__ == '__main__':
     main()
+#!/bin/bash
+# Push to pypi, tag and push to bitbucket
+
+python setup.py sdist upload
+rm -fr build dist
+hg tag -f $(python setup.py --version)
+hg push
 
 setup(
     name='hiver',
-    version='0.1.0',
+    version='0.2.0',
     description='Hive thrift client',
     long_description=open('README.rst').read(),
     author='Miki Tebeka',
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.