/extra/pkgrename.py

https://bitbucket.org/baptr/mirrorball · Python · 212 lines · 134 code · 43 blank · 35 comment · 26 complexity · 3d326ce35795e9f8db014978d5b10d7e MD5 · raw file

  1. #!/usr/bin/python
  2. #
  3. # Copyright (c) SAS Institute, Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. """
  18. Module for testing the renaming of packages.
  19. The goal here is to have code that can be pasted into dep-import.recipe to
  20. collapse package names.
  21. """
  22. class Recipe(object):
  23. def __init__(r):
  24. r.name = None
  25. r.fileNames = []
  26. r.finalNames = set()
  27. r.suffixes = ('-base', '-bin', '-data', '-dev', '-doc', '-dbg', '-pic',
  28. '-runtime')
  29. def filterSuffix(r, pkg):
  30. for suffix in r.suffixes:
  31. if pkg.endswith(suffix):
  32. return pkg[:-len(suffix)]
  33. return pkg
  34. def unpack(r):
  35. for n in r.fileNames:
  36. # collapse common suffixes
  37. n = r.filterSuffix(n)
  38. # collapse libfoo.* -> foo if foo is the base package
  39. if (not r.name.startswith('lib')
  40. and n.startswith('lib')
  41. and n[3:].startswith(r.name)):
  42. n = r.name
  43. # collapse libfoo.* -> libfoo when base package is libfoo
  44. elif (r.name.startswith('lib')
  45. and n.startswith(r.name)):
  46. n = r.name
  47. # collapse foo123 -> foo when foo is the base package
  48. elif (n.startswith(r.name)
  49. and n[len(r.name):].isdigit()):
  50. n = r.name
  51. # collapse foo1[a-z]* when foo is the base package
  52. elif (n.startswith(r.name)
  53. and len(n) > len(r.name)
  54. and n[len(r.name):len(r.name)+1].isdigit()
  55. and n[len(r.name)+1:].isalpha()):
  56. n = r.name
  57. # collapse foo1 -> foo if foo exists
  58. elif n[-1].isdigit():
  59. # find all package names with suffixes trimmed
  60. prefixes = [ r.filterSuffix(x) for x in r.fileNames
  61. if n.startswith(r.filterSuffix(x)) ]
  62. # unique the list
  63. prefixes = list(set(prefixes))
  64. # remove the current package name
  65. if n in prefixes: prefixes.remove(n)
  66. # If there is only one choice left and the current name starts
  67. # with the only choice it must be correct.
  68. if len(prefixes) == 1 and n.startswith(prefixes[0]):
  69. n = prefixes[0]
  70. # handle case where base package is foo123 but sub package is foo
  71. # libfoo -> foo123 and foo -> foo123 if foo123 is the base package
  72. if (r.name[-1].isdigit()
  73. and (r.name.startswith(n)
  74. or (n.startswith('lib')
  75. and r.name.startswith(n[3:]))
  76. )
  77. ):
  78. n = r.name
  79. r.finalNames.add(n)
  80. if __name__ == '__main__':
  81. def test(name, inFiles, outFiles):
  82. obj = Recipe()
  83. obj.name = name
  84. obj.fileNames = inFiles
  85. obj.unpack()
  86. if obj.finalNames != outFiles:
  87. print "expected: %s" % outFiles
  88. print "actual: %s" % obj.finalNames
  89. assert obj.finalNames == outFiles
  90. test('lzma',
  91. ['lzma-dev', 'lzma', 'lzma-dev', 'lzma'],
  92. set(['lzma']))
  93. test('libidn',
  94. ['libidn11', 'libidn11-dev', 'libidn11', 'libidn11-dev'],
  95. set(['libidn']))
  96. test('wireless-tools',
  97. ['libiw-dev', 'wireless-tools', 'libiw29', 'libiw-dev',
  98. 'wireless-tools', 'libiw29'],
  99. set(['wireless-tools', 'libiw']))
  100. test('newt',
  101. ['libnewt0.52', 'python-newt-dbg', 'whiptail', 'libnewt-pic',
  102. 'libnewt-dev', 'python-newt', 'libnewt-pic', 'python-newt',
  103. 'whiptail', 'libnewt-dev', 'libnewt0.52', 'python-newt-dbg'],
  104. set(['newt', 'python-newt', 'whiptail']))
  105. test('libsepol',
  106. ['libsepol1-dev', 'libsepol1', 'libsepol1-dbg', 'sepol-utils',
  107. 'sepol-utils-dbg', 'sepol-utils', 'libsepol1', 'libsepol1-dev',
  108. 'libsepol1-dbg', 'sepol-utils-dbg'],
  109. set(['libsepol', 'sepol-utils']))
  110. test('libedit',
  111. ['libedit-dev', 'libedit2', 'libedit-dev', 'libedit2'],
  112. set(['libedit']))
  113. test('keyutils',
  114. ['libkeyutils1', 'libkeyutils-dev', 'libkeyutils1', 'libkeyutils-dev'],
  115. set(['keyutils']))
  116. test('krb5',
  117. ['krb5-doc', 'libkrb5-dbg', 'libkadm55', 'libkrb5-dev', 'libkrb53',
  118. 'krb5-user', 'libkadm55', 'libkrb5-dbg', 'krb5-user', 'libkrb53',
  119. 'libkrb5-dev'],
  120. set(['krb5', 'libkadm55', 'krb5-user']))
  121. test('libtasn1-3',
  122. ['libtasn1-3-dbg', 'libtasn1-3-dev', 'libtasn1-3', 'libtasn1-3-dev',
  123. 'libtasn1-3-dbg', 'libtasn1-3'],
  124. set(['libtasn1-3']))
  125. test('libgpg-error',
  126. ['libgpg-error0', 'libgpg-error-dev', 'libgpg-error-dev',
  127. 'libgpg-error0'],
  128. set(['libgpg-error']))
  129. test('pam',
  130. ['libpam-doc', 'libpam-runtime', 'libpam-cracklib', 'libpam-modules',
  131. 'libpam0g', 'libpam0g-dev', 'libpam0g', 'libpam-cracklib',
  132. 'libpam0g-dev', 'libpam-modules'],
  133. set(['pam']))
  134. test('libusb',
  135. ['libusb-0.1-4', 'libusb++-dev', 'libusb++-0.1-4c2', 'libusb-dev',
  136. 'libusb++-0.1-4c2', 'libusb++-dev', 'libusb-0.1-4', 'libusb-dev'],
  137. set(['libusb']))
  138. test('ncurses',
  139. ['ncurses-base', 'ncurses-term', 'libncurses5-dbg', 'lib32ncurses5',
  140. 'ncurses-bin', 'libncursesw5-dbg', 'libncursesw5', 'libncurses5-dev',
  141. 'libncurses5', 'lib32ncurses5-dev', 'libncursesw5-dev',
  142. 'libncursesw5-dbg', 'libncurses5-dev', 'lib64ncurses5',
  143. 'libncursesw5-dev', 'libncursesw5', 'libncurses5',
  144. 'lib64ncurses5-dev', 'libncurses5-dbg', 'ncurses-bin'],
  145. set(['ncurses', 'ncurses-term', 'lib32ncurses5', 'lib64ncurses5']))
  146. test('openssl',
  147. ['openssl-doc', 'openssl', 'libssl0.9.8-dbg', 'libssl-dev',
  148. 'libssl0.9.8', 'libssl0.9.8-dbg', 'libssl0.9.8', 'openssl',
  149. 'libssl-dev'],
  150. set(['openssl', 'libssl']))
  151. test('sqlite3',
  152. ['sqlite3-doc', 'libsqlite3-dev', 'libsqlite3-0', 'sqlite3', 'sqlite3',
  153. 'libsqlite3-0', 'libsqlite3-dev'],
  154. set(['sqlite3']))
  155. test('gnutls13',
  156. ['gnutls-doc', 'libgnutlsxx13', 'libgnutls13', 'libgnutls13-dbg',
  157. 'libgnutls-dev', 'libgnutlsxx13', 'libgnutls13-dbg', 'libgnutls-dev',
  158. 'libgnutls13'],
  159. set(['gnutls13']))
  160. test('bzip2',
  161. ['bzip2-doc', 'libbz2-1.0', 'bzip2', 'libbz2-dev', 'lib32bz2-1.0',
  162. 'lib32bz2-dev', 'libbz2-dev', 'libbz2-1.0', 'bzip2', 'lib64bz2-1.0',
  163. 'lib64bz2-dev'],
  164. set(['bzip2', 'libbz2', 'lib32bz2', 'lib64bz2']))
  165. test('zlib',
  166. ['zlib1g-dev', 'lib32z1-dev', 'lib32z1', 'zlib1g-dbg', 'zlib1g',
  167. 'lib64z1-dev', 'zlib1g-dbg', 'zlib1g', 'zlib1g-dev', 'lib64z1'],
  168. set(['zlib', 'lib32z1', 'lib64z1']))