import json import os from userio import say, error, ok, warn import sys # initialization if not os.path.isfile("configuration.json"): warn("No configuration file found. First launch?") say("Generating configuration file...") default_configuration = { "authentication": { "consumer_key": "consumerKeyHere", "consumer_secret": "consumerSecretHere", "access_token": "accessTokenHere", "access_token_secret": "accessTokenSecretHere" }, "monitored_accounts": [25073877, 822215679726100480], "intervals": { "fullpass": 600, "account": 1200, "pass": 5 }, "notifications": { "email": "*****@*****.**" } } with open("configuration.json", "w") as configuration_file: json.dump(default_configuration, configuration_file,
channels = None def get_channel(handle): global channels if channels is None: channels = {} for account in get_accounts(): channels[account[0]] = account[2] channels[account[0][1:]] = account[2] # so that @names defined in config still match return channels[handle] # Generate the default config. Will override existing config. def generate_config(): warn("Generating default config...") with open("config.json", "w") as config: json.dump(default_config, config, indent=4, sort_keys=True) ok("Generated default config!") if not os.path.exists("config.json"): warn("No configuration file found!") generate_config() say("Please edit the configuration file. LibreNews Server will now shutdown." ) sys.exit(1)
def generate_config(): warn("Generating default config...") with open("config.json", "w") as config: json.dump(default_config, config, indent=4, sort_keys=True) ok("Generated default config!")
def doprocess(command, **kwargs): passkwargs = {'bufsize': 1, 'stdin': subprocess.PIPE, 'stdout': subprocess.PIPE, 'stderr': subprocess.PIPE, 'shell': False} OAmmandblock = '' cmdargs = [] printstdout = False retryable = False showchange = False tryagain = True stdin = None trapsignals = False noparse = False returndict = {} mypath = "/bin:/usr/bin:/usr/local/bin" myldlibrarypath = "/lib" myenv = {"PATH": mypath, "LD_LIBRARY_PATH": myldlibrarypath} mylist = command.split(' ') debug = False for item in mylist: cmdargs.append(item) if 'debug' in kwargs.keys(): debug = kwargs['debug'] if 'noparse' in kwargs.keys(): noparse = kwargs['noparse'] if 'env' in kwargs.keys(): for key in kwargs['env'].keys(): if key in myenv.keys(): myenv[key] = myenv[key]+":"+kwargs['env'][key] else: myenv[key] = kwargs['env'][key] passkwargs['env'] = myenv if 'user' in kwargs.keys(): useraccount = kwargs['user'] if 'showchange' in kwargs.keys(): passkwargs['preexec_fn'] = changeuser(useraccount, showchange=kwargs['showchange']) else: passkwargs['preexec_fn'] = changeuser(useraccount, showchange=False) if 'printstdout' in kwargs.keys(): printstdout = kwargs['printstdout'] if 'retry' in kwargs.keys(): retryable = kwargs['retry'] if 'input' in kwargs.keys(): if type(kwargs['input']) is str: stdin = kwargs['input'] stdin = stdin + "\n" elif type(kwargs['input']) is list: stdin = '' for line in kwargs['input']: stdin = stdin + line if not stdin[-1] == "\n": stdin = stdin + "\n" if 'cwd' in kwargs.keys(): passkwargs['cwd'] = kwargs['cwd'] if 'trapsignals' in kwargs.keys(): signal.signal(signal.SIGINT, signal2exit) while tryagain: tryagain = False stdout = [] stderr = [] resultcode = None try: cmd = subprocess.Popen(cmdargs, **passkwargs) except Exception: resultcode = 1 if resultcode is None: if printstdout: if stdin is not None: cmd.stdin.write(stdin) while cmd.poll() is None: nextline = cmd.stdout.readline().rstrip() if len(nextline) > 0: userio.message(nextline) stdout.append(nextline) remainder = cmd.stdout.readlines() for nextline in remainder: if len(nextline) > 0: userio.message(nextline) stdout.append(nextline) stderr = cmd.stderr.readlines() else: if stdin is not None: cmdout, cmderr = cmd.communicate(stdin) else: cmdout, cmderr = cmd.communicate() if noparse: stdout = cmdout stderr = cmderr else: lines = cmdout.splitlines() for line in lines: if len(line) > 0: stdout.append(line) lines = cmderr.splitlines() for line in lines: if len(line) > 0: stderr.append(line) resultcode = cmd.returncode if retryable and resultcode > 0: userio.warn("Errors encountered during '" + command + "'") for line in stdout: userio.message("STDOUT: " + line) for line in stderr: userio.message("STDERR: " + line) tryagain = userio.yesno("Retry?") if debug: for line in stdout: userio.message("STDOUT-->" + line) for line in stderr: userio.message("STDERR-->" + line) returndict['STDOUT'] = stdout returndict['STDERR'] = stderr returndict['RESULT'] = resultcode return(returndict)