<commit_before>#! /usr/bin/env python
"""
arast-client -- commandline client for Assembly RAST

A lot of code taken from <NAME> / ShockClient

"""


import os, sys, json, shutil
import pika
import argparse
import logging
import requests
import uuid
import time
from ConfigParser import SafeConfigParser

import shock


# arg type checking
def file_type(f):
	if not os.path.isfile(f):
		msg = "%s is not a file" % f
		raise argparse.ArgumentTypeError(msg)
	else:
		return f

def dir_type(d):
	if not os.path.isdir(d):
		msg = "%s is not a directory" % d
		raise argparse.ArgumentTypeError(msg)
	else:
		return d


# setup option/arg parser
parser = argparse.ArgumentParser(prog='arast', epilog='Use "arast command -h" for more information about a command.')
parser.add_argument('--ARASTURL', help='Overrules env ARASTURL')
parser.add_argument('--ARASTUSER', help='Overrules env ARASTUSER')
parser.add_argument('--ARASTPASSWORD', help='Overrules env ARASTPASSWORD')
parser.add_argument("-c", "--config", action="store",
                  dest="config", required=True, help="specify parameter configuration file")

subparsers = parser.add_subparsers(dest='command', title='The commands are')

# run -h
p_run = subparsers.add_parser('run', description='Run an Assembly RAST job', help='run job')
p_run.add_argument("-f", "--file", action="store", dest="filename",
                   nargs='*', help="specify sequence file(s)")
p_run.add_argument("-a", "--assemblers", action="store",
                  dest="assemblers", nargs='*')
p_run.add_argument("-d", "--directory", action="store",
                  dest="directory",
                  help="specify input directory")
p_run.add_argument("-p", "--params", action="store",
                  dest="params", nargs='*',
                  help="specify global assembly parameters")
p_run.add_argument("-m", "--message", action="store",
                  dest="message",
                  help="Attach a description to job")



# filetype, special flags, config file
# global, -k=31, -cov
# velvet

# stat -h
p_stat = subparsers.add_parser('stat', description='Query status of running jobs', help='list jobs status')
p_stat.add_argument("-w", "--watch", action="store_true")

# get
p_get = subparsers.add_parser('get', description='Download result data', help='download data')
p_get.add_argument("-a", "--assemblers", action="store",
                  dest="assemblers", nargs='*',
                  help="specify which assembly data to get")


def post(url, files):
	global ARASTUSER, ARASTPASSWORD
	r = None
	if ARASTUSER and ARASTPASSWORD:
            r = requests.post(url, auth=(ARASTUSER, ARASTPASSWORD), files=files)
	else:
            r = requests.post(url, files=files)

        res = json.loads(r.text)
	return res


# upload all files in list, return list of ids
def upload(url, files):
    ids = []
    for f in files:
        files = {}
        print "Uploading: %s" % os.path.basename(f)
        files["file"] = (os.path.basename(f), open(f, 'rb'))
        res = post(url, files)
        ids.append(res['D']['id'])
        
        #Error check
        if res["E"] is None:
        # Prettytable 0.6 breaks this
                    #printNodeTable(res["D"])
		pass
        else:
            print "shock: err from server: %s" % res["E"][0]
    return ids


def main():
	global ARASTURL, ARASTUSER, ARASTPASSWORD

	args = parser.parse_args()
	opt = parser.parse_args()
        options = vars(args)
	
	cparser = SafeConfigParser()
	config_file = args.config
	cparser.read(config_file)

	ARASTUSER = cparser.get('arast', 'user')
	ARASTPASSWORD = cparser.get('arast', 'password')
	ARASTURL = cparser.get('arast', 'url')


	# overwrite env vars in args
	if args.ARASTUSER:
		ARASTUSER = args.ARASTUSER				
	if args.ARASTPASSWORD:
		ARASTPASSWORD = args.ARASTPASSWORD				
	if not ARASTURL:
		print parser.print_usage()
		print "arast: err: ARASTURL not set"
		sys.exit()

        # Request Shock URL
        url_req = {}
        url_req['command'] = 'get_url'
        url_rpc = RpcClient()
	url = "http://%s" % url_rpc.call(json.dumps(url_req))

        # Upload file(s) to Shock<commit_msg>config<commit_after>#! /usr/bin/env python
"""
arast-client -- commandline client for Assembly RAST

A lot of code taken from <NAME> / ShockClient

"""


import os, sys, json, shutil
import pika
import argparse
import logging
import requests
import uuid
import time
from ConfigParser import SafeConfigParser

import shock


# arg type checking
def file_type(f):
	if not os.path.isfile(f):
		msg = "%s is not a file" % f
		raise argparse.ArgumentTypeError(msg)
	else:
		return f

def dir_type(d):
	if not os.path.isdir(d):
		msg = "%s is not a directory" % d
		raise argparse.ArgumentTypeError(msg)
	else:
		return d


# setup option/arg parser
parser = argparse.ArgumentParser(prog='arast', epilog='Use "arast command -h" for more information about a command.')
parser.add_argument('--ARASTURL', help='Overrules env ARASTURL')
parser.add_argument('--ARASTUSER', help='Overrules env ARASTUSER')
parser.add_argument('--ARASTPASSWORD', help='Overrules env ARASTPASSWORD')
parser.add_argument("-c", "--config", action="store",
                  dest="config", help="specify parameter configuration file")

subparsers = parser.add_subparsers(dest='command', title='The commands are')

# run -h
p_run = subparsers.add_parser('run', description='Run an Assembly RAST job', help='run job')
p_run.add_argument("-f", "--file", action="store", dest="filename",
                   nargs='*', help="specify sequence file(s)")
p_run.add_argument("-a", "--assemblers", action="store",
                  dest="assemblers", nargs='*')
p_run.add_argument("-d", "--directory", action="store",
                  dest="directory",
                  help="specify input directory")
p_run.add_argument("-p", "--params", action="store",
                  dest="params", nargs='*',
                  help="specify global assembly parameters")
p_run.add_argument("-m", "--message", action="store",
                  dest="message",
                  help="Attach a description to job")



# filetype, special flags, config file
# global, -k=31, -cov
# velvet

# stat -h
p_stat = subparsers.add_parser('stat', description='Query status of running jobs', help='list jobs status')
p_stat.add_argument("-w", "--watch", action="store_true")

# get
p_get = subparsers.add_parser('get', description='Download result data', help='download data')
p_get.add_argument("-a", "--assemblers", action="store",
                  dest="assemblers", nargs='*',
                  help="specify which assembly data to get")


def post(url, files):
	global ARASTUSER, ARASTPASSWORD
	r = None
	if ARASTUSER and ARASTPASSWORD:
            r = requests.post(url, auth=(ARASTUSER, ARASTPASSWORD), files=files)
	else:
            r = requests.post(url, files=files)

        res = json.loads(r.text)
	return res


# upload all files in list, return list of ids
def upload(url, files):
    ids = []
    for f in files:
        files = {}
        print "Uploading: %s" % os.path.basename(f)
        files["file"] = (os.path.basename(f), open(f, 'rb'))
        res = post(url, files)
        ids.append(res['D']['id'])
        
        #Error check
        if res["E"] is None:
        # Prettytable 0.6 breaks this
                    #printNodeTable(res["D"])
		pass
        else:
            print "shock: err from server: %s" % res["E"][0]
    return ids


def main():
	global ARASTURL, ARASTUSER, ARASTPASSWORD

	args = parser.parse_args()
	opt = parser.parse_args()
        options = vars(args)
	
	cparser = SafeConfigParser()
	if args.config:
		config_file = args.config
	else:
		config_file = "settings.conf"
	cparser.read(config_file)

	try:
		ARASTUSER = cparser.get('arast', 'user')
		ARASTPASSWORD = cparser.get('arast', 'password')
		ARASTURL = cparser.get('arast', 'url')
	except:
		logging.error("Invalid config file")

	# overwrite env vars in args
	if args.ARASTUSER:
		ARASTUSER = args.ARASTUSER				
	if args.ARASTPASSWORD:
		ARASTPASSWORD = args.ARASTPASSWORD				
	if not ARASTURL:
		print parser.print_usage()
		print "arast: err: ARASTURL not set"
		sys.exit()

        # Request Shock URL
        url_req = {}
        url_req['command'] = 'get_url'
        url_rpc = RpcClient()
	url = "http://%s" % url_rpc.call(json.dumps(url_req))

        # Upload file(s) to Shock
{'fraction_non_alphanumeric': 0.14130564506504356, 'fraction_numerical': 0.0011934598400763815, 'mean_word_length': 3.1795511221945136, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5915028', 'n_tokens_mistral': 2790, 'n_tokens_neox': 2692, 'n_words': 714}