Exemple #1
0
    def _update_changelog(self, new_version):
        """
        Update the changelog with the new version.
        """
        # Not thrilled about having to re-read the file here but we need to
        # check for the changelog entry before making any modifications, then
        # bump the version, then update the changelog.
        f = open(self.changes_file, 'r')
        buf = StringIO()
        found_match = False
        done = False
        empty_line_regex = re.compile('^\s*$')

        for line in f.readlines():
            if not done and not found_match and self.changelog_regex.match(line):
                buf.write(line)
                found_match = True
            elif not done and found_match and empty_line_regex.match(line):
                buf.write("\n- version %s\n" % new_version)
                done = True
            else:
                buf.write(line)
        f.close()

        # Write out the new file contents with our modified changelog entry:
        f = open(self.changes_file, 'w')
        f.write(buf.getvalue())
        f.close()
        buf.close()
Exemple #2
0
 def setUp(self):
     self.out = StringIO()
     self.tarfixer = TarFixer(None, self.out, EXPECTED_TIMESTAMP,
                              EXPECTED_REF)
     self.test_file = os.path.join(os.path.dirname(__file__), 'resources',
                                   'archive.tar')
     self.reference_file = os.path.join(os.path.dirname(__file__),
                                        'resources', 'archive-fixed.tar')
     self.reference_hash = self.hash_file(self.reference_file)
Exemple #3
0
def open_mock(content, **kwargs):
    """Mock's mock_open only supports read() and write() which is not very useful.
    This context manager adds support for getting the value of what was written out
    and for iterating through a file line by line."""

    global file_spec
    if file_spec is None:
        # set on first use
        if PY2:
            file_spec = file
        else:
            import _io
            file_spec = list(set(dir(_io.TextIOWrapper)).union(set(dir(_io.BytesIO))))

    m = MagicMock(name='open', spec=open)

    handle = MagicMock(spec=file_spec)
    handle.__enter__.return_value = handle
    m.return_value = handle

    content_out = StringIO()

    if PY2:
        patch_module = "__builtin__.open"
    else:
        patch_module = "builtins.open"
    with patch(patch_module, m, create=True, **kwargs) as mo:
        stream = StringIO(content)
        rv = mo.return_value
        rv.write = lambda x: content_out.write(bytes(x, "utf-8"))
        rv.content_out = lambda: content_out.getvalue()
        rv.__iter__.return_value = iter(stream.readlines())
        rv.read.return_value = stream.read()
        yield rv
Exemple #4
0
    class Tee(object):
        def __init__(self, stream, silent):
            self.buf = StringIO()
            self.stream = stream
            self.silent = silent

        def write(self, data):
            self.buf.write(data)
            if not self.silent:
                self.stream.write(data)

        def getvalue(self):
            return self.buf.getvalue()

        def isatty(self):
            return False
Exemple #5
0
    def _update_setup_py(self, new_version):
        """
        If this project has a setup.py, attempt to update it's version.
        """
        self._update_version_file(new_version)

        setup_file = os.path.join(self.full_project_dir, "setup.py")
        if not os.path.exists(setup_file):
            return

        debug("Found setup.py, attempting to update version.")

        # We probably don't want version-release in setup.py as release is
        # an rpm concept. Hopefully this assumption on
        py_new_version = new_version.split('-')[0]

        f = open(setup_file, 'r')
        buf = StringIO()
        for line in f.readlines():
            buf.write(replace_version(line, py_new_version))
        f.close()

        # Write out the new setup.py file contents:
        f = open(setup_file, 'w')
        f.write(buf.getvalue())
        f.close()
        buf.close()

        run_command("git add %s" % setup_file)
Exemple #6
0
    def _update_setup_py(self, new_version):
        """
        If this project has a setup.py, attempt to update it's version.
        """
        self._update_version_file(new_version)

        setup_file = os.path.join(self.full_project_dir, "setup.py")
        if not os.path.exists(setup_file):
            return

        debug("Found setup.py, attempting to update version.")

        # We probably don't want version-release in setup.py as release is
        # an rpm concept. Hopefully this assumption on
        py_new_version = new_version.split('-')[0]

        f = open(setup_file, 'r')
        buf = StringIO()
        for line in f.readlines():
            buf.write(replace_version(line, py_new_version))
        f.close()

        # Write out the new setup.py file contents:
        f = open(setup_file, 'w')
        f.write(buf.getvalue())
        f.close()
        buf.close()

        run_command("git add %s" % setup_file)
Exemple #7
0
    def test_conformance(self):
        tests = [
            # http://pep8.readthedocs.org/en/latest/intro.html#error-codes
            'E101',  # indentation contains mixed spaces and tabs
            'E111',  # indentation is not a multiple of four
            'E112',  # expected an indented block
            'E113',  # unexpected indentation
            'E121',  # continuation line indentation is not a multiple of four
            'E122',  # continuation line missing indentation or outdented
            'E126',  # continuation line over-indented for hanging indent
            'E2',    # whitespace errors
            'E3',    # blank line errors
            'E4',    # import errors
            'E502',  # the backslash is redundant between brackets
            'E9',    # runtime errors (SyntaxError, IndentationError, IOError)
            'W1',    # indentation warnings
            'W2',    # whitespace warnings
            'W3',    # blank line warnings

            # @FIXME we currently have a lot of these errors introduced to our
            # codebase. Let's temporarily disable the check, so we can get travis
            # working again.
            # 'E7',    # statement errors
            # 'W6',    # deprecated features
        ]

        try:
            checker = pep8.StyleGuide(select=tests, paths=[REPO_DIR], reporter=pep8.StandardReport)
            # Unfortunatelly, IMHO the most interesting information
            # `checker.check_files()` prints to the STDOUT and doesn't provide
            # API to get it - the list of _bad_ files. Let's just read it from
            # the output
            with StringIO() as buf, redirect_stdout(buf):
                report = checker.check_files()
                result = report.total_errors
                output = buf.getvalue()

        except AttributeError:
            # We don't have pep8.StyleGuide, so we must be
            # using pep8 older than git tag 1.1-72-gf20d656.
            os.chdir(REPO_DIR)
            checks = ','.join(tests)
            cmd = "pep8 --select=%s %s | wc -l" % (checks, '.')
            result = int(getoutput(cmd))
            output = getoutput("pep8 --select %s %s" % (checks, '.'))

        if result != 0:
            self.fail("Found PEP8 errors that may break your code in Python 3:\n%s" % output)
Exemple #8
0
    def _update_changelog(self, new_version):
        """
        Update the changelog with the new version.
        """
        # Not thrilled about having to re-read the file here but we need to
        # check for the changelog entry before making any modifications, then
        # bump the version, then update the changelog.
        f = open(self.spec_file, 'r')
        buf = StringIO()
        found_match = False
        for line in f.readlines():
            match = self.changelog_regex.match(line)
            if match and not found_match:
                buf.write("%s %s\n" % (match.group(), new_version))
                found_match = True
            else:
                buf.write(line)
        f.close()

        # Write out the new file contents with our modified changelog entry:
        f = open(self.spec_file, 'w')
        f.write(buf.getvalue())
        f.close()
        buf.close()
Exemple #9
0
    def _update_changelog(self, new_version):
        """
        Update the changelog with the new version.
        """
        # Not thrilled about having to re-read the file here but we need to
        # check for the changelog entry before making any modifications, then
        # bump the version, then update the changelog.
        f = open(self.spec_file, 'r')
        buf = StringIO()
        found_match = False
        for line in f.readlines():
            match = self.changelog_regex.match(line)
            if match and not found_match:
                buf.write("%s %s\n" % (match.group(), new_version))
                found_match = True
            else:
                buf.write(line)
        f.close()

        # Write out the new file contents with our modified changelog entry:
        f = open(self.spec_file, 'w')
        f.write(buf.getvalue())
        f.close()
        buf.close()
Exemple #10
0
 def __init__(self, stream, silent):
     self.buf = StringIO()
     self.stream = stream
     self.silent = silent
Exemple #11
0
 def test_colors(self, mock_user_conf):
     mock_user_conf.return_value = {}
     stream = StringIO()
     _out('Hello world', None, Terminal().red, stream)
     # RHEL 6 doesn't have self.assertRegexpMatches unfortunately
     self.assertTrue(re.match('.+Hello world.+\n', stream.getvalue()))
Exemple #12
0
 def test_turn_off_colors(self, mock_user_conf):
     mock_user_conf.return_value = {'COLOR': '0'}
     stream = StringIO()
     _out('Hello world', None, Terminal().red, stream)
     self.assertEquals('Hello world\n', stream.getvalue())
Exemple #13
0
class TarTest(unittest.TestCase):
    def setUp(self):
        self.out = StringIO()
        self.tarfixer = TarFixer(None, self.out, EXPECTED_TIMESTAMP, EXPECTED_REF)
        self.test_file = os.path.join(os.path.dirname(__file__), 'resources', 'archive.tar')
        self.reference_file = os.path.join(os.path.dirname(__file__), 'resources', 'archive-fixed.tar')
        self.reference_hash = self.hash_file(self.reference_file)

    def tearDown(self):
        self.out = None

    def hash_file(self, filename):
        file_bytes = open(filename, 'rb').read()
        return self.hash_buffer(file_bytes)

    def hash_buffer(self, buf):
        hasher = hashlib.sha256()
        hasher.update(buf)
        return hasher.hexdigest()

    def _irregular_reader(self, items):
        def item_read(read_length):
            try:
                item = items.pop(0)
            except IndexError:
                # If no more items, the buffer is empty and would return empty string
                return ''

            return item.read(read_length)

        mock_fh = Mock()
        mock_fh.read = Mock()
        mock_fh.read.side_effect = item_read

        return mock_fh

    def test_full_read(self):
        items = [StringIO("1" * 5), StringIO("1" * 2), StringIO("1" * 6)]
        self.tarfixer.fh = self._irregular_reader(items)
        self.assertEqual("1" * 10, self.tarfixer.full_read(10))

    def test_full_read_buffer_underflow(self):
        input = StringIO("1" * 9)
        self.tarfixer.fh = input
        self.assertRaises(IOError, self.tarfixer.full_read, 10)

    def test_full_read_eventual_buffer_underflow(self):
        items = [StringIO("1" * 5), StringIO("1" * 2), StringIO("1" * 2)]
        self.tarfixer.fh = self._irregular_reader(items)
        self.assertRaises(IOError, self.tarfixer.full_read, 10)

    def test_fix(self):
        self.fh = open(self.test_file, 'rb')
        self.tarfixer.fh = self.fh
        self.tarfixer.fix()
        self.assertEqual(self.reference_hash, self.hash_buffer(encode_bytes(self.out.getvalue(), "utf8")))

    def test_fix_fails_unless_file_in_binary_mode(self):
        self.fh = open(self.test_file, 'r')
        self.tarfixer.fh = self.fh
        self.assertRaises(IOError, self.tarfixer.fix)

    def test_padded_size_length_small(self):
        length = 10
        block_size = 512
        self.assertEqual(512, self.tarfixer.padded_size(length, block_size))

    def test_padded_size_length_spot_on(self):
        length = 512
        block_size = 512
        self.assertEqual(512, self.tarfixer.padded_size(length, block_size))

    def test_padded_size_length_over(self):
        length = 513
        block_size = 512
        self.assertEqual(1024, self.tarfixer.padded_size(length, block_size))

    def test_padded_size_length_long(self):
        length = 82607
        block_size = 512
        self.assertEqual(82944, self.tarfixer.padded_size(length, block_size))

    def test_create_extended_header(self):
        self.tarfixer.create_extended_header()
        header = self.out.getvalue()
        self.assertEqual(512, len(header))
        self.assertEqual("52 comment=%s\n" % EXPECTED_REF, header[:52])
        self.assertEqual("\x00" * (512 - 53), header[53:])

    def test_calculate_checksum(self):
        fields = {
            'a': '\x01',
            'b': '\x02',
            'c': '\x03',
            'd': '\x04',
        }
        self.tarfixer.struct_members = list(fields.keys()) + ['checksum']
        result = self.tarfixer.calculate_checksum(fields)
        expected_result = 10 + ord(" ") * 8
        self.assertEqual("%07o\x00" % expected_result, result)

    def test_encode_header(self):
        mode = 123
        chunk = {
            'mode': mode,
            'name': 'hello',
        }
        result = self.tarfixer.encode_header(chunk, ['mode', 'name'])
        expected_result = ["%07o\x00" % mode, "hello"]
        expected_result = list(map(lambda x: encode_bytes(x, "utf8"), expected_result))
        self.assertEqual(expected_result, result)
Exemple #14
0
 def setUp(self):
     self.out = StringIO()
     self.tarfixer = TarFixer(None, self.out, EXPECTED_TIMESTAMP, EXPECTED_REF)
     self.test_file = os.path.join(os.path.dirname(__file__), 'resources', 'archive.tar')
     self.reference_file = os.path.join(os.path.dirname(__file__), 'resources', 'archive-fixed.tar')
     self.reference_hash = self.hash_file(self.reference_file)
Exemple #15
0
    def _update_changelog(self, new_version):
        """
        Update the changelog with the new version.
        """
        # Not thrilled about having to re-read the file here but we need to
        # check for the changelog entry before making any modifications, then
        # bump the version, then update the changelog.
        f = open(self.changes_file, 'r')
        buf = StringIO()
        found_match = False
        done = False
        empty_line_regex = re.compile('^\s*$')

        for line in f.readlines():
            if not done and not found_match and self.changelog_regex.match(
                    line):
                buf.write(line)
                found_match = True
            elif not done and found_match and empty_line_regex.match(line):
                buf.write("\n- version %s\n" % new_version)
                done = True
            else:
                buf.write(line)
        f.close()

        # Write out the new file contents with our modified changelog entry:
        f = open(self.changes_file, 'w')
        f.write(buf.getvalue())
        f.close()
        buf.close()
Exemple #16
0
 def test_turn_off_colors(self, mock_user_conf):
     mock_user_conf.return_value = {'COLOR': '0'}
     stream = StringIO()
     _out('Hello world', None, Terminal().red, stream)
     self.assertEquals('Hello world\n', stream.getvalue())
Exemple #17
0
 def test_full_read_eventual_buffer_underflow(self):
     items = [StringIO("1" * 5), StringIO("1" * 2), StringIO("1" * 2)]
     self.tarfixer.fh = self._irregular_reader(items)
     self.assertRaises(IOError, self.tarfixer.full_read, 10)
Exemple #18
0
 def test_full_read_buffer_underflow(self):
     input = StringIO("1" * 9)
     self.tarfixer.fh = input
     self.assertRaises(IOError, self.tarfixer.full_read, 10)
Exemple #19
0
 def test_full_read(self):
     items = [StringIO("1" * 5), StringIO("1" * 2), StringIO("1" * 6)]
     self.tarfixer.fh = self._irregular_reader(items)
     self.assertEqual("1" * 10, self.tarfixer.full_read(10))
Exemple #20
0
 def test_colors(self, mock_user_conf):
     mock_user_conf.return_value = {}
     stream = StringIO()
     _out('Hello world', None, Terminal().red, stream)
     # RHEL 6 doesn't have self.assertRegexpMatches unfortunately
     self.assertTrue(re.match('.+Hello world.+\n', stream.getvalue()))
Exemple #21
0
class TarTest(unittest.TestCase):
    def setUp(self):
        self.out = StringIO()
        self.tarfixer = TarFixer(None, self.out, EXPECTED_TIMESTAMP,
                                 EXPECTED_REF)
        self.test_file = os.path.join(os.path.dirname(__file__), 'resources',
                                      'archive.tar')
        self.reference_file = os.path.join(os.path.dirname(__file__),
                                           'resources', 'archive-fixed.tar')
        self.reference_hash = self.hash_file(self.reference_file)

    def tearDown(self):
        self.out = None

    def hash_file(self, filename):
        file_bytes = open(filename, 'rb').read()
        return self.hash_buffer(file_bytes)

    def hash_buffer(self, buf):
        hasher = hashlib.sha256()
        hasher.update(buf)
        return hasher.hexdigest()

    def _irregular_reader(self, items):
        def item_read(read_length):
            try:
                item = items.pop(0)
            except IndexError:
                # If no more items, the buffer is empty and would return empty string
                return ''

            return item.read(read_length)

        mock_fh = Mock()
        mock_fh.read = Mock()
        mock_fh.read.side_effect = item_read

        return mock_fh

    def test_full_read(self):
        items = [StringIO("1" * 5), StringIO("1" * 2), StringIO("1" * 6)]
        self.tarfixer.fh = self._irregular_reader(items)
        self.assertEqual("1" * 10, self.tarfixer.full_read(10))

    def test_full_read_buffer_underflow(self):
        input = StringIO("1" * 9)
        self.tarfixer.fh = input
        self.assertRaises(IOError, self.tarfixer.full_read, 10)

    def test_full_read_eventual_buffer_underflow(self):
        items = [StringIO("1" * 5), StringIO("1" * 2), StringIO("1" * 2)]
        self.tarfixer.fh = self._irregular_reader(items)
        self.assertRaises(IOError, self.tarfixer.full_read, 10)

    def test_fix(self):
        self.fh = open(self.test_file, 'rb')
        self.tarfixer.fh = self.fh
        self.tarfixer.fix()
        self.assertEqual(
            self.reference_hash,
            self.hash_buffer(encode_bytes(self.out.getvalue(), "utf8")))

    def test_fix_fails_unless_file_in_binary_mode(self):
        self.fh = open(self.test_file, 'r')
        self.tarfixer.fh = self.fh
        self.assertRaises(IOError, self.tarfixer.fix)

    def test_padded_size_length_small(self):
        length = 10
        block_size = 512
        self.assertEqual(512, self.tarfixer.padded_size(length, block_size))

    def test_padded_size_length_spot_on(self):
        length = 512
        block_size = 512
        self.assertEqual(512, self.tarfixer.padded_size(length, block_size))

    def test_padded_size_length_over(self):
        length = 513
        block_size = 512
        self.assertEqual(1024, self.tarfixer.padded_size(length, block_size))

    def test_padded_size_length_long(self):
        length = 82607
        block_size = 512
        self.assertEqual(82944, self.tarfixer.padded_size(length, block_size))

    def test_create_extended_header(self):
        self.tarfixer.create_extended_header()
        header = self.out.getvalue()
        self.assertEqual(512, len(header))
        self.assertEqual("52 comment=%s\n" % EXPECTED_REF, header[:52])
        self.assertEqual("\x00" * (512 - 53), header[53:])

    def test_calculate_checksum(self):
        fields = {
            'a': '\x01',
            'b': '\x02',
            'c': '\x03',
            'd': '\x04',
        }
        self.tarfixer.struct_members = list(fields.keys()) + ['checksum']
        result = self.tarfixer.calculate_checksum(fields)
        expected_result = 10 + ord(" ") * 8
        self.assertEqual("%07o\x00" % expected_result, result)

    def test_encode_header(self):
        mode = 123
        chunk = {
            'mode': mode,
            'name': 'hello',
        }
        result = self.tarfixer.encode_header(chunk, ['mode', 'name'])
        expected_result = ["%07o\x00" % mode, "hello"]
        expected_result = list(
            map(lambda x: encode_bytes(x, "utf8"), expected_result))
        self.assertEqual(expected_result, result)