Skip to content

Commit

Permalink
Merge pull request #84 from masayukig/use-context-managers
Browse files Browse the repository at this point in the history
Use context managers for file open()
  • Loading branch information
mtreinish committed Aug 10, 2017
2 parents 65ebea3 + 02a4e5f commit 6aaf91f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 51 deletions.
10 changes: 2 additions & 8 deletions stestr/repository/file.py
Expand Up @@ -41,11 +41,8 @@ def initialise(klass, url):
"""Create a repository at url/path."""
base = os.path.join(os.path.expanduser(url), '.stestr')
os.mkdir(base)
stream = open(os.path.join(base, 'format'), 'wt')
try:
with open(os.path.join(base, 'format'), 'wt') as stream:
stream.write('1\n')
finally:
stream.close()
result = Repository(base)
result._write_next_stream(0)
return result
Expand Down Expand Up @@ -166,11 +163,8 @@ def _write_next_stream(self, value):
# term. Likewise we don't fsync - this data isn't valuable enough to
# force disk IO.
prefix = self._path('next-stream')
stream = open(prefix + '.new', 'wt')
try:
with open(prefix + '.new', 'wt') as stream:
stream.write('%d\n' % value)
finally:
stream.close()
atomicish_rename(prefix + '.new', prefix)


Expand Down
31 changes: 16 additions & 15 deletions stestr/selection.py
Expand Up @@ -10,6 +10,7 @@
# License for the specific language governing permissions and limitations
# under the License.

import contextlib
import re


Expand All @@ -36,21 +37,21 @@ def include(test_id):


def black_reader(blacklist_file):
black_file = open(blacklist_file, 'r')
regex_comment_lst = [] # tuple of (regex_compiled, msg, skipped_lst)
for line in black_file:
raw_line = line.strip()
split_line = raw_line.split('#')
# Before the # is the regex
line_regex = split_line[0].strip()
if len(split_line) > 1:
# After the # is a comment
comment = ''.join(split_line[1:]).strip()
else:
comment = 'Skipped because of regex %s:' % line_regex
if not line_regex:
continue
regex_comment_lst.append((re.compile(line_regex), comment, []))
with contextlib.closing(open(blacklist_file, 'r')) as black_file:
regex_comment_lst = [] # tuple of (regex_compiled, msg, skipped_lst)
for line in black_file:
raw_line = line.strip()
split_line = raw_line.split('#')
# Before the # is the regex
line_regex = split_line[0].strip()
if len(split_line) > 1:
# After the # is a comment
comment = ''.join(split_line[1:]).strip()
else:
comment = 'Skipped because of regex %s:' % line_regex
if not line_regex:
continue
regex_comment_lst.append((re.compile(line_regex), comment, []))
return regex_comment_lst


Expand Down
6 changes: 3 additions & 3 deletions stestr/test_processor.py
Expand Up @@ -170,9 +170,9 @@ def make_listfile(self):
else:
fd, name = tempfile.mkstemp()
stream = os.fdopen(fd, 'wb')
self.list_file_name = name
testlist.write_list(stream, self.test_ids)
stream.close()
with stream:
self.list_file_name = name
testlist.write_list(stream, self.test_ids)
except Exception:
if name:
os.unlink(name)
Expand Down
10 changes: 2 additions & 8 deletions stestr/tests/repository/test_file.py
Expand Up @@ -63,17 +63,11 @@ def test_initialise(self):
base = os.path.join(self.tempdir, '.stestr')
self.assertTrue(os.path.isdir(base))
self.assertTrue(os.path.isfile(os.path.join(base, 'format')))
stream = open(os.path.join(base, 'format'), 'rt')
try:
with open(os.path.join(base, 'format'), 'rt') as stream:
contents = stream.read()
finally:
stream.close()
self.assertEqual("1\n", contents)
stream = open(os.path.join(base, 'next-stream'), 'rt')
try:
with open(os.path.join(base, 'next-stream'), 'rt') as stream:
contents = stream.read()
finally:
stream.close()
self.assertEqual("0\n", contents)

# Skip if windows since ~ in a path doesn't work there
Expand Down
2 changes: 0 additions & 2 deletions stestr/tests/test_return_codes.py
Expand Up @@ -23,8 +23,6 @@

from stestr.tests import base

DEVNULL = open(os.devnull, 'wb')


class TestReturnCodes(base.TestCase):
def setUp(self):
Expand Down
30 changes: 15 additions & 15 deletions tools/testr_to_stestr.py
Expand Up @@ -21,14 +21,15 @@
print("Testr config file not found")
sys.exit(1)

testr_conf_file = open('.testr.conf', 'r')
config = six.moves.configparser.ConfigParser()
config.readfp(testr_conf_file)
with open('.testr.conf', 'r') as testr_conf_file:
config = six.moves.configparser.ConfigParser()
config.readfp(testr_conf_file)

test_command = config.get('DEFAULT', 'test_command')
group_regex = None
if config.has_option('DEFAULT', 'group_regex'):
group_regex = config.get('DEFAULT', 'group_regex')

test_command = config.get('DEFAULT', 'test_command')
group_regex = None
if config.has_option('DEFAULT', 'group_regex'):
group_regex = config.get('DEFAULT', 'group_regex')
top_dir = None
test_dir = None
for line in test_command.split('\n'):
Expand All @@ -44,11 +45,10 @@
if val == 'discover':
test_dir = command_parts[idx + 2]

stestr_conf_file = open('.stestr.conf', 'w')
stestr_conf_file.write('[DEFAULT]\n')
stestr_conf_file.write('test_path=%s\n' % test_dir)
if top_dir:
stestr_conf_file.write('top_dir=%s\n' % top_dir)
if group_regex:
stestr_conf_file.write('group_regex=%s\n' % group_regex)
stestr_conf_file.close()
with open('.stestr.conf', 'w') as stestr_conf_file:
stestr_conf_file.write('[DEFAULT]\n')
stestr_conf_file.write('test_path=%s\n' % test_dir)
if top_dir:
stestr_conf_file.write('top_dir=%s\n' % top_dir)
if group_regex:
stestr_conf_file.write('group_regex=%s\n' % group_regex)

0 comments on commit 6aaf91f

Please sign in to comment.