summaryrefslogtreecommitdiff
path: root/modules/atoms/macros/atomsInstall.sci
blob: d6c10487fc350d2a95a809e26f1d68283b9933d8 (plain)
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2009-2010 - DIGITEO - Pierre MARECHAL <pierre.marechal@scilab.org>
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt

// End user function

// Installation of a toolbox

function result = atomsInstall(packages,section)

    result = [];

    // Load Atoms Internals lib if it's not already loaded
    // =========================================================================
    if ~ exists("atomsinternalslib") then
        load("SCI/modules/atoms/macros/atoms_internals/lib");
    end

    // Check write access on allusers zone
    // =========================================================================
    ATOMSALLUSERSWRITEACCESS = atomsAUWriteAccess();

    // Save the initial path
    // =========================================================================
    ATOMSINITIALPATH = pwd();

    // Get scilab version (needed for later)
    // =========================================================================
    sciversion = strcat(string(getversion("scilab")) + ".");

    // Check input parameters
    // =========================================================================

    rhs = argn(2);

    if rhs < 1 | rhs > 2 then
        error(msprintf(gettext("%s: Wrong number of input arguments: %d to %d expected.\n"),"atomsInstall",1,2))
    end

    if type(packages) <> 10 then
        error(msprintf(gettext("%s: Wrong type for input argument #%d: String array expected.\n"),"atomsInstall",1));
    end

    if size(packages(1,:),"*") > 2 then
        error(msprintf(gettext("%s: Wrong size for input argument #%d: mx1 or mx2 string matrix expected.\n"),"atomsInstall",1));
    end

    // Remove leading and trailing whitespace
    // =========================================================================
    packages = stripblanks(packages);

    // Operating system detection + Architecture detection
    // =========================================================================
    [OSNAME,ARCH,LINUX,MACOSX,SOLARIS,BSD] = atomsGetPlatform();

    // Verbose Mode ?
    // =========================================================================
    if strcmpi(atomsGetConfig("Verbose"),"True") == 0 then
        ATOMSVERBOSE = %T;
    else
        ATOMSVERBOSE = %F;
    end

    // Allusers/user management
    //   - If Allusers is equal to "allusers", packages will installed in the "allusers" section :
    //       → SCI/contrib    : location of the packages
    //       → SCI/.atoms     : ATOMS system files
    //   - Otherwise, packages will installed in the "user" section :
    //       → SCIHOME/atoms  : location of the packages
    //       → SCIHOME/.atoms : location of the packages & ATOMS system files
    // =========================================================================

    if rhs <= 1 then

        // By default:
        //  → Install in the "allusers" section if we have the write access to
        //    SCI directory
        //  → Install in the "user" otherwise

        if ATOMSALLUSERSWRITEACCESS then
            section = "allusers";
        else
            section = "user";
        end

    else

        // Process the 2nd input argument : section
        // Allusers can be a boolean or equal to "user" or "allusers"

        if type(section) <> 10 then
            error(msprintf(gettext("%s: Wrong type for input argument #%d: Single string expected.\n"),"atomsInstall",2));
        end

        if and(section<>["user","allusers"]) then
            error(msprintf(gettext("%s: Wrong value for input argument #%d: ''user'' or ''allusers'' expected.\n"),"atomsInstall",2));
        end

        // Check if we have the write access
        if (section=="allusers") & ~ ATOMSALLUSERSWRITEACCESS then
            error(msprintf(gettext("%s: You haven''t write access on this directory : %s.\n"),"atomsInstall",pathconvert(SCI+"/.atoms")));
        end
    end

    // Create needed directories
    // =========================================================================
    atoms_system_directory  = atomsPath("system" ,section);
    atoms_install_directory = atomsPath("install",section);
    atoms_session_directory = atomsPath("system","session");
    atoms_tmp_directory     = pathconvert( atomsPath("system" ,section) + "tmp_" + sprintf("%d\n",getdate("s")) );

    directories2create = [  atoms_system_directory ;   ..
    atoms_install_directory ;  ..
    atoms_session_directory ;  ..
    atoms_tmp_directory ];

    for i=1:size(directories2create,"*")
        if ~ isdir( directories2create(i) ) & (mkdir( directories2create(i) ) <> 1) then
            error(msprintf( ..
            gettext("%s: The directory ''%s'' cannot been created, please check if you have write access on this directory.\n"),..
            "atomsInstall", directories2create(i)));
        end
    end

    // Define the "archives" directory path
    // Create it if it's not exist
    // =========================================================================
    archives_directory = atoms_install_directory + "archives";

    if ~ isdir( archives_directory ) & (mkdir( archives_directory ) <> 1) then
        error(msprintf( ..
        gettext("%s: The directory ''%s'' cannot been created, please check if you have write access on this directory.\n"),..
        "atomsInstall", ..
        archives_directory));
    end

    // Complete packages matrix with empty columns
    // =========================================================================

    if size(packages(1,:),"*") == 1 then
        packages = [ packages emptystr(size(packages(:,1),"*"),1) ];
    end

    // "Archive" installation
    // =========================================================================

    from_localarchive = %F;

    for i=1:size(packages(:,1),"*")
        this_package = packages(i,1);

        if ~ isempty(regexp(this_package,"/(\.tar\.gz|\.tgz|\.zip)$/","o")) then

            if fileinfo( this_package ) then
                error(msprintf(gettext("%s: The file ''%s'' does not exist or is not read accessible.\n"),"atomsInstall",this_package));
            end

            // expand filename - bug 10707
            this_package = pathconvert(this_package, %f);

            tmp_dir = atomsExtract(this_package,atoms_tmp_directory);
            tmp_dir = pathconvert(atoms_tmp_directory+tmp_dir);

            if fileinfo( tmp_dir + "DESCRIPTION" ) then
                error(msprintf(gettext("%s: DESCRIPTION file cannot be found in the package ''%s''\n"),"atomsInstall",this_package));
            end

            this_package_description = atomsDESCRIPTIONread(tmp_dir + "DESCRIPTION");
            this_package_packages    = this_package_description("packages");

            // Get package name and version
            // -----------------------------------------------------------------

            this_package_name    = getfield(1,this_package_packages);
            this_package_name    = this_package_name(3);

            this_package_version = getfield(1,this_package_packages(this_package_name));
            this_package_version = this_package_version(3);

            // Save the extracted directory
            // -----------------------------------------------------------------

            this_package_description = atomsDESCRIPTIONaddField( ..
            this_package_description, ..
            this_package_name,        ..
            this_package_version,     ..
            "extractedDirectory",     ..
            tmp_dir);

            this_package_description = atomsDESCRIPTIONaddField( ..
            this_package_description, ..
            this_package_name,        ..
            this_package_version,     ..
            "archiveFile",            ..
            this_package);

            this_package_description = atomsDESCRIPTIONaddField( ..
            this_package_description, ..
            this_package_name,        ..
            this_package_version,     ..
            "fromRepository",         ..
            "0");

            // Save the DESCRIPTION_archives
            // -----------------------------------------------------------------

            if fileinfo( atoms_tmp_directory + "DESCRIPTION_archives" )<>[] then
                packages_description = atomsDESCRIPTIONread(atomsPath("system","session")+"DESCRIPTION_archives");
                packages_description = atomsDESCRIPTIONcat(packages_description,this_package_description);
            else
                packages_description = this_package_description;
            end

            atomsDESCRIPTIONwrite(packages_description,atomsPath("system","session")+"DESCRIPTION_archives");

            // change the packages var
            // -----------------------------------------------------------------
            packages(i,:) = [ this_package_name this_package_version ];


            // Installation from a local archive
            // -----------------------------------------------------------------
            from_localarchive = %T;

        end

    end

    // Force update the system information
    // =========================================================================
    if from_localarchive then
        if (atomsGetConfig("offLine") == "True" | atomsGetConfig("offline") == "True") then
            warning(msprintf(gettext("Option offline of ATOMS configuration is set to True. atomsSystemUpdate did not check the latest modules availables.")));
        else
            atomsDESCRIPTIONget(%T);
        end
    end

    // Get the install list
    // =========================================================================
    [install_package_list,dependency_tree] = atomsInstallList(packages,section);

    // Loop on install_package_list to print if a package has to be installed
    // or not
    // =========================================================================
    for i=1:size(install_package_list(:,1),"*")
        if install_package_list(i,1) == "+" then
            atomsDisp(msprintf("\t%s (%s) will be installed in the ''%s'' section\n",install_package_list(i,3),install_package_list(i,4),section));
        elseif install_package_list(i,1) == "~" then
            atomsDisp(msprintf("\t%s (%s) is already installed in the ''%s'' section and up-to-date\n",install_package_list(i,3),install_package_list(i,4),section));
        end
    end

    // Now really install the packages
    // =========================================================================

    for i=1:size(install_package_list(:,1),"*")

        toarchive = %T;

        this_package_name    = install_package_list(i,3);
        this_package_version = install_package_list(i,4);

        this_package_details = dependency_tree(this_package_name+" - "+this_package_version);

        if install_package_list(i,1) <> "+" then
            continue;
        end

        atomsDisp(msprintf("\tInstalling %s (%s) ...",this_package_name,this_package_version));

        // Define the path of the directory where will be installed this toolbox
        // =====================================================================
        this_package_directory = atomsPath("install",section) + this_package_name + filesep();

        // Create the parent directory of this toolbox if it's not already exist
        // =====================================================================

        if ~isdir(this_package_directory) & (mkdir(this_package_directory)<>1) then
            atomsError("error", ..
            msprintf( gettext("%s: The directory ""%s"" cannot been created, please check if you have write access on this directory.\n"), ..
            "atomsInstall", ..
            strsubst(this_package_directory,"\","\\") ));
        end

        // "Repository" installation ; Download and Extract
        // =====================================================================

        if this_package_details("fromRepository") == "1" then

            // Define the path of the downloaded file
            // =================================================================

            if isfield(this_package_details,"binaryName") then
                fileprefix = "binary";
            elseif isfield(this_package_details,OSNAME+ARCH+"Name") then
                fileprefix = OSNAME+ARCH;
            else
                fileprefix = OSNAME;
            end

            fileout     = pathconvert(this_package_directory+this_package_details(fileprefix+"Name"),%F);
            filein      = this_package_details(fileprefix+"Url");
            filemd5     = this_package_details(fileprefix+"Md5");
            filearchive = archives_directory + filesep() + this_package_details(fileprefix+"Name");

            if isfile(filearchive) & getmd5(filearchive)==filemd5 then
                // Check if the file has already been successfully downloaded
                // =============================================================
                if copyfile( filearchive , this_package_directory ) <> 1 then
                    atomsError("error", ..
                    msprintf(gettext("%s: Error while copying the file ''%s'' to the directory ''%s''.\n"), ..
                    "atomsInstall", ..
                    strsubst(filearchive,"\","\\"), ..
                    strsubst(this_package_directory,"\","\\") ));
                end
                toarchive = %F;
            else
                // Launch the download
                // =============================================================
                atomsDownload(filein,fileout,filemd5);
            end

            // unarchive it
            // =================================================================
            this_package_details("extractedDirectory") = this_package_directory + atomsExtract(fileout,this_package_directory);
        end

        // Rename the created directory
        // =====================================================================

        if getos() == "Windows" then
            rename_cmd = "rename """+this_package_details("extractedDirectory")+""" """+this_package_version+"""";
        else
            rename_cmd = "mv """+this_package_details("extractedDirectory")+""" """+this_package_directory+this_package_version+"""";
        end

        [rep,stat,err]=unix_g(rename_cmd);

        if stat <> 0 then

            // Second try after a sleep
            // This is needed on windows platforms

            if getos() == "Windows" then
                sleep(2000);
                [rep,stat,err]=unix_g(rename_cmd);
            end

            if stat <> 0 then
                atomsError("error", ..
                msprintf(gettext("%s: Error while creating the directory ''%s''.\n"),..
                "atomsInstall", ..
                strsubst(pathconvert(this_package_directory+this_package_version),"\","\\") ));
            end

        end

        // Move the created directory
        // → Only under windows
        // → Only if it's a local package
        // =====================================================================

        if getos() == "Windows" & (this_package_details("fromRepository") == "0") then

            move_cmd = "move """+atoms_tmp_directory+this_package_version+""" """+pathconvert(this_package_directory,%F)+"""";

            [rep,stat,err]=unix_g(move_cmd);

            if stat <> 0 then

                // Second try after a sleep
                // This is needed on windows platforms

                if getos() == "Windows" then
                    sleep(2000);
                    [rep,stat,err]=unix_g(move_cmd);
                end

                atomsError("error", ..
                msprintf(gettext("%s: Error while creating the directory ''%s''.\n"),..
                "atomsInstall",..
                strsubst(pathconvert(this_package_directory+this_package_version),"\","\\") ));
            end

        end

        // Register the successfully installed package
        // =====================================================================

        if install_package_list(i,2) == "U" then
            // Intentionnaly Installed
            this_package_status = "I";
        else
            // Automatically installed
            this_package_status = "A";
        end

        atomsInstallRegister(this_package_name,this_package_version,this_package_status,section);

        // Autoload the toolbox unless precised
        // =====================================================================

        if ~ (atomsGetConfig("autoloadAddAfterInstall") == "False") then

            // Add a package to the autoload list only if it's intentionnaly
            // installed

            if this_package_status=="I" then
                // If another version is on the autoload list, remove it
                atomsAutoloadDel([this_package_name ""                   section],section);
                atomsAutoloadAdd([this_package_name this_package_version section],section);
            end

        end

        // Move the archive file (.tar.gz or .zip file) to the archive directory
        // =====================================================================

        if toarchive then
            if this_package_details("fromRepository")=="1" then
                this_package_archive = fileout;
            else
                this_package_archive = this_package_details("archiveFile");
            end

            if copyfile( this_package_archive , archives_directory ) <> 1 then
                if isfile(this_package_archive) then
                    atomsError("warning", ..
                    msprintf(gettext("%s: Previously existing file with the same name in ''%s''.\n"), ..
                    "atomsInstall", ..
                    strsubst(archives_directory,"\","\\") ));
                else
                    atomsError("error", ..
                    msprintf(gettext("%s: Error while copying the file ''%s'' to the directory ''%s''.\n"), ..
                    "atomsInstall", ..
                    strsubst(this_package_archive,"\","\\"), ..
                    strsubst(archives_directory,"\","\\") ));
                end
            end
        end

        if this_package_details("fromRepository")=="1" then
            mdelete( fileout );
        end

        // Fill the result matrix
        // =====================================================================
        result = [ result ; atomsGetInstalledDetails([this_package_name this_package_version]) ];

        // Save the description
        // Needed to remove the toolbox if it's has been removed from the
        // repository list
        // =====================================================================

        DESCRIPTION_file = atoms_system_directory+"DESCRIPTION_installed";

        if isempty(fileinfo(DESCRIPTION_file)) then
            DESCRIPTION = struct();
        else
            DESCRIPTION = atomsDESCRIPTIONread(DESCRIPTION_file);
        end

        DESCRIPTION = atomsDESCRIPTIONadd(DESCRIPTION,this_package_name,this_package_version,this_package_details);
        atomsDESCRIPTIONwrite(DESCRIPTION,DESCRIPTION_file);

        // Sucess message if needed
        // =====================================================================
        atomsDisp(msprintf(" success"));

    end

    // The TMPDIR DESCRIPTION_archives is no more needed
    // =========================================================================

    if ~ isempty(fileinfo(atoms_tmp_directory + "DESCRIPTION_archives")) then
        mdelete(atoms_tmp_directory + "DESCRIPTION_archives");
    end

    // The atoms_tmp_directory is no more needed
    // =========================================================================

    if ~ isempty(fileinfo(atoms_tmp_directory)) then
        rmdir(atoms_tmp_directory,"s");
    end

    // Update the dependencies of packages that use another version of packages
    // that have been installed
    // =========================================================================

    for i=1:size( result(:,1) , "*" )

        packages_out = atomsUpdateDeps([result(i,1) result(i,2)],section);

        if ATOMSVERBOSE then
            for j=1:size(packages_out(:,1),"*")
                atomsDisp(msprintf("\t%s (%s) will now use the version %s of the package %s",packages_out(j,1),packages_out(j,2),result(i,1),result(i,2)));
            end
        end

    end

    // Remove orphan packages
    // =========================================================================

    if section=="all" then
        sections = ["user";"allusers"];
    else
        sections = section;
    end

    for i=1:size(sections,"*")

        orphan_list = atomsOrphanList(sections(i));

        if ~ isempty(orphan_list) then
            atomsRemove( [ orphan_list(:,1) orphan_list(:,2) ] , sections(i) );
        end

    end

    // Go to the initial location
    // =========================================================================
    chdir(ATOMSINITIALPATH);

endfunction