2015-07-26 11:45:01 +08:00
|
|
|
import os, subprocess
|
|
|
|
import sgprops
|
|
|
|
import hashlib # for MD5
|
|
|
|
import shutil # for copy2
|
|
|
|
import catalogTags
|
|
|
|
|
|
|
|
standardTagSet = frozenset(catalogTags.tags)
|
|
|
|
def isNonstandardTag(t):
|
|
|
|
return t not in standardTagSet
|
|
|
|
|
|
|
|
thumbnailNames = ["thumbnail.png", "thumbnail.jpg"]
|
|
|
|
|
|
|
|
class VariantData:
|
|
|
|
def __init__(self, path, node):
|
|
|
|
#self._primary = primary
|
|
|
|
self._path = path
|
|
|
|
self._name = node.getValue("sim/description")
|
2016-01-04 09:49:54 +08:00
|
|
|
if (not self._name):
|
|
|
|
print "Missing description for " + path
|
|
|
|
self._name = "Missing description:" + self.id
|
2015-07-26 11:45:01 +08:00
|
|
|
|
|
|
|
# ratings
|
|
|
|
|
|
|
|
# seperate thumbnails
|
|
|
|
|
2016-01-04 09:49:54 +08:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def id(self):
|
|
|
|
return self._path[:-8] # "remove -set.xml" (8 chars)
|
|
|
|
|
2015-07-26 11:45:01 +08:00
|
|
|
@property
|
|
|
|
def catalogNode(self):
|
|
|
|
n = sgprops.Node("variant")
|
2016-01-04 09:49:54 +08:00
|
|
|
n.addChild("id").value = self.id
|
|
|
|
n.addChild("name").value = self.name
|
|
|
|
return n
|
2015-07-26 11:45:01 +08:00
|
|
|
|
|
|
|
class PackageData:
|
2015-07-29 04:26:00 +08:00
|
|
|
def __init__(self, path, scmRepo):
|
2015-07-26 11:45:01 +08:00
|
|
|
self._path = path
|
2015-07-29 04:26:00 +08:00
|
|
|
self._scm = scmRepo
|
2015-07-26 11:45:01 +08:00
|
|
|
self._previousSCMRevision = None
|
|
|
|
self._previousRevision = 0
|
|
|
|
self._thumbnails = []
|
|
|
|
self._variants = {}
|
|
|
|
self._revision = 0
|
|
|
|
self._md5 = None
|
|
|
|
self._fileSize = 0
|
2015-09-20 22:20:43 +08:00
|
|
|
self._primarySetXmlPath = None
|
2015-07-26 11:45:01 +08:00
|
|
|
self._node = sgprops.Node("package")
|
|
|
|
|
|
|
|
def setPreviousData(self, node):
|
|
|
|
self._previousRevision = node.getValue("revision")
|
|
|
|
self._previousMD5 = node.getValue("md5")
|
|
|
|
self._previousSCMRevision = node.getValue("scm-revision")
|
|
|
|
self._fileSize = int(node.getValue("file-size-bytes"))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def id(self):
|
2015-09-20 22:20:43 +08:00
|
|
|
return self._primarySetXmlPath
|
2015-07-26 11:45:01 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def thumbnails(self):
|
|
|
|
return self._thumbnails
|
|
|
|
|
|
|
|
@property
|
|
|
|
def path(self):
|
|
|
|
return self._path
|
|
|
|
|
|
|
|
@property
|
|
|
|
def variants(self):
|
|
|
|
return self._variants
|
|
|
|
|
2015-07-29 04:26:00 +08:00
|
|
|
@property
|
|
|
|
def scmRevision(self):
|
2015-09-25 09:39:03 +08:00
|
|
|
currentRev = self._scm.scmRevisionForPath(self.path)
|
2015-07-26 11:45:01 +08:00
|
|
|
if (currentRev is None):
|
|
|
|
raise RuntimeError("Unable to query SCM revision of files")
|
|
|
|
|
|
|
|
return currentRev
|
|
|
|
|
2015-07-29 04:26:00 +08:00
|
|
|
@property
|
|
|
|
def isSourceModified(self):
|
2015-07-26 11:45:01 +08:00
|
|
|
if (self._previousSCMRevision == None):
|
|
|
|
return True
|
|
|
|
|
2015-07-29 04:26:00 +08:00
|
|
|
if (self._previousSCMRevision == self.scmRevision):
|
2015-07-26 11:45:01 +08:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def scanSetXmlFiles(self, includes):
|
|
|
|
foundPrimary = False
|
|
|
|
foundMultiple = False
|
|
|
|
|
2015-09-25 09:39:03 +08:00
|
|
|
for f in os.listdir(self.path):
|
2015-07-26 11:45:01 +08:00
|
|
|
if not f.endswith("-set.xml"):
|
|
|
|
continue
|
|
|
|
|
2015-09-25 09:39:03 +08:00
|
|
|
p = os.path.join(self.path, f)
|
2015-07-26 11:45:01 +08:00
|
|
|
node = sgprops.readProps(p, includePaths = includes)
|
|
|
|
if not node.hasChild("sim"):
|
|
|
|
continue
|
|
|
|
|
|
|
|
simNode = node.getChild("sim")
|
2016-01-14 09:47:13 +08:00
|
|
|
# honour variosu exclusion flags
|
|
|
|
if (simNode.getValue("exclude-from-catalog", False) or simNode.getValue("exclude-from-gui", False)):
|
2015-07-26 11:45:01 +08:00
|
|
|
continue
|
|
|
|
|
|
|
|
primary = simNode.getValue("variant-of", None)
|
|
|
|
if primary:
|
|
|
|
if not primary in self.variants:
|
|
|
|
self._variants[primary] = []
|
2015-09-20 22:20:43 +08:00
|
|
|
self._variants[primary].append(VariantData(f, node))
|
2015-07-26 11:45:01 +08:00
|
|
|
continue
|
|
|
|
|
|
|
|
if foundPrimary:
|
|
|
|
if not foundMultiple:
|
2015-09-25 09:39:03 +08:00
|
|
|
print "Multiple primary -set.xml files at:" + self.path
|
2016-01-15 00:30:34 +08:00
|
|
|
print "\t" + p
|
2015-07-26 11:45:01 +08:00
|
|
|
foundMultiple = True
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
foundPrimary = True;
|
2015-09-20 22:20:43 +08:00
|
|
|
self._primarySetXmlPath = f[:-8] # trim -set.xml
|
2015-07-26 11:45:01 +08:00
|
|
|
|
|
|
|
self.parsePrimarySetNode(simNode)
|
|
|
|
|
|
|
|
for n in thumbnailNames:
|
2015-09-25 09:39:03 +08:00
|
|
|
if os.path.exists(os.path.join(self.path, n)):
|
2015-07-26 11:45:01 +08:00
|
|
|
self._thumbnails.append(n)
|
|
|
|
|
|
|
|
if not foundPrimary:
|
2015-09-25 09:39:03 +08:00
|
|
|
raise RuntimeError("No primary -set.xml found at:" + self.path)
|
2015-07-26 11:45:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parsePrimarySetNode(self, sim):
|
|
|
|
|
|
|
|
# basic / mandatory values
|
|
|
|
self._node.addChild('name').value = sim.getValue('description')
|
|
|
|
|
|
|
|
longDesc = sim.getValue('long-description')
|
|
|
|
if longDesc is not None:
|
|
|
|
self._node.addChild('description').value = longDesc
|
|
|
|
|
|
|
|
# copy all the standard values
|
|
|
|
for p in ['status', 'author', 'license']:
|
|
|
|
v = sim.getValue(p)
|
|
|
|
if v is not None:
|
|
|
|
self._node.addChild(p).value = v
|
|
|
|
|
|
|
|
# ratings
|
|
|
|
if sim.hasChild('rating'):
|
|
|
|
pkgRatings = self._node.addChild('rating')
|
|
|
|
for r in ['FDM', 'systems', 'cockpit', 'model']:
|
|
|
|
pkgRatings.addChild(r).value = sim.getValue('rating/' + r, 0)
|
|
|
|
|
|
|
|
# copy tags
|
|
|
|
if sim.hasChild('tags'):
|
|
|
|
for c in sim.getChild('tags').getChildren('tag'):
|
|
|
|
if isNonstandardTag(c.value):
|
|
|
|
print "Skipping non-standard tag:", c.value, self.path
|
|
|
|
else:
|
|
|
|
self._node.addChild('tag').value = c.value
|
|
|
|
|
|
|
|
for t in sim.getChildren("thumbnail"):
|
|
|
|
self._thumbnails.append(t.value)
|
|
|
|
|
|
|
|
def validate(self):
|
|
|
|
for t in self._thumbnails:
|
2015-09-25 09:39:03 +08:00
|
|
|
if not os.path.exists(os.path.join(self.path, t)):
|
2015-07-26 11:45:01 +08:00
|
|
|
raise RuntimeError("missing thumbnail:" + t);
|
|
|
|
|
2015-07-31 01:04:17 +08:00
|
|
|
def generateZip(self, outDir, globalExcludePath):
|
2015-07-26 11:45:01 +08:00
|
|
|
self._revision = self._previousRevision + 1
|
|
|
|
|
2015-09-25 09:39:03 +08:00
|
|
|
baseName = os.path.basename(self.path)
|
|
|
|
zipName = baseName + ".zip"
|
2015-07-26 11:45:01 +08:00
|
|
|
zipFilePath = os.path.join(outDir, zipName)
|
|
|
|
|
|
|
|
os.chdir(os.path.dirname(self.path))
|
|
|
|
|
|
|
|
print "Creating zip", zipFilePath
|
|
|
|
# anything we can do to make this faster?
|
2015-07-30 04:09:01 +08:00
|
|
|
|
|
|
|
zipArgs = ['zip', '--quiet', '-r']
|
2015-07-31 01:04:17 +08:00
|
|
|
if os.path.exists(globalExcludePath):
|
|
|
|
zipArgs += [ "-x@" + globalExcludePath]
|
|
|
|
|
2015-07-30 04:09:01 +08:00
|
|
|
excludePath = os.path.join(self.path, 'package-exclude.lst')
|
|
|
|
if (os.path.exists(excludePath)):
|
|
|
|
print self.id, "has zip exclude list"
|
|
|
|
zipArgs += ["-x@" + excludePath]
|
|
|
|
|
2015-09-25 09:39:03 +08:00
|
|
|
zipArgs += [zipFilePath, baseName]
|
2015-07-30 04:09:01 +08:00
|
|
|
subprocess.call(zipArgs)
|
2015-07-26 11:45:01 +08:00
|
|
|
|
|
|
|
zipFile = open(zipFilePath, 'r')
|
|
|
|
self._md5 = hashlib.md5(zipFile.read()).hexdigest()
|
|
|
|
self._fileSize = os.path.getsize(zipFilePath)
|
|
|
|
|
|
|
|
def useExistingCatalogData(self):
|
|
|
|
self._md5 = self._previousMD5
|
|
|
|
|
2015-07-29 04:26:00 +08:00
|
|
|
def packageNode(self, mirrorUrls, thumbnailUrl):
|
2015-09-20 22:20:43 +08:00
|
|
|
self._node.addChild("id").value = self.id
|
2015-07-26 11:45:01 +08:00
|
|
|
self._node.getChild("md5", create = True).value = self._md5
|
|
|
|
self._node.getChild("file-size-bytes", create = True).value = self._fileSize
|
|
|
|
self._node.getChild("revision", create = True).value = int(self._revision)
|
2015-07-29 04:26:00 +08:00
|
|
|
self._node.getChild("scm-revision", create = True).value = self.scmRevision
|
2015-07-26 11:45:01 +08:00
|
|
|
|
2015-09-28 12:15:44 +08:00
|
|
|
baseName = os.path.basename(self.path)
|
|
|
|
self._node.getChild("dir", create = True).value = baseName
|
|
|
|
zipName = baseName + ".zip"
|
|
|
|
|
2015-07-26 11:45:01 +08:00
|
|
|
for m in mirrorUrls:
|
2015-09-28 12:15:44 +08:00
|
|
|
self._node.addChild("url").value = m + "/" + zipName
|
2015-07-26 11:45:01 +08:00
|
|
|
|
|
|
|
for t in self._thumbnails:
|
2015-09-20 22:20:43 +08:00
|
|
|
self._node.addChild("thumbnail-path").value = t
|
2015-07-26 11:45:01 +08:00
|
|
|
self._node.addChild("thumbnail").value = thumbnailUrl + "/" + self.id + "_" + t
|
|
|
|
|
|
|
|
for pr in self._variants:
|
|
|
|
for vr in self._variants[pr]:
|
|
|
|
self._node.addChild(vr.catalogNode)
|
|
|
|
|
|
|
|
return self._node
|
|
|
|
|
|
|
|
def extractThumbnails(self, thumbnailDir):
|
|
|
|
for t in self._thumbnails:
|
|
|
|
fullName = self.id + "_" + t
|
|
|
|
shutil.copy2(os.path.join(self._path, t),
|
|
|
|
os.path.join(thumbnailDir, fullName)
|
|
|
|
)
|
|
|
|
# TODO : verify image format, size and so on
|