File: macBundleCreation.py

package info (click to toggle)
massxpert 7.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 71,188 kB
  • sloc: cpp: 47,431; xml: 23,090; javascript: 22,231; python: 501; sh: 272; makefile: 88
file content (354 lines) | stat: -rwxr-xr-x 10,999 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python3

import os
import argparse
import re
import shutil
import subprocess
from time import gmtime, strftime


# At this point we have all the required data: bundle directory and its location
# in the filesystem, along with the binary program file to be used as a starting
# point for all the framework stuff.

def createBundleSubDirs(dirDic):

    macosDir = dirDic["bundleDirAbsPathName"] + "/Contents/MacOS";
    if not os.path.exists(macosDir):
        os.mkdir(macosDir);
        print("Created " + macosDir + "\n");
    dirDic["macosDir"] = macosDir;
    
    frameworksDir = dirDic["bundleDirAbsPathName"] + "/Contents/Frameworks";
    if not os.path.exists(frameworksDir):
        os.mkdir(frameworksDir);
        print("Created " + frameworksDir + "\n");
    dirDic["frameworksDir"] = frameworksDir; 

    pluginsDir = dirDic["bundleDirAbsPathName"] + "/Contents/Plugins";
    if not os.path.exists(pluginsDir):
        os.mkdir(pluginsDir);
        print("Created " + pluginsDir + "\n");
    dirDic["pluginsDir"] = pluginsDir; 
    
    platformsDir = pluginsDir + "/platforms";
    # In this platforms directory we'll later copy the libqcocoa.dylib (see below)
    if not os.path.exists(platformsDir):
        os.mkdir(platformsDir);
        print("Created " + platformsDir + "\n");
    dirDic["platformsDir"] = platformsDir;
    
    sqldriversDir = pluginsDir + "/sqldrivers";
    # In this sqldrivers directory we'll later copy the libqsqlite.dylib (see below)
    if not os.path.exists(sqldriversDir):
        os.mkdir(sqldriversDir);
        print("Created " + sqldriversDir + "\n");
    dirDic["sqldriversDir"] = sqldriversDir;
    
    resourcesDir = dirDic["bundleDirAbsPathName"] + "/Contents/Resources";
    if not os.path.exists(resourcesDir):
        os.mkdir(resourcesDir);
        print("Created " + resourcesDir + "\n");
    dirDic["resourcesDir"] = resourcesDir;
    

def usrLocalDepLibs(binaryImage):
    if not os.path.exists(binaryImage):
        print("Could not find " + binaryImage + " .Exiting");
        exit(1);

    # Use the otool utility to check for the dependencies of binaryImage. This
    # tool is roughly like using objdump -p <binaryImage> | grep NEEDED on
    # GNU/Linux.

    completedProcess = subprocess.run(["otool", "-L", binaryImage], stdout=subprocess.PIPE)
    completedProcess.check_returncode();
    
    output = completedProcess.stdout.decode("utf-8");
    # print(output);
    
    # The various dependency libraries are all contained in the /opt/local/lib
    # directory. The output thus contains the full absolute path of the dep
    # libs.

    # Thus retain only the "/local/lib"-containing lines from the single string
    # that is output by the command call.
    
    lines = output.split("\n");
    # print(lines);
    libDependencies = [ ];
    
    for line in lines:
        if "/local/lib" in line:
            print(line + "\n");

            # Now extract only the library absolute path.
            match = re.match(r"^\s*(/\S+) .*$", line)
            if match:
                # print(match.group(1));
                # Store the dep lib absolute path
                libDependencies.append(match.group(1));
            
    #print("All the local/lib dependencies of the binary " + binaryImage + ": \n");
    #print('\n'.join(libDependencies));
       
    return libDependencies;


def copyLibsToBundle(libList, frameworksDir, overwrite = False):

    if not os.path.exists(frameworksDir):
        print("Could not find " + frameworksDir + " .Exiting");
        exit(1);

    copiedFilesList = [];

    for lib in libList:
        if not os.path.exists(frameworksDir + "/" + os.path.basename(lib)) or overwrite:
            shutil.copy2(lib, frameworksDir);
            copiedFilesList.append(lib);

    print("List of copied libraries:\n" + "\n".join(copiedFilesList) + "\n");

    completedProcess = subprocess.run(["ls", frameworksDir], stdout=subprocess.PIPE)
    completedProcess.check_returncode();
    output = completedProcess.stdout.decode("utf-8");

    print("\nThe list of frameworks copied to the bundle:\n" + output);


def doInstallNameDashI(lib, frameworksDir):
    
    if not os.path.exists(frameworksDir):
        print("Could not find " + frameworksDir + " .Exiting");
        exit(1);
    if not os.path.exists(frameworksDir + "/" + lib):
        print("Could not find " + frameworksDir + "/" + lib + " .Exiting");
        exit(1);

    string1 = "@executable_path/../Frameworks/{lib}".format(lib=lib);
    string2 = "{frameworksDir}/{lib}".format(lib=lib, frameworksDir=frameworksDir);

    completedProcess = subprocess.run(["install_name_tool",
        "-id", string1, string2], stdout=subprocess.PIPE)
    completedProcess.check_returncode();
    output = completedProcess.stdout.decode("utf-8");
    # print(output);


def doInstallNameDashChange(binaryImage, directory):

    if not os.path.exists(directory):
        print("Could not find " + directory + " .Exiting");
        exit(1);
    if not os.path.exists(directory + "/" + binaryImage):
        print("Could not find " + directory + "/" + binaryImage + " .Exiting");
        exit(1);

    print("Processing binaryImage: " + binaryImage + "\n");
    
    # returns the dependency libraries as system paths.
    depLibs = usrLocalDepLibs(directory + "/" + binaryImage);

    print("Dep libs:\n" + " ".join(depLibs) + "\n");

    for depLib in depLibs:

        print("Sub-processing lib: " + depLib + "\n");

        baseName = os.path.basename(depLib);

        string1 = "{depLib}".format(depLib = depLib);
        string2 = "@executable_path/../Frameworks/{baseName}".format(baseName =
                baseName);
        string3 = "{directory}/{binaryImage}".format(directory =
                directory, binaryImage = binaryImage);
        
        commandLine = ["install_name_tool", "-change", string1, string2, string3];
        print("Command line:\n" + " ".join(commandLine) + "\n");

        completedProcess = subprocess.run(["install_name_tool",
            "-change", string1, string2, string3], stdout=subprocess.PIPE)
        completedProcess.check_returncode();
        output = completedProcess.stdout.decode("utf-8");
        # print(output);


def doProgramSpecific(dirDic):
   
    if(dirDic["programName"] == "massXpert"):
        doMassXpert(dirDic);

    if(dirDic["programName"] == "mineXpert"):
        doMineXpert(dirDic);


def doMassXpert(dirDic):

    # The data directory is filled-in by CMake make install directives
    # in Contents/Resources/massxpert/data
    # and the documentation goes in 
    # Contents/Resources/massxpert/doc 
   
    print("Copy the massXpert user manual to the bundle\n");
    shutil.copy2(dirDic["devDirName"] + "/doc/user-manuals/massxpert-doc.pdf",
        dirDic["bundleDirAbsPathName"] + "/Contents/Resources/massxpert/doc");
    

def doMineXpert(dirDic):

    print("Copy the mineXpert user manual to the bundle\n");
    shutil.copy2(dirDic["devDirName"] + "/doc/user-manuals/minexpert-doc.pdf",
        dirDic["bundleDirAbsPathName"] + "/Contents/Resources/minexpert/doc");
    

def backupLibraries(libList):

    # print("All backupLibs:\n");
    # print(str(libList));
    
    # allLibs = libList[0].split(" ");
    
    # print("allLibs\n");
    # print(allLibs);
    # print("done allLibs\n");
    
    for lib in libList:
    
        # print("Making a backup of library: " + lib + "\n");
        baseName = os.path.basename(lib);
        dirName = os.path.dirname(lib);
        backupDirName = dirName + "/backup";
    
        if not os.path.exists(backupDirName):
            os.mkdir(backupDirName);
            print("Creating " + backupDirName);
            if not os.path.exists(backupDirName):
                print("Could not create " + backupDirName + " Exiting.\n");
                exit(1);
        
        bkpLibName = backupDirName + "/" + baseName;
        
        command = "shutil.copy2({lib}, {bkpLibName})".format(lib = lib,
                bkpLibName = bkpLibName);
        print(command);
    
        newPath = shutil.copy2(lib, bkpLibName);
        if newPath != bkpLibName:
            print("Failed to backup " + lib + "\n");
            exit(1);
    
    print("\nDone making backup of all the libraries\n");
    

def renameLibraries(libList, dirDic):

    # print("\nRename the libraries in list:\n");
    # print(libList);
    # print("\n");

    # allLibs = libList[0].split(" ");

    # Open a text file with write bit set

    libRenamingScriptFile = dirDic["devDirName"] + "/libRenamingScript.sh";
    dirDic["libRenamingScriptFile"] = libRenamingScriptFile;

    f = open(libRenamingScriptFile , 'w')

    for lib in libList:

        baseName = os.path.basename(lib);
        dirName = os.path.dirname(lib);

        wasLibName = dirName + "/was-" + baseName;
        
        command = "mv -v {lib} {wasLibName}\n".format(lib = lib,
                wasLibName = wasLibName);

        f.write(command);

    f.close();

    print("\nDone scripting all the renaming calls in " 
            + libRenamingScriptFile
            + "\n");


def reinstateLibraries(libList, dirDic):
  
    # print("\nReinstate the libraries in list:\n");
    # print(libList);
    # print("\n");

    # allLibs = backupLibs[0].split(" ");

    libReinstatingScriptFile = dirDic["devDirName"] + "/libReinstatingScript.sh";
    dirDic["libReinstatingScriptFile"] = libReinstatingScriptFile;

    f = open(libReinstatingScriptFile , 'w')

    for lib in libList:

        baseName = os.path.basename(lib);
        dirName = os.path.dirname(lib);

        wasLibName = dirName + "/was-" + baseName;
        
        command = "mv -v {wasLibName} {lib}\n".format(lib = lib,
                wasLibName = wasLibName);

        f.write(command);

    f.close();

    print("\nDone scripting all the reinstating calls in " 
            + libReinstatingScriptFile
            + "\n");


def moveBundleToDesktop(dirDic):

    bundleDirAbsPathName = dirDic["bundleDirAbsPathName"];
    print("bundleDirAbsPathName: " + bundleDirAbsPathName);

    if not os.path.exists(bundleDirAbsPathName):
        print("The bundle directory does not exist.\n");

    homeDir = os.getenv('USERPROFILE') or os.getenv('HOME')
    if not os.path.exists(homeDir):
        print("Home directory not found.\n");

    print("homeDir: " + homeDir);

    destDir = homeDir + "/Desktop/" + dirDic["bundleDir"];
    print("destDir: " + destDir);

    backupDir = destDir + strftime("-at-%Y%H%M%S", gmtime());
    print("backupDir: " + backupDir);

    print("Copying bundle to " + destDir + "\n");

    if os.path.exists(destDir):
        print("Creating backup of destDir to backupDir\n");
        shutil.move(destDir, backupDir);

    newDir = shutil.copytree(bundleDirAbsPathName, destDir);

    if newDir != destDir:
        print("Failed to copy the bundle to the Desktop.\n");

    print("Copied " + bundleDirAbsPathName + " to " + destDir);