reverse slug and uuid order in filenames

Change-Id: I7346f6bd9965db3680143c0f5bdd0448d12e05eb
This commit is contained in:
Doug Hellmann 2015-10-01 21:14:50 +00:00
parent 64763db3f6
commit d1aaff7bc7
7 changed files with 101 additions and 15 deletions

View File

@ -0,0 +1,9 @@
---
upgrade:
- |
Change the order of the slug and UUID value in the note filename so
the slug comes before the UUIUD to make tab completion easier to
use.
Older files are still supported, and can be renamed to use
the new style.

View File

@ -43,7 +43,7 @@ def _pick_note_file_name(notesdir, slug):
"Pick a unique name in notesdir."
for i in range(50):
newid = utils.get_random_string()
notefilename = os.path.join(notesdir, '%s-%s.yaml' % (newid, slug))
notefilename = os.path.join(notesdir, '%s-%s.yaml' % (slug, newid))
if not os.path.exists(notefilename):
return notefilename
else:

View File

@ -62,6 +62,17 @@ def _file_exists_at_commit(reporoot, filename, sha):
return bool(get_file_at_commit(reporoot, filename, sha))
def _get_unique_id(filename):
base = os.path.basename(filename)
root, ext = os.path.splitext(base)
uniqueid = root[-16:]
if '-' in uniqueid:
# This is an older file with the UUID at the beginning
# of the name.
uniqueid = root[:16]
return uniqueid
# TODO(dhellmann): Add branch arg?
def get_notes_by_version(reporoot, notesdir, branch=None):
@ -81,9 +92,9 @@ def get_notes_by_version(reporoot, notesdir, branch=None):
current_version = _get_current_version(reporoot, branch)
# print('current_version = %s' % current_version)
# Remember the most current filename for each prefix, to allow for
# Remember the most current filename for each id, to allow for
# renames.
last_name_by_prefix = {}
last_name_by_id = {}
# FIXME(dhellmann): This might need to be more line-oriented for
# longer histories.
@ -119,23 +130,22 @@ def get_notes_by_version(reporoot, notesdir, branch=None):
if current_version not in versions:
versions.append(current_version)
# Remember the files seen, using their prefix as a unique id.
# Remember the files seen, using their UUID suffix as a unique id.
for f in filenames:
# Updated as older tags are found, handling edits to release
# notes.
prefix = os.path.basename(f)[:16]
earliest_seen[prefix] = tags[0]
if prefix in last_name_by_prefix:
# We already have a filename for this prefix from a
uniqueid = _get_unique_id(f)
earliest_seen[uniqueid] = tags[0]
if uniqueid in last_name_by_id:
# We already have a filename for this id from a
# new commit, so use that one in case the name has
# changed.
continue
if _file_exists_at_commit(reporoot, f, sha):
# Remember this filename as the most recent version of
# the unique prefix we have seen, in case the name
# the unique id we have seen, in case the name
# changed from an older commit.
last_name_by_prefix[prefix] = (f, sha)
# print('remembering %s as last name for %s' % (f, prefix))
last_name_by_id[uniqueid] = (f, sha)
# Invert earliest_seen to make a list of notes files for each
# version.
@ -144,8 +154,8 @@ def get_notes_by_version(reporoot, notesdir, branch=None):
files_and_tags[v] = []
# Produce a list of the actual files present in the repository. If
# a note is removed, this step should let us ignore it.
for prefix, version in earliest_seen.items():
base, sha = last_name_by_prefix[prefix]
for uniqueid, version in earliest_seen.items():
base, sha = last_name_by_id[uniqueid]
files_and_tags[version].append((base, sha))
for version, filenames in files_and_tags.items():
files_and_tags[version] = list(reversed(filenames))

View File

@ -128,9 +128,12 @@ class Base(base.TestCase):
self._run_git('add', '.')
self._run_git('commit', '-m', message)
def _add_notes_file(self, slug='slug', commit=True):
def _add_notes_file(self, slug='slug', commit=True, legacy=False):
n = self.get_note_num()
basename = '%016x-%s.yaml' % (n, slug)
if legacy:
basename = '%016x-%s.yaml' % (n, slug)
else:
basename = '%s-%016x.yaml' % (slug, n)
filename = os.path.join(self.reporoot, 'releasenotes', 'notes',
basename)
create._make_note_file(filename)
@ -362,6 +365,70 @@ class BasicTest(Base):
results,
)
def test_legacy_file(self):
self._make_python_package()
self._run_git('tag', '-s', '-m', 'first tag', '1.0.0')
f1 = self._add_notes_file('slug1', legacy=True)
self._run_git('tag', '-s', '-m', 'first tag', '2.0.0')
f2 = f1.replace('slug1', 'slug2')
self._run_git('mv', f1, f2)
self._git_commit('rename note file')
raw_results = scanner.get_notes_by_version(
self.reporoot,
'releasenotes/notes',
)
results = {
k: [f for (f, n) in v]
for (k, v) in raw_results.items()
}
self.assertEqual(
{'2.0.0': [f2],
'2.0.0-1': [],
},
results,
)
def test_rename_legacy_file_to_new(self):
self._make_python_package()
self._run_git('tag', '-s', '-m', 'first tag', '1.0.0')
f1 = self._add_notes_file('slug1', legacy=True)
self._run_git('tag', '-s', '-m', 'first tag', '2.0.0')
# Rename the file with the new convention of placing the UUID
# after the slug instead of before.
f2 = f1.replace('0000000000000001-slug1',
'slug1-0000000000000001')
self._run_git('mv', f1, f2)
self._git_commit('rename note file')
raw_results = scanner.get_notes_by_version(
self.reporoot,
'releasenotes/notes',
)
results = {
k: [f for (f, n) in v]
for (k, v) in raw_results.items()
}
self.assertEqual(
{'2.0.0': [f2],
'2.0.0-1': [],
},
results,
)
class UniqueIdTest(Base):
def test_legacy(self):
uid = scanner._get_unique_id(
'releasenotes/notes/0000000000000001-slug1.yaml'
)
self.assertEqual('0000000000000001', uid)
def test_modern(self):
uid = scanner._get_unique_id(
'releasenotes/notes/slug1-0000000000000001.yaml'
)
self.assertEqual('0000000000000001', uid)
class BranchTest(Base):