Пример #1
0
def h_run(args):
    # Maybe do the config loading here?
    if not os.path.exists("config.json"):
        print(
            "Failed to start: No halibot configuration found in the current directory!"
        )
        return

    bot = halibot.Halibot()
    bot._load_config()

    logfile = None
    loglevel = logging.DEBUG
    loglevel_str = "DEBUG"
    loglevels = {
        "CRITICAL": logging.CRITICAL,
        "ERROR": logging.ERROR,
        "WARNING": logging.WARNING,
        "INFO": logging.INFO,
        "DEBUG": logging.DEBUG,
        "NOTSET": logging.NOTSET
    }

    # Set log level if argument is provided
    if args.log_level:
        loglevel_str = args.log_level
    elif "log-level" in bot.config:
        loglevel_str = bot.config["log-level"]

    # Error check log level string
    if loglevel_str not in loglevels:
        print("Log level '{}' invalid.".format(loglevel_str))
        return
    else:
        loglevel = loglevels[loglevel_str]

    # Set log file if argument is provided
    if args.log_file:
        logfile = args.log_file
    elif "log-file" in bot.config:
        logfile = bot.config["log-file"]

    logging.basicConfig(filename=logfile, level=loglevel)

    # Print used configuration
    if logfile:
        print("Logs at '{}' with log level '{}'".format(logfile, loglevel_str))
    else:
        print("Log level '{}'".format(loglevel_str))

    bot.start(block=True)

    if args.interactive:
        local = {
            "bot": bot,
            "halibot": halibot,
        }
        code.interact(banner="Halibot welcomes you!", local=local)
        bot.shutdown()
        print("Halibot bides you farewell.")
Пример #2
0
def h_search(args):
    # In order to access the config easily
    bot = halibot.Halibot()
    bot._load_config()

    # Error checking
    if not "repos" in bot.config:
        print("There are no repos specified in the config.json.")
        print("I have nowhere to search!")
        return

    # Query all listed repositories
    results = {}
    for r in bot.config["repos"]:
        url = r + "/search/"
        if args.term != None:
            url += args.term

        data = urllib.request.urlopen(url).read().decode('utf-8')
        subres = json.loads(data)
        results = dict(list(subres.items()) + list(results.items()))

    # Output results
    sorted_keys = list(results.keys())
    sorted_keys.sort()
    for name in sorted_keys:
        print(name, "-", results[name]['description'])
Пример #3
0
def h_add(args):
    # In order to access the config easily
    bot = halibot.Halibot()
    bot._load_config()

    for clspath in args.things:
        # Validate that it is actually an object
        split = clspath.split(":")
        if len(split) > 2:
            print(
                "Invalid class path '{}', expected no more than 1 colon (:).".
                format(clspath))
            continue

        pkg = bot.get_package(split[0])
        if pkg == None:
            print("Cannot find package '{}'.".format(split[0]))
            continue

        if len(split) == 1:
            if not hasattr(pkg, "Default"):
                print(
                    "Package '{}' has no default class, must specify the class to add explicitly."
                    .format(split[0]))
                continue
            cls = pkg.Default
            clspath += ":Default"
        else:
            cls = getattr(pkg, split[1], None)
            if cls == None:
                print("Class '{}' does not exist on package '{}'.".format(
                    split[1], split[0]))
                continue

        if args.destkey:
            destkey = args.destkey
        else:
            if issubclass(cls, halibot.HalModule):
                destkey = "module-instances"
            elif issubclass(cls, halibot.HalAgent):
                destkey = "agent-instances"
            else:
                print(
                    "Cannot determine if '{}' is a module or agent, use '-m' or '-a'."
                )
                continue

        (name, conf) = cls.configure({'of': clspath})

        if name in bot.config["agent-instances"] or name in bot.config[
                "module-instances"]:
            print(
                "Instance name '{}' is already in configuration, please choose a different instance name"
                .format(name))
            return

        bot.config[destkey][name] = conf

    with open("config.json", "w") as f:
        f.write(json.dumps(bot.config, sort_keys=True, indent=4))
Пример #4
0
def h_fetch(args):
    # In order to access the config easily
    bot = halibot.Halibot()
    bot._load_config()

    # Error checking
    if not "repos" in bot.config:
        print("There are no repos specified in the config.json.")
        print("I have nothing to fetch from!")
        return

    # Try to fetch each requested package
    for name in args.packages:
        # Install to the first package path by default
        dst = os.path.join(bot.config["package-path"][0], name)

        success = False
        for r in bot.config["repos"]:
            src = r + "/fetch/" + name
            try:
                print("Trying to fetch package from '{}'...".format(r))
                bio = io.BytesIO(urllib.request.urlopen(src).read())
                tar = tarfile.open(mode="r:*", fileobj=bio)
                os.mkdir(dst)
                tar.extractall(dst)

                print("\033[92mSuccessfully fetched '{}' into '{}'.\033[0m".
                      format(name, dst))
                success = True
                break
            except Exception as e:
                print(e)

        if not success:
            print("\033[91mFailed to fetch '{}'!\033[0m".format(name))
Пример #5
0
def h_list_packages(args):
    bot = halibot.Halibot()
    bot._load_config()

    pkgs = []
    for path in bot.config.get("package-path"):
        pkgs = pkgs + os.listdir(path)
    pkgs.sort()

    print("\nInstalled packages:")
    for p in pkgs:
        print("  {}".format(p))
    print("")
Пример #6
0
def h_fetch(args):
	# In order to access the config easily
	bot = halibot.Halibot()
	bot._load_config()

	# Error checking
	if not "repos" in bot.config:
		print("There are no repos specified in the config.json.")
		print("I have nothing to fetch from!")
		return

	# Try to fetch each requested package
	for name in args.packages:
		# Install to the first package path by default
		dst = os.path.join(bot.config["package-path"][0], name)

		success = False
		for r in bot.config["repos"]:
			src = r.format(name)
			try:
				if src.startswith("git://"):
					if noGit:
						raise Exception("The git module is not installed, I cannot clone git repos.")
					print("Trying to git clone '{}'...".format(src))
					Repo.clone_from(src, dst)
				elif src.endswith(".zip"):
					print("Trying to extract zip from '{}'...".format(src))
					bio = io.BytesIO(urllib.request.urlopen(src).readall())
					z = zipfile.ZipFile(bio)
					os.mkdir(dst)
					z.extractall(dst)
				elif src.endswith( (".tar", ".tar.gz", ".tar.bz", ".tar.xz" ) ):
					print("Trying to extract tarball from '{}'...".format(src))
					bio = io.BytesIO(urllib.request.urlopen(src).readall())
					tar = tarfile.open(mode="r:*", fileobj=bio)
					os.mkdir(dst)
					tar.extractall(dst)
				else:
					raise Exception("I do not know how to handle the path '{}'".format(src))

				print("\033[92mSuccessfully fetched '{}' into '{}'.\033[0m".format(name, dst))
				success = True
				break
			except Exception as e:
				print(e)

		if not success:
			print("\033[91mFailed to fetch '{}'!\033[0m".format(name))
Пример #7
0
def h_rm(args):
    # In order to access the config easily
    bot = halibot.Halibot()
    bot._load_config()

    for name in args.names:
        if name in bot.config["agent-instances"]:
            bot.config["agent-instances"].pop(name)
        elif name in bot.config["module-instances"]:
            bot.config["module-instances"].pop(name)
        else:
            print("No such object '{}'".format(name))
            continue
        print("Removed '{}'.".format(name))

    bot._write_config()
Пример #8
0
def h_rm(args):
    # In order to access the config easily
    bot = halibot.Halibot()
    bot._load_config()

    for name in args.names:
        if name in bot.config["agent-instances"]:
            bot.config["agent-instances"].pop(name)
        elif name in bot.config["module-instances"]:
            bot.config["module-instances"].pop(name)
        else:
            print("No such object '{}'".format(name))
            continue
        print("Removed '{}'.".format(name))

    with open("config.json", "w") as f:
        f.write(json.dumps(bot.config, sort_keys=True, indent=4))
Пример #9
0
def h_unfetch(args):
    # In order to access the config easily
    bot = halibot.Halibot()
    bot._load_config()

    # Remove each requested package
    for name in args.packages:
        # Install to the first package path by default
        success = False
        for pkgpath in bot.config["package-path"]:
            path = os.path.join(pkgpath, name)
            if os.path.exists(path):
                shutil.rmtree(path)
                print("Removed '{}' installed to '{}'.".format(name, path))
                success = True

        if not success:
            print("Could not find package '{}'".format(name))
Пример #10
0
def h_run(args):
	# Maybe do the config loading here?
	if not os.path.exists("config.json"):
		print("Failed to start: No halibot configuration found in the current directory!")
		return

	logging.basicConfig(level=logging.DEBUG)
	bot = halibot.Halibot()
	bot.start(block=True)

	if args.interactive:
		local = {
			"bot": bot,
			"halibot": halibot,
		}
		code.interact(banner="Halibot welcomes you!", local=local)
		bot.shutdown()
		print("Halibot bides you farewell.")
Пример #11
0
def h_info(args):
    bot = halibot.Halibot()
    bot._load_config()

    if args.object_name:
        # Show configuration of specific object

        conf = bot.config.get("agent-instances", {}).get(args.object_name)
        conf = bot.config.get("module-instances", {}).get(
            args.object_name) if not conf else conf
        if not conf:
            print("No such agent or module")
            return

        print("\n{args.object_name}: ({conf['of']})".format(args=args,
                                                            conf=conf))
        for k in conf:
            if k != "of":
                print("  {k}: {conf[k]}".format(k=k, conf=conf))
    else:
        # Show all configured objects
        if len(bot.config.get("agent-instances", {})) > 0:
            print("\nConfigured agents:")
            agents = bot.config.get("agent-instances")
            for name in agents:
                print("  {name} ({agents[name]['of']})".format(name=name,
                                                               agents=agents))

        if len(bot.config.get("module-instances", {})) > 0:
            print("\nConfigured modules:")
            modules = bot.config.get("module-instances")
            for name in modules:
                print("  {name} ({modules[name]['of']})".format(
                    name=name, modules=modules))

    print("")
Пример #12
0
def h_config(args):
    # In order to access the config easily
    bot = halibot.Halibot()
    bot._load_config()

    if args.name in bot.config["agent-instances"]:
        destkey = "agent-instances"
    elif args.name in bot.config["module-instances"]:
        destkey = "module-instances"
    else:
        print('No such module or agent exists.')
        return
    pkgconf = bot.config[destkey][args.name]

    # Show or edit the config?
    if args.show:
        # Only a single key?
        if args.key != None:
            print(pkgconf[args.key])
        else:
            for k in pkgconf:
                print(k, "=", pkgconf[k])
    else:
        # Reconfigure the package
        if args.key != None:
            if args.value == None:
                print("You must specify a value with -v.")
                return

            # Detect the type to set
            if args.type:
                ty = args.type
            else:
                if args.key in pkgconf:
                    typecls = type(pkgconf[args.key])
                    if typecls == int: ty = "number"
                    elif typecls == float: ty = "number"
                    elif typecls == str: ty = "string"
                    elif typecls == bool: ty = "boolean"
                    else:
                        print("The type of the key '" + args.key +
                              "' is not a settable type.")
                        return
                else:
                    print(
                        "That key does not exist, you must specify the type with -t."
                    )

            # Get the value
            if ty == "string": value = args.value
            if ty == "number": value = float(args.value)
            if ty == "boolean":
                if args.value.lower() == 'true': value = True
                elif args.value.lower() == 'false': value = False
                else:
                    print("Invalid boolean value, specify 'true' or 'false'.")
                    return

            pkgconf[args.key] = value
        else:
            if not "of" in pkgconf:
                print("Corrupt config, package has no 'of' key.")
                return

            split = pkgconf["of"].split(":")
            if len(split) != 2:
                print("Corrupt config, malformed 'of' value.")
                return

            pkg = bot.get_package(split[0])
            if pkg == None:
                print("The package '" + split[0] +
                      "' does not exist so it cannot be configured.")
                return

            cls = getattr(pkg, split[1], None)
            if not cls:
                print("The package '" + split[0] +
                      "' has no module or agent named '" + split[1] + "'.")
                return

            (name, conf) = cls.configure(pkgconf, name=args.name)
            bot.config[destkey][name] = conf

        with open("config.json", "w") as f:
            f.write(json.dumps(bot.config, sort_keys=True, indent=4))
Пример #13
0
 def setUp(self):
     # Silence expected error messages
     logging.basicConfig(level=logging.CRITICAL)
     self.bot = halibot.Halibot(use_config=False)
Пример #14
0
 def setUp(self):
     self.bot = halibot.Halibot(use_config=False)