mirror of
https://github.com/yavook/kiwi-scp.git
synced 2024-11-25 14:03:00 +00:00
Project class
This commit is contained in:
parent
a345baf1da
commit
0cf934965e
3 changed files with 105 additions and 40 deletions
|
@ -1,9 +1,5 @@
|
||||||
# system
|
|
||||||
import logging
|
|
||||||
import os
|
|
||||||
|
|
||||||
# local
|
# local
|
||||||
from .utils.misc import get_project_names, get_project_dir, get_project_down_dir
|
from .utils.project import Project
|
||||||
from ._subcommand import ProjectCommand
|
from ._subcommand import ProjectCommand
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,20 +15,7 @@ class DisableCommand(ProjectCommand):
|
||||||
def run(self, runner, config, args):
|
def run(self, runner, config, args):
|
||||||
result = True
|
result = True
|
||||||
|
|
||||||
for project_name in get_project_names(args):
|
for project in Project.from_args(args):
|
||||||
project_dir = get_project_dir(config, project_name)
|
result = project.disable()
|
||||||
project_down_dir = get_project_down_dir(config, project_name)
|
|
||||||
|
|
||||||
if os.path.isdir(project_dir):
|
|
||||||
logging.info(f"Disabling project '{project_name}'")
|
|
||||||
os.rename(project_dir, project_down_dir)
|
|
||||||
|
|
||||||
elif os.path.isdir(project_down_dir):
|
|
||||||
logging.warning(f"Project '{project_name}' is already disabled!")
|
|
||||||
result = False
|
|
||||||
|
|
||||||
else:
|
|
||||||
logging.warning(f"Project '{project_name}' not found in instance!")
|
|
||||||
result = False
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
|
@ -1,9 +1,5 @@
|
||||||
# system
|
|
||||||
import logging
|
|
||||||
import os
|
|
||||||
|
|
||||||
# local
|
# local
|
||||||
from .utils.misc import get_project_names, get_project_dir, get_project_down_dir
|
from .utils.project import Project
|
||||||
from ._subcommand import ProjectCommand
|
from ._subcommand import ProjectCommand
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,20 +15,7 @@ class EnableCommand(ProjectCommand):
|
||||||
def run(self, runner, config, args):
|
def run(self, runner, config, args):
|
||||||
result = True
|
result = True
|
||||||
|
|
||||||
for project_name in get_project_names(args):
|
for project in Project.from_args(args):
|
||||||
project_dir = get_project_dir(config, project_name)
|
result = project.enable()
|
||||||
project_down_dir = get_project_down_dir(config, project_name)
|
|
||||||
|
|
||||||
if os.path.isdir(project_down_dir):
|
|
||||||
logging.info(f"Enabling project '{project_name}'")
|
|
||||||
os.rename(project_down_dir, project_dir)
|
|
||||||
|
|
||||||
elif os.path.isdir(project_dir):
|
|
||||||
logging.warning(f"Project '{project_name}' is already enabled!")
|
|
||||||
result = False
|
|
||||||
|
|
||||||
else:
|
|
||||||
logging.warning(f"Project '{project_name}' not found in instance!")
|
|
||||||
result = False
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
99
src/kiwi/subcommands/utils/project.py
Normal file
99
src/kiwi/subcommands/utils/project.py
Normal file
|
@ -0,0 +1,99 @@
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
|
||||||
|
from kiwi._constants import CONF_DIRECTORY_NAME
|
||||||
|
from kiwi.config import LoadedConfig
|
||||||
|
|
||||||
|
|
||||||
|
class Project:
|
||||||
|
__name = None
|
||||||
|
__config = None
|
||||||
|
|
||||||
|
def __init__(self, name):
|
||||||
|
self.__name = name
|
||||||
|
self.__config = LoadedConfig.get()
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_names(cls, names):
|
||||||
|
return [cls(name) for name in names]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def all(cls):
|
||||||
|
# current directory content
|
||||||
|
content = os.listdir()
|
||||||
|
|
||||||
|
# filter subdirectories
|
||||||
|
dirs = [dir_name for dir_name in content if os.path.isdir(dir_name)]
|
||||||
|
|
||||||
|
# filter by suffix
|
||||||
|
project_dirs = [dir_name for dir_name in dirs if dir_name.endswith(cls.__config['markers:project'])]
|
||||||
|
|
||||||
|
# remove suffix
|
||||||
|
project_names = [project_name[:-len(cls.__config['markers:project'])] for project_name in project_dirs]
|
||||||
|
|
||||||
|
return cls.from_names(project_names)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_args(cls, args):
|
||||||
|
if args is not None and 'projects' in args:
|
||||||
|
if isinstance(args.projects, list) and args.projects:
|
||||||
|
return cls.from_names(args.projects)
|
||||||
|
elif isinstance(args.projects, str):
|
||||||
|
return cls.from_names([args.projects])
|
||||||
|
|
||||||
|
return []
|
||||||
|
|
||||||
|
def get_name(self):
|
||||||
|
return self.__name
|
||||||
|
|
||||||
|
def dir_name(self):
|
||||||
|
return f"{self.__name}{self.__config['markers:project']}"
|
||||||
|
|
||||||
|
def down_dir_name(self):
|
||||||
|
return f"{self.dir_name()}{self.__config['markers:down']}"
|
||||||
|
|
||||||
|
def conf_dir_name(self):
|
||||||
|
return os.path.join(self.dir_name(), CONF_DIRECTORY_NAME)
|
||||||
|
|
||||||
|
def target_dir_name(self):
|
||||||
|
return os.path.join(self.__config['runtime:storage'], self.dir_name())
|
||||||
|
|
||||||
|
def exists(self):
|
||||||
|
return os.path.isdir(self.dir_name()) or os.path.isdir(self.down_dir_name())
|
||||||
|
|
||||||
|
def is_enabled(self):
|
||||||
|
return os.path.isdir(self.dir_name())
|
||||||
|
|
||||||
|
def is_disabled(self):
|
||||||
|
return os.path.isdir(self.down_dir_name())
|
||||||
|
|
||||||
|
def has_configs(self):
|
||||||
|
return os.path.isdir(self.dir_name())
|
||||||
|
|
||||||
|
def enable(self):
|
||||||
|
if self.is_disabled():
|
||||||
|
logging.info(f"Enabling project '{self.get_name()}'")
|
||||||
|
os.rename(self.down_dir_name(), self.dir_name())
|
||||||
|
|
||||||
|
elif self.is_enabled():
|
||||||
|
logging.warning(f"Project '{self.get_name()}' is enabled!")
|
||||||
|
|
||||||
|
else:
|
||||||
|
logging.warning(f"Project '{self.get_name()}' not found in instance!")
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def disable(self):
|
||||||
|
if self.is_enabled():
|
||||||
|
logging.info(f"Disabling project '{self.get_name()}'")
|
||||||
|
os.rename(self.dir_name(), self.down_dir_name())
|
||||||
|
|
||||||
|
elif self.is_disabled():
|
||||||
|
logging.warning(f"Project '{self.get_name()}' is disabled!")
|
||||||
|
|
||||||
|
else:
|
||||||
|
logging.warning(f"Project '{self.get_name()}' not found in instance!")
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
Loading…
Reference in a new issue