From: Mathieu Bridon <bochecha(a)daitauha.fr>
Not all rpkg-based tools will be migrating to the new sources file
format at the same time, if ever.
For example, if rpkg was updated only in Rawhide, then a package
maintainer who uses Rawhide would commit sources file in the new format,
and another maintainer (or Koji!) on an older Fedora version would not
be able to read the new file.
So we would need to upgrade rpkg in all versions of Fedora at the same
time.
But then all Fedora downstreams would also need to have the new rpkg at
the same time, or at least before maintainers start pushing new-format
files.
This kind of lock-in can't happen at the rpkg level.
Instead, this new patch:
* makes rpkg able to write files in either format
* makes rpkg write files in the old format by default (to preserve
compatibility)
* allows rpkg-based tools to specify whether they want to use the new
format.
This means we can upgrade rpkg more easily everywhere, taking our time.
Eventually, Fedora can decide to move to the new format simply by
overriding pyrpkg.Commands.source_entry_type to 'bsd' in fedpkg.Commands
---
src/pyrpkg/__init__.py | 7 +++++--
src/pyrpkg/sources.py | 20 +++++++++++-------
test/test_sources.py | 55 +++++++++++++++++++++++++++++++++-----------------
3 files changed, 54 insertions(+), 28 deletions(-)
diff --git a/src/pyrpkg/__init__.py b/src/pyrpkg/__init__.py
index a5f02ea..60c1a6e 100644
--- a/src/pyrpkg/__init__.py
+++ b/src/pyrpkg/__init__.py
@@ -158,6 +158,8 @@ class Commands(object):
self._branch_remote = None
# Name of default remote to be used for new clone
self.default_branch_remote = 'origin'
+ # Default sources file output format type
+ self.source_entry_type = 'old'
# Define properties here
# Properties allow us to "lazy load" various attributes, which also means
@@ -1569,7 +1571,7 @@ class Commands(object):
if not outdir:
outdir = self.path
- sourcesf = SourcesFile(self.sources_filename)
+ sourcesf = SourcesFile(self.sources_filename, self.source_entry_type)
for entry in sourcesf.entries:
# See if we already have a valid copy downloaded
@@ -2238,7 +2240,8 @@ class Commands(object):
oldpath = os.getcwd()
os.chdir(self.path)
- sourcesf = SourcesFile(self.sources_filename, replace=replace)
+ sourcesf = SourcesFile(self.sources_filename, self.source_entry_type,
+ replace=replace)
# Will add new sources to .gitignore if they are not already there.
gitignore = GitIgnore(os.path.join(self.path, '.gitignore'))
diff --git a/src/pyrpkg/sources.py b/src/pyrpkg/sources.py
index ea49ea6..a3ff1b1 100644
--- a/src/pyrpkg/sources.py
+++ b/src/pyrpkg/sources.py
@@ -37,8 +37,10 @@ class MalformedLineError(Exception):
class SourcesFile(object):
- def __init__(self, sourcesfile, replace=False):
+ def __init__(self, sourcesfile, entry_type, replace=False):
self.sourcesfile = sourcesfile
+ self.entry_type = {'old': SourceFileEntry,
+ 'bsd': BSDSourceFileEntry}[entry_type]
self.entries = []
if not replace:
@@ -60,7 +62,7 @@ class SourcesFile(object):
m = LINE_PATTERN.match(stripped)
if m is not None:
- return SourceFileEntry(m.group('hashtype'), m.group('file'),
+ return self.entry_type(m.group('hashtype'), m.group('file'),
m.group('hash'))
# Try falling back on the old format
@@ -70,10 +72,10 @@ class SourcesFile(object):
except ValueError:
raise MalformedLineError(line)
- return SourceFileEntry('md5', file, hash)
+ return self.entry_type('md5', file, hash)
def add_entry(self, hashtype, file, hash):
- entry = SourceFileEntry(hashtype, file, hash)
+ entry = self.entry_type(hashtype, file, hash)
for e in self.entries:
if entry.hashtype != e.hashtype:
@@ -97,10 +99,14 @@ class SourceFileEntry(object):
self.file = file
def __str__(self):
- return '%s (%s) = %s\n' % (self.hashtype.upper(), self.file,
- self.hash)
-
+ return '%s %s\n' % (self.hash, self.file)
def __eq__(self, other):
return ((self.hashtype, self.hash, self.file) ==
(other.hashtype, other.hash, other.file))
+
+
+class BSDSourceFileEntry(SourceFileEntry):
+ def __str__(self):
+ return '%s (%s) = %s\n' % (self.hashtype.upper(), self.file,
+ self.hash)
diff --git a/test/test_sources.py b/test/test_sources.py
index 13f7c06..576bd2c 100644
--- a/test/test_sources.py
+++ b/test/test_sources.py
@@ -14,6 +14,11 @@ sys.path = old_path
class SourceFileEntryTestCase(unittest.TestCase):
def test_entry(self):
e = sources.SourceFileEntry('md5', 'afile', 'ahash')
+ expected = 'ahash afile\n'
+ self.assertEqual(str(e), expected)
+
+ def test_bsd_style_entry(self):
+ e = sources.BSDSourceFileEntry('md5', 'afile', 'ahash')
expected = 'MD5 (afile) = ahash\n'
self.assertEqual(str(e), expected)
@@ -27,22 +32,34 @@ class SourcesFileTestCase(unittest.TestCase):
shutil.rmtree(self.workdir)
def test_parse_empty_line(self):
- s = sources.SourcesFile(self.sourcesfile)
+ s = sources.SourcesFile(self.sourcesfile, 'bsd')
entry = s.parse_line('')
self.assertIsNone(entry)
def test_parse_eol_line(self):
- s = sources.SourcesFile(self.sourcesfile)
+ s = sources.SourcesFile(self.sourcesfile, 'bsd')
entry = s.parse_line('\n')
self.assertIsNone(entry)
def test_parse_whitespace_line(self):
- s = sources.SourcesFile(self.sourcesfile)
+ s = sources.SourcesFile(self.sourcesfile, 'bsd')
entry = s.parse_line(' \n')
self.assertIsNone(entry)
def test_parse_old_style_line(self):
- s = sources.SourcesFile(self.sourcesfile)
+ s = sources.SourcesFile(self.sourcesfile, 'old')
+
+ line = 'ahash afile\n'
+ entry = s.parse_line(line)
+
+ self.assertTrue(isinstance(entry, sources.SourceFileEntry))
+ self.assertEqual(entry.hashtype, 'md5')
+ self.assertEqual(entry.hash, 'ahash')
+ self.assertEqual(entry.file, 'afile')
+ self.assertEqual(str(entry), line)
+
+ def test_migrate_old_style_line(self):
+ s = sources.SourcesFile(self.sourcesfile, 'bsd')
line = 'ahash afile\n'
newline = 'MD5 (afile) = ahash\n'
@@ -55,7 +72,7 @@ class SourcesFileTestCase(unittest.TestCase):
self.assertEqual(str(entry), newline)
def test_parse_entry_line(self):
- s = sources.SourcesFile(self.sourcesfile)
+ s = sources.SourcesFile(self.sourcesfile, 'bsd')
line = 'MD5 (afile) = ahash\n'
entry = s.parse_line(line)
@@ -67,7 +84,7 @@ class SourcesFileTestCase(unittest.TestCase):
self.assertEqual(str(entry), line)
def test_parse_wrong_lines(self):
- s = sources.SourcesFile(self.sourcesfile)
+ s = sources.SourcesFile(self.sourcesfile, 'bsd')
lines = ['ahash',
'ahash ',
@@ -81,12 +98,12 @@ class SourcesFileTestCase(unittest.TestCase):
s.parse_line(line)
def test_open_new_file(self):
- s = sources.SourcesFile(self.sourcesfile)
+ s = sources.SourcesFile(self.sourcesfile, 'bsd')
self.assertEqual(len(s.entries), 0)
def test_open_empty_file(self):
open(self.sourcesfile, 'w').write('')
- s = sources.SourcesFile(self.sourcesfile)
+ s = sources.SourcesFile(self.sourcesfile, 'bsd')
self.assertEqual(len(s.entries), 0)
def test_open_existing_file_with_old_style_lines(self):
@@ -98,7 +115,7 @@ class SourcesFileTestCase(unittest.TestCase):
for line in lines:
f.write(line)
- s = sources.SourcesFile(self.sourcesfile)
+ s = sources.SourcesFile(self.sourcesfile, 'bsd')
for i, entry in enumerate(s.entries):
self.assertTrue(isinstance(entry, sources.SourceFileEntry))
@@ -111,7 +128,7 @@ class SourcesFileTestCase(unittest.TestCase):
for line in lines:
f.write(line)
- s = sources.SourcesFile(self.sourcesfile)
+ s = sources.SourcesFile(self.sourcesfile, 'bsd')
for i, entry in enumerate(s.entries):
self.assertTrue(isinstance(entry, sources.SourceFileEntry))
@@ -132,7 +149,7 @@ class SourcesFileTestCase(unittest.TestCase):
for line in lines:
f.write(line)
- s = sources.SourcesFile(self.sourcesfile)
+ s = sources.SourcesFile(self.sourcesfile, 'bsd')
for i, entry in enumerate(s.entries):
self.assertTrue(isinstance(entry, sources.SourceFileEntry))
@@ -147,7 +164,7 @@ class SourcesFileTestCase(unittest.TestCase):
for line in lines:
f.write(line)
- s = sources.SourcesFile(self.sourcesfile)
+ s = sources.SourcesFile(self.sourcesfile, 'bsd')
self.assertEqual(len(s.entries), 1)
self.assertEqual(s.entries[0].hashtype, 'md5')
@@ -162,10 +179,10 @@ class SourcesFileTestCase(unittest.TestCase):
f.write(line)
with self.assertRaises(sources.MalformedLineError):
- return sources.SourcesFile(self.sourcesfile)
+ return sources.SourcesFile(self.sourcesfile, 'bsd')
def test_add_entry(self):
- s = sources.SourcesFile(self.sourcesfile)
+ s = sources.SourcesFile(self.sourcesfile, 'bsd')
self.assertEqual(len(s.entries), 0)
s.add_entry('md5', 'afile', 'ahash')
@@ -177,7 +194,7 @@ class SourcesFileTestCase(unittest.TestCase):
self.assertEqual(str(s.entries[-1]), 'MD5 (anotherfile) =
anotherhash\n')
def test_add_entry_twice(self):
- s = sources.SourcesFile(self.sourcesfile)
+ s = sources.SourcesFile(self.sourcesfile, 'bsd')
self.assertEqual(len(s.entries), 0)
s.add_entry('md5', 'afile', 'ahash')
@@ -188,7 +205,7 @@ class SourcesFileTestCase(unittest.TestCase):
self.assertEqual(len(s.entries), 1)
def test_add_entry_mixing_hashtypes(self):
- s = sources.SourcesFile(self.sourcesfile)
+ s = sources.SourcesFile(self.sourcesfile, 'bsd')
self.assertEqual(len(s.entries), 0)
s.add_entry('md5', 'afile', 'ahash')
@@ -199,7 +216,7 @@ class SourcesFileTestCase(unittest.TestCase):
s.add_entry('sha512', 'anotherfile', 'anotherhash')
def test_write_new_file(self):
- s = sources.SourcesFile(self.sourcesfile)
+ s = sources.SourcesFile(self.sourcesfile, 'bsd')
self.assertEqual(len(s.entries), 0)
s.add_entry('md5', 'afile', 'ahash')
@@ -220,7 +237,7 @@ class SourcesFileTestCase(unittest.TestCase):
for line in lines:
f.write(line)
- s = sources.SourcesFile(self.sourcesfile)
+ s = sources.SourcesFile(self.sourcesfile, 'bsd')
s.add_entry('md5', 'thirdfile', 'thirdhash')
s.write()
@@ -239,7 +256,7 @@ class SourcesFileTestCase(unittest.TestCase):
for line in lines:
f.write(line)
- s = sources.SourcesFile(self.sourcesfile, replace=True)
+ s = sources.SourcesFile(self.sourcesfile, 'bsd', replace=True)
s.add_entry('md5', 'thirdfile', 'thirdhash')
s.write()
--
2.1.0