sh_cpl/src/cpl_cli/publish/publisher_service.py

413 lines
15 KiB
Python
Raw Normal View History

2021-03-08 20:29:08 +01:00
import importlib
2021-03-04 19:06:53 +01:00
import os
import shutil
2021-03-04 21:57:18 +01:00
from string import Template as stringTemplate
2021-03-04 19:06:53 +01:00
2021-03-08 20:29:08 +01:00
import setuptools
2021-03-14 11:27:01 +01:00
from packaging import version
2021-03-08 20:29:08 +01:00
from setuptools import sandbox
2021-03-12 16:06:30 +01:00
from cpl.console.foreground_color_enum import ForegroundColorEnum
2021-03-04 19:06:53 +01:00
from cpl.console.console import Console
from cpl.environment.application_environment_abc import ApplicationEnvironmentABC
2021-03-08 20:29:08 +01:00
from cpl_cli.configuration.build_settings import BuildSettings
from cpl_cli.configuration.project_settings import ProjectSettings
2021-03-30 11:53:07 +02:00
from cpl_cli.configuration.project_type_enum import ProjectTypeEnum
2021-03-04 19:06:53 +01:00
from cpl_cli.publish.publisher_abc import PublisherABC
2021-03-10 14:07:52 +01:00
from cpl_cli.templates.build.init_template import InitTemplate
from cpl_cli.templates.publish.setup_template import SetupTemplate
2021-03-04 19:06:53 +01:00
2021-03-12 16:10:43 +01:00
class PublisherService(PublisherABC):
2021-03-04 19:06:53 +01:00
def __init__(self, env: ApplicationEnvironmentABC, project: ProjectSettings, build: BuildSettings):
2021-03-14 16:01:15 +01:00
"""
Service to build or publish files for distribution
:param env:
2021-03-14 16:01:15 +01:00
:param project:
:param build:
"""
2021-03-04 19:06:53 +01:00
PublisherABC.__init__(self)
self._env = env
2021-03-08 20:29:08 +01:00
self._project_settings = project
self._build_settings = build
self._source_path = os.path.join(self._env.working_directory, self._build_settings.source_path)
self._output_path = os.path.join(self._env.working_directory, self._build_settings.output_path)
2021-03-04 21:57:18 +01:00
self._included_files: list[str] = []
2021-03-09 21:07:47 +01:00
self._included_dirs: list[str] = []
2021-03-08 20:29:08 +01:00
self._distributed_files: list[str] = []
2021-03-04 19:06:53 +01:00
@property
def source_path(self) -> str:
2021-03-30 11:02:58 +02:00
return self._source_path
2021-03-04 19:06:53 +01:00
@property
def dist_path(self) -> str:
2021-03-30 11:02:58 +02:00
return self._output_path
2021-03-04 19:06:53 +01:00
2021-03-04 21:57:18 +01:00
@staticmethod
def _get_module_name_from_dirs(file: str) -> str:
2021-03-14 16:01:15 +01:00
"""
Extracts module name from directories
:param file:
:return:
"""
2021-03-08 20:32:09 +01:00
if 'src/' in file:
2021-03-09 22:26:54 +01:00
file = file.replace('src/', '', 1)
2021-03-08 20:32:09 +01:00
2021-03-04 21:57:18 +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)
@staticmethod
def _delete_path(path: str):
2021-03-14 16:01:15 +01:00
"""
Deletes full path tree
:param path:
:return:
"""
2021-03-04 21:57:18 +01:00
if os.path.isdir(path):
2021-03-04 19:06:53 +01:00
try:
2021-03-04 21:57:18 +01:00
shutil.rmtree(path)
2021-03-04 19:06:53 +01:00
except Exception as e:
Console.error(f'{e}')
exit()
2021-03-04 21:57:18 +01:00
@staticmethod
def _create_path(path: str):
2021-03-14 16:01:15 +01:00
"""
Creates full path tree
:param path:
:return:
"""
2021-03-04 21:57:18 +01:00
if not os.path.isdir(path):
2021-03-04 19:06:53 +01:00
try:
2021-03-04 21:57:18 +01:00
os.makedirs(path)
2021-03-04 19:06:53 +01:00
except Exception as e:
Console.error(f'{e}')
exit()
2021-03-10 14:16:03 +01:00
def _is_path_included(self, path: str) -> bool:
2021-03-14 16:01:15 +01:00
"""
Checks if the path is included
:param path:
:return:
"""
2021-03-10 14:16:03 +01:00
for included in self._build_settings.included:
if included.startswith('*'):
included = included.replace('*', '')
if included in path and path not in self._build_settings.excluded:
return True
return False
2021-03-08 20:29:08 +01:00
def _is_path_excluded(self, path: str) -> bool:
2021-03-14 16:01:15 +01:00
"""
Checks if the path is excluded
:param path:
:return:
"""
2021-03-08 20:29:08 +01:00
for excluded in self._build_settings.excluded:
if excluded.startswith('*'):
excluded = excluded.replace('*', '')
2021-03-10 14:16:03 +01:00
if excluded in path and not self._is_path_included(path):
2021-03-08 20:29:08 +01:00
return True
return False
2021-03-04 21:57:18 +01:00
def _read_sources(self):
2021-03-14 16:01:15 +01:00
"""
Reads all source files and save included files
:return:
"""
2021-03-08 20:29:08 +01:00
for file in self._build_settings.included:
2021-03-04 21:57:18 +01:00
rel_path = os.path.relpath(file)
if os.path.isdir(rel_path):
for r, d, f in os.walk(rel_path):
for sub_file in f:
relative_path = os.path.relpath(r)
file_path = os.path.join(relative_path, os.path.relpath(sub_file))
2021-03-14 12:05:57 +01:00
print(file_path)
2021-03-09 22:26:54 +01:00
self._included_files.append(os.path.relpath(file_path))
2021-03-04 21:57:18 +01:00
elif os.path.isfile(rel_path):
2021-03-14 12:05:57 +01:00
print(rel_path)
2021-03-04 21:57:18 +01:00
self._included_files.append(rel_path)
2021-03-08 20:29:08 +01:00
for r, d, f in os.walk(self._build_settings.source_path):
2021-03-04 21:57:18 +01:00
for file in f:
relative_path = os.path.relpath(r)
file_path = os.path.join(relative_path, os.path.relpath(file))
2021-03-14 12:05:57 +01:00
if self._is_path_excluded(relative_path):
break
2021-03-04 21:57:18 +01:00
2021-03-09 21:07:47 +01:00
if len(d) > 0:
for directory in d:
empty_dir = os.path.join(os.path.dirname(file_path), directory)
if len(os.listdir(empty_dir)) == 0:
self._included_dirs.append(empty_dir)
2021-03-08 20:29:08 +01:00
if not self._is_path_excluded(relative_path):
2021-03-04 21:57:18 +01:00
self._included_files.append(os.path.relpath(file_path))
2021-03-04 19:06:53 +01:00
def _create_packages(self):
2021-03-14 16:01:15 +01:00
"""
Writes information from template to all included __init__.py
:return:
"""
2021-03-04 21:57:18 +01:00
for file in self._included_files:
2021-03-10 08:09:56 +01:00
if file.endswith('__init__.py'):
2021-03-04 21:57:18 +01:00
template_content = ''
module_file_lines: list[str] = []
title = self._get_module_name_from_dirs(file)
if title == '':
2021-03-08 20:29:08 +01:00
title = self._project_settings.name
2021-03-04 21:57:18 +01:00
elif not title.__contains__('.'):
2021-03-08 20:29:08 +01:00
title = f'{self._project_settings.name}.{title}'
2021-03-04 21:57:18 +01:00
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:
imports = '# imports:'
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)
2021-03-10 14:07:52 +01:00
template_content = stringTemplate(InitTemplate.get_init_py()).substitute(
2021-03-10 08:09:56 +01:00
Name=self._project_settings.name,
Description=self._project_settings.description,
LongDescription=self._project_settings.long_description,
CopyrightDate=self._project_settings.copyright_date,
CopyrightName=self._project_settings.copyright_name,
LicenseName=self._project_settings.license_name,
LicenseDescription=self._project_settings.license_description,
Title=title if title is not None and title != '' else self._project_settings.name,
Author=self._project_settings.author,
2021-03-14 11:27:01 +01:00
Version=version.parse(self._project_settings.version.to_str()),
2021-03-10 08:09:56 +01:00
Major=self._project_settings.version.major,
Minor=self._project_settings.version.minor,
Micro=self._project_settings.version.micro,
Imports=imports
)
2021-03-04 21:57:18 +01:00
with open(file, 'w+') as py_file:
py_file.write(template_content)
py_file.close()
2021-03-04 19:06:53 +01:00
def _dist_files(self):
2021-03-14 16:01:15 +01:00
"""
Copies all included source files to dist_path
:return:
"""
2021-03-08 20:29:08 +01:00
build_path = os.path.join(self._output_path)
2021-03-04 21:57:18 +01:00
self._delete_path(build_path)
self._create_path(build_path)
for file in self._included_files:
2021-03-08 20:29:08 +01:00
dist_file = file
if 'src/' in dist_file:
2021-03-09 22:26:54 +01:00
dist_file = dist_file.replace('src/', '', 1)
2021-03-08 20:29:08 +01:00
output_path = os.path.join(build_path, os.path.dirname(dist_file))
output_file = os.path.join(build_path, dist_file)
2021-03-04 21:57:18 +01:00
try:
if not os.path.isdir(output_path):
os.makedirs(output_path, exist_ok=True)
except Exception as e:
Console.error(__name__, f'Cannot create directories: {output_path} -> {e}')
2021-03-08 20:29:08 +01:00
return
2021-03-04 21:57:18 +01:00
try:
2021-03-08 20:29:08 +01:00
self._distributed_files.append(output_file)
2021-03-04 21:57:18 +01:00
shutil.copy(os.path.abspath(file), output_file)
except Exception as e:
Console.error(__name__, f'Cannot copy file: {file} to {output_path} -> {e}')
2021-03-08 20:29:08 +01:00
return
2021-03-09 21:07:47 +01:00
for empty_dir in self._included_dirs:
dist_dir = empty_dir
if 'src/' in dist_dir:
dist_dir = dist_dir.replace('src/', '', 1)
output_path = os.path.join(build_path, dist_dir)
if not os.path.isdir(output_path):
os.makedirs(output_path)
2021-03-08 20:29:08 +01:00
def _clean_dist_files(self):
2021-03-14 16:01:15 +01:00
"""
Deletes all included source files from dist_path
:return:
"""
2021-03-08 20:29:08 +01:00
paths: list[str] = []
for file in self._distributed_files:
paths.append(os.path.dirname(file))
if os.path.isfile(file):
os.remove(file)
for path in paths:
2021-03-30 11:02:58 +02:00
if path != self._output_path and os.path.isdir(path):
2021-03-08 20:29:08 +01:00
shutil.rmtree(path)
def _create_setup(self):
2021-03-14 16:01:15 +01:00
"""
Generates setup.py
Dependencies: ProjectSettings, BuildSettings
:return:
"""
2021-03-13 14:25:08 +01:00
setup_file = os.path.join(self._output_path, 'setup.py')
2021-03-08 20:29:08 +01:00
if os.path.isfile(setup_file):
os.remove(setup_file)
main = None
try:
main = importlib.import_module(self._build_settings.main)
except Exception as e:
Console.error('Could not find entry point', str(e))
2021-03-30 11:53:07 +02:00
return
2021-03-08 20:29:08 +01:00
2021-03-30 11:53:07 +02:00
if main is None or not hasattr(main, 'main'):
2021-03-08 20:29:08 +01:00
Console.error('Could not find entry point')
return
with open(setup_file, 'w+') as setup_py:
2021-03-10 14:07:52 +01:00
setup_string = stringTemplate(SetupTemplate.get_setup_py()).substitute(
2021-03-08 20:29:08 +01:00
Name=self._project_settings.name,
Version=self._project_settings.version.to_str(),
2021-03-09 21:07:47 +01:00
Packages=setuptools.find_packages(where=self._output_path, exclude=self._build_settings.excluded),
2021-03-08 20:29:08 +01:00
URL=self._project_settings.url,
LicenseName=self._project_settings.license_name,
Author=self._project_settings.author,
AuthorMail=self._project_settings.author_email,
2021-03-09 21:07:47 +01:00
IncludePackageData=self._build_settings.include_package_data,
2021-03-08 20:29:08 +01:00
Description=self._project_settings.description,
PyRequires=self._project_settings.python_version,
Dependencies=self._project_settings.dependencies,
EntryPoints={
'console_scripts': [
f'{self._build_settings.entry_point} = {main.__name__}:{main.main.__name__}'
]
2021-03-09 21:07:47 +01:00
},
PackageData=self._build_settings.package_data
2021-03-08 20:29:08 +01:00
)
setup_py.write(setup_string)
setup_py.close()
def _run_setup(self):
2021-03-14 16:01:15 +01:00
"""
Starts setup.py
:return:
"""
2021-03-13 14:25:08 +01:00
setup_py = os.path.join(self._output_path, 'setup.py')
2021-03-08 20:29:08 +01:00
if not os.path.isfile(setup_py):
2021-03-13 14:25:08 +01:00
Console.error(__name__, f'setup.py not found in {self._output_path}')
2021-03-08 20:29:08 +01:00
return
try:
sandbox.run_setup(os.path.abspath(setup_py), [
'sdist',
f'--dist-dir={os.path.join(self._output_path, "setup")}',
'bdist_wheel',
f'--bdist-dir={os.path.join(self._output_path, "bdist")}',
f'--dist-dir={os.path.join(self._output_path, "setup")}'
])
os.remove(setup_py)
except Exception as e:
2021-03-13 14:25:08 +01:00
Console.error('Executing setup.py failed', str(e))
2021-03-04 19:06:53 +01:00
def include(self, path: str):
2021-03-14 16:01:15 +01:00
"""
Includes given path from sources
:param path:
:return:
"""
2021-03-08 20:29:08 +01:00
self._build_settings.included.append(path)
2021-03-04 19:06:53 +01:00
def exclude(self, path: str):
2021-03-14 16:01:15 +01:00
"""
Excludes given path from sources
:param path:
:return:
"""
2021-03-08 20:29:08 +01:00
self._build_settings.excluded.append(path)
2021-03-04 19:06:53 +01:00
def build(self):
2021-03-14 16:01:15 +01:00
"""
Build the CPL project to dist_path/build
1. Reads all included source files
2. Writes informations from template to all included __init__.py
3. Copies all included source files to dist_path/build
:return:
"""
2021-03-08 20:29:08 +01:00
self._output_path = os.path.join(self._output_path, 'build')
2021-03-30 11:02:58 +02:00
Console.spinner('Reading source files:', self._read_sources, text_foreground_color=ForegroundColorEnum.green,
spinner_foreground_color=ForegroundColorEnum.blue)
Console.spinner('Creating internal packages:', self._create_packages,
text_foreground_color=ForegroundColorEnum.green,
spinner_foreground_color=ForegroundColorEnum.blue)
Console.spinner('Building application:', self._dist_files, text_foreground_color=ForegroundColorEnum.green,
spinner_foreground_color=ForegroundColorEnum.blue)
2021-03-04 19:06:53 +01:00
def publish(self):
2021-03-14 16:01:15 +01:00
"""
Publishes the CPL project to dist_path/publish
1. Builds the project
2. Generates setup.py
3. Start setup.py
4. Remove all included source from dist_path/publish
:return:
"""
2021-03-30 11:53:07 +02:00
if self._build_settings.project_type != ProjectTypeEnum.library.value:
Console.error(f'Project must be a {ProjectTypeEnum.library.value} for publishing.')
return
2021-03-08 20:29:08 +01:00
self._output_path = os.path.join(self._output_path, 'publish')
Console.write_line('Build:')
2021-03-30 11:02:58 +02:00
Console.spinner('Reading source files:', self._read_sources, text_foreground_color=ForegroundColorEnum.green,
spinner_foreground_color=ForegroundColorEnum.blue)
Console.spinner('Creating internal packages:', self._create_packages,
text_foreground_color=ForegroundColorEnum.green,
spinner_foreground_color=ForegroundColorEnum.blue)
Console.spinner('Building application:', self._dist_files, text_foreground_color=ForegroundColorEnum.green,
spinner_foreground_color=ForegroundColorEnum.blue)
2021-03-08 20:29:08 +01:00
Console.write_line('\nPublish:')
2021-03-30 11:02:58 +02:00
Console.spinner('Generating setup.py:', self._create_setup, text_foreground_color=ForegroundColorEnum.green,
spinner_foreground_color=ForegroundColorEnum.blue)
2021-03-13 14:25:08 +01:00
Console.write_line('Running setup.py:\n')
2021-03-08 20:29:08 +01:00
self._run_setup()
2021-03-30 11:02:58 +02:00
Console.spinner('Cleaning dist path:', self._clean_dist_files, text_foreground_color=ForegroundColorEnum.green,
spinner_foreground_color=ForegroundColorEnum.blue)