Make init --refresh clean up indexes
This commit is contained in:
parent
8e99788fcd
commit
f79e5bfd87
@ -1,6 +1,12 @@
|
||||
from cli.helpers import add_argument, no_argument, config_get, config_set, CONFIG_GET_ROOT_VALUE
|
||||
import os
|
||||
|
||||
@add_argument("--update", action="store_true", help="Refresh an existing config directory")
|
||||
from cli.helpers import (
|
||||
add_argument, no_argument,
|
||||
config_get, config_set, CONFIG_GET_ROOT_VALUE)
|
||||
|
||||
@add_argument(
|
||||
"--refresh", action="store_true",
|
||||
help="Refresh an existing config directory")
|
||||
def command_init(args):
|
||||
"""
|
||||
Initialize a config directory at --config-dir
|
||||
@ -8,10 +14,21 @@ def command_init(args):
|
||||
A clean config directory will contain a config.json, a
|
||||
pidfile, a lexicon config directory, and a user config
|
||||
directory.
|
||||
"""
|
||||
import config.init
|
||||
|
||||
config.init.create_config_dir(args.config_dir, args.update)
|
||||
Refreshing an existing directory will add keys to the global config that
|
||||
are present in the default configs. Users and lexicons that are missing
|
||||
from the indexes will be deleted, and stale index entries will be removed.
|
||||
"""
|
||||
# Module imports
|
||||
from config.init import create_config_dir
|
||||
|
||||
# Verify arguments
|
||||
if args.refresh and not os.path.isdir(args.config_dir):
|
||||
print("Error: couldn't find directory '{}'".format(args.config_dir))
|
||||
|
||||
# Internal call
|
||||
create_config_dir(args.config_dir, args.refresh)
|
||||
|
||||
|
||||
@no_argument
|
||||
def command_generate_secret(args):
|
||||
@ -30,6 +47,7 @@ def command_generate_secret(args):
|
||||
cfg['secret_key'] = secret_key.hex()
|
||||
config.logger.info("Regenerated Flask secret key")
|
||||
|
||||
|
||||
@add_argument("-a", "--address", default="127.0.0.1")
|
||||
@add_argument("-p", "--port", default="5000")
|
||||
def command_run(args):
|
||||
@ -47,6 +65,7 @@ def command_run(args):
|
||||
return -1
|
||||
server.app.run(host=args.address, port=args.port)
|
||||
|
||||
|
||||
@add_argument("--get", metavar="PATHSPEC", dest="get",
|
||||
nargs="?", const=CONFIG_GET_ROOT_VALUE, help="Get the value of a config key")
|
||||
@add_argument("--set", metavar=("PATHSPEC", "VALUE"), dest="set",
|
||||
|
@ -3,6 +3,7 @@ import copy
|
||||
import json
|
||||
import logging.config
|
||||
import os
|
||||
import shutil
|
||||
|
||||
# Module imports
|
||||
from errors import MissingConfigError, MalformedConfigError
|
||||
@ -10,9 +11,9 @@ import config
|
||||
from config.loader import json_ro
|
||||
import resources
|
||||
|
||||
def create_config_dir(config_dir, update=False):
|
||||
def create_config_dir(config_dir, refresh=False):
|
||||
"""
|
||||
Create or update a config directory
|
||||
Create or refresh a config directory
|
||||
"""
|
||||
from collections import OrderedDict
|
||||
import fcntl
|
||||
@ -27,14 +28,14 @@ def create_config_dir(config_dir, update=False):
|
||||
config.CONFIG_DIR = config_dir
|
||||
|
||||
# The directory should be empty if we're not updating an existing one.
|
||||
if len(os.listdir(config_dir)) > 0 and not update:
|
||||
if len(os.listdir(config_dir)) > 0 and not refresh:
|
||||
print("Directory {} is not empty".format(config_dir))
|
||||
return -1
|
||||
|
||||
# Update or create global config.
|
||||
def_cfg = resources.get_stream("global.json")
|
||||
global_config_path = path("config.json")
|
||||
if update and os.path.isfile(global_config_path):
|
||||
if refresh and os.path.isfile(global_config_path):
|
||||
# We need to write an entirely different ordereddict to the config
|
||||
# file, so we mimic the config.loader functionality manually.
|
||||
with open(global_config_path, 'r+', encoding='utf8') as cfg_file:
|
||||
@ -77,6 +78,30 @@ def create_config_dir(config_dir, update=False):
|
||||
with open(path('user', 'index.json'), 'w') as f:
|
||||
json.dump({}, f)
|
||||
|
||||
if refresh:
|
||||
for dir_name in ('lexicon', 'user'):
|
||||
# Clean up unindexed folders
|
||||
with config.json_ro(dir_name, 'index.json') as index:
|
||||
known = list(index.values())
|
||||
entries = os.listdir(path(dir_name))
|
||||
for dir_entry in entries:
|
||||
if dir_entry == "index.json":
|
||||
continue
|
||||
if dir_entry in known:
|
||||
continue
|
||||
print("Removing unindexed folder: '{}/{}'"
|
||||
.format(dir_name, dir_entry))
|
||||
shutil.rmtree(path(dir_name, dir_entry))
|
||||
|
||||
# Remove orphaned index listings
|
||||
with config.json_rw(dir_name, 'index.json') as index:
|
||||
for name, entry in index.items():
|
||||
if not os.path.isdir(path(dir_name, entry)):
|
||||
print("Removing stale {} index entry '{}: {}'"
|
||||
.format(dir_name, name, entry))
|
||||
del index[name]
|
||||
|
||||
|
||||
def verify_config_dir(config_dir):
|
||||
"""
|
||||
Verifies that the given directory has a valid global config in it and
|
||||
@ -99,6 +124,7 @@ def verify_config_dir(config_dir):
|
||||
# Configs verified
|
||||
return True
|
||||
|
||||
|
||||
def init_logging(args, logging_config):
|
||||
"""
|
||||
Initializes logging by using the logging section of the global config
|
||||
|
Loading…
Reference in New Issue
Block a user