instanceVersions.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. ########################################################################
  2. # Searx-qt - Lightweight desktop application for SearX.
  3. # Copyright (C) 2020 CYBERDEViL
  4. #
  5. # This file is part of Searx-qt.
  6. #
  7. # Searx-qt is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # Searx-qt is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. ########################################################################
  21. import re
  22. class InstanceVersionTypes:
  23. No = 0 # just empty string, or malformed.
  24. Stable = 1
  25. Git = 2
  26. Unknown = 3 # string with the '-unknown' suffix
  27. VERSION_PATTERNS = {
  28. InstanceVersionTypes.Stable:
  29. r"^([0-9]+)\.([0-9]+)\.([0-9]+)(-([0-9]+)\.([0-9]+)\.([0-9]+))?$",
  30. InstanceVersionTypes.Unknown:
  31. r"^([0-9]+)\.([0-9]+)\.([0-9]+)-unknown$",
  32. InstanceVersionTypes.Git:
  33. r"^([0-9]+)\.([0-9]+)\.([0-9]+)-([0-9]+)-([0-9A-Fa-f]{8})$"
  34. }
  35. class InstanceVersion:
  36. def __init__(
  37. self,
  38. str_,
  39. semVer=(0, 0, 0),
  40. verType=InstanceVersionTypes.No
  41. ):
  42. """
  43. @param semVer: Semantic version in a tuple
  44. @type semVer: tuple
  45. @param verType: Version type
  46. @type verType: int from InstanceVersionTypes
  47. """
  48. self.__str = str_
  49. self.__semVer = semVer
  50. self.__type = verType
  51. def __str__(self):
  52. return self.__str
  53. def data(self):
  54. return {
  55. 'type': self.type,
  56. 'semVer': self.semanticVersion
  57. }
  58. @property
  59. def semanticVersion(self):
  60. """ Returns the semantic version in a tuple (with 3 int's, Major, Minor
  61. and Patch)
  62. @rtype: tuple
  63. """
  64. return self.__semVer
  65. @property
  66. def type(self):
  67. """ Returns this version it's type; see class InstanceVersionTypes
  68. @rtype: str
  69. """
  70. return self.__type
  71. class InstanceVersionGit(InstanceVersion):
  72. def __init__(self, str_, semVer, rev, shortHash):
  73. """
  74. @param semVer: Semantic version in a tuple
  75. @type semVer: tuple
  76. @param rev: Rev count
  77. @type rev: int
  78. @param shortHash: Short commit hash
  79. @type shortHash: str
  80. """
  81. InstanceVersion.__init__(
  82. self,
  83. str_,
  84. semVer=semVer,
  85. verType=InstanceVersionTypes.Git
  86. )
  87. self.__rev = rev
  88. self.__shortHash = shortHash
  89. def data(self):
  90. data = InstanceVersion.data(self)
  91. data.update({
  92. 'rev': self.rev,
  93. 'hash': self.shortHash
  94. })
  95. return data
  96. @property
  97. def rev(self):
  98. return self.__rev
  99. @property
  100. def shortHash(self):
  101. return self.__shortHash
  102. def parseVersionString(versionStr):
  103. """
  104. @param versionStr: searx instance version string.
  105. @type: versionStr: str
  106. @rtype: InstanceVersion or InstanceVersionGit
  107. """
  108. if not versionStr:
  109. return InstanceVersion("")
  110. for versionType, pattern in VERSION_PATTERNS.items():
  111. result = re.match(pattern, versionStr)
  112. if result:
  113. semVer = (
  114. int(result.group(1)),
  115. int(result.group(2)),
  116. int(result.group(3))
  117. )
  118. if versionType == InstanceVersionTypes.Stable:
  119. return InstanceVersion(
  120. versionStr,
  121. semVer=semVer,
  122. verType=InstanceVersionTypes.Stable
  123. )
  124. elif versionType == InstanceVersionTypes.Git:
  125. return InstanceVersionGit(
  126. versionStr,
  127. semVer,
  128. int(result.group(4)),
  129. result.group(5)
  130. )
  131. elif versionType == InstanceVersionTypes.Unknown:
  132. return InstanceVersion(
  133. versionStr,
  134. semVer=semVer,
  135. verType=InstanceVersionTypes.Unknown
  136. )
  137. return InstanceVersion(versionStr)
  138. def InstanceVersionGtEqCmp(versionA, versionB):
  139. """ Checks if versionA is greater or equal to versionB
  140. Used for minumum version filter.
  141. Notes:
  142. - Git versions (development) go above Stable version (with the same
  143. semantic version).
  144. @param versionA: InstanceVersion object to compare against versionB
  145. @type versionA: InstanceVersion
  146. @param versionB: InstanceVersion object to comapre against versionA
  147. @type versionB: InstanceVersion
  148. @rtype: bool
  149. """
  150. # Check if Major version is greater
  151. if versionA.semanticVersion[0] < versionB.semanticVersion[0]:
  152. return False
  153. elif versionA.semanticVersion[0] == versionB.semanticVersion[0]:
  154. # Check if Minor version is greater
  155. if versionA.semanticVersion[1] < versionB.semanticVersion[1]:
  156. return False
  157. elif versionA.semanticVersion[1] == versionB.semanticVersion[1]:
  158. # Check if Patch version is greater
  159. if versionA.semanticVersion[2] < versionB.semanticVersion[2]:
  160. return False
  161. # Check if semantic versions are equals
  162. if versionA.semanticVersion == versionB.semanticVersion:
  163. # Check if both are git versions
  164. if (
  165. versionA.type == InstanceVersionTypes.Git and
  166. versionB.type == InstanceVersionTypes.Git
  167. ):
  168. # Both are git versions, now compare the rev.
  169. if versionA.rev < versionB.rev:
  170. return False
  171. elif versionB.type == InstanceVersionTypes.Git:
  172. # Only versionB is type of Git, so it is greater then versionA
  173. return False
  174. return True