[rtems-central commit] content: Reject copyright statements without a year

Sebastian Huber sebh at rtems.org
Tue Nov 21 13:35:40 UTC 2023


Module:    rtems-central
Branch:    master
Commit:    48058608c263970f310837ae0787d1f2ef490529
Changeset: http://git.rtems.org/rtems-central/commit/?id=48058608c263970f310837ae0787d1f2ef490529

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Tue Nov 21 11:13:15 2023 +0100

content: Reject copyright statements without a year

---

 rtemsspec/content.py                       | 49 +++++++++++-------------------
 rtemsspec/tests/test_content_copyrights.py | 39 +++++++++++++-----------
 rtemsspec/tests/test_content_sphinx.py     |  4 +--
 3 files changed, 40 insertions(+), 52 deletions(-)

diff --git a/rtemsspec/content.py b/rtemsspec/content.py
index cd122f0b..75b4958d 100644
--- a/rtemsspec/content.py
+++ b/rtemsspec/content.py
@@ -51,40 +51,32 @@ class Copyright:
     contributions.
     """
 
-    def __init__(self, holder):
-        self._holder = holder
-        self._years = set()
+    def __init__(self, holder: str):
+        self.holder = holder
+        self.years: Set[int] = set()
 
-    def add_year(self, year: str):
+    def add_year(self, year: int):
         """
         Adds a year to the set of substantial contributions of this copyright
         holder.
         """
-        self._years.add(year)
+        self.years.add(year)
 
     def get_statement(self) -> str:
         """ Returns a copyright statement. """
         line = "Copyright (C)"
-        years = sorted(self._years)
-        year_count = len(years)
+        year_count = len(self.years)
         if year_count == 1:
-            line += " " + years[0]
-        elif year_count > 1:
-            line += " " + years[0] + ", " + years[-1]
-        line += " " + self._holder
+            line += f" {min(self.years)}"
+        else:
+            line += f" {min(self.years)}, {max(self.years)}"
+        line += f" {self.holder}"
         return line
 
     def __lt__(self, other: "Copyright") -> bool:
-        # pylint: disable=protected-access
-        if self._years and other._years:
-            self_first_year = sorted(self._years)[0]
-            other_first_year = sorted(other._years)[0]
-            if self_first_year == other_first_year:
-                return self._holder > other._holder
-            return self_first_year > other_first_year
-        if self._years or other._years:
-            return True
-        return self._holder > other._holder
+        return (min(self.years), max(self.years),
+                other.holder) < (min(other.years), max(other.years),
+                                 self.holder)
 
 
 class Copyrights:
@@ -104,8 +96,8 @@ class Copyrights:
             holder = match.group(3)
             the_copyright = self.copyrights.setdefault(holder,
                                                        Copyright(holder))
-            the_copyright.add_year(match.group(1))
-            the_copyright.add_year(match.group(2))
+            the_copyright.add_year(int(match.group(1)))
+            the_copyright.add_year(int(match.group(2)))
             return
         match = re.search(
             r"^\s*Copyright\s+\(C\)\s+([0-9]+)\s+(.+)\s*$",
@@ -116,21 +108,14 @@ class Copyrights:
             holder = match.group(2)
             the_copyright = self.copyrights.setdefault(holder,
                                                        Copyright(holder))
-            the_copyright.add_year(match.group(1))
-            return
-        match = re.search(r"^\s*Copyright\s+\(C\)\s+(.+)\s*$",
-                          statement,
-                          flags=re.I)
-        if match:
-            holder = match.group(1)
-            self.copyrights.setdefault(holder, Copyright(holder))
+            the_copyright.add_year(int(match.group(1)))
             return
         raise ValueError(statement)
 
     def get_statements(self):
         """ Returns all registered copyright statements as a sorted list. """
         statements = []
-        for the_copyright in sorted(self.copyrights.values()):
+        for the_copyright in sorted(self.copyrights.values(), reverse=True):
             statements.append(the_copyright.get_statement())
         return statements
 
diff --git a/rtemsspec/tests/test_content_copyrights.py b/rtemsspec/tests/test_content_copyrights.py
index 99db8ed4..d33bcf0c 100644
--- a/rtemsspec/tests/test_content_copyrights.py
+++ b/rtemsspec/tests/test_content_copyrights.py
@@ -32,16 +32,15 @@ from rtemsspec.content import Copyrights
 
 def test_copyright_get_statement():
     c = Copyright("John Doe")
-    assert "Copyright (C) John Doe" == c.get_statement()
-    c.add_year("3")
+    c.add_year(3)
     assert "Copyright (C) 3 John Doe" == c.get_statement()
-    c.add_year("3")
+    c.add_year(3)
     assert "Copyright (C) 3 John Doe" == c.get_statement()
-    c.add_year("5")
+    c.add_year(5)
     assert "Copyright (C) 3, 5 John Doe" == c.get_statement()
-    c.add_year("4")
+    c.add_year(4)
     assert "Copyright (C) 3, 5 John Doe" == c.get_statement()
-    c.add_year("2")
+    c.add_year(2)
     assert "Copyright (C) 2, 5 John Doe" == c.get_statement()
 
 
@@ -49,30 +48,34 @@ def test_copyright_lt():
     a = Copyright("A")
     b = Copyright("B")
     c = Copyright("C")
+    a.add_year(2)
+    b.add_year(2)
+    c.add_year(2)
     assert b < a
     assert c < a
     assert c < b
-    b.add_year("1")
-    assert b < c
-    a.add_year("2")
-    assert a < b
+    b.add_year(3)
+    assert c < b
+    b.add_year(1)
+    assert b < a
 
 
 def test_copyrights_register():
     c = Copyrights()
     with pytest.raises(ValueError):
         c.register("abc")
-    c.register("Copyright (C) A")
     c.register("Copyright (C) 2 A")
     c.register("Copyright (C) 2, 3 A")
-    c.register("Copyright (C) D")
+    c.register("Copyright (C) 2, 4 C")
+    c.register("Copyright (C) 3 E")
+    c.register("Copyright (C) 2, 4 E")
     c.register("Copyright (C) 1 D")
     c.register("Copyright (C) 1, 4 D")
-    c.register("Copyright (C) C")
     c.register("Copyright (C) 1 B")
     s = c.get_statements()
-    assert 4 == len(s)
-    assert "Copyright (C) C" == s[0]
-    assert "Copyright (C) 2, 3 A" == s[1]
-    assert "Copyright (C) 1, 4 D" == s[2]
-    assert "Copyright (C) 1 B" == s[3]
+    assert 5 == len(s)
+    assert "Copyright (C) 2, 4 C" == s[0]
+    assert "Copyright (C) 2, 4 E" == s[1]
+    assert "Copyright (C) 2, 3 A" == s[2]
+    assert "Copyright (C) 1, 4 D" == s[3]
+    assert "Copyright (C) 1 B" == s[4]
diff --git a/rtemsspec/tests/test_content_sphinx.py b/rtemsspec/tests/test_content_sphinx.py
index 7a9ecd7c..00be7a5f 100644
--- a/rtemsspec/tests/test_content_sphinx.py
+++ b/rtemsspec/tests/test_content_sphinx.py
@@ -276,12 +276,12 @@ def test_license_and_copyrights():
     content = SphinxContent()
     with pytest.raises(ValueError):
         content.register_license("x")
-    content.register_copyright("Copyright (C) A")
+    content.register_copyright("Copyright (C) 123 A")
     assert str(content) == ""
     content.add_licence_and_copyrights()
     assert str(content) == """.. SPDX-License-Identifier: CC-BY-SA-4.0
 
-.. Copyright (C) A
+.. Copyright (C) 123 A
 
 """
 



More information about the vc mailing list