Skip to content

Commit

Permalink
Added log statements
Browse files Browse the repository at this point in the history
  • Loading branch information
TNick committed Nov 15, 2019
1 parent eb4811a commit c23cdf9
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@
from pubqlib.logic.plugin import PubPlugin
from pubqlib.logic.toolset import Toolset

logger = logging.getLogger('pubq.command.install')
logger = logging.getLogger('pubq.cmd.install')


def install_command(args, log, the_app):
""" The command handler for version command. """
logger.debug("install command (%r)", args)
the_app.source_py = bool(args.source_py)
the_app.destination = os.path.abspath(args.destination)
the_app.toolset.from_args(args)
the_app.install(args.source, force_recompile=args.force_recompile)
the_app.install(
args.source,
force_recompile=args.force_recompile,
clear_opt=args.on_existing,
)


def create_install_command(subparsers, the_app):
Expand All @@ -29,6 +34,8 @@ def create_install_command(subparsers, the_app):
# The toolset also gets some arguments here.
the_app.toolset.prepare_parser(parser)

destination = the_app.get_plugin_directory()

parser.add_argument(
"--source-py", default=False,
action="store_true",
Expand All @@ -40,10 +47,15 @@ def create_install_command(subparsers, the_app):
help="forces the recompilation even if the timestamps would suggest "
"that there's no need")
parser.add_argument(
"--destination", default=False,
action="store_true",
help="deploy source files; by default the program deploys compiled "
".pyc files")
"--destination", default=destination,
help="where to copy the files")
parser.add_argument(
"--on-existing", default='error',
choices=['error', 'clear', 'overwrite'],
help="what to do when the target directory exists; can be "
"error (will refuse to go forward if the directory is not empty), "
"clear (will delete any files or directories found inside prior to installation) or "
"overwrite (will only overwrite the files that are installed)")
parser.add_argument(
"source", nargs='+',
help="The source directory from where we install the plugin")
Expand Down
16 changes: 12 additions & 4 deletions pubqlib/logic/file_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class PubFile(object):
The path of the compiled file.
"""

def __init__(self, path_in=None, path_out=None):
def __init__(self, path_in=None, path_out=None, use_compiled=True):
"""
Constructor.
Expand All @@ -35,21 +35,29 @@ def __init__(self, path_in=None, path_out=None):
super().__init__()
self.path_in = path_in
self.path_out = path_out
self.use_compiled = True
self.use_compiled = use_compiled

def __str__(self):
""" Represent this object as a human-readable string. """
return 'PubFile()'
return 'PubFile("%s")' % self.path_in

def __repr__(self):
""" Represent this object as a python constructor. """
return 'PubFile()'
return 'PubFile(path_in=%r, path_out=%r, use_compiled=%r)' % (
self.path_in,
self.path_out,
self.use_compiled,
)

@property
def copy_target(self):
""" The file that should be copied by the deploy process. """
return self.path_out if self.use_compiled else self.path_in

def default_output(self):
""" Computes the default output file. """
raise NotImplementedError

def compile(self, toolset, force=False):
""" Create path_out file from path_in. """
raise NotImplementedError
Expand Down
8 changes: 7 additions & 1 deletion pubqlib/logic/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ def collect_py_files(self,
pkg_path = self.path
if pkg_name is None:
pkg_name = self.name

logger.debug("module %s is collecting files from %s(%r)",
self.name, pkg_name, pkg_path)
for importer, modname, is_pkg in pkgutil.walk_packages(
path=[pkg_path],
prefix='',
Expand All @@ -87,7 +88,12 @@ def collect_py_files(self,
source_py=source_py,
pkg_name='%s.%s' % (pkg_name, modname),
pkg_path=fs_name)
else:
logger.debug("module %r excluded by exclude_modules",
modname)

def compile(self, toolset, force=False):
""" Create path_out file from path_in. """
logger.debug("compiling module %s at %s", self.name, self.path)
compileall.compile_dir(dir=self.path, legacy=True)
logger.debug("done compiling module %s at %s", self.name, self.path)
89 changes: 83 additions & 6 deletions pubqlib/logic/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ def __init__(self, source_path=None, modules=None,
"""
super().__init__()
self.source_path = source_path
self.target_name = None

self.config_obj = configparser.ConfigParser(
comment_prefixes='/', allow_no_value=True)
allow_no_value=True)
self.modules = [] if modules is None else modules
self.extra_files = []
self.ui_files = []
Expand Down Expand Up @@ -139,7 +141,9 @@ def init_from_directory(self, path, source_py=False):
source_py (bool):
Set to true to force collection of source files.
"""
logger.debug("Initializing plugin from directory %s", path)
self.source_path = path
self.target_name = os.path.split(path)[1]

metadata_path = os.path.join(path, 'metadata.txt')
if not os.path.isfile(metadata_path):
Expand Down Expand Up @@ -185,7 +189,10 @@ def to_metadata(self):
Saves the attributes to config object.
"""
if not self.has_required_metadata():
logger.debug("will not fill in config object with metadata "
"because required fields are missing")
return False

self.config_obj.set(
'general', 'about', self.about)
self.config_obj.set(
Expand Down Expand Up @@ -225,10 +232,13 @@ def to_metadata(self):
'general', 'tracker', self.tracker)
self.config_obj.set(
'general', 'version', self.version)

logger.debug("config object has been updated with metadata")
return True

def read_metadata(self, in_file):
""" Reads the metadata.txt file. """
logger.debug("reading metadata from %s", in_file)
with open(in_file, 'r') as fin:
self.config_obj.read_file(fin)

Expand Down Expand Up @@ -272,13 +282,39 @@ def read_metadata(self, in_file):
self.version = self.config_obj.get(
'general', 'version', fallback=self.version)

logger.debug("about = %r", self.about)
logger.debug("author = %r", self.author)
logger.debug("category = %r", self.category)
logger.debug("changelog = %r", self.changelog)
logger.debug("deprecated = %r", self.deprecated)
logger.debug("description = %r", self.description)
logger.debug("email = %r", self.email)
logger.debug("experimental = %r", self.experimental)
logger.debug("has_processing_provider = %r", self.has_processing_provider)
logger.debug("homepage = %r", self.homepage)
logger.debug("icon = %r", self.icon)
logger.debug("name = %r", self.name)
logger.debug("plugin_dependencies = %r", self.plugin_dependencies)
logger.debug("qgis_maximum_version = %r", self.qgis_maximum_version)
logger.debug("qgis_minimum_version = %r", self.qgis_minimum_version)
logger.debug("repository = %r", self.repository)
logger.debug("tags = %r", self.tags)
logger.debug("tracker = %r", self.tracker)
logger.debug("version = %r", self.version)

logger.debug("metadata has been read")

def write_metadata(self, out_file):
""" Writes the metadata.txt file. """

if not self.to_metadata():
logger.debug("Will not write metadata to %s because "
"to_metadata failed", out_file)
return

with open(out_file, 'w') as fout:
self.config_obj.write(fout)
logger.debug("Metadata was written to %s", out_file)

def parse_init(self, init_path):
"""
Expand All @@ -291,16 +327,19 @@ def parse_init(self, init_path):
Returns:
The list of modules detected inside the file.
"""
logger.debug("parsing %s", init_path)
b_inside = False
modules =[]
with open(init_path, 'r') as fin:
with open(init_path, 'r', encoding="utf-8") as fin:
for line in fin:
line = line.strip()
if 'classFactory' in line:
b_inside = True
elif b_inside:
if line.startswith('from ') and ' import ' in line:
modules.append(line[5:line.find(' import ')].strip())

logger.debug("parsed %s and found %d modules", init_path, len(modules))
return modules

def load_modules(self, path, modules, source_py):
Expand All @@ -316,6 +355,7 @@ def load_modules(self, path, modules, source_py):
Returns:
A list of PubModule instances.
"""
logger.debug("loading %d modules in %r", len(modules), path)
result = []
for module_name in modules:
if module_name.startswith('.'):
Expand All @@ -325,6 +365,7 @@ def load_modules(self, path, modules, source_py):
name=module_name, path=os.path.join(path, *parts))
m.collect_py_files(source_py=source_py)
result.append(m)
logger.debug("created %d modules", len(result))
return result

def load_extra_files(self, path):
Expand All @@ -338,14 +379,20 @@ def load_extra_files(self, path):
Returns:
A list of files.
"""
logger.debug("loading extra files in %r", path)
result = []

include_files = self.config_obj.get('extra', 'files', fallback='')
logger.debug("include_files = %r", include_files)

include_dirs = self.config_obj.get('extra', 'directories', fallback='')
logger.debug("include_dirs = %r", include_dirs)

for file in include_files.split("\n"):
if len(file) > 0:
file = os.path.join(path, file.strip())
if os.path.isfile(file):
logger.log(1, "- %s", file)
result.append(file)
else:
logger.error("Extra file does not exist: %s", file)
Expand All @@ -357,10 +404,13 @@ def load_extra_files(self, path):
if os.path.isdir(file):
for root, dirs, files in os.walk(".", topdown=False):
for name in files:
result.append(os.path.join(root, name))
to_add = os.path.join(root, name)
logger.log(1, "- %s", to_add)
result.append(to_add)
else:
logger.error("Extra directory does not exist: %s", file)

logger.debug("found %d extra files", len(result))
return result

def load_ui_files(self, path):
Expand All @@ -374,6 +424,7 @@ def load_ui_files(self, path):
Returns:
A list of files.
"""
logger.debug("loading .ui files in %r", path)
result = []
include_ui = self.config_obj.get('extra', 'ui', fallback='')

Expand All @@ -390,6 +441,7 @@ def load_ui_files(self, path):
else:
logger.error("Extra file does not exist: %s", file)

logger.debug("found %d .ui files", len(result))
return result

def load_qrc_files(self, path):
Expand All @@ -403,6 +455,7 @@ def load_qrc_files(self, path):
Returns:
A list of files.
"""
logger.debug("loading .qrc files in %r", path)
result = []
include_ui = self.config_obj.get('extra', 'qrc', fallback='')

Expand All @@ -419,28 +472,37 @@ def load_qrc_files(self, path):
else:
logger.error("Extra file does not exist: %s", file)

logger.debug("found %d .qrc files", len(result))
return result

def compile(self, toolset, force=False):
""" Creates output files from input files. """
for module in self.modules:
module.compile(toolset=toolset, force=force)
logger.debug("plugin %s is being compiled ...", self.name)
for ui_file in self.ui_files:
ui_file.compile(toolset=toolset, force=force)
for qrc_file in self.qrc_files:
qrc_file.compile(toolset=toolset, force=force)
for module in self.modules:
module.compile(toolset=toolset, force=force)
logger.debug("plugin %s was compiled", self.name)

def collect_files_to_deploy(self):
""" Creates a single list of all files to be copied. """
logger.debug("collecting files to deploy...")
result = []

for module in self.modules:
for file in module.files:
result.append(file.copy_target)

result.extend(self.extra_files)

for ui_file in self.ui_files:
result.append(ui_file.copy_target)
for qrc_file in self.qrc_files:
result.append(qrc_file.copy_target)

logger.debug("collected %d files to deploy", len(result))
return result

def deploy(self, target, clear_opt='error'):
Expand All @@ -456,33 +518,48 @@ def deploy(self, target, clear_opt='error'):
- *clear*: remove all files and directories
- *overwrite*: replace each file but keep other files.
"""
target = os.path.join(target, self.target_name)
logger.debug("deploying plugin %s to %s", self.name, target)

if not os.path.isdir(target):
logger.debug("target does not exist; creating...")
os.makedirs(target)
else:
has_files = False
for _ in os.listdir(target):
has_files = True

if has_files:
logger.debug("target exists and has files")
if clear_opt == 'error':
logger.error("Path %r exists nad is not empty", target)
logger.error("Path %r exists and is not empty", target)
return
if clear_opt == 'clear':
logger.debug("all files in %s are being deleted", target)
shutil.rmtree(target)
os.mkdir(target)
clear_opt = False
elif clear_opt == 'overwrite':
logger.debug("files with same name will be overwritten")
clear_opt = True
else:
raise ValueError
else:
logger.debug("target exists but has no files")

for file in self.collect_files_to_deploy():
rel_path = os.path.relpath(file, self.source_path)
output_path = os.path.join(target, rel_path)
if clear_opt and os.path.isfile(output_path):
logger.debug("removing %r", output_path)
os.remove(output_path)

out_base, file_name = os.path.split(output_path)
if not os.path.isdir(out_base):
logger.debug("creating directory %r", out_base)
os.mkdir(out_base)

logger.debug("copying %s to %s", file, output_path)
shutil.copy(file, output_path)

logger.debug("plugin %s has been deployed to %s", self.name, target)

0 comments on commit c23cdf9

Please sign in to comment.