Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/gardenlinux/apt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,31 @@
APT module
"""

from .changelog_file import ChangelogFile
from .control_file import ControlFile
from .copyright_file import CopyrightFile
from .debsource import Debsrc, DebsrcFile
from .docs_file import DocsFile
from .install_file import InstallFile
from .package_repo_info import GardenLinuxRepo
from .preinst_file import PreinstFile
from .prerm_file import PrermFile
from .postinst_file import PostinstFile
from .postrm_file import PostrmFile
from .rules_file import RulesFile

__all__ = ["Debsrc", "DebsrcFile", "GardenLinuxRepo"]
__all__ = [
"ChangelogFile",
"ControlFile",
"CopyrightFile",
"Debsrc",
"DebsrcFile",
"DocsFile",
"GardenLinuxRepo",
"InstallFile",
"PreinstFile",
"PrermFile",
"PostinstFile",
"PostrmFile",
"RulesFile",
]
64 changes: 64 additions & 0 deletions src/gardenlinux/apt/changelog_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# -*- coding: utf-8 -*-

from time import gmtime, strftime
import textwrap

from ..constants import GL_AUTHORS_EMAIL, GL_AUTHORS_NAME
from .debian_file_mixin import DebianFileMixin


class ChangelogFile(DebianFileMixin):
def __init__(self, package_name, maintainer=None, maintainer_email=None):
DebianFileMixin.__init__(self)

self._entries = []
self._package_name = package_name

if maintainer is None:
maintainer = GL_AUTHORS_NAME
if maintainer_email is None:
maintainer_email = GL_AUTHORS_EMAIL

self._maintainer = maintainer
self._maintainer_email = maintainer_email

@property
def content(self):
content = ""

for entry in self._entries:
content += f"{entry}\n\n"

return content.strip() + "\n"

def add_entry(
self,
package_version,
package_timestamp,
changes,
maintainer=None,
maintainer_email=None,
):
if maintainer is None:
maintainer = self._maintainer
if maintainer_email is None:
maintainer_email = self._maintainer_email

changes = textwrap.indent(changes.strip(), " ")

package_timestamp_string = strftime(
"%a, %d %b %Y %H:%M:%S +0000", gmtime(package_timestamp)
)

content = f"""
{self._package_name} ({package_version}) universal; urgency=low

{changes}

-- {maintainer} <{maintainer_email}> {package_timestamp_string}
"""

self._entries.append(content.strip())

def generate(self, target_dir):
self._generate(target_dir, "changelog", self.content)
35 changes: 35 additions & 0 deletions src/gardenlinux/apt/code_file_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-


class CodeFileMixin(object):
def __init__(self, executable=None, header_code=None, footer_code=None):
self._code = []
self._executable = executable
self._footer_code = footer_code
self._header_code = header_code

@property
def content(self):
content = ""

if len(self._code) > 0:
if self._executable is not None:
content += f"#!{self._executable}\n\n"

if self._header_code is not None:
content += self._header_code + "\n"

for code in self._code:
content += f"\n{code}\n"

if self._footer_code is not None:
content += "\n" + self._footer_code

return content.strip() + "\n"

@property
def empty(self):
return len(self._code) < 1

def add_code(self, content):
self._code.append(content.strip())
108 changes: 108 additions & 0 deletions src/gardenlinux/apt/control_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# -*- coding: utf-8 -*-

from os import PathLike
from pathlib import Path
import textwrap

from ..constants import GL_AUTHORS_EMAIL, GL_AUTHORS_NAME, GL_HOME_URL
from .debian_file_mixin import DebianFileMixin


class ControlFile(dict, DebianFileMixin):
def __init__(self, package_name, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
DebianFileMixin.__init__(self)

self._conflicts = []
self._breaking_packages = []
self._dependencies = []
self["package_name"] = package_name
self._packages = []

@property
def content(self):
homepage = self.get("homepage", GL_HOME_URL)
maintainer = self.get("maintainer", GL_AUTHORS_NAME)
maintainer_email = self.get("maintainer_email", GL_AUTHORS_EMAIL)

content = f"""
Source: {self["package_name"]}
Standards-Version: 4.0.0
Section: universe
Priority: optional
Maintainer: {maintainer} <{maintainer_email}>
Build-Depends: debhelper-compat (=12)
Homepage: {homepage}
""".strip()

for package in self._packages:
content += f"\n\n{package}"

if len(self._dependencies) > 0:
dependencies = textwrap.indent(", ".join(self._dependencies), " ")
content += f"\nDepends:{dependencies}"

if len(self._conflicts) > 0:
dependencies = textwrap.indent(", ".join(self._conflicts), " ")
content += f"\nConflicts:{dependencies}"

if len(self._breaking_packages) > 0:
dependencies = textwrap.indent(", ".join(self._breaking_packages), " ")
content += f"\nBreaks:{dependencies}"

content += "\n"

return content

def add_breaking_package(self, package_name):
if (
package_name not in self._breaking_packages
and package_name not in self._conflicts
):
self._breaking_packages.append(package_name)

def add_conflict(self, package_name):
if (
package_name not in self._breaking_packages
and package_name not in self._conflicts
):
self._conflicts.append(package_name)

def add_dependency(self, package_name):
if package_name not in self._dependencies:
self._dependencies.append(package_name)

def add_package(self, package_name, description, architecture=None):
if architecture is None:
architecture = "any"

description = textwrap.indent(description.strip(), " ")

content = f"""
Package: {package_name}
Architecture: {architecture}
Description:{description}
"""

self._packages.append(content.strip())

def generate(self, target_dir):
if not isinstance(target_dir, PathLike):
target_dir = Path(target_dir)

if not target_dir.is_dir():
raise ValueError("Target directory given is invalid")

source_dir_path = target_dir.joinpath("source")
format_file_path = source_dir_path.joinpath("format")

if format_file_path.is_file():
raise RuntimeError(
"Target directory already contains a 'source/format' file"
)

if not source_dir_path.is_dir():
source_dir_path.mkdir()

self._generate(target_dir, "control", self.content)
self._generate(source_dir_path, "format", "3.0 (native)")
111 changes: 111 additions & 0 deletions src/gardenlinux/apt/copyright_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# -*- coding: utf-8 -*-

from collections import OrderedDict
import textwrap

from ..constants import GL_AUTHORS_EMAIL, GL_AUTHORS_NAME, GL_REPOSITORY_URL
from .debian_file_mixin import DebianFileMixin


class CopyrightFile(dict, DebianFileMixin):
def __init__(self, package_name, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
DebianFileMixin.__init__(self)

self["package_name"] = package_name

self._licensed_files_entries = OrderedDict()
self._license_text_entries = {}

def add_licensed_files_declaration(
self,
files_definition_string,
copyright_note,
license_id=None,
license_text=None,
):
if files_definition_string in self._licensed_files_entries:
raise ValueError(
"A license has already been declared for the files definition given"
)

if license_id is None and license_text is None:
license_id = "MIT"

license_text = f"""
{copyright_note}

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

if license_id is None or license_text is None:
raise ValueError("Invalid input for license ID or text")

if (
license_id in self._license_text_entries
and self._license_text_entries[license_id] != license_text
):
raise ValueError("Given license text for already used license ID differs")

self._license_text_entries[license_id] = license_text.strip()

files_definition_string = textwrap.indent(files_definition_string.strip(), " ")
copyright_note = textwrap.indent(copyright_note.strip(), " ")

content = f"""
Files:{files_definition_string}
Copyright:{copyright_note}
License: {license_id}
""".strip()

self._licensed_files_entries[files_definition_string] = content

@property
def content(self):
if len(self._licensed_files_entries) < 1:
self.add_licensed_files_declaration("*", self._generate_copyright_note())

maintainer = self.get("maintainer", GL_AUTHORS_NAME)
maintainer_email = self.get("maintainer_email", GL_AUTHORS_EMAIL)
package_name = self["package_name"]
source_url = self.get("source_url", GL_REPOSITORY_URL)

content = f"""
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Source: {source_url}
Upstream-Name: {package_name}
Upstream-Contact: {maintainer} <{maintainer_email}>
""".strip()

for entry in self._licensed_files_entries.values():
content += f"\n\n{entry}"

for entry_id, entry_text in self._license_text_entries.items():
entry_text = textwrap.indent(entry_text, " ")
content += f"\n\nLicense: {entry_id}\n{entry_text}"

content += "\n"

return content

def generate(self, target_dir):
self._generate(target_dir, "copyright", self.content)

def _generate_copyright_note(self):
return self.get("copyright", f"Copyright (c) {GL_AUTHORS_NAME}")
23 changes: 23 additions & 0 deletions src/gardenlinux/apt/debian_file_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-

from os import PathLike
from pathlib import Path


class DebianFileMixin(object):
def _generate(self, target_dir, file_name, content, chmod_octet=0o644):
if not isinstance(target_dir, PathLike):
target_dir = Path(target_dir)

if not target_dir.is_dir():
raise ValueError("Target directory given is invalid")

file_path_name = target_dir.joinpath(file_name)

if file_path_name.is_file():
raise RuntimeError(
f"Target directory already contains a '{file_path_name}' file"
)

file_path_name.write_text(content)
file_path_name.chmod(chmod_octet)
Loading
Loading