import os import sys import shutil import zipfile import json from lib import utils as Utils def GetVersion (rootDir): packageJson = None with open (os.path.join (rootDir, 'package.json')) as packageJsonFile: packageJson = json.load (packageJsonFile) return packageJson['version'] def CreateWebsite (config, rootDir, websiteDir, version, testBuild): if not os.path.exists (websiteDir): os.makedirs (websiteDir) if not os.path.exists (os.path.join (websiteDir, 'o3dv')): os.makedirs (os.path.join (websiteDir, 'o3dv')) shutil.copy2 (os.path.join (rootDir, 'website', 'index.html'), websiteDir) shutil.copy2 (os.path.join (rootDir, 'website', 'embed.html'), websiteDir) shutil.copy2 (os.path.join (rootDir, 'website', 'robots.txt'), websiteDir) shutil.copy2 (os.path.join (rootDir, 'build', 'o3dv.website.min.js'), os.path.join (websiteDir, 'o3dv')) shutil.copy2 (os.path.join (rootDir, 'build', 'o3dv.website.min.css'), os.path.join (websiteDir, 'o3dv')) shutil.copytree (os.path.join (rootDir, 'libs'), os.path.join (websiteDir, 'libs')) shutil.copytree (os.path.join (rootDir, 'website', 'assets'), os.path.join (websiteDir, 'assets')) shutil.copytree (os.path.join (rootDir, 'website', 'css', 'Quicksand'), os.path.join (websiteDir, 'o3dv', 'Quicksand')) shutil.copytree (os.path.join (rootDir, 'website', 'css', 'O3DVIcons'), os.path.join (websiteDir, 'o3dv', 'O3DVIcons')) shutil.copytree (os.path.join (rootDir, 'website', 'info'), os.path.join (websiteDir, 'info')) pluginFiles = [] pluginsDir = os.path.join (rootDir, 'plugins') if os.path.exists (pluginsDir): for pluginFile in os.listdir (pluginsDir): if os.path.splitext (pluginFile)[1] != '.js': continue websitePluginsDir = os.path.join (websiteDir, 'plugins'); if not os.path.exists (websitePluginsDir): os.makedirs (websitePluginsDir) shutil.copy2 (os.path.join (pluginsDir, pluginFile), os.path.join (websitePluginsDir, pluginFile)) pluginFiles.append ('plugins/' + pluginFile) websiteLibFiles = config['website_lib_files'] websiteFiles = [ 'o3dv/o3dv.website.min.css', 'o3dv/o3dv.website.min.js' ] htmlFileNames = [ 'index.html', 'embed.html', os.path.join ('info', 'index.html'), os.path.join ('info', 'cookies.html'), os.path.join ('info', 'faq.html') ] for htmlFileName in htmlFileNames: htmlFilePath = os.path.join (websiteDir, htmlFileName) replacer = Utils.TokenReplacer (htmlFilePath, False) replacer.ReplaceTokenFileLinks ('', '', websiteLibFiles, version) replacer.ReplaceTokenFileLinks ('', '', websiteFiles, version) replacer.ReplaceTokenFileLinks ('', '', pluginFiles, version) initScriptContent = '' initScriptContent += '' embedInitScriptContent = '' embedInitScriptContent += '' replacer.ReplaceTokenContent ('', '', initScriptContent) replacer.ReplaceTokenContent ('', '', embedInitScriptContent) metaFile = os.path.join (rootDir, 'plugins', 'website_meta_data.txt') if os.path.exists (metaFile): metaContent = Utils.GetFileContent (metaFile) replacer.ReplaceTokenContent ('', '', metaContent) analyticsFile = os.path.join (rootDir, 'plugins', 'website_analytics_data.txt') if os.path.exists (analyticsFile) and not testBuild: analyticsContent = Utils.GetFileContent (analyticsFile) replacer.ReplaceTokenContent ('', '', analyticsContent) introFile = os.path.join (rootDir, 'plugins', 'website_intro_data.txt') if os.path.exists (introFile): introContent = Utils.GetFileContent (introFile) replacer.ReplaceTokenContent ('', '', introContent) replacer.WriteToFile (htmlFilePath) def CreatePackage (rootDir, websiteDir, packageDir): if not os.path.exists (packageDir): os.makedirs (packageDir) zipPath = os.path.join (packageDir, 'o3dv.zip') zip = zipfile.ZipFile (zipPath, mode = 'w', compression = zipfile.ZIP_DEFLATED) for file in os.listdir (os.path.join (websiteDir, 'libs', 'loaders')): zip.write (os.path.join (websiteDir, 'libs', 'loaders', file), 'libs/loaders/' + file) for file in os.listdir (os.path.join (websiteDir, 'assets', 'envmaps')): filePath = os.path.join (websiteDir, 'assets', 'envmaps', file) if os.path.isdir (filePath): for fileInDir in os.listdir (filePath): zip.write (os.path.join (filePath, fileInDir), 'envmaps/' + file + '/' + fileInDir) else: zip.write (filePath, 'envmaps/' + file) zip.write (os.path.join (rootDir, 'build', 'o3dv.min.js'), 'o3dv.min.js') zip.write (os.path.join (rootDir, 'LICENSE.md'), 'o3dv.license.md') zip.close () return True def Main (argv): toolsDir = os.path.dirname (os.path.abspath (__file__)) rootDir = os.path.dirname (toolsDir) os.chdir (rootDir) testBuild = False buildDir = os.path.join (rootDir, 'build', 'final') if len (argv) >= 2 and argv[1] == 'test': testBuild = True buildDir = os.path.join (rootDir, 'build', 'test') Utils.PrintInfo ('Creating test build.') websiteDir = os.path.join (buildDir, 'website') packageDir = os.path.join (buildDir, 'package') if os.path.exists (buildDir): shutil.rmtree (buildDir) config = None with open (os.path.join (toolsDir, 'config.json')) as configJson: config = json.load (configJson) version = GetVersion (rootDir) Utils.PrintInfo ('Create build directory') CreateWebsite (config, rootDir, websiteDir, version, testBuild) Utils.PrintInfo ('Create package.') packageResult = CreatePackage (rootDir, websiteDir, packageDir) if not packageResult: Utils.PrintError ('Create package failed.') return 1 return 0 sys.exit (Main (sys.argv))