Coverage for src/P4OO/Change.py: 77%

35 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-09-07 17:17 +0000

1###################################################################### 

2# Copyright (c)2011-2012, 2015, 2024 David L. Armstrong. 

3# Copyright (c)2012, Cisco Systems, Inc. 

4# 

5# P4OO.Change.py 

6# 

7###################################################################### 

8 

9from dataclasses import dataclass, field 

10 

11from P4OO.Exceptions import P4Fatal 

12from P4OO._SpecObj import _P4OOSpecObj 

13from P4OO._Set import _P4OOSet 

14 

15@dataclass(unsafe_hash=True) 

16class P4OOChange(_P4OOSpecObj): 

17 """ 

18 Perforce Change Spec Object 

19 

20 id Required: Yes 

21 

22 Forcible: Yes 

23 

24 Attributes: 

25 change (int): Change # 

26 user (P4OOUser): User that created the change spec 

27 description (str): Description field 

28 client (P4OOClient | str): Current client workspace 

29 status (str): [`pending`|`shelved`|`submitted`|`new`] 

30 type (str): [`restricted`|`public`] 

31 jobs (P4OOJobSet | P4OOJob | str): List of jobs that are fixed by this change 

32 date (datetime): Time of last update to the spec 

33 

34 See Also: 

35 Perforce Helix Core Command Reference: 

36 https://www.perforce.com/manuals/cmdref/Content/CmdRef/p4_change.html 

37 """ 

38 

39 # Subclasses must define SPECOBJ_TYPE 

40 _SPECOBJ_TYPE = 'change' 

41 

42 def getChangesFromChangeNums(self, otherChange, client): 

43 """ Fetch the list of changes from this change to another one. 

44 

45 ASSUMPTIONS: 

46 - self represents the lower of the two changes. If the other 

47 direction is desired, then make the call against the other 

48 change instead. 

49 """ 

50 

51 if not isinstance(otherChange, P4OOChange): 

52 raise TypeError(otherChange) 

53 

54 # +1 to not include the from change 

55 firstChange = int(self._getSpecID()) + 1 

56 lastChange = int(otherChange._getSpecID()) 

57 

58 aggregatedChanges = P4OOChangeSet() 

59 view = client._getSpecAttr('View') 

60 for viewLine in view: 

61 viewSpec = viewLine.split(" ", 2) 

62 

63 fileChangeRange = '%s@%d,%d' % (viewSpec[0], firstChange, 

64 lastChange) 

65 

66 changeSet = P4OOChangeSet(_p4Conn=self._getP4Connection()) 

67 viewChanges = changeSet.query(files=fileChangeRange, 

68 longOutput=1) 

69 

70 aggregatedChanges |= viewChanges 

71 

72 return aggregatedChanges 

73 

74# def reopenFiles(self): 

75# return self._runCommand('reopen', 

76# change=self, 

77# files="//%s/..." % 

78# self._getSpecAttr('client'), 

79# p4client=self._getSpecAttr('client') 

80# ) 

81 

82 def revertOpenedFiles(self): 

83 """ 

84 Executes `p4 revert -k` command for all files in this change 

85 

86 Returns: 

87 (list(str)): Output returned from _P4OOBase._runCommand() 

88 

89 See Also: 

90 Perforce Helix Core Command Reference: 

91 https://www.perforce.com/manuals/cmdref/Content/CmdRef/p4_revert.html 

92 """ 

93 

94# self.reopenFiles() 

95 return self._runCommand('revert', 

96 change=self, 

97 noclientrefresh=True, 

98 files="//%s/..." % self._getSpecAttr('client'), 

99 p4client=self._getSpecAttr('client')) 

100# try: 

101# return self._runCommand('revert', 

102# change=self, 

103# noclientrefresh=True, 

104# files="//%s/..." % 

105# self._getSpecAttr('client'), 

106# p4client=self._getSpecAttr('client') 

107# ) 

108# except P4Fatal: 

109 

110 def deleteShelf(self): 

111 """ 

112 Executes `p4 shelve -d` command for this change 

113 

114 Returns: 

115 (list(str)): Output returned from _P4OOBase._runCommand() 

116 

117 See Also: 

118 Perforce Helix Core Command Reference: 

119 https://www.perforce.com/manuals/cmdref/Content/CmdRef/p4_shelve.html 

120 """ 

121 

122 try: 

123 return self._runCommand('shelve', 

124 delete=True, 

125 change=self, 

126 force=True, 

127 p4client=self._getSpecAttr('client')) 

128 except P4Fatal: 

129 return True 

130 

131 def deleteWithVengeance(self): 

132 """ 

133 Performs all operations necessary to delete this (pending) change. 

134 

135 - Delete shelf 

136 - Delete change spec 

137 

138 Returns: 

139 (Boolean): Result from _P4OOSpecObj.deleteSpec() 

140 

141 See Also: 

142 Perforce Helix Core Knowledge Base: 

143 https://portal.perforce.com/s/article/3452 

144 """ 

145 

146# self.revertOpenedFiles() 

147 self.deleteShelf() 

148 return self.deleteSpec(force=True) 

149 

150 

151@dataclass 

152class P4OOChangeSet(_P4OOSet): 

153 """ `P4OOSet` of `P4OOChange` objects """ 

154 

155 def query(self, client: str=None, user: str=None, maxresults: int=None, 

156 status: str=None, files: str=None, longoutput: bool=None, 

157 **kwargs): 

158 """ 

159 Executes `p4 changes` query 

160 

161 Args: 

162 client (P4OOClient | str, optional): The client (viewspec) to 

163 filter file revisions through 

164 user (P4OOUser | str, optional): The user that created the change 

165 maxresults (int, optional): Return only the first [max] results 

166 status (str, optional): Filter changes by status: 

167 [`pending`|`shelved`|`submitted`] 

168 files (P4OOFileSet | P4OOFile | str, optional): The set of file 

169 revisions to query 

170 longoutput (bool, optional): include the full changelist 

171 descriptions 

172 

173 Returns: 

174 (P4OOChangeSet): `P4OOSet` of `P4OOChange` objects matching query 

175 parameters 

176 

177 See Also: 

178 Perforce Helix Core Command Reference: 

179 https://www.perforce.com/manuals/cmdref/Content/CmdRef/p4_changes.html 

180 """ 

181 

182 return self._query(setObjType='changes', client=client, user=user, 

183 maxresults=maxresults, status=status, files=files, 

184 longoutput=longoutput, **kwargs)