#!/usr/bin/python3 """ Run as: python3 thisscript.py "/bundles/directory" "/website/template/root/directory/ All sub-directories of bundles directory will be scanned for activity bundles i.e. .xo files. """ import glob import json import os import shutil import sys from urllib.parse import quote as strToHtmlFmt import zipfile from GeneralFunctions.DataStructureManipulations import ( StrListToDictionary ) from GeneralFunctions.InputOutput import ( WriteTextFiles, WriteBinaryToFile ) from GeneralFunctions.OS import CreateDir """ FIXME: paths hard coded UNIX style & most likely will not work on Windows. Use code written for IMM to handle it. """ class extractData: def findInfoFiles(self, bundle): infoFiles = [] for File in bundle.namelist(): if File.endswith("activity/activity.info"): infoFiles.append(File) return infoFiles def copyBundle(self, bundleSrc, activityName): shutil.copy2( bundleSrc, self.websiteDir+"bundles/"+activityName+".xo" ) def createDirectories(self): assert CreateDir(self.websiteDir+"app") assert CreateDir(self.websiteDir+"icons") assert CreateDir(self.websiteDir+"bundles") assert CreateDir(self.websiteDir+"js") def extractActivityInfo(self, infoFilePath, zipFile): infoList = [] infoList = zipFile.read( infoFilePath).decode("utf-8").split('\n') return StrListToDictionary(infoList) def extractInfoAndIconFromBundles(self): for bundlePath in self.activityBundles: bundle = zipfile.ZipFile(bundlePath, "r") infoFiles = self.findInfoFiles(bundle) if len(infoFiles) != 1: self.bundlesNotExactlyOneInfoFile.append(bundlePath) else: infoDict = self.extractActivityInfo(infoFiles[0], bundle) self.bundlesInfoList.append(infoDict) # FIXME: create seprate function for it # extract and copy icon activityName = infoDict.get("name") if type(activityName) == str: iconRelativePath = infoDict.get("icon") if type(iconRelativePath) == str: iconFolder = infoFiles[0][:infoFiles[0].rfind("/")+1] iconAbsolutePath = ( iconFolder+iconRelativePath+".svg") if iconAbsolutePath in bundle.namelist(): icon = bundle.read(iconAbsolutePath) iconPath = ( self.websiteDir+"icons/" + activityName + ".svg" ) WriteBinaryToFile(iconPath, icon) else: # Continue without icon since non-fatal error self.iconErrorBundles.append(bundlePath) bundle.close() # FIXME: uncomment below function. # Disabled sometime during development as time consuming self.copyBundle(bundlePath, activityName) bundle.close() def findBundles(self): self.activityBundles = glob.glob( self.bundlesDir+"**/*.xo", recursive=True ) def generateAppsHtmlPages(self): iconsDir = "../icons/" bundlesDir = "../bundles/" for appInfo in self.indexDictList: pathName = strToHtmlFmt(appInfo["name"], safe='') html = ( '\n\n
\n' + appInfo["summary"] + '
\n' + appInfo["description"] + '
\n