github.com/noironetworks/cilium-net@v1.6.12/contrib/packaging/deb/changelog_parser.py (about)

     1  #!/usr/bin/env python
     2  
     3  import sys
     4  from docutils.core import publish_doctree
     5  from datetime import datetime
     6  
     7  
     8  class ParseSection(object):
     9  
    10      @property
    11      def date(self):
    12          return datetime.strptime(self._meta.get("date"), "%Y-%m-%d")
    13  
    14      @property
    15      def unixtime(self):
    16          return int(self.date.strftime("%s"))
    17  
    18      def __init__(self, section):
    19          self._section = section
    20          self._meta = {}
    21          self._invalid = False
    22  
    23      def parse(self):
    24          if self._section.tagname != "section":
    25              return False
    26  
    27          children = self._section.children
    28  
    29          self._parse_title(children[0])
    30          self._parse_metadata(children[1])
    31          self._parse_changelog(children[2:])
    32          return self
    33  
    34      def _parse_title(self, title):
    35          try:
    36              self._meta["version"] = float(
    37                  title.astext().lower().strip("version"))
    38          except:
    39              self._invalid = True
    40  
    41      def _parse_metadata(self, metadata):
    42          for x in metadata:
    43              self._meta[x.children[0].astext()] = x.children[1].astext()
    44  
    45      def _parse_changelog(self, body):
    46          self._changelog = ""
    47          for section in body:
    48              data = [x for x in section.astext().split("\n") if len(x) > 0]
    49              self._changelog += "\n    ".join(data)
    50  
    51      def export(self):
    52          if self._invalid:
    53              return False
    54          return """cilium ({1}) zesty; urgency=medium
    55    * Changes:
    56      commit: {4}
    57      {0._changelog}
    58  
    59   -- Cilium Team <info@cilium.io>  {3}
    60          """.format(
    61              self,
    62              self._meta.get("version"),
    63              self._meta.get("author"),
    64              self.date.strftime("%a, %d %b %Y %H:%M:%S +0000"),
    65              self._meta.get("commit"))
    66  
    67  
    68  def main():
    69      data = ""
    70      with open(sys.argv[1], 'r') as f:
    71          data = f.readlines()
    72  
    73      doctree = publish_doctree("".join(data))
    74  
    75      result = []
    76      for x in doctree:
    77          p = ParseSection(x)
    78          if p.parse():
    79              result.append(p)
    80  
    81      for section in sorted(result, key=lambda x: x.unixtime, reverse=True):
    82          log = section.export()
    83          if log:
    84              print log
    85  
    86  
    87  if __name__ == "__main__":
    88      main()