def main(options): global program program = propyte.Program(options=options, name=name, version=version, logo=logo) global log from propyte import log interval = float(options["--interval"]) clock_restart = shijian.Clock(name="restart") log.info("\nrestart interval: {interval} s".format(interval=interval)) while True: log.info("\ncurrent run time: {run_time}".format( run_time=clock_restart.time())) log.info("restart yet? (i.e. current run time >= interval): {restart}". format(restart=clock_restart.time() >= interval)) if clock_restart.time() >= interval: print( pyprel.center_string(text=pyprel.render_banner( text="restart"))) propyte.restart() time.sleep(1)
def main(): options = docopt.docopt(__doc__, version=__version__) interval = int(options['--interval']) warn_SIGINT_country = options['--warn_SIGINT_country'] display = options['--display'] restart_regularly = options['--restart_regularly'] countries_whitelist = options['--countries_whitelist'] if countries_whitelist: countries_whitelist = countries_whitelist.split(',') message = name + ' ' + __version__ + ' monitoring internet connection security' print('\n' + message + '\n^c to stop\n') notify(text=message) clock_restart = shijian.Clock(name='restart') while True: try: data_IP = requests.get('http://ipinfo.io/json').json() IP = data_IP['ip'] organisation = data_IP['org'] coordinates = data_IP['loc'] city = data_IP['city'] country = data_IP['country'] region = data_IP['region'] if not countries_whitelist: if IP not in whitelist_IPs + whitelist_Tor: notify(text='WARNING: IP not identified as AirVPN or Tor', subtext='IP: ' + IP) if warn_SIGINT_country and country in countries_SIGINT: notify(text='WARNING: IP in SIGINT country', subtext='IP: ' + IP) else: if country not in countries_whitelist: notify( text= f'WARNING: country {country} not in whitelist {countries_whitelist}' ) if display: text = textwrap.dedent(""" IP: {IP} organisation: {organisation} coordinates: {coordinates} city: {city} country: {country} region: {region} """.format( IP=IP if IP else 'unknown', organisation=organisation if organisation else 'unknown', coordinates=coordinates if coordinates else 'unknown', city=city if city else 'unknown', country=country if country else 'unknown', region=region if region else 'unknown')) print(chr(27) + '[2J') print(text) except: notify( text='WARNING: error observing IP, unable to identify as secure' ) if restart_regularly and clock_restart.time() >= 500: print('regular restart procedure engaged') restart() time.sleep(interval)
def function_1(): function_name = inspect.stack()[0][3] clock = shijian.Clock(name=function_name) print("initiate {function_name}".format(function_name=function_name)) time.sleep(3) print("terminate {function_name}".format(function_name=function_name)) clock.stop() return (3)
def __init__(self, parent=None, options=None, name=None, version=None, logo=None, engage_log=True, filename_log=None, instance=None): global clock clock = shijian.Clock(name="program run time") if options is None: options = dict() self.options = options self.username = self.options["--username"] self.verbose = self.options["--verbose"] self.silent = self.options["--silent"] self.name = name self.version = version self.logo = logo self.engage_log = engage_log self.filename_log = filename_log self.instance = instance if self.username is None: self.username = os.getenv("USER") if self.logo is not None: self.display_logo = True elif self.logo is None and self.name is not None: self.logo = pyprel.render_banner(text=self.name.upper()) self.display_logo = True else: self.display_logo = False if self.instance is None: self.instance = str(uuid.uuid4()) # logging if engage_log: global log log = logging.getLogger(__name__) logging.root.addHandler(technicolor.ColorisingStreamHandler()) # logging level if self.verbose: logging.root.setLevel(logging.DEBUG) else: logging.root.setLevel(logging.INFO) if self.filename_log: logging.root.addHandler(logging.FileHandler(self.filename_log)) self.engage()
def __init__(self, parent=None, options=None): # internal options self.display_logo = True # clock global clock clock = shijian.Clock(name="program run time") # name, version, logo if "name" in globals(): self.name = name else: self.name = None if "version" in globals(): self.version = version else: self.version = None if "logo" in globals(): self.logo = logo elif "logo" not in globals() and hasattr(self, "name"): self.logo = pyprel.render_banner(text=self.name.upper()) else: self.display_logo = False self.logo = None # options self.options = options self.username = self.options["--username"] self.verbose = self.options["--verbose"] self.files = self.options["--files"] self.configuration_filename = self.options["--configuration"] # default values if self.username is None: self.username = os.getenv("USER") if self.files is not None: self.files = self.files.split(",") # logging global log log = logging.getLogger(__name__) logging.root.addHandler(technicolor.ColorisingStreamHandler()) # logging level if self.verbose: logging.root.setLevel(logging.DEBUG) else: logging.root.setLevel(logging.INFO) self.engage() # configuration self.configuration = pyrecon.open_configuration( self.configuration_filename)
def main(options): interval = float(options["--interval"]) print(pyprel.center_string(text = pyprel.render_banner(text = "start"))) clock_restart = shijian.Clock(name = "restart") print("restart interval: {interval} s".format(interval = interval)) while True: print("\ncurrent run time: {run_time}".format(run_time = clock_restart.time())) print("restart yet? (i.e. current run time >= interval): {restart}".format(restart = clock_restart.time() >= interval)) if clock_restart.time() >= interval: print(pyprel.center_string(text = pyprel.render_banner(text = "restart"))) os.execv(__file__, sys.argv) time.sleep(1)
def main(): print("start") options = docopt.docopt(__doc__, version=__version__) if options["--version"]: print(version) exit() global interval interval = float(options["--interval"]) global clock_restart clock_restart = shijian.Clock(name="restart") restart_control = multiprocessing.Process( target=restart_if_beyond_interval) restart_control.start() while True: print(options["--data"]) time.sleep(2)
def main(): print("create clock alpha") alpha = shijian.Clock(name="alpha") print("clock alpha start time: {time}".format(time=alpha.start_time())) print("sleep 2 seconds") time.sleep(2) print("clock alpha current time (s): {time}".format(time=alpha.time())) print("\ncreate clock beta") beta = shijian.Clock(name="beta") print("clock beta start time: {time}".format(time=beta.start_time())) print("clock beta stop time: {time}".format(time=beta.stop_time())) print("sleep 2 seconds") time.sleep(2) print("clock beta current time (s): {time}".format(time=beta.time())) print("stop clock beta") beta.stop() print("clock beta start time: {time}".format(time=beta.start_time())) print("clock beta stop time: {time}".format(time=beta.stop_time())) print("sleep 2 seconds") time.sleep(2) print("clock beta start time: {time}".format(time=beta.start_time())) print("clock beta stop time: {time}".format(time=beta.stop_time())) print("clock beta current time (s): {time}".format(time=beta.time())) print("\nclock beta printout:\n") beta.printout() print("create two gamma clocks") gamma = shijian.Clock(name="gamma") gamma = shijian.Clock(name="gamma") print("sleep 2 seconds") time.sleep(2) print("\ncreate two unnamed clocks") delta = shijian.Clock() epsilon = shijian.Clock() print("sleep 2 seconds") time.sleep(2) print("\nrun function 1 (which is timed using internal clocks)") print("result of function 1: {result}".format(result=function_1())) print("\nrun function 2 (which is timed using a decorator)") print("result of function 2: {result}".format(result=function_2())) print("\ncreate clock zeta, to illustrate clock resets") zeta = shijian.Clock(name="zeta") print("clock zeta start time: {time}".format(time=zeta.start_time())) print("sleep 2 seconds") time.sleep(2) print("clock zeta current time (s): {time}".format(time=zeta.time())) print("reset clock zeta and start it again") zeta.reset() zeta.start() print("clock zeta start time: {time}".format(time=zeta.start_time())) print("sleep 2 seconds") time.sleep(2) print("clock zeta current time (s): {time}".format(time=zeta.time())) print("\nclocks full printout:\n") shijian.clocks.printout(style="full") print("clocks statistics printout:\n") shijian.clocks.printout()