Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

#!/usr/bin/env python 

 

""" 

@file ion/core/_version.py 

@author Dorian Raymer 

@author Michael Meisinger 

@brief Version-handling for ION classes 

@note From twisted.python.versions 

""" 

 

import os 

import sys 

 

 

class _inf(object): 

    """ 

    An object that is bigger than all other objects. 

    """ 

    def __cmp__(self, other): 

        """ 

        @param other: Another object. 

        @type other: any 

 

        @return: 0 if other is inf, 1 otherwise. 

        @rtype: C{int} 

        """ 

        if other is _inf: 

            return 0 

        return 1 

 

_inf = _inf() 

 

class IncomparableVersions(TypeError): 

    """ 

    Two versions could not be compared. 

    """ 

 

class Version(object): 

    """look for git commit to include in the version number. 

 

    An object that represents a three-part version number. 

    @note Taken from twisted python (twisted.python.versions) 

    """ 

 

    def __init__(self, package, major, minor, micro, prerelease=None): 

        """ 

        @param package: Name of the package that this is a version of. 

        @type package: C{str} 

        @param major: The major version number. 

        @type major: C{int} 

        @param minor: The minor version number. 

        @type minor: C{int} 

        @param micro: The micro version number. 

        @type micro: C{int} 

        @param prerelease: The prerelease number. 

        @type prerelease: C{int} 

        """ 

        self.package = package 

        self.major = major 

        self.minor = minor 

        self.micro = micro 

        self.prerelease = prerelease 

 

    def short(self): 

        """ 

        Return a string in canonical short version format, 

        <major>.<minor>.<micro>[+rSVNVer]. 

        """ 

        s = self.base() 

        gitcommit = self._getGitCommit() 

        if gitcommit: 

            s += '+git:' + str(gitcommit) 

        return s 

 

    def base(self): 

        """ 

        Like L{short}, but without the +git. 

        """ 

        if self.prerelease is None: 

            pre = "" 

        else: 

            pre = "pre%s" % (self.prerelease,) 

        return '%d.%d.%d%s' % (self.major, 

                               self.minor, 

                               self.micro, 

                               pre) 

 

    def __repr__(self): 

        gitcommit = self._formatGitCommit() 

        if gitcommit: 

            gitcommit = '  #' + gitcommit 

        if self.prerelease is None: 

            prerelease = "" 

        else: 

            prerelease = ", prerelease=%r" % (self.prerelease,) 

        return '%s(%r, %d, %d, %d%s)%s' % ( 

            self.__class__.__name__, 

            self.package, 

            self.major, 

            self.minor, 

            self.micro, 

            prerelease, 

            gitcommit) 

 

    def __str__(self): 

        return '[%s, version %s]' % ( 

            self.package, 

            self.short()) 

 

    def __cmp__(self, other): 

        """ 

        Compare two versions, considering major versions, minor versions, micro 

        versions, then prereleases. 

 

        A version with a prerelease is always less than a version without a 

        prerelease. If both versions have prereleases, they will be included in 

        the comparison. 

 

        @param other: Another version. 

        @type other: L{Version} 

 

        @return: NotImplemented when the other object is not a Version, or one 

            of -1, 0, or 1. 

 

        @raise IncomparableVersions: when the package names of the versions 

            differ. 

        """ 

        if not isinstance(other, self.__class__): 

            return NotImplemented 

        if self.package != other.package: 

            raise IncomparableVersions("%r != %r" 

                                       % (self.package, other.package)) 

 

        if self.prerelease is None: 

            prerelease = _inf 

        else: 

            prerelease = self.prerelease 

 

        if other.prerelease is None: 

            otherpre = _inf 

        else: 

            otherpre = other.prerelease 

 

        x = cmp((self.major, 

                    self.minor, 

                    self.micro, 

                    prerelease), 

                   (other.major, 

                    other.minor, 

                    other.micro, 

                    otherpre)) 

        return x 

 

    def _getGitCommit(self): 

        mod = sys.modules.get(self.package) 

        if mod: 

            git = os.path.join(os.path.dirname(mod.__file__), '..', '.git') 

            if not os.path.exists(git): 

                # It's not an git working copy 

                return None 

 

            headFile = os.path.join(git, 'HEAD') 

            if os.path.exists(headFile): 

                head = file(headFile).read().strip() 

                versplit = head.split(':') 

                if len(versplit) == 2: 

                    headRef = versplit[1].strip() 

                    refFile = os.path.join(git, headRef) 

                    if os.path.exists(refFile): 

                        commit = file(refFile).read().strip() 

                        return commit 

                    else: 

                        return 'Unknown' 

                else: 

                    # this indicates a non named ref is checked out 

                    return versplit[0] 

            else: 

                return 'Unknown' 

 

    def _formatGitCommit(self): 

        commit = self._getGitCommit() 

        if commit is None: 

            return '' 

        return ' (Git:%s)' % commit