<s> '''<STR_LIT>''' <EOL> import socket <EOL> import os <EOL> import sys <EOL> import urllib <EOL> import multiprocessing <EOL> import time <EOL> import platform <EOL> import tempfile <EOL> import shutil <EOL> from multiprocessing . reduction import reduce_connection <EOL> try : <EOL> import cPickle as pickle <EOL> except ImportError : <EOL> import pickle <EOL> from cola . core . errors import DependencyNotInstalledError <EOL> def add_localhost ( func ) : <EOL> def inner ( * args , ** kwargs ) : <EOL> ips = func ( * args , ** kwargs ) <EOL> localhost = '<STR_LIT:127.0.0.1>' <EOL> if localhost not in ips : <EOL> ips . append ( localhost ) <EOL> return ips <EOL> return inner <EOL> @ add_localhost <EOL> def get_ips ( ) : <EOL> localIP = socket . gethostbyname ( socket . gethostname ( ) ) <EOL> ex = socket . gethostbyname_ex ( socket . gethostname ( ) ) [ <NUM_LIT:2> ] <EOL> if len ( ex ) == <NUM_LIT:1> : <EOL> return [ ex [ <NUM_LIT:0> ] ] <EOL> return [ ip for ip in ex if ip != localIP ] <EOL> def get_ip ( ) : <EOL> localIP = socket . gethostbyname ( socket . gethostname ( ) ) <EOL> ex = socket . gethostbyname_ex ( socket . gethostname ( ) ) [ <NUM_LIT:2> ] <EOL> if len ( ex ) == <NUM_LIT:1> : <EOL> return ex [ <NUM_LIT:0> ] <EOL> for ip in ex : <EOL> if ip != localIP : <EOL> return ip <EOL> def root_dir ( ) : <EOL> def _get_dir ( f ) : <EOL> return os . path . dirname ( f ) <EOL> f = os . path . abspath ( __file__ ) <EOL> for _ in range ( <NUM_LIT:3> ) : <EOL> f = _get_dir ( f ) <EOL> return f <EOL> def import_job_desc ( path ) : <EOL> dir_ , name = os . path . split ( path ) <EOL> if os . path . isfile ( path ) : <EOL> name = name . rstrip ( '<STR_LIT>' ) <EOL> else : <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . dirname ( dir_ ) ) <EOL> sys . path . insert ( <NUM_LIT:0> , dir_ ) <EOL> job_module = __import__ ( name ) <EOL> job_desc = job_module . get_job_desc ( ) <EOL> return job_desc <EOL> def urldecode ( link ) : <EOL> decodes = { } <EOL> if '<STR_LIT:?>' in link : <EOL> params = link . split ( '<STR_LIT:?>' ) [ <NUM_LIT:1> ] <EOL> for param in params . split ( '<STR_LIT:&>' ) : <EOL> k , v = tuple ( param . split ( '<STR_LIT:=>' ) ) <EOL> decodes [ k ] = urllib . unquote ( v ) <EOL> return decodes <EOL> def beautiful_soup ( html , logger = None ) : <EOL> try : <EOL> from bs4 import BeautifulSoup , FeatureNotFound <EOL> except ImportError : <EOL> raise DependencyNotInstalledError ( "<STR_LIT>" ) <EOL> try : <EOL> return BeautifulSoup ( html , '<STR_LIT>' ) <EOL> except FeatureNotFound : <EOL> if logger is not None : <EOL> logger . warning ( '<STR_LIT>' ) <EOL> return BeautifulSoup ( html ) <EOL> def iterable ( obj ) : <EOL> try : <EOL> iter ( obj ) <EOL> except TypeError : <EOL> return False <EOL> return True <EOL> def get_rpc_prefix ( app_name = None , prefix = None ) : <EOL> if app_name and not app_name . endswith ( '<STR_LIT:_>' ) : <EOL> app_name += '<STR_LIT:_>' <EOL> elif not app_name : <EOL> app_name = '<STR_LIT>' <EOL> if prefix and not prefix . endswith ( '<STR_LIT:_>' ) : <EOL> prefix += '<STR_LIT:_>' <EOL> elif not prefix : <EOL> prefix = '<STR_LIT>' <EOL> return app_name + prefix <EOL> ALPHABET = "<STR_LIT>" <EOL> def base58_encode ( num , alphabet = ALPHABET ) : <EOL> """<STR_LIT>""" <EOL> if ( num == <NUM_LIT:0> ) : <EOL> return alphabet [ <NUM_LIT:0> ] <EOL> arr = [ ] <EOL> base = len ( alphabet ) <EOL> while num : <EOL> rem = num % base <EOL> num = num // base <EOL> arr . append ( alphabet [ rem ] ) <EOL> arr . reverse ( ) <EOL> return '<STR_LIT>' . join ( arr ) <EOL> def get_cpu_count ( ) : <EOL> return multiprocessing . cpu_count ( ) <EOL> def get_os_name ( ) : <EOL> return platform . system ( ) <EOL> def is_windows ( ) : <EOL> return get_os_name ( ) == '<STR_LIT>' <EOL> class Clock ( object ) : <EOL> def __init__ ( self , start = None ) : <EOL> self . start = start <EOL> if self . start is None : <EOL> self . start = time . time ( ) <EOL> self . paused = <NUM_LIT:0.0> <EOL> self . is_paused = False <EOL> self . acc_paused = <NUM_LIT:0.0> <EOL> def pause ( self ) : <EOL> if not self . is_paused : <EOL> self . paused = time . time ( ) <EOL> self . is_paused = True <EOL> def resume ( self ) : <EOL> if self . is_paused : <EOL> self . acc_paused += time . time ( ) - self . paused <EOL> self . is_paused = False <EOL> def clock ( self ) : <EOL> return time . time ( ) - self . start - self . acc_paused <EOL> def pickle_connection ( connection ) : <EOL> return pickle . dumps ( reduce_connection ( connection ) ) <EOL> def unpickle_connection ( pickled_connection ) : <EOL> ( func , args ) = pickle . loads ( pickled_connection ) <EOL> return func ( * args ) <EOL> def import_module ( module_name ) : <EOL> root , module = module_name . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> return getattr ( __import__ ( root , fromlist = [ '<STR_LIT>' ] ) , module ) <EOL> def pack_local_job_error ( job_name , working_dir = None , logger = None ) : <EOL> if working_dir is None : <EOL> working_dir = os . path . join ( tempfile . gettempdir ( ) , '<STR_LIT>' , '<STR_LIT>' , job_name ) <EOL> if not os . path . exists ( working_dir ) : <EOL> if logger is not None : <EOL> logger . warning ( '<STR_LIT>' ) <EOL> return <EOL> pack_dir = os . path . join ( working_dir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if os . path . exists ( pack_dir ) : <EOL> shutil . rmtree ( pack_dir ) <EOL> if not os . path . exists ( pack_dir ) : <EOL> os . makedirs ( pack_dir ) <EOL> for name in os . listdir ( working_dir ) : <EOL> path = os . path . join ( working_dir , name ) <EOL> if os . path . isdir ( path ) and name . isdigit ( ) : <EOL> error_dir = os . path . join ( path , '<STR_LIT>' ) <EOL> if os . path . exists ( error_dir ) : <EOL> for error_detail_dir in os . listdir ( error_dir ) : <EOL> shutil . copytree ( os . path . join ( error_dir , error_detail_dir ) , <EOL> os . path . join ( pack_dir , error_detail_dir ) ) <EOL> return pack_dir </s>
