예제 #1
0
def _user_agent_safe_for_requirement(user_agent):

    # very early versions of fwupd used 'fwupdmgr' as the user agent
    if user_agent == 'fwupdmgr':
        return False

    # gnome-software/3.26.5 (Linux x86_64 4.14.0) fwupd/1.0.4
    sections = user_agent.split(' ')
    for chunk in sections:
        toks = chunk.split('/')
        if len(toks) == 2 and toks[0] == 'fwupd':
            return vercmp(toks[1], '0.8.0') >= 0

    # this is a heuristic; the logic is that it's unlikely that a distro would
    # ship a very new gnome-software and a very old fwupd
    for chunk in sections:
        toks = chunk.split('/')
        if len(toks) == 2 and toks[0] == 'gnome-software':
            return vercmp(toks[1], '3.26.0') >= 0

    # is is probably okay
    return True
    def test_vercmp(self):

        # same
        assert vercmp('1.2.3', '1.2.3') == 0
        assert vercmp('001.002.003', '001.002.003') == 0
        self.assertEqual(vercmp('0x123', '0x123'), 0)
        self.assertEqual(vercmp('0x123', '291'), 0)
        self.assertEqual(vercmp('291', '0x123'), 0)

        # upgrade and downgrade
        assert vercmp('1.2.3', '1.2.4') < 0
        assert vercmp('001.002.000', '001.002.009') < 0
        assert vercmp('1.2.3', '1.2.2') > 0
        assert vercmp('001.002.009', '001.002.000') > 0

        # int parsing
        assert vercmp('4.01', '4.10') < 0

        # unequal depth
        assert vercmp('1.2.3', '1.2.3.1') < 0
        assert vercmp('1.2.3.1', '1.2.3') > 0
        assert vercmp('1.2.3.1', '1.2.4') < 0

        # mixed-alpha-numeric
        assert vercmp('1.2.3a', '1.2.3a') == 0
        assert vercmp('1.2.3a', '1.2.3b') < 0
        assert vercmp('1.2.3b', '1.2.3a') > 0

        # alpha version append
        assert vercmp('1.2.3', '1.2.3a') < 0
        assert vercmp('1.2.3a', '1.2.3') > 0

        # alpha only
        assert vercmp('alpha', 'alpha') == 0
        assert vercmp('alpha', 'beta') < 0
        assert vercmp('beta', 'alpha') > 0

        # alpha-compare
        assert vercmp('1.2a.3', '1.2a.3') == 0
        assert vercmp('1.2a.3', '1.2b.3') < 0
        assert vercmp('1.2b.3', '1.2a.3') > 0

        # tilde is all-powerful
        assert vercmp('1.2.3~rc1', '1.2.3~rc1') == 0
        assert vercmp('1.2.3~rc1', '1.2.3') < 0
        assert vercmp('1.2.3', '1.2.3~rc1') > 0
        assert vercmp('1.2.3~rc2', '1.2.3~rc1') > 0

        # invalid
        with self.assertRaises(TypeError):
            assert vercmp('1', None) is None
        with self.assertRaises(TypeError):
            assert vercmp(None, '1') is None
        with self.assertRaises(TypeError):
            assert vercmp(None, None) is None