PageRenderTime 25ms CodeModel.GetById 10ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 19"""
 20Module for testing the renaming of packages.
 21
 22The goal here is to have code that can be pasted into dep-import.recipe to
 23collapse package names.
 24"""
 25
 26class Recipe(object):
 27    def __init__(r):
 28        r.name = None
 29        r.fileNames = []
 30        r.finalNames = set()
 31        r.suffixes = ('-base', '-bin', '-data', '-dev', '-doc', '-dbg', '-pic',
 32                      '-runtime')
 33
 34    def filterSuffix(r, pkg):
 35        for suffix in r.suffixes:
 36            if pkg.endswith(suffix):
 37                return pkg[:-len(suffix)]
 38        return pkg
 39
 40    def unpack(r):
 41        for n in r.fileNames:
 42            # collapse common suffixes
 43            n = r.filterSuffix(n)
 44
 45            # collapse libfoo.* -> foo if foo is the base package
 46            if (not r.name.startswith('lib')
 47                and n.startswith('lib')
 48                and n[3:].startswith(r.name)):
 49
 50                n = r.name
 51
 52            # collapse libfoo.* -> libfoo when base package is libfoo
 53            elif (r.name.startswith('lib')
 54                  and n.startswith(r.name)):
 55
 56                n = r.name
 57
 58            # collapse foo123 -> foo when foo is the base package
 59            elif (n.startswith(r.name)
 60                  and n[len(r.name):].isdigit()):
 61
 62                n = r.name
 63
 64            # collapse foo1[a-z]* when foo is the base package
 65            elif (n.startswith(r.name)
 66                  and len(n) > len(r.name)
 67                  and n[len(r.name):len(r.name)+1].isdigit()
 68                  and n[len(r.name)+1:].isalpha()):
 69
 70                n = r.name
 71
 72            # collapse foo1 -> foo if foo exists
 73            elif n[-1].isdigit():
 74                # find all package names with suffixes trimmed
 75                prefixes = [ r.filterSuffix(x) for x in r.fileNames
 76                             if n.startswith(r.filterSuffix(x)) ]
 77
 78                # unique the list
 79                prefixes = list(set(prefixes))
 80
 81                # remove the current package name
 82                if n in prefixes: prefixes.remove(n)
 83
 84                # If there is only one choice left and the current name starts
 85                # with the only choice it must be correct.
 86                if len(prefixes) == 1 and n.startswith(prefixes[0]):
 87                    n = prefixes[0]
 88
 89
 90            # handle case where base package is foo123 but sub package is foo
 91            # libfoo -> foo123 and foo -> foo123 if foo123 is the base package
 92            if (r.name[-1].isdigit()
 93                and (r.name.startswith(n)
 94                     or (n.startswith('lib')
 95                         and r.name.startswith(n[3:]))
 96                    )
 97               ):
 98
 99                n = r.name
100
101            r.finalNames.add(n)
102
103
104if __name__ == '__main__':
105    def test(name, inFiles, outFiles):
106        obj = Recipe()
107        obj.name = name
108        obj.fileNames = inFiles
109        obj.unpack()
110
111        if obj.finalNames != outFiles:
112            print "expected: %s" % outFiles
113            print "actual: %s" % obj.finalNames
114
115        assert obj.finalNames == outFiles
116
117    test('lzma',
118        ['lzma-dev', 'lzma', 'lzma-dev', 'lzma'],
119        set(['lzma']))
120
121    test('libidn',
122        ['libidn11', 'libidn11-dev', 'libidn11', 'libidn11-dev'],
123        set(['libidn']))
124
125    test('wireless-tools',
126        ['libiw-dev', 'wireless-tools', 'libiw29', 'libiw-dev',
127         'wireless-tools', 'libiw29'],
128        set(['wireless-tools', 'libiw']))
129
130    test('newt',
131        ['libnewt0.52', 'python-newt-dbg', 'whiptail', 'libnewt-pic',
132         'libnewt-dev', 'python-newt', 'libnewt-pic', 'python-newt',
133         'whiptail', 'libnewt-dev', 'libnewt0.52', 'python-newt-dbg'],
134        set(['newt', 'python-newt', 'whiptail']))
135
136    test('libsepol',
137        ['libsepol1-dev', 'libsepol1', 'libsepol1-dbg', 'sepol-utils',
138         'sepol-utils-dbg', 'sepol-utils', 'libsepol1', 'libsepol1-dev',
139         'libsepol1-dbg', 'sepol-utils-dbg'],
140        set(['libsepol', 'sepol-utils']))
141
142    test('libedit',
143        ['libedit-dev', 'libedit2', 'libedit-dev', 'libedit2'],
144        set(['libedit']))
145
146    test('keyutils',
147        ['libkeyutils1', 'libkeyutils-dev', 'libkeyutils1', 'libkeyutils-dev'],
148        set(['keyutils']))
149
150    test('krb5',
151        ['krb5-doc', 'libkrb5-dbg', 'libkadm55', 'libkrb5-dev', 'libkrb53',
152         'krb5-user', 'libkadm55', 'libkrb5-dbg', 'krb5-user', 'libkrb53',
153         'libkrb5-dev'],
154        set(['krb5', 'libkadm55', 'krb5-user']))
155
156    test('libtasn1-3',
157        ['libtasn1-3-dbg', 'libtasn1-3-dev', 'libtasn1-3', 'libtasn1-3-dev',
158         'libtasn1-3-dbg', 'libtasn1-3'],
159        set(['libtasn1-3']))
160
161    test('libgpg-error',
162        ['libgpg-error0', 'libgpg-error-dev', 'libgpg-error-dev',
163         'libgpg-error0'],
164        set(['libgpg-error']))
165
166    test('pam',
167        ['libpam-doc', 'libpam-runtime', 'libpam-cracklib', 'libpam-modules',
168         'libpam0g', 'libpam0g-dev', 'libpam0g', 'libpam-cracklib',
169         'libpam0g-dev', 'libpam-modules'],
170        set(['pam']))
171
172    test('libusb',
173        ['libusb-0.1-4', 'libusb++-dev', 'libusb++-0.1-4c2', 'libusb-dev',
174         'libusb++-0.1-4c2', 'libusb++-dev', 'libusb-0.1-4', 'libusb-dev'],
175        set(['libusb']))
176
177    test('ncurses',
178        ['ncurses-base', 'ncurses-term', 'libncurses5-dbg', 'lib32ncurses5',
179         'ncurses-bin', 'libncursesw5-dbg', 'libncursesw5', 'libncurses5-dev',
180         'libncurses5', 'lib32ncurses5-dev', 'libncursesw5-dev',
181         'libncursesw5-dbg', 'libncurses5-dev', 'lib64ncurses5',
182         'libncursesw5-dev', 'libncursesw5', 'libncurses5',
183         'lib64ncurses5-dev', 'libncurses5-dbg', 'ncurses-bin'],
184        set(['ncurses', 'ncurses-term', 'lib32ncurses5', 'lib64ncurses5']))
185
186    test('openssl',
187        ['openssl-doc', 'openssl', 'libssl0.9.8-dbg', 'libssl-dev',
188         'libssl0.9.8', 'libssl0.9.8-dbg', 'libssl0.9.8', 'openssl',
189         'libssl-dev'],
190        set(['openssl', 'libssl']))
191
192    test('sqlite3',
193        ['sqlite3-doc', 'libsqlite3-dev', 'libsqlite3-0', 'sqlite3', 'sqlite3',
194         'libsqlite3-0', 'libsqlite3-dev'],
195        set(['sqlite3']))
196
197    test('gnutls13',
198        ['gnutls-doc', 'libgnutlsxx13', 'libgnutls13', 'libgnutls13-dbg',
199         'libgnutls-dev', 'libgnutlsxx13', 'libgnutls13-dbg', 'libgnutls-dev',
200         'libgnutls13'],
201        set(['gnutls13']))
202
203    test('bzip2',
204        ['bzip2-doc', 'libbz2-1.0', 'bzip2', 'libbz2-dev', 'lib32bz2-1.0',
205         'lib32bz2-dev', 'libbz2-dev', 'libbz2-1.0', 'bzip2', 'lib64bz2-1.0',
206         'lib64bz2-dev'],
207        set(['bzip2', 'libbz2', 'lib32bz2', 'lib64bz2']))
208
209    test('zlib',
210         ['zlib1g-dev', 'lib32z1-dev', 'lib32z1', 'zlib1g-dbg', 'zlib1g',
211          'lib64z1-dev', 'zlib1g-dbg', 'zlib1g', 'zlib1g-dev', 'lib64z1'],
212         set(['zlib', 'lib32z1', 'lib64z1']))