default to using a "data directory" for bitbot (~/.bitbot/)
This commit is contained in:
parent
8ff4ad19eb
commit
417c4f302e
3 changed files with 38 additions and 21 deletions
35
bitbotd
35
bitbotd
|
@ -13,19 +13,23 @@ from src import LockFile, Logging, ModuleManager, Timers, utils
|
|||
faulthandler.enable()
|
||||
|
||||
directory = os.path.dirname(os.path.realpath(__file__))
|
||||
home = os.path.expanduser("~")
|
||||
default_data = os.path.join(home, ".bitbot")
|
||||
|
||||
arg_parser = argparse.ArgumentParser(
|
||||
description="Python3 event-driven modular IRC bot")
|
||||
|
||||
arg_parser.add_argument("--version", "-v", action="store_true")
|
||||
|
||||
arg_parser.add_argument("--config", "-c",
|
||||
help="Location of the JSON config file",
|
||||
default=os.path.join(directory, "bot.conf"))
|
||||
arg_parser.add_argument("--config", "-c", help="Location of config file",
|
||||
default=os.path.join(default_data, "bot.conf"))
|
||||
|
||||
arg_parser.add_argument("--data-dir", "-x",
|
||||
help="Location of data files (database, lock, socket)",
|
||||
default=default_data)
|
||||
|
||||
arg_parser.add_argument("--database", "-d",
|
||||
help="Location of the sqlite3 database file",
|
||||
default=os.path.join(directory, "databases", "bot.db"))
|
||||
help="Location of the sqlite3 database file")
|
||||
|
||||
arg_parser.add_argument("--log-dir", "-l",
|
||||
help="Location of the log directory",
|
||||
|
@ -56,6 +60,20 @@ if args.version:
|
|||
print("BitBot %s" % IRCBot.VERSION)
|
||||
sys.exit(0)
|
||||
|
||||
database_location = None
|
||||
lock_location = None
|
||||
sock_locaiton = None
|
||||
if not args.database == None:
|
||||
database_location = args.database
|
||||
lock_location = "%s.lock" % args.database
|
||||
sock_location = "%s.sock" % args.database
|
||||
else:
|
||||
if not os.path.isdir(args.data_dir):
|
||||
os.mkdir(args.data_dir)
|
||||
database_location = os.path.join(args.data_dir, "bot.db")
|
||||
lock_location = os.path.join(args.data_dir, "bot.lock")
|
||||
sock_location = os.path.join(args.data_dir, "bot.sock")
|
||||
|
||||
log_level = args.log_level
|
||||
if not log_level:
|
||||
log_level = "debug" if args.verbose else "warn"
|
||||
|
@ -65,7 +83,7 @@ log = Logging.Log(not args.no_logging, log_level, args.log_dir)
|
|||
log.info("Starting BitBot %s (Python v%s)",
|
||||
[IRCBot.VERSION, platform.python_version()])
|
||||
|
||||
lock_file = LockFile.LockFile(args.database)
|
||||
lock_file = LockFile.LockFile(lock_location)
|
||||
if not lock_file.available():
|
||||
log.critical("Database is locked. Is BitBot already running?")
|
||||
sys.exit(1)
|
||||
|
@ -73,7 +91,7 @@ if not lock_file.available():
|
|||
atexit.register(lock_file.unlock)
|
||||
lock_file.lock()
|
||||
|
||||
database = Database.Database(log, args.database)
|
||||
database = Database.Database(log, database_location)
|
||||
|
||||
if args.remove_server:
|
||||
alias = args.remove_server
|
||||
|
@ -111,7 +129,7 @@ bot.add_poll_hook(cache)
|
|||
bot.add_poll_hook(lock_file)
|
||||
bot.add_poll_hook(timers)
|
||||
|
||||
control = Control.Control(bot, args.database)
|
||||
control = Control.Control(bot, sock_location)
|
||||
control.bind()
|
||||
bot.add_poll_source(control)
|
||||
|
||||
|
@ -121,7 +139,6 @@ if args.module:
|
|||
module.module.command_line(args.module_args)
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
server_configs = bot.database.servers.get_all()
|
||||
|
||||
if len(server_configs):
|
||||
|
|
|
@ -39,11 +39,11 @@ class ControlClient(object):
|
|||
|
||||
|
||||
class Control(PollSource.PollSource):
|
||||
def __init__(self, bot: IRCBot.Bot, database_location: str):
|
||||
def __init__(self, bot: IRCBot.Bot, filename: str):
|
||||
self._bot = bot
|
||||
self._bot.log.hook(self._on_log)
|
||||
|
||||
self._socket_location = "%s.sock" % database_location
|
||||
self._filename = filename
|
||||
self._socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
self._clients = {}
|
||||
|
||||
|
@ -53,9 +53,9 @@ class Control(PollSource.PollSource):
|
|||
self._send_action(client, "log", line)
|
||||
|
||||
def bind(self):
|
||||
if os.path.exists(self._socket_location):
|
||||
os.remove(self._socket_location)
|
||||
self._socket.bind(self._socket_location)
|
||||
if os.path.exists(self._filename):
|
||||
os.remove(self._filename)
|
||||
self._socket.bind(self._filename)
|
||||
self._socket.listen(1)
|
||||
|
||||
def get_readables(self) -> typing.List[int]:
|
||||
|
|
|
@ -4,14 +4,14 @@ from src import PollHook, utils
|
|||
EXPIRATION = 60 # 1 minute
|
||||
|
||||
class LockFile(PollHook.PollHook):
|
||||
def __init__(self, database_location: str):
|
||||
self._lock_location = "%s.lock" % database_location
|
||||
def __init__(self, filename: str):
|
||||
self._filename = filename
|
||||
self._next_lock = None
|
||||
|
||||
def available(self):
|
||||
now = utils.datetime_utcnow()
|
||||
if os.path.exists(self._lock_location):
|
||||
with open(self._lock_location, "r") as lock_file:
|
||||
if os.path.exists(self._filename):
|
||||
with open(self._filename, "r") as lock_file:
|
||||
timestamp_str = lock_file.read().strip().split(" ", 1)[0]
|
||||
|
||||
timestamp = utils.iso8601_parse(timestamp_str)
|
||||
|
@ -22,7 +22,7 @@ class LockFile(PollHook.PollHook):
|
|||
return True
|
||||
|
||||
def lock(self):
|
||||
with open(self._lock_location, "w") as lock_file:
|
||||
with open(self._filename, "w") as lock_file:
|
||||
last_lock = utils.datetime_utcnow()
|
||||
lock_file.write("%s" % utils.iso8601_format(last_lock))
|
||||
self._next_lock = last_lock+datetime.timedelta(
|
||||
|
@ -34,5 +34,5 @@ class LockFile(PollHook.PollHook):
|
|||
self.lock()
|
||||
|
||||
def unlock(self):
|
||||
if os.path.isfile(self._lock_location):
|
||||
os.remove(self._lock_location)
|
||||
if os.path.isfile(self._filename):
|
||||
os.remove(self._filename)
|
||||
|
|
Loading…
Reference in a new issue