Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Package Management Logic to a Module #114

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions halibot/halibot.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ class Halibot():

VERSION = "0.2.0"

config = {}

running = False
log = None

Expand All @@ -44,6 +42,11 @@ def __init__(self, **kwargs):
self.use_config = kwargs.get("use_config", True)
self.use_auth = kwargs.get("use_auth", True)
self.workdir = kwargs.get("workdir", ".")
self.config = kwargs.get("config", {})
if self.config:
halibot.packages.__path__ = self.config.get("package-path", [])

self.configfile = kwargs.get("configfile", "config.json")

self.auth = HalAuth()
self.objects = ObjectDict()
Expand All @@ -53,7 +56,8 @@ def start(self, block=True):
self.running = True

if self.use_config:
self._load_config()
if not self.config:
self._load_config(configfile=self.configfile)
self._instantiate_objects("agent")
self._instantiate_objects("module")
if self.use_auth:
Expand Down Expand Up @@ -89,13 +93,13 @@ def add_instance(self, name, inst):
inst.init()
self.log.info("Instantiated object '" + name + "'")

def _load_config(self):
with open(os.path.join(self.workdir, "config.json"), "r") as f:
def _load_config(self, configfile="config.json"):
with open(os.path.join(self.workdir, configfile), "r") as f:
self.config = json.loads(f.read())
halibot.packages.__path__ = self.config.get("package-path", [])

def _write_config(self):
with open(os.path.join(self.workdir, "config.json"), "w") as f:
def _write_config(self, configfile="config.json"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we pull out the "config.json" string and make it a constant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Late to the party here, but I'll update this PR when #126, #113, etc are merged. This line probably won't apply with those changes.

with open(os.path.join(self.workdir, configfile), "w") as f:
f.write(json.dumps(self.config, sort_keys=True, indent=4))


Expand Down
166 changes: 37 additions & 129 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,24 @@
import sys
import os
import shutil
import tarfile
import urllib.request
import io
import argparse
import code

DEFAULT_CONFIG = {
"package-path": [
"packages",
os.path.join(os.path.abspath(os.path.dirname(__file__)), "packages")
],
"repos": ["https://halibot.fish:4842"],
"agent-instances": {},
"module-instances": {},
"cli-instances": {
"pm": {
"of": "core:PackageManager"
}
}
}
Copy link
Member

@sjrct sjrct Jan 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should make this a file and add an option for init to specify the default config. I was considering doing that as part of a fix for #122 and #121, so there may be a conflict here.


def h_init(args):
confpath = os.path.join(args.path, "config.json")
pkgpath = os.path.join(args.path, "packages")
Expand All @@ -30,18 +42,8 @@ def h_init(args):
if not r.lower() in ("y", "yes", "yolo"):
return

config = {
"package-path": [
"packages",
os.path.join(os.path.abspath(os.path.dirname(__file__)), "packages")
],
"repos": ["https://halibot.fish:4842"],
"agent-instances": {},
"module-instances": {}
}

with open(confpath, "w") as f:
f.write(json.dumps(config, sort_keys=True, indent=4))
f.write(json.dumps(DEFAULT_CONFIG, sort_keys=True, indent=4))

if not os.path.exists(permpath):
with open(permpath, "w") as f:
Expand Down Expand Up @@ -113,103 +115,6 @@ def h_run(args):
bot.shutdown()
print("Halibot bides you farewell.")

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))

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'])


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))


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("")

def h_add(args):
# In order to access the config easily
Expand Down Expand Up @@ -364,12 +269,8 @@ def h_config(args):
subcmds = {
"init": h_init,
"run": h_run,
"fetch": h_fetch,
"unfetch": h_unfetch,
"add": h_add,
"rm": h_rm,
"packages": h_list_packages,
"search": h_search,
"config": h_config,
}

Expand All @@ -386,12 +287,6 @@ def h_config(args):
run.add_argument("-f", "--log-file", help="file to output logs to, none by default")
run.add_argument("-l", "--log-level", help="level of logs, DEBUG by default")

fetch = sub.add_parser("fetch", help="fetch remote packages")
fetch.add_argument("packages", help="name of package to fetch", nargs="+", metavar="package")

unfetch = sub.add_parser("unfetch", help="as if you never fetched them at all")
unfetch.add_argument("packages", help="name of package to unfetch", nargs="+", metavar="package")

add = sub.add_parser("add", help="add agents or modules to the local halibot instance")
add.add_argument("things", help="path to class to add", nargs="+", metavar="class")
addtype = add.add_mutually_exclusive_group()
Expand All @@ -401,22 +296,35 @@ def h_config(args):
rm = sub.add_parser("rm", help="remove agents or modules from the local halibot instance")
rm.add_argument("names", help="names of agents or modules to remove", nargs="+", metavar="name")

list_packages = sub.add_parser("packages", help="list all installed packages")

search = sub.add_parser("search", help="search for packages")
search.add_argument("term", help="what to search for", nargs="?", metavar="term")

config_cmd = sub.add_parser("config", help="configure or reconfigure a module or agent")
config_cmd.add_argument("name", help="name of the module or agent to show or reconfigure")
config_cmd.add_argument("-s", "--show", action="store_true", help="show the configuration rather than set it", required=False)
config_cmd.add_argument("-k", "--key", help="key to set or key to display with -s", required=False)
config_cmd.add_argument("-v", "--value", help="value to set key to", required=False)
config_cmd.add_argument("-t", "--type", choices=["string", "number", "boolean"], help="the type used while setting a config value with -k. If not given, it uses the type of the existing value")

hal = halibot.Halibot(config=DEFAULT_CONFIG)
try:
hal._load_config("config.json")
except Exception as e:
pass

if "cli-instances" in hal.config.keys():
hal._instantiate_objects("cli")
for o in hal.objects.values():
o.cli(sub)

args = parser.parse_args()

# Try to run a subcommand
if args.cmd != None:
subcmds[args.cmd](args)
func = subcmds.get(args.cmd)
if func:
func(args)
else:
parser.print_help()
for i in hal.objects.values():
if i.cli_receive(args):
break
else:
parser.print_help()

hal.shutdown()
3 changes: 2 additions & 1 deletion packages/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@

from .pm import PackageManager
from .help import Help
Default = None
Loading