Improved publishing

This commit is contained in:
Sven Heidemann 2021-03-09 21:07:47 +01:00
parent 11399d7ea7
commit e8afff0e48
5 changed files with 42 additions and 13 deletions

View File

@ -31,10 +31,14 @@
"Main": "cpl_cli.main", "Main": "cpl_cli.main",
"EntryPoint": "cpl", "EntryPoint": "cpl",
"IncludePackageData": "False", "IncludePackageData": "False",
"Included": [], "Included": [
"src/cpl_cli/templates/new/console/src/tests"
],
"Excluded": [ "Excluded": [
"*/__pycache__", "*/__pycache__",
"*/logs" "*/logs",
] "*/tests"
],
"PackageData": {}
} }
} }

View File

@ -19,6 +19,7 @@ class BuildSettings(ConfigurationModelABC):
self._include_package_data: Optional[bool] = None self._include_package_data: Optional[bool] = None
self._included: Optional[list[str]] = None self._included: Optional[list[str]] = None
self._excluded: Optional[list[str]] = None self._excluded: Optional[list[str]] = None
self._package_data: Optional[dict[str, list[str]]] = None
@property @property
def source_path(self) -> str: def source_path(self) -> str:
@ -48,15 +49,20 @@ class BuildSettings(ConfigurationModelABC):
def excluded(self) -> list[str]: def excluded(self) -> list[str]:
return self._excluded return self._excluded
@property
def package_data(self) -> dict[str, list[str]]:
return self._package_data
def from_dict(self, settings: dict): def from_dict(self, settings: dict):
try: try:
self._source_path = settings[BuildSettingsName.sourcePath.value] self._source_path = settings[BuildSettingsName.source_path.value]
self._output_path = settings[BuildSettingsName.outputPath.value] self._output_path = settings[BuildSettingsName.output_path.value]
self._include_package_data = bool(settings[BuildSettingsName.include_package_data.value]) self._include_package_data = bool(settings[BuildSettingsName.include_package_data.value])
self._main = settings[BuildSettingsName.main.value] self._main = settings[BuildSettingsName.main.value]
self._entry_point = settings[BuildSettingsName.entry_point.value] self._entry_point = settings[BuildSettingsName.entry_point.value]
self._included = settings[BuildSettingsName.included.value] self._included = settings[BuildSettingsName.included.value]
self._excluded = settings[BuildSettingsName.excluded.value] self._excluded = settings[BuildSettingsName.excluded.value]
self._package_data = settings[BuildSettingsName.package_data.value]
except Exception as e: except Exception as e:
Console.set_foreground_color(ForegroundColor.red) Console.set_foreground_color(ForegroundColor.red)
Console.write_line( Console.write_line(

View File

@ -3,10 +3,11 @@ from enum import Enum
class BuildSettingsName(Enum): class BuildSettingsName(Enum):
sourcePath = 'SourcePath' source_path = 'SourcePath'
outputPath = 'OutputPath' output_path = 'OutputPath'
main = 'Main' main = 'Main'
entry_point = 'EntryPoint' entry_point = 'EntryPoint'
include_package_data = 'IncludePackageData' include_package_data = 'IncludePackageData'
included = 'Included' included = 'Included'
excluded = 'Excluded' excluded = 'Excluded'
package_data = 'PackageData'

View File

@ -26,6 +26,7 @@ class Publisher(PublisherABC):
self._output_path = os.path.join(self._runtime.working_directory, self._build_settings.output_path) self._output_path = os.path.join(self._runtime.working_directory, self._build_settings.output_path)
self._included_files: list[str] = [] self._included_files: list[str] = []
self._included_dirs: list[str] = []
self._distributed_files: list[str] = [] self._distributed_files: list[str] = []
@property @property
@ -74,7 +75,7 @@ class Publisher(PublisherABC):
if excluded.startswith('*'): if excluded.startswith('*'):
excluded = excluded.replace('*', '') excluded = excluded.replace('*', '')
if excluded in path: if excluded in path and path not in self._build_settings.included:
return True return True
return False return False
@ -102,6 +103,12 @@ class Publisher(PublisherABC):
relative_path = os.path.relpath(r) relative_path = os.path.relpath(r)
file_path = os.path.join(relative_path, os.path.relpath(file)) file_path = os.path.join(relative_path, os.path.relpath(file))
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)
if not self._is_path_excluded(relative_path): if not self._is_path_excluded(relative_path):
self._included_files.append(os.path.relpath(file_path)) self._included_files.append(os.path.relpath(file_path))
@ -187,6 +194,15 @@ class Publisher(PublisherABC):
Console.error(__name__, f'Cannot copy file: {file} to {output_path} -> {e}') Console.error(__name__, f'Cannot copy file: {file} to {output_path} -> {e}')
return return
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)
def _clean_dist_files(self): def _clean_dist_files(self):
paths: list[str] = [] paths: list[str] = []
for file in self._distributed_files: for file in self._distributed_files:
@ -228,12 +244,12 @@ class Publisher(PublisherABC):
setup_string = stringTemplate(template_string).substitute( setup_string = stringTemplate(template_string).substitute(
Name=self._project_settings.name, Name=self._project_settings.name,
Version=self._project_settings.version.to_str(), Version=self._project_settings.version.to_str(),
Packages=setuptools.find_packages(where=self._build_settings.source_path, exclude=self._build_settings.excluded), Packages=setuptools.find_packages(where=self._output_path, exclude=self._build_settings.excluded),
URL=self._project_settings.url, URL=self._project_settings.url,
LicenseName=self._project_settings.license_name, LicenseName=self._project_settings.license_name,
Author=self._project_settings.author, Author=self._project_settings.author,
AuthorMail=self._project_settings.author_email, AuthorMail=self._project_settings.author_email,
InstallPackageData=self._build_settings.include_package_data, IncludePackageData=self._build_settings.include_package_data,
Description=self._project_settings.description, Description=self._project_settings.description,
PyRequires=self._project_settings.python_version, PyRequires=self._project_settings.python_version,
Dependencies=self._project_settings.dependencies, Dependencies=self._project_settings.dependencies,
@ -241,7 +257,8 @@ class Publisher(PublisherABC):
'console_scripts': [ 'console_scripts': [
f'{self._build_settings.entry_point} = {main.__name__}:{main.main.__name__}' f'{self._build_settings.entry_point} = {main.__name__}:{main.main.__name__}'
] ]
} },
PackageData=self._build_settings.package_data
) )
setup_py.write(setup_string) setup_py.write(setup_string)
setup_py.close() setup_py.close()

View File

@ -12,9 +12,10 @@ setuptools.setup(
license='$LicenseName', license='$LicenseName',
author='$Author', author='$Author',
author_email='$AuthorMail', author_email='$AuthorMail',
include_package_data=$InstallPackageData, include_package_data=$IncludePackageData,
description='$Description', description='$Description',
python_requires='$PyRequires', python_requires='$PyRequires',
install_requires=$Dependencies, install_requires=$Dependencies,
entry_points=$EntryPoints entry_points=$EntryPoints,
package_data=$PackageData
) )