#!/usr/bin/env python
#
# Autogenerated by Thrift
#
# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
#

import sys
import pprint
from urlparse import urlparse
from thrift.transport import TTransport
from thrift.transport import TSocket
from thrift.transport import THttpClient
from thrift.protocol import TBinaryProtocol

import ThriftHadoopFileSystem
from ttypes import *

if len(sys.argv) <= 1 or sys.argv[1] == '--help':
  print ''
  print 'Usage: ' + sys.argv[0] + ' [-h host:port] [-u url] [-f[ramed]] function [arg1 [arg2...]]'
  print ''
  print 'Functions:'
  print '  void setInactivityTimeoutPeriod(i64 periodInSeconds)'
  print '  void shutdown(i32 status)'
  print '  ThriftHandle create(Pathname path)'
  print '  ThriftHandle createFile(Pathname path, i16 mode, bool overwrite, i32 bufferSize, i16 block_replication, i64 blocksize)'
  print '  ThriftHandle open(Pathname path)'
  print '  ThriftHandle append(Pathname path)'
  print '  bool write(ThriftHandle handle, string data)'
  print '  string read(ThriftHandle handle, i64 offset, i32 size)'
  print '  bool close(ThriftHandle out)'
  print '  bool rm(Pathname path, bool recursive)'
  print '  bool rename(Pathname path, Pathname dest)'
  print '  bool mkdirs(Pathname path)'
  print '  bool exists(Pathname path)'
  print '  FileStatus stat(Pathname path)'
  print '   listStatus(Pathname path)'
  print '  void chmod(Pathname path, i16 mode)'
  print '  void chown(Pathname path, string owner, string group)'
  print '  void setReplication(Pathname path, i16 replication)'
  print '   getFileBlockLocations(Pathname path, i64 start, i64 length)'
  print ''
  sys.exit(0)

pp = pprint.PrettyPrinter(indent = 2)
host = 'localhost'
port = 9090
uri = ''
framed = False
http = False
argi = 1

if sys.argv[argi] == '-h':
  parts = sys.argv[argi+1].split(':') 
  host = parts[0]
  port = int(parts[1])
  argi += 2

if sys.argv[argi] == '-u':
  url = urlparse(sys.argv[argi+1])
  parts = url[1].split(':') 
  host = parts[0]
  if len(parts) > 1:
    port = int(parts[1])
  else:
    port = 80
  uri = url[2]
  http = True
  argi += 2

if sys.argv[argi] == '-f' or sys.argv[argi] == '-framed':
  framed = True
  argi += 1

cmd = sys.argv[argi]
args = sys.argv[argi+1:]

if http:
  transport = THttpClient.THttpClient(host, port, uri)
else:
  socket = TSocket.TSocket(host, port)
  if framed:
    transport = TTransport.TFramedTransport(socket)
  else:
    transport = TTransport.TBufferedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = ThriftHadoopFileSystem.Client(protocol)
transport.open()

if cmd == 'setInactivityTimeoutPeriod':
  if len(args) != 1:
    print 'setInactivityTimeoutPeriod requires 1 args'
    sys.exit(1)
  pp.pprint(client.setInactivityTimeoutPeriod(eval(args[0]),))

elif cmd == 'shutdown':
  if len(args) != 1:
    print 'shutdown requires 1 args'
    sys.exit(1)
  pp.pprint(client.shutdown(eval(args[0]),))

elif cmd == 'create':
  if len(args) != 1:
    print 'create requires 1 args'
    sys.exit(1)
  pp.pprint(client.create(eval(args[0]),))

elif cmd == 'createFile':
  if len(args) != 6:
    print 'createFile requires 6 args'
    sys.exit(1)
  pp.pprint(client.createFile(eval(args[0]),eval(args[1]),eval(args[2]),eval(args[3]),eval(args[4]),eval(args[5]),))

elif cmd == 'open':
  if len(args) != 1:
    print 'open requires 1 args'
    sys.exit(1)
  pp.pprint(client.open(eval(args[0]),))

elif cmd == 'append':
  if len(args) != 1:
    print 'append requires 1 args'
    sys.exit(1)
  pp.pprint(client.append(eval(args[0]),))

elif cmd == 'write':
  if len(args) != 2:
    print 'write requires 2 args'
    sys.exit(1)
  pp.pprint(client.write(eval(args[0]),args[1],))

elif cmd == 'read':
  if len(args) != 3:
    print 'read requires 3 args'
    sys.exit(1)
  pp.pprint(client.read(eval(args[0]),eval(args[1]),eval(args[2]),))

elif cmd == 'close':
  if len(args) != 1:
    print 'close requires 1 args'
    sys.exit(1)
  pp.pprint(client.close(eval(args[0]),))

elif cmd == 'rm':
  if len(args) != 2:
    print 'rm requires 2 args'
    sys.exit(1)
  pp.pprint(client.rm(eval(args[0]),eval(args[1]),))

elif cmd == 'rename':
  if len(args) != 2:
    print 'rename requires 2 args'
    sys.exit(1)
  pp.pprint(client.rename(eval(args[0]),eval(args[1]),))

elif cmd == 'mkdirs':
  if len(args) != 1:
    print 'mkdirs requires 1 args'
    sys.exit(1)
  pp.pprint(client.mkdirs(eval(args[0]),))

elif cmd == 'exists':
  if len(args) != 1:
    print 'exists requires 1 args'
    sys.exit(1)
  pp.pprint(client.exists(eval(args[0]),))

elif cmd == 'stat':
  if len(args) != 1:
    print 'stat requires 1 args'
    sys.exit(1)
  pp.pprint(client.stat(eval(args[0]),))

elif cmd == 'listStatus':
  if len(args) != 1:
    print 'listStatus requires 1 args'
    sys.exit(1)
  pp.pprint(client.listStatus(eval(args[0]),))

elif cmd == 'chmod':
  if len(args) != 2:
    print 'chmod requires 2 args'
    sys.exit(1)
  pp.pprint(client.chmod(eval(args[0]),eval(args[1]),))

elif cmd == 'chown':
  if len(args) != 3:
    print 'chown requires 3 args'
    sys.exit(1)
  pp.pprint(client.chown(eval(args[0]),args[1],args[2],))

elif cmd == 'setReplication':
  if len(args) != 2:
    print 'setReplication requires 2 args'
    sys.exit(1)
  pp.pprint(client.setReplication(eval(args[0]),eval(args[1]),))

elif cmd == 'getFileBlockLocations':
  if len(args) != 3:
    print 'getFileBlockLocations requires 3 args'
    sys.exit(1)
  pp.pprint(client.getFileBlockLocations(eval(args[0]),eval(args[1]),eval(args[2]),))

transport.close()
