<s> """<STR_LIT>""" <EOL> import hashlib <EOL> import os <EOL> import re <EOL> import signal <EOL> import subprocess <EOL> import syslog <EOL> import time <EOL> import shell_tools <EOL> import sys_tools <EOL> def activate_application ( app_name ) : <EOL> """<STR_LIT>""" <EOL> launch_app = """<STR_LIT>""" % ( app_name ) <EOL> os . system ( launch_app ) <EOL> def create_local_account ( user , full_name , password , admin = False , hidden = False ) : <EOL> """<STR_LIT>""" <EOL> dscl_command = "<STR_LIT>" <EOL> home_dir = "<STR_LIT>" % user <EOL> uids = shell_tools . run ( <EOL> "<STR_LIT>" % ( dscl_command ) , <EOL> sanitize = False <EOL> ) [ "<STR_LIT>" ] . split ( ) <EOL> next_id = map ( int , uids ) <EOL> next_id . sort ( ) <EOL> next_id = next_id [ - <NUM_LIT:1> ] <EOL> if next_id < <NUM_LIT> : <EOL> if not hidden : <EOL> next_id = <NUM_LIT> <EOL> user_id = next_id + <NUM_LIT:1> <EOL> create_user_commands = [ <EOL> "<STR_LIT>" % home_dir , <EOL> "<STR_LIT>" % home_dir , <EOL> "<STR_LIT>" % ( home_dir , full_name ) , <EOL> "<STR_LIT>" % ( home_dir , user_id ) , <EOL> "<STR_LIT>" % home_dir , <EOL> "<STR_LIT>" % ( home_dir , home_dir ) , <EOL> "<STR_LIT>" % ( home_dir , password ) , <EOL> ] <EOL> if admin : <EOL> create_user_commands . append ( <EOL> "<STR_LIT>" % user <EOL> ) <EOL> for command in create_user_commands : <EOL> shell_tools . run ( "<STR_LIT>" % ( dscl_command , command ) ) <EOL> def configure_time ( ) : <EOL> """<STR_LIT>""" <EOL> time_server = "<STR_LIT>" <EOL> time_commands = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" % time_server , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" % time_server , <EOL> ] <EOL> for command in time_commands : <EOL> shell_tools . run ( command ) <EOL> def enough_space ( required_space ) : <EOL> """<STR_LIT>""" <EOL> return True if get_free_hd_space ( "<STR_LIT>" ) - required_space < <NUM_LIT:3> else False <EOL> def get_computer_name ( ) : <EOL> """<STR_LIT>""" <EOL> return shell_tools . run ( "<STR_LIT>" ) [ "<STR_LIT>" ] <EOL> def get_model ( short = False ) : <EOL> """<STR_LIT>""" <EOL> models = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> model = query_profiler ( <EOL> "<STR_LIT>" , [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> ) <EOL> if short : <EOL> return models . get ( model , "<STR_LIT>" ) <EOL> else : <EOL> return model <EOL> def get_os_version ( ) : <EOL> """<STR_LIT>""" <EOL> return shell_tools . run ( "<STR_LIT>" ) [ "<STR_LIT>" ] <EOL> def get_serial ( ) : <EOL> """<STR_LIT>""" <EOL> return query_profiler ( <EOL> "<STR_LIT>" , [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> ) <EOL> def get_shard ( serial = None , salt = None , chunks = <NUM_LIT:10> ) : <EOL> """<STR_LIT>""" <EOL> md5 = hashlib . md5 ( ) <EOL> if not serial : <EOL> serial = get_serial ( ) <EOL> if salt : <EOL> serial = str ( serial ) + str ( salt ) <EOL> md5 . update ( serial ) <EOL> digest = md5 . hexdigest ( ) <EOL> number = int ( digest , <NUM_LIT:16> ) <EOL> shard = number % int ( chunks ) <EOL> return shard <EOL> def get_total_memory ( ) : <EOL> """<STR_LIT>""" <EOL> total_memory = shell_tools . run ( '<STR_LIT>' ) [ '<STR_LIT>' ] <EOL> return ( int ( total_memory . split ( '<STR_LIT:=>' ) [ - <NUM_LIT:1> ] ) / ( <NUM_LIT> * <NUM_LIT:3> ) ) <EOL> def get_time_since ( time , mode = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> now = shell_tools . get_unix_time ( ) <EOL> unit = { <EOL> '<STR_LIT>' : <NUM_LIT> * <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> } <EOL> since = now - time <EOL> if unit [ mode ] == <NUM_LIT:0> : <EOL> return since <EOL> return since / unit [ mode ] <EOL> def get_used_memory ( ) : <EOL> """<STR_LIT>""" <EOL> get_top_memory = shell_tools . run ( <EOL> '<STR_LIT>' ) [ '<STR_LIT>' ] . split ( ) <EOL> return get_top_memory [ <NUM_LIT:1> ] <EOL> def get_uptime ( ) : <EOL> """<STR_LIT>""" <EOL> boot_time = int ( shell_tools . run ( <EOL> "<STR_LIT>" ) [ "<STR_LIT>" ] . split ( ) [ <NUM_LIT:3> ] . strip ( '<STR_LIT:U+002C>' ) <EOL> ) <EOL> return ( shell_tools . get_unix_time ( ) - boot_time ) / <NUM_LIT> <EOL> def query_profiler ( <EOL> data_type , <EOL> path , <EOL> needle = None , <EOL> verbose = False , <EOL> ending = True , <EOL> numeric = False , <EOL> periods = True , <EOL> ) : <EOL> """<STR_LIT>""" <EOL> output = subprocess . Popen ( <EOL> [ "<STR_LIT>" , data_type ] , stdout = subprocess . PIPE ) . communicate ( ) [ <NUM_LIT:0> ] <EOL> path_index = <NUM_LIT:0> <EOL> if needle : <EOL> return True if needle in output else False <EOL> for line in output . splitlines ( ) : <EOL> if line . strip ( ) . startswith ( path [ path_index ] ) : <EOL> if path_index >= len ( path ) - <NUM_LIT:1> : <EOL> value = line . strip ( ) [ len ( path [ path_index ] ) + <NUM_LIT:2> : ] <EOL> if not verbose : <EOL> if value [ - <NUM_LIT:1> ] == "<STR_LIT:)>" : <EOL> value = value [ : value . find ( "<STR_LIT:(>" ) - <NUM_LIT:1> ] <EOL> if not ending : <EOL> value = value . rsplit ( "<STR_LIT:U+0020>" , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> if not periods : <EOL> value = re . sub ( r"<STR_LIT>" , "<STR_LIT>" , value ) <EOL> elif numeric : <EOL> value = re . sub ( r"<STR_LIT>" , "<STR_LIT>" , value ) <EOL> return value <EOL> else : <EOL> path_index += <NUM_LIT:1> <EOL> def launchctl_load ( name_of_daemon ) : <EOL> """<STR_LIT>""" <EOL> shell_tools . run ( <EOL> "<STR_LIT>" % <EOL> ( sys_tools . get_sys_path ( '<STR_LIT>' ) , name_of_daemon ) <EOL> ) <EOL> def launchctl_reload ( name_of_daemon ) : <EOL> """<STR_LIT>""" <EOL> launchctl_unload ( name_of_daemon ) <EOL> sleep ( secs = <NUM_LIT:1> ) <EOL> launchctl_load ( name_of_daemon ) <EOL> def launchctl_unload ( name_of_daemon ) : <EOL> """<STR_LIT>""" <EOL> sleep ( secs = <NUM_LIT:3> ) <EOL> shell_tools . run ( <EOL> "<STR_LIT>" % <EOL> ( sys_tools . get_sys_path ( '<STR_LIT>' ) , name_of_daemon ) <EOL> ) <EOL> def log ( tag , message ) : <EOL> """<STR_LIT>""" <EOL> syslog . openlog ( tag ) <EOL> syslog . syslog ( syslog . LOG_ALERT , message ) <EOL> def logout ( ) : <EOL> """<STR_LIT>""" <EOL> logout = """<STR_LIT>""" <EOL> os . system ( logout ) <EOL> def get_hd_capacity ( ) : <EOL> """<STR_LIT>""" <EOL> hd_name = get_hd_name ( ) <EOL> return float ( query_profiler ( <EOL> "<STR_LIT>" , [ hd_name , "<STR_LIT>" ] , ending = False ) <EOL> ) <EOL> def get_free_hd_space ( unit ) : <EOL> """<STR_LIT>""" <EOL> gigabyte_size = <NUM_LIT> <EOL> megabyte_size = <NUM_LIT> <EOL> hd_name = get_hd_name ( ) <EOL> free_space = query_profiler ( <EOL> "<STR_LIT>" , [ hd_name , "<STR_LIT>" ] , ending = False ) <EOL> free_space = int ( float ( free_space ) ) <EOL> if unit == "<STR_LIT>" : <EOL> return free_space <EOL> if unit == "<STR_LIT>" : <EOL> return free_space * gigabyte_size <EOL> if unit == "<STR_LIT>" : <EOL> return ( free_space * gigabyte_size ) * megabyte_size <EOL> def get_hd_name ( ) : <EOL> """<STR_LIT>""" <EOL> hd_name = shell_tools . run ( <EOL> "<STR_LIT>" <EOL> ) [ "<STR_LIT>" ] . split ( ) [ <NUM_LIT:2> : ] <EOL> return "<STR_LIT:U+0020>" . join ( hd_name ) <EOL> def get_hd_used_space ( ) : <EOL> """<STR_LIT>""" <EOL> capacity = float ( get_hd_capacity ( ) ) <EOL> available = float ( get_free_hd_space ( "<STR_LIT>" ) ) <EOL> return capacity - available <EOL> def install_pkg ( pkg , base_dir = '<STR_LIT:/>' ) : <EOL> """<STR_LIT>""" <EOL> install_cmd = '<STR_LIT>' % ( pkg , base_dir ) <EOL> install_results = shell_tools . run ( install_cmd ) <EOL> if not install_results [ '<STR_LIT:success>' ] : <EOL> raise Exception ( install_results [ '<STR_LIT>' ] , install_results [ '<STR_LIT>' ] ) <EOL> def is_process_running ( process ) : <EOL> """<STR_LIT>""" <EOL> all_processes = os . popen ( "<STR_LIT>" ) . read ( ) <EOL> return True if process in all_processes else False <EOL> def is_desktop ( ) : <EOL> """<STR_LIT>""" <EOL> return True if not is_laptop ( ) else False <EOL> def is_laptop ( ) : <EOL> """<STR_LIT>""" <EOL> return True if '<STR_LIT>' in get_model ( ) . lower ( ) else False <EOL> def kill_process ( pid ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> os . kill ( int ( pid ) , signal . SIGKILL ) <EOL> except OSError : <EOL> print "<STR_LIT>" <EOL> def set_machine_name ( hostname ) : <EOL> """<STR_LIT>""" <EOL> shell_tools . run ( "<STR_LIT>" % hostname ) <EOL> shell_tools . run ( "<STR_LIT>" % hostname ) <EOL> def sleep ( secs = None , mins = None , hrs = None , days = None ) : <EOL> """<STR_LIT>""" <EOL> sleep_time = secs <EOL> if mins : <EOL> sleep_time = <NUM_LIT> * mins <EOL> if hrs : <EOL> sleep_time = <NUM_LIT> * <NUM_LIT> * hrs <EOL> if days : <EOL> sleep_time = <NUM_LIT> * <NUM_LIT> * <NUM_LIT> * days <EOL> time . sleep ( sleep_time ) <EOL> def verify_hd_name ( ) : <EOL> """<STR_LIT>""" <EOL> if get_hd_name ( ) != "<STR_LIT>" : <EOL> shell_tools . run ( "<STR_LIT>" ) </s>
