<s> from time import * <EOL> import click <EOL> import traceback <EOL> import dateutil . parser <EOL> import sys <EOL> import os <EOL> from model import CommandForm <EOL> import rest_client <EOL> import bashhub_setup <EOL> import bashhub_globals <EOL> from bashhub_globals import BH_FILTER <EOL> from version import __version__ <EOL> import shutil <EOL> import requests <EOL> import subprocess <EOL> import shell_utils <EOL> import re <EOL> from view . status import * <EOL> def print_version ( ctx , param , value ) : <EOL> if not value or ctx . resilient_parsing : <EOL> return <EOL> click . echo ( '<STR_LIT>' % __version__ ) <EOL> ctx . exit ( ) <EOL> CONTEXT_SETTINGS = dict ( help_option_names = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> @ click . group ( context_settings = CONTEXT_SETTINGS ) <EOL> @ click . option ( '<STR_LIT>' , '<STR_LIT>' , default = False , is_flag = True , callback = print_version , <EOL> help = '<STR_LIT>' , expose_value = False , is_eager = True ) <EOL> def bashhub ( ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> @ bashhub . command ( ) <EOL> def version ( ) : <EOL> """<STR_LIT>""" <EOL> click . echo ( '<STR_LIT>' % __version__ ) <EOL> @ bashhub . command ( ) <EOL> @ click . argument ( '<STR_LIT>' , type = str ) <EOL> @ click . argument ( '<STR_LIT:path>' , type = click . Path ( exists = True ) ) <EOL> @ click . argument ( '<STR_LIT>' , type = long ) <EOL> @ click . argument ( '<STR_LIT>' , type = long ) <EOL> @ click . argument ( '<STR_LIT>' , type = int ) <EOL> def save ( command , path , pid , process_start_time , exit_status ) : <EOL> """<STR_LIT>""" <EOL> pid_start_time = unix_time_to_epoc_millis ( process_start_time ) <EOL> command = command . strip ( ) <EOL> if "<STR_LIT>" in command : <EOL> return <EOL> bh_filter = bashhub_globals . BH_FILTER <EOL> if bh_filter and re . findall ( bh_filter , command ) : <EOL> return <EOL> if bashhub_globals . BH_AUTH ( ) == "<STR_LIT>" : <EOL> print ( "<STR_LIT>" ) <EOL> return <EOL> command = CommandForm ( command , path , exit_status , pid , pid_start_time ) <EOL> rest_client . save_command ( command ) <EOL> @ bashhub . command ( ) <EOL> def setup ( ) : <EOL> """<STR_LIT>""" <EOL> bashhub_setup . main ( ) <EOL> @ bashhub . command ( ) <EOL> def status ( ) : <EOL> """<STR_LIT>""" <EOL> ( ppid , start_time ) = shell_utils . get_session_information ( ) <EOL> status_view = rest_client . get_status_view ( ppid , start_time ) <EOL> if status_view : <EOL> click . echo ( build_status_view ( status_view ) ) <EOL> @ bashhub . command ( ) <EOL> @ click . pass_context <EOL> def help ( ctx ) : <EOL> """<STR_LIT>""" <EOL> click . echo ( ctx . parent . get_help ( ) ) <EOL> filtered_text = "<STR_LIT>" . format ( BH_FILTER ) if BH_FILTER else "<STR_LIT>" <EOL> filter_help_text = """<STR_LIT>""" . format ( filtered_text ) <EOL> @ bashhub . command ( help = filter_help_text ) <EOL> @ click . argument ( '<STR_LIT>' , type = str ) <EOL> @ click . option ( '<STR_LIT>' , '<STR_LIT>' , default = BH_FILTER , help = '<STR_LIT>' ) <EOL> def filter ( command , regex ) : <EOL> if not bashhub_globals . is_valid_regex ( regex ) : <EOL> click . secho ( "<STR_LIT>" . format ( regex ) , fg = '<STR_LIT>' ) <EOL> return <EOL> v = re . findall ( regex , command ) <EOL> click . echo ( filtered_text ) <EOL> if v and regex : <EOL> matched = [ str ( s ) for s in set ( v ) ] <EOL> output = click . style ( "<STR_LIT>" . format ( command ) , <EOL> fg = '<STR_LIT>' ) + click . style ( str ( matched ) , fg = '<STR_LIT>' ) <EOL> click . echo ( output ) <EOL> else : <EOL> click . echo ( "<STR_LIT>" . format ( command ) ) <EOL> @ bashhub . command ( ) <EOL> @ click . argument ( '<STR_LIT:version>' , type = str , default = '<STR_LIT>' ) <EOL> def update ( version ) : <EOL> """<STR_LIT>""" <EOL> if version != '<STR_LIT>' : <EOL> github = "<STR_LIT>" . format ( version ) <EOL> response = requests . get ( github ) <EOL> if response . status_code is not <NUM_LIT:200> : <EOL> click . echo ( "<STR_LIT>" . format ( version ) ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> query_param = '<STR_LIT>' . format ( version ) if version else '<STR_LIT>' <EOL> url = '<STR_LIT>' + query_param <EOL> response = requests . get ( url , stream = True ) <EOL> filename = '<STR_LIT>' <EOL> with open ( filename , '<STR_LIT:wb>' ) as out_file : <EOL> shutil . copyfileobj ( response . raw , out_file ) <EOL> shell_command = "<STR_LIT>" . format ( filename , version ) <EOL> subprocess . call ( shell_command , shell = True ) <EOL> os . remove ( filename ) <EOL> @ bashhub . group ( ) <EOL> def util ( ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> @ util . command ( ) <EOL> def update_system_info ( ) : <EOL> """<STR_LIT>""" <EOL> result = bashhub_setup . update_system_info ( ) <EOL> sys . exit ( <NUM_LIT:0> ) if result != None else sys . exit ( <NUM_LIT:1> ) <EOL> @ util . command ( ) <EOL> @ click . argument ( '<STR_LIT>' , type = str ) <EOL> def parsedate ( date_string ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> date = dateutil . parser . parse ( date_string ) <EOL> unix_time = int ( mktime ( date . timetuple ( ) ) ) <EOL> click . echo ( unix_time ) <EOL> except Exception as e : <EOL> click . echo ( <NUM_LIT:0> ) <EOL> def unix_time_to_epoc_millis ( unix_time ) : <EOL> return int ( unix_time ) * <NUM_LIT:1000> <EOL> def main ( ) : <EOL> try : <EOL> bashhub ( ) <EOL> except Exception as e : <EOL> formatted = traceback . format_exc ( e ) <EOL> click . echo ( "<STR_LIT>" + str ( e ) ) <EOL> sys . exit ( <NUM_LIT:1> ) </s>
