sh_cpl/src/sh_edraft/publishing/publisher.py

280 lines
12 KiB
Python
Raw Normal View History

2020-11-21 12:51:05 +01:00
import os
import shutil
from string import Template as stringTemplate
2020-11-19 23:06:57 +01:00
2020-12-16 18:14:11 +01:00
from setuptools import sandbox
2020-11-22 20:56:31 +01:00
from sh_edraft.logging.base.logger_base import LoggerBase
from sh_edraft.publishing.base.publisher_base import PublisherBase
2020-11-29 11:32:35 +01:00
from sh_edraft.publishing.model.publish_settings_model import PublishSettings
from sh_edraft.publishing.model.template import Template
2020-11-19 23:06:57 +01:00
class Publisher(PublisherBase):
2020-11-19 23:06:57 +01:00
2020-11-29 11:32:35 +01:00
def __init__(self, logger: LoggerBase, publish_settings: PublishSettings):
2020-11-26 11:20:21 +01:00
PublisherBase.__init__(self)
self._logger: LoggerBase = logger
2020-11-29 11:32:35 +01:00
self._publish_settings: PublishSettings = publish_settings
2020-11-21 12:51:05 +01:00
2020-12-16 17:37:43 +01:00
self._included_files: list[str] = []
2020-11-21 12:51:05 +01:00
@property
def source_path(self) -> str:
2020-11-25 21:41:45 +01:00
return self._publish_settings.source_path
2020-11-19 23:06:57 +01:00
@property
2020-11-21 12:51:05 +01:00
def dist_path(self):
2020-11-25 21:41:45 +01:00
return self._publish_settings.dist_path
2020-11-21 12:51:05 +01:00
2020-11-22 20:56:31 +01:00
def _get_template_output(self, t: Template, name: str, imports: str) -> str:
self._logger.trace(__name__, f'Started {__name__}._get_template_output')
2020-11-21 12:51:05 +01:00
try:
if t.file_content == '':
raise Exception(f'Template is empty: {t.template_path}')
2020-11-22 20:56:31 +01:00
self._logger.trace(__name__, f'Stopped {__name__}._get_template_output')
2020-11-21 12:51:05 +01:00
return stringTemplate(t.file_content).substitute(
Name=name,
Description=t.description,
LongDescription=t.long_description,
CopyrightDate=t.copyright_date,
CopyrightName=t.copyright_name,
LicenseName=t.license_name,
LicenseDescription=t.license_description,
Title=t.title if t.title is not None and t.title != '' else name,
Author=t.author,
Version=t.version.to_str(),
Major=t.version.major,
Minor=t.version.minor,
Micro=t.version.micro,
Imports=imports
)
except Exception as e:
2020-11-22 20:56:31 +01:00
self._logger.fatal(__name__, f'Cannot read Template: {t.template_path}', e)
self._logger.trace(__name__, f'Stopped {__name__}._get_template_output')
2020-11-21 12:51:05 +01:00
def _read_source_path(self):
2020-11-22 20:56:31 +01:00
self._logger.trace(__name__, f'Started {__name__}._read_source_path')
2020-12-16 17:37:43 +01:00
included_files = self._publish_settings.included_files
for included in included_files:
if os.path.isdir(included):
self._publish_settings.included_files.remove(included)
for r, d, f in os.walk(included):
for file in f:
2020-12-20 15:20:32 +01:00
rel_path = os.path.relpath(r)
if not rel_path.startswith('.'):
rel_path = f'./{rel_path}'
2020-12-16 17:37:43 +01:00
file_path = os.path.join(self._publish_settings.source_path, r, file)
if os.path.isfile(file_path):
self._included_files.append(file_path)
2020-12-20 15:20:32 +01:00
elif os.path.isfile(os.path.join(rel_path, file)):
self._included_files.append(os.path.join(rel_path, file))
2020-12-16 17:37:43 +01:00
else:
self._logger.fatal(__name__, f'File not found: {file}')
2020-12-16 17:54:29 +01:00
elif os.path.isfile(included):
2020-12-20 15:20:32 +01:00
self._included_files.append(included)
2020-12-16 17:54:29 +01:00
else:
self._logger.fatal(__name__, f'File not found: {included}')
2020-11-25 21:41:45 +01:00
for r, d, f in os.walk(self._publish_settings.source_path):
2020-11-21 12:51:05 +01:00
for file in f:
2020-12-16 17:37:43 +01:00
is_file_excluded = False
if os.path.join(r, file) in self._publish_settings.excluded_files:
is_file_excluded = True
else:
for excluded in self._publish_settings.excluded_files:
if os.path.join(r, file).__contains__(excluded):
is_file_excluded = True
if not is_file_excluded and file.endswith('.py') or file in self._publish_settings.included_files:
self._included_files.append(os.path.join(r, file))
2020-11-21 12:51:05 +01:00
2020-11-22 20:56:31 +01:00
self._logger.trace(__name__, f'Stopped {__name__}._read_source_path')
2020-11-21 12:51:05 +01:00
def _read_templates(self):
2020-11-22 20:56:31 +01:00
self._logger.trace(__name__, f'Started {__name__}._read_templates')
2020-11-25 21:41:45 +01:00
for t in self._publish_settings.templates:
2020-11-21 12:51:05 +01:00
output_template: str = ''
if not os.path.isfile(t.template_path):
self._logger.fatal(__name__, f'Template not found: {t.template_path}')
2020-11-21 12:51:05 +01:00
with open(t.template_path) as template:
t.file_content = template.read()
template.close()
if t.file_content == '':
self._logger.fatal(__name__, f'Template is empty: {t.template_path}')
2020-11-21 12:51:05 +01:00
2020-11-22 20:56:31 +01:00
self._logger.trace(__name__, f'Stopped {__name__}._read_templates')
2020-11-21 12:51:05 +01:00
def _create_dist_path(self):
2020-11-22 20:56:31 +01:00
self._logger.trace(__name__, f'Started {__name__}._create_dist_path')
2020-11-25 21:41:45 +01:00
if os.path.isdir(self._publish_settings.dist_path):
2020-11-21 12:51:05 +01:00
try:
2020-11-25 21:41:45 +01:00
shutil.rmtree(self._publish_settings.dist_path)
self._logger.info(__name__, f'Deleted {self._publish_settings.dist_path}')
2020-11-21 12:51:05 +01:00
except Exception as e:
2020-11-22 20:56:31 +01:00
self._logger.fatal(__name__, f'Cannot delete old dist directory', e)
2020-11-21 12:51:05 +01:00
2020-11-25 21:41:45 +01:00
if not os.path.isdir(self._publish_settings.dist_path):
2020-11-21 12:51:05 +01:00
try:
2020-11-25 21:41:45 +01:00
os.makedirs(self._publish_settings.dist_path)
self._logger.debug(__name__, f'Created directories: {self._publish_settings.dist_path}')
2020-11-22 20:56:31 +01:00
self._logger.info(__name__, f'Created dist directory')
2020-11-21 12:51:05 +01:00
except Exception as e:
2020-11-22 20:56:31 +01:00
self._logger.fatal(__name__, f'Cannot create dist directory', e)
self._logger.trace(__name__, f'Stopped {__name__}._create_dist_path')
2020-11-21 12:51:05 +01:00
2020-11-22 14:15:39 +01:00
@staticmethod
def _get_template_name_from_dirs(file: str) -> str:
2020-11-21 12:51:05 +01:00
dirs = os.path.dirname(file).split('/')
for d in dirs:
if d.__contains__('.'):
dirs.remove(d)
if len(dirs) == 0:
return os.path.basename(file)
else:
return '.'.join(dirs)
def _write_templates(self):
2020-11-22 20:56:31 +01:00
self._logger.trace(__name__, f'Started {__name__}._write_templates')
2020-11-25 21:41:45 +01:00
for template in self._publish_settings.templates:
2020-12-16 17:37:43 +01:00
for file in self._included_files:
2020-11-25 21:41:45 +01:00
if os.path.basename(file) == '__init__.py' and file not in self._publish_settings.excluded_files:
2020-11-21 12:51:05 +01:00
template_name = template.name
2020-11-26 10:36:49 +01:00
if template.name == 'all' or template.name == '':
2020-11-21 12:51:05 +01:00
template_name = self._get_template_name_from_dirs(file)
else:
name = self._get_template_name_from_dirs(file)
if name.__contains__('.'):
if template.name != name.split('.')[len(name.split('.')) - 1]:
continue
2020-11-21 12:51:05 +01:00
else:
if template.name != name:
continue
2020-11-21 12:51:05 +01:00
try:
module_file_lines: list[str] = []
module_py_lines: list[str] = []
imports = ''
with open(file, 'r') as py_file:
module_file_lines = py_file.readlines()
py_file.close()
if len(module_file_lines) == 0:
with open(file, 'w+') as py_file:
py_file.write(self._get_template_output(template, template_name, '# imports:'))
py_file.close()
2020-11-22 20:56:31 +01:00
self._logger.debug(__name__, f'Written to {file}')
2020-11-21 12:51:05 +01:00
else:
is_started = False
for line in module_file_lines:
if line.__contains__('# imports'):
is_started = True
if (line.__contains__('from') or line.__contains__('import')) and is_started:
module_py_lines.append(line.replace('\n', ''))
if len(module_py_lines) > 0:
imports = '\n'.join(module_py_lines)
with open(file, 'w+') as py_file:
py_file.write(self._get_template_output(template, template_name, imports))
py_file.close()
2020-11-22 20:56:31 +01:00
self._logger.debug(__name__, f'Written to {file}')
2020-11-21 12:51:05 +01:00
except Exception as e:
2020-11-22 20:56:31 +01:00
self._logger.error(__name__, f'Cannot write to file: {file}', e)
self._logger.info(__name__, f'Written to all included modules')
self._logger.trace(__name__, f'Stopped {__name__}._write_templates')
2020-11-21 12:51:05 +01:00
def _copy_all_included_files(self):
2020-11-22 20:56:31 +01:00
self._logger.trace(__name__, f'Started {__name__}._copy_all_included_files')
2020-11-25 21:41:45 +01:00
dist_path = self._publish_settings.dist_path
if self._publish_settings.dist_path.endswith('/'):
2020-11-21 12:51:05 +01:00
dist_path = dist_path[:len(dist_path) - 1]
2020-12-16 17:37:43 +01:00
for file in self._included_files:
2020-11-22 20:56:31 +01:00
is_file_excluded = False
2020-11-25 21:41:45 +01:00
if file in self._publish_settings.excluded_files:
2020-11-22 20:56:31 +01:00
is_file_excluded = True
else:
2020-11-25 21:41:45 +01:00
for excluded in self._publish_settings.excluded_files:
2020-11-22 20:56:31 +01:00
if file.__contains__(excluded):
is_file_excluded = True
2020-11-22 20:56:31 +01:00
if not is_file_excluded:
output_file = ''
2020-11-21 12:51:05 +01:00
2020-11-22 20:56:31 +01:00
if file.startswith('..'):
output_file = file.replace('..', '')
elif file.startswith('.'):
output_file = file.replace('.', '', 1)
2020-11-21 12:51:05 +01:00
2020-12-16 17:54:29 +01:00
if output_file.__contains__('..'):
output_file = os.path.join(dist_path, os.path.basename(file))
else:
output_file = f'{dist_path}{output_file}'
2020-11-22 20:56:31 +01:00
output_path = os.path.dirname(output_file)
2020-11-21 12:51:05 +01:00
2020-11-22 20:56:31 +01:00
try:
if not os.path.isdir(output_path):
os.makedirs(output_path)
except Exception as e:
self._logger.error(__name__, f'Cannot create directories: {output_path}', e)
2020-11-21 12:51:05 +01:00
2020-11-22 20:56:31 +01:00
try:
shutil.copy(file, output_file)
except Exception as e:
self._logger.error(__name__, f'Cannot copy file: {file} to {output_path}', e)
self._logger.debug(__name__, f'Copied {file} to {output_path}')
2020-11-21 12:51:05 +01:00
2020-11-22 20:56:31 +01:00
self._logger.info(__name__, f'Copied all included files')
self._logger.trace(__name__, f'Stopped {__name__}._copy_all_included_files')
2020-11-21 12:51:05 +01:00
def include(self, path: str):
2020-11-22 20:56:31 +01:00
self._logger.trace(__name__, f'Started {__name__}.include')
2020-11-25 21:41:45 +01:00
self._publish_settings.included_files.append(path)
2020-11-22 20:56:31 +01:00
self._logger.trace(__name__, f'Stopped {__name__}.include')
2020-11-21 12:51:05 +01:00
def exclude(self, path: str):
2020-11-22 20:56:31 +01:00
self._logger.trace(__name__, f'Started {__name__}.exclude')
2020-11-25 21:41:45 +01:00
self._publish_settings.excluded_files.append(path)
2020-11-22 20:56:31 +01:00
self._logger.trace(__name__, f'Stopped {__name__}.exclude')
2020-11-21 12:51:05 +01:00
def create(self):
2020-11-22 20:56:31 +01:00
self._logger.trace(__name__, f'Started {__name__}.create')
2020-11-25 21:41:45 +01:00
if not self._publish_settings.dist_path.endswith('/'):
self._publish_settings.dist_path += '/'
2020-11-19 23:06:57 +01:00
2020-11-21 12:51:05 +01:00
self._read_source_path()
self._read_templates()
self._create_dist_path()
2020-11-22 20:56:31 +01:00
self._logger.trace(__name__, f'Stopped {__name__}.create')
2020-11-19 23:06:57 +01:00
2020-12-16 18:14:11 +01:00
def build(self):
self._logger.trace(__name__, f'Started {__name__}.build')
2020-11-21 12:51:05 +01:00
self._write_templates()
self._copy_all_included_files()
2020-12-16 18:14:11 +01:00
self._logger.trace(__name__, f'Stopped {__name__}.build')
def publish(self):
self._logger.trace(__name__, f'Started {__name__}.publish')
setup_py = os.path.join(self._publish_settings.dist_path, 'setup.py')
if not os.path.isfile(setup_py):
self._logger.fatal(__name__, f'setup.py not found in {self._publish_settings.dist_path}')
sandbox.run_setup(os.path.abspath(setup_py), ['sdist', 'bdist_wheel'])
2020-11-22 20:56:31 +01:00
self._logger.trace(__name__, f'Stopped {__name__}.publish')