def test_run_follow_cmd(self): spy = mock.Mock(wraps=Feed()) cli = Cli(spy) cli.run(Command(Action.FOLLOW, "Josh", "Bob")) spy.follow.assert_called_once_with(follower="Josh", followed="Bob") spy.get_messages_of.assert_not_called()
def test_env(self): cmd = Command(['/usr/bin/env'], env_vars={'FOO': 'BAR'}) cmd.execute() self.assertTrue("FOO=BAR\n" in cmd.getoutput())
def __init__(self, webqq): self.webqq = webqq self.cmd = Command()
def test_subst_append_default(self): cmd = Command(['foo', 'ARG', 'bar'], args_subst={"ARG": "blah"}, args_append=["1", "2"]) self.assertEqual(['foo', 'blah', 'bar', '1', '2'], cmd.cmd)
def test_execute_nonexistent(self): cmd = Command(['/baaah', '/etc/passwd']) cmd.execute() self.assertEqual(None, cmd.getretcode()) self.assertEqual(Command.ERRORED, cmd.getstate())
os.chdir("/") except OSError as e: logger.error("cannot change working directory to /", exc_info=True) sys.exit(1) lock = filelock.FileLock( os.path.join(tempfile.gettempdir(), "opengrok-sync.lock")) try: with lock.acquire(timeout=0): pool = Pool(processes=int(args.workers)) if args.projects: dirs_to_process = args.projects elif args.indexed: # XXX replace this with REST request after issue #1801 cmd = Command([messages_file, '-n', 'project', 'list-indexed']) cmd.execute() if cmd.state is "finished": for line in cmd.getoutput(): dirs_to_process.append(line.strip()) else: logger.error("cannot get list of projects") sys.exit(1) else: directory = args.directory for entry in os.listdir(directory): if path.isdir(path.join(directory, entry)): dirs_to_process.append(entry) logger.debug("to process: {}".format(dirs_to_process))
def processMessages(self): """Locks own instance and acts on incoming messages.""" self.lock.acquire() logger.debug("begin message handling") for c in self.messageQueue: if not self.uid in c.hops: if c.hops[0] == 'local': del c.hops[0] # relay if we're not already in the hops list c.hops.append(self.uid) if c.msgType == 1: # apply friend's trustlevel if not 'Trustlevel' in c.parameter: logger.warn( "Incoming Message has no Trustlevel, I won't trust it. Never." ) c.parameter['Trustlevel'] = 0 if c.sender != "local": c.parameter['Trustlevel'] = int( (float(c.sender.trustLevel) / 100 * float(c.parameter['Trustlevel']) / 100) * 100) logger.debug("Message now has trust level " + str(c.parameter['Trustlevel'])) # Aggregate trustlevel if that IP is already in our database under these conditions: # * Timestamp of the received message has changed # * The originator of this message did not send it before relay = True ipindb = False if len(self.banList) > 0: for ban in self.banList: if ban['AttackerIP'] == c.parameter['AttackerIP']: ipindb = True logger.debug("IP already in database.") if int(ban['Timestamp']) != int( c.parameter['Timestamp']): if not c.hops[0] in ban['Hops']: trustold = ban['Trustlevel'] trustnew = int(trustold) + int( c.parameter['Trustlevel']) if trustnew > 100: trustnew = 100 ban['Trustlevel'] = trustnew ban['Hops'].append(c.hops[0]) logger.debug( "TrustLevel for this IP is now " + str(trustnew)) c.parameter['Trustlevel'] = trustnew else: relay = False logger.debug( "There is already an entry from %s in our database, do nothing with this message." % c.hops[0]) else: relay = False logger.debug( "Timestamp has not changed, do nothing with this message" ) if not ipindb: self.banList.append({ 'AttackerIP': c.parameter['AttackerIP'], 'Timestamp': c.parameter['Timestamp'], 'BanTime': self.banTime, 'Trustlevel': c.parameter['Trustlevel'], 'Hops': [c.hops[0]] }) logger.debug("Added %s to internal banlist" % (c.parameter['AttackerIP'])) # write ban entry to log if the message's trust level is above our own threshold if relay: if int(c.parameter['Trustlevel']) >= int( config.Config().threshold): logger.ban(c.parameter['AttackerIP']) else: logger.debug( "Message's trust level (%s) was below our threshold (%s)" % (c.parameter['Trustlevel'], config.Config().threshold)) # Relay message for friend in self.friends: logger.debug("sending message to all friends") friend.sendCommand(c) if c.msgType == 3: # dump all ips from banlist to the friend who send this dump request sender_uid = c.hops[0] for friend in self.friends: logger.debug( "Comparing senders uid (%s) with one of our friends uid (%s)" % (sender_uid, friend.uid)) if friend.uid == sender_uid: logger.debug( "The message is from our friend %s (uid: %s)" % (friend.name, friend.uid)) logger.debug("Dumping banlist to %s (uid: %s)" % (friend.name, friend.uid)) if len(self.banList) > 0: for ban in self.banList: c = Command() c.msgType = 1 c.hops = [self.uid] c.protocolVersion = version.protocolVersion c.parameter = { "AttackerIP": ban['AttackerIP'], "Timestamp": ban['Timestamp'], "Trustlevel": ban['Trustlevel'] } friend.sendCommand(c) else: logger.debug( "I know this message, I won't resend it to prevent loops") logger.debug("end message handling") self.messageQueue = [] logger.debug("deleted processed messages") self.lock.release()
def main(): server_queue = Queue() server = Server(server_queue, LOCALADDR) server.setDaemon(True) # command functions (those that are too complex for lambdas) def shutdown(): print("\n[**] Shutting down Kardinal.\n") server.shutdown_flag.set() exit(0) def set_targets(): print("\nAvailable nodes:") print("\n{}\n".format( to_table(("INDEX", "IP ADDRESS", "PORT"), [(n + 1, node.addr[0], node.addr[1]) for n, node in enumerate(nodes)]))) print("Current targets:") print("\n{}\n".format( to_table(("INDEX", "IP ADDRESS", "PORT"), [(n + 1, node.addr[0], node.addr[1]) for n, node in enumerate(nodes) if node.is_target]))) targets = [ int(t) - 1 for t in input( "Enter space separated list of indices: ").split(" ") ] for n, node in enumerate(nodes): node.is_target = True if n in targets else False # command list commands = [ Command("LIST", "Show all connected nodes.", (lambda: "\n{}\n".format( to_table(("IP ADDRESS", "PORT"), [node.addr for node in nodes])))), Command( "CLEAR", "Clear the screen.", (lambda: sys.stderr.write("\x1b[2J\x1b[H")) ), # previously `chr(27) + "[2J" was working on windows, but not in zsh gnome-terminal Command("EXIT", "Shutdown Kardinal.", shutdown), Command("SET_TARGETS", "Set targets for next set of commands", set_targets), Command("LIST_TARGETS", "List all currently selected targets", (lambda: "\n{}\n".format( to_table( ("IP ADDRESS", "PORT"), [node.addr for node in nodes if node.is_target])))), Command( "[node #s] [command]", "run a command on a comma separated list of nodes\ne.g. /1,2 ifconfig", tmp_command) ] # NOTE: must put help outside list literal to avoid "commands uninitialized" error commands.append( Command("HELP", "Show this help.", (lambda: "\n" + "\n".join([ "/" + cmd.name + "\n".join([ calc_tab( len(cmd.name) if l == 0 else -TABSIZE - 1, max([len(c.name) for c in commands])) + line for l, line in enumerate(cmd.desc.split("\n")) ]) for cmd in commands ]) + "\n"))) server.start() print("[**] Listening on {}:{}".format(LOCALADDR[0], LOCALADDR[1])) try: while True: requested_cmd = input(">> ") if requested_cmd[0] == "/": for cmd in commands: if requested_cmd[1:].upper() == cmd.name: print(cmd.todo()) else: with server.commands.mutex: server.commands.queue.clear() server.commands.put(requested_cmd) except KeyboardInterrupt: server.shutdown_flag.set()
def test_override_default_verbosity_false(self): Command.set_default_verbosity(True) ls = Command('ls', False) self.assertFalse(ls._verbose)
class TestCliFeed(unittest.TestCase): josh_hello_cmd = Command(action=Action.WRITE, actor="Josh", arg_dependant_on_action="Hello!") jonny_display_cmd = Command(action=Action.DISPLAY_OWN_POSTS, actor="Jonny", arg_dependant_on_action=None) def test_run_display_own_msg(self): spy = mock.Mock(wraps=Feed()) cli = Cli(spy) cli.run(self.jonny_display_cmd) spy.get_messages_of.assert_called() def test_parse_display_own_msg(self): parsed_cmd = self.jonny_display_cmd self.assertEqual(parsed_cmd, Cli(None).parse("Jonny")) def test_run_write(self): spy = mock.Mock(wraps=Feed()) cli = Cli(spy) cli.run(self.josh_hello_cmd) spy.post_message.assert_called_once_with(username="******", message="Hello!") spy.get_messages_of.assert_not_called() def test_parse_write(self): cli = Cli(None) res = cli.parse("Josh -> Hello!") parsed_cmd = self.josh_hello_cmd self.assertEqual(parsed_cmd, res) def test_run_follow_cmd(self): spy = mock.Mock(wraps=Feed()) cli = Cli(spy) cli.run(Command(Action.FOLLOW, "Josh", "Bob")) spy.follow.assert_called_once_with(follower="Josh", followed="Bob") spy.get_messages_of.assert_not_called() def test_parse_follow_command(self): cmd_str = "Josh follows Bob" cmd = Command(Action.FOLLOW, "Josh", "Bob") self.assertEqual(cmd, Cli(None).parse(cmd_str)) def test_run_follow_cmd(self): spy = mock.Mock(wraps=Feed()) cli = Cli(spy) cli.run(Command(Action.DISPLAY_RELEVANT_POSTS, "Josh", None)) spy.get_wall_for.assert_called_once_with("Josh") spy.get_messages_of.assert_not_called() def test_parse_wall_command(self): cmd_str = "Josh wall" cmd = Command(Action.DISPLAY_RELEVANT_POSTS, "Josh", None) self.assertEqual(cmd, Cli(None).parse(cmd_str)) def test_hash_command(self): another_josh_hello_cmd = Command(action=Action.WRITE, actor="Josh", arg_dependant_on_action="Hello!") self.assertEqual(another_josh_hello_cmd, self.josh_hello_cmd) self.assertEqual(hash(another_josh_hello_cmd), hash(self.josh_hello_cmd))
def call(cmd, timeout=None): cmd = Command(cmd) return cmd.run( ARGS.timeout, shell=True, preexec_fn=os.setsid ) # http://stackoverflow.com/questions/4789837/how-to-terminate-a-python-subprocess-launched-with-shell-true
def test_parse_wall_command(self): cmd_str = "Josh wall" cmd = Command(Action.DISPLAY_RELEVANT_POSTS, "Josh", None) self.assertEqual(cmd, Cli(None).parse(cmd_str))
def test_run_follow_cmd(self): spy = mock.Mock(wraps=Feed()) cli = Cli(spy) cli.run(Command(Action.DISPLAY_RELEVANT_POSTS, "Josh", None)) spy.get_wall_for.assert_called_once_with("Josh") spy.get_messages_of.assert_not_called()
def test_parse_follow_command(self): cmd_str = "Josh follows Bob" cmd = Command(Action.FOLLOW, "Josh", "Bob") self.assertEqual(cmd, Cli(None).parse(cmd_str))
from telebot.types import * from botstate import BotState from command import Command from database import DataBase from yandexdisk import YandexDisk def do(bot: TeleBot, bot_state: BotState, message: Message, database: DataBase, ydisk: YandexDisk): bot.send_message( message.chat.id, "Напишите SQL запрос на изменение БД. Запросы для получения информации не работают!" ) def echo(bot: TeleBot, bot_state: BotState, message: Message, database: DataBase, ydisk: YandexDisk): result = database.execute(message.text) print("{}: tried to execute SQL command ({}) with result: {}".format( message.from_user.username, message.text, result)) bot.send_message(message.chat.id, result) execute_command = Command("execute", "Выполнить SQL запрос на изменение", do=do, is_admin_command=True, need_answer=True, echo=echo)
def _get_parameterized_commands(self): touch = Command('touch "{ins}"') remove = Command('rm "{ins}"') return (touch, remove)
) def end(bot: TeleBot, bot_state: BotState, message: Message, database: DataBase, ydisk: YandexDisk): print("{}: ended photo validation".format(message.from_user.username)) state_additional = bot_state.get_state(message.from_user.id)["additional"] state_additional["keyboard"]["normal"] = ReplyKeyboardRemove() state_additional["keyboard"]["with_cancel"] = ReplyKeyboardRemove() bot.send_message(message.chat.id, "Подождите немного. Выполняются изменения...", reply_markup=ReplyKeyboardRemove()) photos_for_deleting = database.get_photos_for_deleting( message.from_user.id) for photo in photos_for_deleting: if ydisk.disk.exists(photo["filepath"]): ydisk.disk.remove(photo["filepath"]) if photo["source"] is not None: database.increment_insta_stat(photo["source"], "unapproved_photos") database.delete_photo(photo["hash"]) bot.send_message(message.chat.id, "Ваши правки применены.") check_photos_command = Command("check_photos", "Валидация контента", do=do, is_admin_command=True, echo=echo, need_answer=True, end=end)
def test_implicit_outs(self): touch, remove = self._get_parameterized_commands() touch(self.file_) copy = Command('cat "{ins}" > "{outs}"') copy(self.file_, self.filename + '2') self.assertTrue(Path(self.filename + '2').exists())
f.write( "# File: slave.py\n" "import time, sys\n" "print 'Start count'\n" "for i in range(1, 5):\n" " print str(i)\n\n" " # important otherwise may trick the subprocess.stdout.readline function" " sys.stdout.flush()\n" " time.sleep(1)\n" "print 'Stop count'\n") f.close() if sys.platform == 'win32': sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) command = Command('python slave.py') command.run(silent=False) print 'command log (timeout = ' + str(command.timeout) + ', expectedString = '\ + str(command.expectedString) + ', silent = ' + str(command.silent) + ')' print ' command: ' + ' '.join(command.cmd) print ' output: "' + command.output.replace('\n', ' - ') + '"' print ' returncode: ' + str(command.returncode) print ' expectedStringFound: ' + str(command.expectedStringFound) print ' isTimeout: ' + str(command.isTimeout) command.run(timeout=2, silent=False) print 'command log (timeout = ' + str(command.timeout) + ', expectedString = '\ + str(command.expectedString) + ', silent = ' + str(command.silent) + ')' print ' command: ' + ' '.join(command.cmd) print ' output: "' + command.output.replace('\n', ' - ') + '"'
def test_only_outs(self): write_hi = Command('echo hi > "{outs}"') write_hi(outs=self.file_) self.assertTrue(self.file_.exists())
help="config file") # subparsers = parser.add_subparsers(dest='command', help='sub-command help') # member_parser = subparsers.add_parser('member', help='member operations') # member_parser.add_argument('-n',help='add new member') # add_parser = subparsers.add_parser('add', help='expenses operaiton') # add_parser.add_argument('-m', help='member name') args = parser.parse_args() print(args) return args def create_def_config(): config['remote'] = { 'url': "https://docs.google.com/spreadsheets/d/e/2PACX-1vQ3SK7T0uZq0DotBWk5gFOj8fYa_chLYHlFHcQR-cUwSo2Tt-9DMs-q3clBYk2IBuB5B6abVLy3q0wX/pub?gid=0&single=true&output=csv" } if __name__ == "__main__": print("Welcome in expenses equalizer.") create_def_config() eq = Equalizer(url=config['remote']['url']) cmd = Command() load_exp_data() cmd.cmdloop()
def test_default_verbosity_is_false(self): ls = Command('ls') self.assertFalse(ls._verbose)
def test_subst_append_exclsubst(self): cmd = Command(['foo', 'ARG', 'bar'], args_subst={"ARG": "blah"}, args_append=["1", "2"], excl_subst=True) self.assertEqual(['foo', 'blah', 'bar'], cmd.cmd)
def test_verbosity_true(self): ls = Command('ls', verbose=True) self.assertTrue(ls._verbose)
def test_getoutput(self): cmd = Command(['/bin/ls', '/etc/passwd']) cmd.execute() self.assertEqual(['/etc/passwd\n'], cmd.getoutput())
def test_set_default_verbosity_true(self): Command.set_default_verbosity(True) ls = Command('ls') self.assertTrue(ls._verbose)
def test_str(self): cmd = Command(["foo", "bar"]) self.assertEqual("foo bar", str(cmd))
def export(**args): command = Command() command.run_export(**args)
def get_command(self): return Command( self.ope_list[self.current_index]) if self.remain_command else None
def pushImage(dockerImageTagList, sshHost, sshIdentityFile, sshPort, primeImages, registryPort): # Setup remote docker registry print("Setting up secure private registry... ") registryCommandResult = Command("ssh", [ "-i", sshIdentityFile, "-p", sshPort, "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", sshHost, "sh -l -c \"docker run -d -v /etc/docker-push-ssh/registry:/var/lib/registry " + "--name docker-push-ssh-registry -p 127.0.0.1:{0}:5000 registry\"".format(registryPort) ]).execute() if registryCommandResult.failed(): print("ERROR") print(registryCommandResult.stdout) print(registryCommandResult.stderr) return False try: # Establish ssh tunnel print("Establishing SSH Tunnel...") sshTunnelCommandResult = Command("docker", [ "run", "-d", "--name", "docker-push-ssh-tunnel", "-p", "127.0.0.1:5000:5000", "-v", "{0}:/etc/ssh_key_file".format(sshIdentityFile), "brthornbury/docker-alpine-ssh", "ssh", "-N", "-L", "*:5000:localhost:{0}".format(registryPort), "-i", "/etc/ssh_key_file", "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", "-p", sshPort, sshHost ]).environment_dict(os.environ).execute() if sshTunnelCommandResult.failed(): print("ERROR") print(sshTunnelCommandResult.stdout) print(sshTunnelCommandResult.stderr) return False print("Waiting for SSH Tunnel Initialization...") if not waitForSshTunnelInit(): print("ERROR") print("SSH Tunnel failed to initialize.") logsCmd = Command("docker", ["logs", "docker-push-ssh-tunnel"]).environment_dict(os.environ).execute() print(logsCmd.stdout, logsCmd.stderr) return False if sshTunnelCommandResult.failed(): print("ERROR") print(sshTunnelCommandResult.stdout) print(sshTunnelCommandResult.stderr) return False print("Priming Registry with base images...") for primeImage in (primeImages or []): print("Priming base image ({0})".format(primeImage)) primingCommand = Command("ssh", [ "-i", sshIdentityFile, "-p", sshPort, "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", sshHost, "sh -l -c \"docker pull {0}".format(primeImage) + " && docker tag {0} localhost:{1}/{0} && docker push localhost:{1}/{0}\"".format(primeImage, registryPort) ]).execute() if primingCommand.failed(): print("ERROR") print(primingCommand.stdout) print(primingCommand.stderr) return False print("Tagging image(s) for push...") for dockerImageTag in dockerImageTagList: tagCommandResult = Command("docker", [ "tag", dockerImageTag, "localhost:5000/{0}".format(dockerImageTag) ]).environment_dict(os.environ).execute() if tagCommandResult.failed(): print("ERROR") print(tagCommandResult.stdout) print(tagCommandResult.stderr) return False print("Pushing Image(s) from local host...") for dockerImageTag in dockerImageTagList: pushDockerImageCommandResult = Command("docker", [ "push", "localhost:5000/{0}".format(dockerImageTag) ]).environment_dict(os.environ).execute() if pushDockerImageCommandResult.failed(): print("ERROR") print(pushDockerImageCommandResult.stdout) print(pushDockerImageCommandResult.stderr) print("Error Pushing Image: Ensure localhost:5000 is added to your insecure registries.") print("More Details (OS X): " "https://stackoverflow.com/questions/32808215/where-to-set-the-insecure-registry-flag-on-mac-os") return False print("Pushed Image {0} Successfully...".format(dockerImageTag)) print("Pulling and Retagging Image on remote host...") for dockerImageTag in dockerImageTagList: pullDockerImageCommandResult = Command("ssh", [ "-i", sshIdentityFile, "-p", sshPort, "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", sshHost, "sh -l -c \"docker pull " + "localhost:{1}/{0}".format(dockerImageTag, registryPort) + " && docker tag localhost:{1}/{0} {0}\"".format(dockerImageTag, registryPort) ]).execute() if pullDockerImageCommandResult.failed(): print("ERROR") print(pullDockerImageCommandResult.stdout) print(pullDockerImageCommandResult.stderr) return False print("Pulled Image {0} Successfully...".format(dockerImageTag)) finally: print("Cleaning up...") Command("ssh", [ "-i", sshIdentityFile, "-p", sshPort, "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", sshHost, "sh -l -c \"docker rm -f docker-push-ssh-registry\"" ]).execute() Command("docker", [ "rm", "-f", "docker-push-ssh-tunnel" ]).environment_dict(os.environ).execute() for dockerImageTag in dockerImageTagList: Command("docker", [ "image", "rm", "localhost:5000/{0}".format(dockerImageTag) ]).environment_dict(os.environ).execute() return True