-
Notifications
You must be signed in to change notification settings - Fork 19
/
Installer.cls
295 lines (240 loc) · 8.51 KB
/
Installer.cls
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
Class %ZPM.Installer Extends %Projection.AbstractProjection
{
Projection Reference As Installer;
/// Version used for autoinstall release
Parameter VERSION;
/// Number of files for autoinstall release
Parameter FILESCOUNT = 0;
/// Application Definition
XData PM [ XMLNamespace = INSTALLER ]
{
<Manifest>
<Default Name="MODDIR" Dir="${MGRDIR}zpm" />
<Default Name="SRCDIR" Dir="${MODDIR}src" />
<Default Name="REGISTRY" Value="https://pm.community.intersystems.com" />
<Default Name="TRACKINGID" Value="UA-126752118-1" />
<Namespace Name="%SYS">
<Import File="${SRCDIR}" Recurse="1"/>
<Invoke Class="%ZPM.PackageManager" Method="UpdateLanguageExtensions" CheckStatus="true" />
<Invoke Class="%ZPM.Installer" Method="ZPMInit" CheckStatus="true">
<Arg Value="${REGISTRY}"/>
<Arg Value="${TRACKINGID}"/>
</Invoke>
<Invoke Class="%ZPM.Installer" Method="ZPMLoad" CheckStatus="true">
<Arg Value="${MODDIR}"/>
</Invoke>
<Invoke Class="%ZPM.Installer" Method="ZPMCompile" CheckStatus="true" />
</Namespace>
</Manifest>
}
/// This is a method generator whose code is generated by XGL.
ClassMethod setup(ByRef pVars, pLogLevel As %Integer, pInstaller As %Installer.Installer, pLogger As %Installer.AbstractLogger) As %Status [ CodeMode = objectgenerator, Internal ]
{
#; Let our XGL document generate code for this method.
Do %code.WriteLine(" If $Data(pVars, path)#10,path'="""" Set pVars(""MODDIR"")=path")
Quit ##class(%Installer.Manifest).%Generate(%compiledclass, %code, "PM")
}
ClassMethod AsyncSetup(Args...)
{
Try {
Set tSC = ..setup(Args...)
} Catch ex {
Set tSC = ex.AsStatus()
}
Do $System.Event.Signal($ZParent, tSC)
}
ClassMethod ZPMInit(pRegistry As %String = "", pAnalyticsTrackingID As %String = "")
{
If (pRegistry'="") {
$$$QuitOnError(##class(%ZPM.PackageManager).Shell("repo -t registry -name registry -url "_ pRegistry))
}
$$$QuitOnError(##class(%ZPM.PackageManager.Client.Settings).SetDefaultRegistry(pRegistry))
$$$QuitOnError(##class(%ZPM.PackageManager.Client.Settings).SetAnalyticsAvailable(1))
$$$QuitOnError(##class(%ZPM.PackageManager.Client.Settings).SetAnalyticsTrackingId(pAnalyticsTrackingID))
$$$QuitOnError(##class(%ZPM.PackageManager.Client.Settings).SetValue("ColorScheme",""))
$$$QuitOnError(##class(%ZPM.PackageManager.Client.Settings).SetValue("PipCaller",""))
$$$QuitOnError(##class(%ZPM.PackageManager.Client.Settings).SetValue("UseStandalonePip",""))
Quit $$$OK
}
ClassMethod ZPMLoad(pDirectoryName)
{
Quit ##class(%ZPM.PackageManager).Shell("load -nodev "_pDirectoryName)
}
ClassMethod ZPMCompile()
{
Quit ##class(%ZPM.PackageManager).Shell("ZPM compile")
}
ClassMethod CreateProjection(classname As %String, ByRef parameters As %String, modified As %String, qstruct) As %Status
{
#; Behave as simple installer
If (..#FILESCOUNT'>0)||(..#VERSION="") QUIT $$$OK
#; This flag used in development
if $DATA(^%ZPM("skipinstall")) QUIT $$$OK
#; Installed version newer
if '..NeedInstall() QUIT $$$OK
Write !,"Extract package"
$$$QuitOnError(..ExtractPackage(.tModDir))
Write !,"Install ZPM"
set ts = $ZHOROLOG
set logFile=##class(%File).TempFilename()
JOB ..AsyncSetup(tModDir, 3):(:::logFile):0
if '$test {
quit $$$ERROR($$$GeneralError, "Job did not start")
}
#; Wait setup
set child=$zchild
Set tSC = $$$OK
do {
hang 1
Set $Listbuild(res, tSC) = $System.Event.WaitMsg("", 10)
Quit:res=1
} while $data(^$JOB(child))
set fs=##class(%Stream.FileCharacter).%New()
set fs.Filename=logFile
while 'fs.AtEnd {
write !,fs.ReadLine()
}
if ##class(%File).Delete(logFile)
Write !,"Time spent ",$ZHOROLOG - ts
QUIT tSC
}
/// Check currently installed version, if any
ClassMethod NeedInstall(Output version As %String)
{
try {
set module = ##class(%ZPM.PackageManager.Developer.Module).NameOpen("zpm")
if '$ISOBJECT(module) {
RETURN 1
}
set version = ##class(%ZPM.PackageManager.Core.SemanticVersion).FromString(..#VERSION)
if $isobject(version) {
RETURN version.Follows(module.Version)
}
} CATCH ex {
}
QUIT 1
}
ClassMethod MakeFile(pPackagePath As %String, pInstaller As %String)
{
set tPackage = ##class(%Stream.FileBinary).%New()
set tInstaller = ##class(%Stream.FileCharacter).%New()
if tPackage.LinkToFile(pPackagePath),tInstaller.LinkToFile(pInstaller) {
$$$QuitOnError(..Make(tPackage, tInstaller))
Quit tInstaller.%Save()
}
Quit $$$OK
}
ClassMethod Make(pPackage As %Stream.Object, ByRef pStream As %Stream.TmpCharacter) As %Status
{
Set tSC = $$$OK
try {
Set tModule = ##class(%ZPM.PackageManager.Developer.Module).NameOpen("zpm")
Set tmpDir = ..CreateTempDir()
$$$ThrowOnError(##class(%ZPM.PackageManager.Developer.Archive).Extract(pPackage, tmpDir))
Do ..CollectPackage(tmpDir, .tList, tmpDir)
Do ..CompactPackage(.tList)
Set param = ##class(%Dictionary.ParameterDefinition).%OpenId($CLASSNAME()_"||VERSION")
Set param.Default = tModule.VersionString
Do param.%Save()
Set param = ##class(%Dictionary.ParameterDefinition).%OpenId($CLASSNAME()_"||FILESCOUNT")
Set param.Default = $Get(tList)
Do param.%Save()
Set tSC = $SYSTEM.OBJ.ExportToStream($CLASSNAME()_".CLS", .pStream, "/diffexport/exportversion=iris2019.1")
} Catch e {
Set tSC = e.AsStatus()
}
QUIT tSC
}
ClassMethod CollectPackage(pPath As %String, Output pList, pRoot As %String)
{
set rs = ##class(%File).FileSetFunc(pPath)
WHILE rs.%Next() {
If rs.Type="D" {
do ..CollectPackage(rs.Name, .pList, pRoot)
} ELSE {
#; Set $LISTBUILD(url) = $SYSTEM.CSP.FilenameToUrls(rs.Name)
Set pList($INCREMENT(pList)) = $LISTBUILD(rs.Name, $EXTRACT(rs.Name, $LENGTH(pRoot) + 1, *))
}
}
}
ClassMethod CompactPackage(ByRef pList) As %Status
{
set file = ""
for i=1:1:$Get(pList) {
Set $LISTBUILD(file, name) = pList(i)
Write !,"Compact: ", file
set tmpFile = ##class(%File).TempFilename("gz")
set stream = ##class(%Stream.FileBinary).%New()
set stream.Filename = file
Set gzip = ##class(%Stream.FileBinaryGzip).%New()
Set gzip.Filename = tmpFile
do gzip.CopyFromAndSave(stream)
Set gzip = ##class(%Stream.FileBinary).%New()
Set gzip.Filename = tmpFile
Set xdata = ##class(%Dictionary.XDataDefinition).%New()
Set xdata.Name = "Data"_i
Set xdata.Description = name
Set xdata.SequenceNumber = 100 + i
Do xdata.parentSetObjectId($CLASSNAME())
set chunkSize = 22800
while 'gzip.AtEnd {
set data = gzip.Read(chunkSize)
set base64 = $SYSTEM.Encryption.Base64Encode(data)
do xdata.Data.WriteLine(base64)
set data1 = $SYSTEM.Encryption.Base64Decode(base64)
}
do xdata.%Save()
set hash = $SYSTEM.Encryption.SHA1HashStream(gzip)
do ##class(%File).Delete(tmpFile)
}
QUIT $$$OK
Quit $$$OK
}
ClassMethod ExtractPackage(Output pFolder As %String) As %String
{
Set pFolder = ..CreateTempDir()
For i=1:1:..#FILESCOUNT {
Set xdata = ##class(%Dictionary.XDataDefinition).%OpenId($CLASSNAME()_"||Data"_i)
Set name = xdata.Description
Set fileName = pFolder_name
Set tParentFolder = ##class(%File).ParentDirectoryName(fileName)
if '##class(%File).DirectoryExists(tParentFolder) {
Do ##class(%File).CreateDirectoryChain(tParentFolder)
}
set tmpFile = ##class(%File).TempFilename("gz")
set stream = ##class(%Stream.FileBinary).%New()
set stream.Filename = tmpFile
set prev = ""
set chunkSize = 30400
do {
set data = prev
if 'xdata.Data.AtEnd {
set data = data _ xdata.Data.Read()
}
set data = $ZSTRIP(data, "*C")
set prev = $EXTRACT(data, chunkSize + 1, *)
set data = $EXTRACT(data, 1, chunkSize)
set chunk = $SYSTEM.Encryption.Base64Decode(data)
do stream.Write(chunk)
} while (prev'="")||('xdata.Data.AtEnd)
do stream.%Save()
set gzip = ##class(%Stream.FileBinaryGzip).%New()
set gzip.Filename = tmpFile
set fs = ##class(%Stream.FileCharacter).%New()
set fs.Filename = fileName
do fs.CopyFromAndSave(gzip)
do ##class(%File).Delete(tmpFile)
}
Quit $$$OK
}
ClassMethod CreateTempDir() As %String
{
Set tDir = $$$FileTempDir
/// In some cases, the $zu(140, 17) will return a negated error code.
/// For example, it can return -28, where 28 is Unix errno for "no space left on device".
If tDir = ("-" _ -tDir) {
Throw ##class(%Exception.General).%New("Failed to create temporary directory. Error code: " _ -tDir)
}
Return ##class(%File).NormalizeDirectory(tDir)
}
}