99RPM_AVAILABLE = False
1010try :
1111 import rpm
12- import rpmUtils .miscutils
1312 RPM_AVAILABLE = True
1413except ImportError :
1514 pass
1615
1716
17+ def split_filename (filename ):
18+ """
19+ Received a standard style rpm fullname and returns
20+ name, version, release, epoch, arch
21+ Example: foo-1.0-1.i386.rpm returns foo, 1.0, 1, i386
22+ 1:bar-9-123a.ia64.rpm returns bar, 9, 123a, 1, ia64
23+
24+ This function replaces rpmUtils.miscutils.splitFilename, see
25+ https://bugzilla.redhat.com/1452801
26+ """
27+
28+ # Remove .rpm suffix
29+ if filename .endswith ('.rpm' ):
30+ filename = filename .split ('.rpm' )[0 ]
31+
32+ # is there an epoch?
33+ components = filename .split (':' )
34+ if len (components ) > 1 :
35+ epoch = components [0 ]
36+ else :
37+ epoch = ''
38+
39+ # Arch is the last item after .
40+ arch = filename .rsplit ('.' )[- 1 ]
41+ remaining = filename .rsplit ('.%s' % arch )[0 ]
42+ release = remaining .rsplit ('-' )[- 1 ]
43+ version = remaining .rsplit ('-' )[- 2 ]
44+ name = '-' .join (remaining .rsplit ('-' )[:- 2 ])
45+
46+ return name , version , release , epoch , arch
47+
48+
49+ def string_to_version (verstring ):
50+ """
51+ Return a tuple of (epoch, version, release) from a version string
52+
53+ This function replaces rpmUtils.miscutils.stringToVersion, see
54+ https://bugzilla.redhat.com/1364504
55+ """
56+ # is there an epoch?
57+ components = verstring .split (':' )
58+ if len (components ) > 1 :
59+ epoch = components [0 ]
60+ else :
61+ epoch = 0
62+
63+ remaining = components [:2 ][0 ].split ('-' )
64+ version = remaining [0 ]
65+ release = remaining [1 ]
66+
67+ return (epoch , version , release )
68+
69+
1870def spec_fn (spec_dir = '.' ):
1971 specs = [f for f in os .listdir (spec_dir )
2072 if os .path .isfile (f ) and f .endswith ('.spec' )]
@@ -32,7 +84,8 @@ def get_patches_from_files(patches_dir='.'):
3284 return []
3385 patches = []
3486 for pfn in patches_fns :
35- txt = codecs .open (pfn , 'r' , encoding = 'utf-8' ).read ()
87+ with codecs .open (pfn , 'r' , encoding = 'utf-8' ) as fp :
88+ txt = fp .read ()
3689 hash = None
3790 m = re .search (r'^From ([a-z0-9]+)' , txt , flags = re .M )
3891 if m :
@@ -84,8 +137,8 @@ def has_macros(s):
84137def nvrcmp (nvr1 , nvr2 ):
85138 if not RPM_AVAILABLE :
86139 raise exception .RpmModuleNotAvailable ()
87- t1 = rpmUtils . miscutils . stringToVersion (nvr1 )
88- t2 = rpmUtils . miscutils . stringToVersion (nvr2 )
140+ t1 = string_to_version (nvr1 )
141+ t2 = string_to_version (nvr2 )
89142 return rpm .labelCompare (t1 , t2 )
90143
91144
@@ -98,9 +151,7 @@ def vcmp(v1, v2):
98151
99152
100153def nvr2version (nvr ):
101- if not RPM_AVAILABLE :
102- raise exception .RpmModuleNotAvailable ()
103- _ , v , _ , _ , _ = rpmUtils .miscutils .splitFilename (nvr )
154+ _ , v , _ , _ , _ = split_filename (nvr )
104155 return v
105156
106157
@@ -130,7 +181,8 @@ def fn(self):
130181 @property
131182 def txt (self ):
132183 if not self ._txt :
133- self ._txt = codecs .open (self .fn , 'r' , encoding = 'utf-8' ).read ()
184+ with codecs .open (self .fn , 'r' , encoding = 'utf-8' ) as fp :
185+ self ._txt = fp .read ()
134186 return self ._txt
135187
136188 @property
@@ -548,7 +600,7 @@ def get_last_changelog_entry(self, strip=False):
548600 entry = entries [0 ]
549601 lines = entry .split ("\n " )
550602 if strip :
551- lines = map (lambda x : x .lstrip (" -*\t " ), lines )
603+ lines = list ( map (lambda x : x .lstrip (" -*\t " ), lines ) )
552604 return lines [0 ], lines [1 :]
553605
554606 def get_requires (self , versions_as_string = False , remove_epoch = True ):
0 commit comments