-
-
Notifications
You must be signed in to change notification settings - Fork 583
/
Copy pathcreate_package.py
121 lines (104 loc) · 4.9 KB
/
create_package.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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 (rootDir, websiteDir, version, testBuild):
if not os.path.exists (websiteDir):
os.makedirs (websiteDir)
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.copytree (os.path.join (rootDir, 'build', 'website'), os.path.join (websiteDir, 'o3dv'))
shutil.copytree (os.path.join (rootDir, 'website', 'assets'), os.path.join (websiteDir, 'assets'))
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)
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 ('<!-- website start -->', '<!-- website end -->', websiteFiles, version)
replacer.ReplaceTokenFileLinks ('<!-- plugins start -->', '<!-- plugins end -->', pluginFiles, version)
metaFile = os.path.join (rootDir, 'plugins', 'website_meta_data.txt')
if os.path.exists (metaFile):
metaContent = Utils.GetFileContent (metaFile)
replacer.ReplaceTokenContent ('<!-- meta start -->', '<!-- meta end -->', metaContent)
introFooterFile = os.path.join (rootDir, 'plugins', 'website_intro_footer_data.txt')
if os.path.exists (introFooterFile) and not testBuild:
introFooterContent = Utils.GetFileContent (introFooterFile)
replacer.ReplaceTokenContent ('<!-- intro footer start -->', '<!-- intro footer end -->', introFooterContent)
websiteAnalyticsFile = os.path.join (rootDir, 'plugins', 'website_analytics_data.txt')
if os.path.exists (websiteAnalyticsFile) and not testBuild:
websiteAnalyticsContent = Utils.GetFileContent (websiteAnalyticsFile)
replacer.ReplaceTokenContent ('<!-- website analytics start -->', '<!-- website analytics end -->', websiteAnalyticsContent)
embedAnalyticsFile = os.path.join (rootDir, 'plugins', 'embed_analytics_data.txt')
if os.path.exists (websiteAnalyticsFile) and not testBuild:
embedAnalyticsContent = Utils.GetFileContent (embedAnalyticsFile)
replacer.ReplaceTokenContent ('<!-- embed analytics start -->', '<!-- embed analytics end -->', embedAnalyticsContent)
replacer.WriteToFile (htmlFilePath)
def CreateEnginePackage (rootDir, engineDir, websiteDir):
if not os.path.exists (engineDir):
os.makedirs (engineDir)
zipPath = os.path.join (engineDir, 'o3dv.zip')
zip = zipfile.ZipFile (zipPath, mode = 'w', compression = zipfile.ZIP_DEFLATED)
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', 'engine', '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', 'package')
if len (argv) >= 2 and argv[1] == 'test':
testBuild = True
buildDir = os.path.join (rootDir, 'build', 'package_test')
Utils.PrintInfo ('Creating test build.')
websiteDir = os.path.join (buildDir, 'website')
engineDir = os.path.join (buildDir, 'engine')
if os.path.exists (buildDir):
shutil.rmtree (buildDir)
version = GetVersion (rootDir)
Utils.PrintInfo ('Create build directory')
CreateWebsite (rootDir, websiteDir, version, testBuild)
Utils.PrintInfo ('Create package.')
packageResult = CreateEnginePackage (rootDir, engineDir, websiteDir)
if not packageResult:
Utils.PrintError ('Create package failed.')
return 1
return 0
sys.exit (Main (sys.argv))