add code to read lists of review ids

Change-Id: Ied5375b5646bddf6b1645dbbbbbb049a45d17c48
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
This commit is contained in:
Doug Hellmann 2018-04-29 14:25:02 -04:00
parent ab4cd7d192
commit d30ea5228e
3 changed files with 146 additions and 0 deletions

44
goal_tools/gerrit.py Normal file
View File

@ -0,0 +1,44 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import fileinput
import logging
import urllib.parse
LOG = logging.getLogger(__name__)
def parse_review_lists(filenames):
"""Generator that produces review IDs as strings.
Read the files expecting to find one review URL or ID per
line. Ignore lines that start with # as comments. Ignore blank
lines.
:param filenames: Iterable of filenames to read.
:return: Generator of str
"""
for line in fileinput.input(filenames):
line = line.strip()
if not line:
continue
if line.startswith('#'):
continue
LOG.debug('parsing %r', line)
parsed = urllib.parse.urlparse(line)
if parsed.fragment:
# https://review.openstack.org/#/c/561507/
yield parsed.fragment.lstrip('/c').partition('/')[0]
else:
# https://review.openstack.org/555353/
yield parsed.path.lstrip('/').partition('/')[0]

37
goal_tools/tests/base.py Normal file
View File

@ -0,0 +1,37 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import logging
import fixtures
import testtools
class TestCase(testtools.TestCase):
def setUp(self):
super().setUp()
self.useFixture(fixtures.NestedTempfile())
self.tmpdir = self.useFixture(fixtures.TempDir()).path
self._stdout_fixture = fixtures.StringStream('stdout')
self.stdout = self.useFixture(self._stdout_fixture).stream
self.useFixture(fixtures.MonkeyPatch('sys.stdout', self.stdout))
self._stderr_fixture = fixtures.StringStream('stderr')
self.stderr = self.useFixture(self._stderr_fixture).stream
self.useFixture(fixtures.MonkeyPatch('sys.stderr', self.stderr))
self.logger = self.useFixture(
fixtures.FakeLogger(
format='%(name)s: %(message)s',
level=logging.DEBUG,
nuke_handlers=True,
)
)

View File

@ -0,0 +1,65 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os.path
import textwrap
from goal_tools import gerrit
from goal_tools.tests import base
class TestParseReviewLists(base.TestCase):
def setUp(self):
super().setUp()
self.comments_name = os.path.join(self.tmpdir, 'comments.txt')
self._write_file(
self.comments_name,
textwrap.dedent('''
# this line is ignored
''')
)
self.url_name = os.path.join(self.tmpdir, 'url.txt')
self._write_file(
self.url_name,
textwrap.dedent('''
https://review.openstack.org/#/c/561507/
https://review.openstack.org/555353/
''')
)
self.ids_name = os.path.join(self.tmpdir, 'ids.txt')
self._write_file(
self.ids_name,
textwrap.dedent('''
561507
555353/
''')
)
def _write_file(self, filename, contents):
with open(filename, 'w', encoding='utf-8') as f:
f.write(contents)
def test_ignore_comments(self):
expected = []
actual = list(gerrit.parse_review_lists([self.comments_name]))
self.assertEqual(expected, actual)
def test_parse_urls(self):
expected = ['561507', '555353']
actual = list(gerrit.parse_review_lists([self.url_name]))
self.assertEqual(expected, actual)
def test_parse_ids(self):
expected = ['561507', '555353']
actual = list(gerrit.parse_review_lists([self.ids_name]))
self.assertEqual(expected, actual)