Package conary :: Package conaryclient :: Module newtrove
[hide private]
[frames] | no frames]

Source Code for Module conary.conaryclient.newtrove

  1  # 
  2  # Copyright (c) 2008 rPath, Inc. 
  3  # 
  4  # This program is distributed under the terms of the Common Public License, 
  5  # version 1.0. A copy of this license should have been distributed with this 
  6  # source file in a file called LICENSE. If it is not present, the license 
  7  # is always available at http://www.rpath.com/permanent/licenses/CPL-1.0. 
  8  # 
  9  # This program is distributed in the hope that it will be useful, but 
 10  # without any warranty; without even the implied warranty of merchantability 
 11  # or fitness for a particular purpose. See the Common Public License for 
 12  # full details. 
 13   
 14  import os 
 15   
 16  from conary.deps import deps 
 17  from conary.repository import changeset 
 18  from conary.repository import filecontents 
 19  from conary import files 
 20  from conary import trove 
 21  from conary import versions 
 22   
23 -def makePathId():
24 """returns 16 random bytes, for use as a pathId""" 25 return os.urandom(16)
26
27 -class ClientNewTrove:
28 - def _createTroves(self, troveAndPathList):
29 cs = changeset.ChangeSet() 30 troveList = [ x[0] for x in troveAndPathList ] 31 previousVersionMap = self._targetNewTroves(troveList) 32 self._addAllNewFiles(cs, troveAndPathList, previousVersionMap) 33 for trove in troveList: 34 trove.computeDigests() 35 trvCs = trove.diff(None, absolute=True)[0] 36 cs.newTrove(trvCs) 37 return cs
38
39 - def createSourceTrove(self, name, label, upstreamVersion, pathDict, 40 changeLog, factory = None, 41 pkgCreatorData = None):
42 """ 43 Create a source trove. 44 @param name: trove name of source components. 45 @type name: str 46 @param label: trove label 47 @type label: str 48 @param upstreamVersion: upstream version of source component 49 @type upstreamVersion: str 50 @param pathDict: Dictionary mapping path strings to 51 conaryclient.filetypes._File objects, which represent the contents 52 of each file 53 @type pathDict: dict(str: conaryclient.filetypes._File) 54 @param changeLog: Change log associated with this source trove. 55 @type changeLog: changelog.ChangeLog 56 @param factory: designate a factory associated with this source 57 trove. 58 @type factory: str 59 @type pkgCreatorData: arbitrary string set in 60 _TROVEINFO_TAG_PKGCREATORDATA 61 @type pkgCreatorData: str 62 """ 63 if not name.endswith(':source'): 64 raise RuntimeError('Only source components allowed') 65 versionStr = '/%s/%s-1' % (label, upstreamVersion) 66 version = versions.VersionFromString(versionStr).copy() 67 version.resetTimeStamps() 68 troveObj = trove.Trove(name, version, deps.Flavor(), 69 changeLog = changeLog) 70 troveObj.setFactory(factory) 71 if pkgCreatorData: 72 troveObj.troveInfo.pkgCreatorData.set(pkgCreatorData) 73 74 return self._createTroves([(troveObj, pathDict)])
75 76
77 - def _targetNewTroves(self, troveList):
78 repos = self.getRepos() 79 previousVersionMap = {} 80 versionDict = {} 81 troveSpecs = {} 82 trovesSeen = set() 83 for troveObj in troveList: 84 name, version, flavor = troveObj.getNameVersionFlavor() 85 if not name.endswith(':source'): 86 raise RuntimeError('Only source components allowed') 87 versionSpec = '%s/%s' % (version.trailingLabel(), 88 version.trailingRevision().getVersion()) 89 if (name, versionSpec) in trovesSeen: 90 raise RuntimeError('Cannot create multiple versions of %s with same version' % name) 91 92 trovesSeen.add((name, versionSpec)) 93 troveSpecs[name, versionSpec, None] = troveObj 94 95 results = repos.findTroves(None, troveSpecs, None, allowMissing=True) 96 for troveSpec, troveObj in troveSpecs.iteritems(): 97 tupList = results.get(troveSpec, []) 98 if tupList: 99 assert(len(tupList) == 1) 100 latestVersion = tupList[0][1] 101 newVersion = latestVersion.copy() 102 newVersion.incrementSourceCount() 103 troveObj.changeVersion(newVersion) 104 previousVersionMap[troveObj.getNameVersionFlavor()] = tupList[0] 105 else: 106 version = troveObj.getVersion() 107 branch = version.branch() 108 upstreamVer = version.trailingRevision().getVersion() 109 revision = versions.Revision('%s-1' % upstreamVer) 110 newVersion = branch.createVersion(revision) 111 troveObj.changeVersion(newVersion) 112 return previousVersionMap
113
114 - def _addAllNewFiles(self, cs, troveAndPathList, previousVersionMap):
115 repos = self.getRepos() 116 existingTroves = repos.getTroves(previousVersionMap.values(), 117 withFiles=True) 118 troveDict = dict(zip(previousVersionMap.values(), existingTroves)) 119 for trove, pathDict in troveAndPathList: 120 existingTroveTup = previousVersionMap.get( 121 trove.getNameVersionFlavor(), None) 122 if existingTroveTup: 123 existingTrove = troveDict[existingTroveTup] 124 else: 125 existingTrove = None 126 self._addNewFiles(cs, trove, pathDict, existingTrove)
127
128 - def _removeOldPathIds(self, troveObj):
129 allPathIds = [x[0] for x in troveObj.iterFileList()] 130 for pathId in allPathIds: 131 troveObj.removePath(pathId)
132
133 - def _addNewFiles(self, cs, trove, pathDict, existingTrove):
134 existingPaths = {} 135 pathIds = {} 136 if existingTrove: 137 for pathId, path, fileId, fileVer in existingTrove.iterFileList(): 138 existingPaths[path] = (fileId, pathId, fileVer) 139 self._removeOldPathIds(trove) 140 141 for path, fileObj in pathDict.iteritems(): 142 if path in existingPaths: 143 oldFileId, pathId, oldFileVer = existingPaths[path] 144 else: 145 pathId = makePathId() 146 oldFileId = oldFileVer = None 147 f = fileObj.get(pathId) 148 f.flags.isSource(set = True) 149 newFileId = f.fileId() 150 if oldFileId == newFileId: 151 newFileVer = oldFileVer 152 else: 153 newFileVer = trove.getVersion() 154 cs.addFile(None, newFileId, f.