|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +from collections import OrderedDict |
| 4 | +import textwrap |
| 5 | + |
| 6 | +from ..constants import GL_AUTHORS_EMAIL, GL_AUTHORS_NAME, GL_REPOSITORY_URL |
| 7 | +from .debian_file_mixin import DebianFileMixin |
| 8 | + |
| 9 | + |
| 10 | +class CopyrightFile(dict, DebianFileMixin): |
| 11 | + def __init__(self, package_name, *args, **kwargs): |
| 12 | + dict.__init__(self, *args, **kwargs) |
| 13 | + DebianFileMixin.__init__(self) |
| 14 | + |
| 15 | + self["package_name"] = package_name |
| 16 | + |
| 17 | + self._licensed_files_entries = OrderedDict() |
| 18 | + self._license_text_entries = {} |
| 19 | + |
| 20 | + def add_licensed_files_declaration( |
| 21 | + self, |
| 22 | + files_definition_string, |
| 23 | + copyright_note, |
| 24 | + license_id=None, |
| 25 | + license_text=None, |
| 26 | + ): |
| 27 | + if files_definition_string in self._licensed_files_entries: |
| 28 | + raise ValueError( |
| 29 | + "A license has already been declared for the files definition given" |
| 30 | + ) |
| 31 | + |
| 32 | + if license_id is None and license_text is None: |
| 33 | + license_id = "MIT" |
| 34 | + |
| 35 | + license_text = f""" |
| 36 | +{copyright_note} |
| 37 | +
|
| 38 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 39 | +of this software and associated documentation files (the "Software"), to |
| 40 | +deal in the Software without restriction, including without limitation the |
| 41 | +rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 42 | +sell copies of the Software, and to permit persons to whom the Software is |
| 43 | +furnished to do so, subject to the following conditions: |
| 44 | +
|
| 45 | +The above copyright notice and this permission notice shall be included in |
| 46 | +all copies or substantial portions of the Software. |
| 47 | +
|
| 48 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 49 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 50 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 51 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 52 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 53 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 54 | +THE SOFTWARE. |
| 55 | + """ |
| 56 | + |
| 57 | + if license_id is None or license_text is None: |
| 58 | + raise ValueError("Invalid input for license ID or text") |
| 59 | + |
| 60 | + if ( |
| 61 | + license_id in self._license_text_entries |
| 62 | + and self._license_text_entries[license_id] != license_text |
| 63 | + ): |
| 64 | + raise ValueError("Given license text for already used license ID differs") |
| 65 | + |
| 66 | + self._license_text_entries[license_id] = license_text.strip() |
| 67 | + |
| 68 | + files_definition_string = textwrap.indent(files_definition_string.strip(), " ") |
| 69 | + copyright_note = textwrap.indent(copyright_note.strip(), " ") |
| 70 | + |
| 71 | + content = f""" |
| 72 | +Files:{files_definition_string} |
| 73 | +Copyright:{copyright_note} |
| 74 | +License: {license_id} |
| 75 | + """.strip() |
| 76 | + |
| 77 | + self._licensed_files_entries[files_definition_string] = content |
| 78 | + |
| 79 | + @property |
| 80 | + def content(self): |
| 81 | + if len(self._licensed_files_entries) < 1: |
| 82 | + self.add_licensed_files_declaration("*", self._generate_copyright_note()) |
| 83 | + |
| 84 | + maintainer = self.get("maintainer", GL_AUTHORS_NAME) |
| 85 | + maintainer_email = self.get("maintainer_email", GL_AUTHORS_EMAIL) |
| 86 | + package_name = self["package_name"] |
| 87 | + source_url = self.get("source_url", GL_REPOSITORY_URL) |
| 88 | + |
| 89 | + content = f""" |
| 90 | +Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ |
| 91 | +Source: {source_url} |
| 92 | +Upstream-Name: {package_name} |
| 93 | +Upstream-Contact: {maintainer} <{maintainer_email}> |
| 94 | + """.strip() |
| 95 | + |
| 96 | + for entry in self._licensed_files_entries.values(): |
| 97 | + content += f"\n\n{entry}" |
| 98 | + |
| 99 | + for entry_id, entry_text in self._license_text_entries.items(): |
| 100 | + entry_text = textwrap.indent(entry_text, " ") |
| 101 | + content += f"\n\nLicense: {entry_id}\n{entry_text}" |
| 102 | + |
| 103 | + content += "\n" |
| 104 | + |
| 105 | + return content |
| 106 | + |
| 107 | + def generate(self, target_dir): |
| 108 | + self._generate(target_dir, "copyright", self.content) |
| 109 | + |
| 110 | + def _generate_copyright_note(self): |
| 111 | + return self.get("copyright", f"Copyright (c) {GL_AUTHORS_NAME}") |
0 commit comments