summaryrefslogtreecommitdiffstats
path: root/release-tools/bldinstallercommon.py
blob: 95eceb968b30c39d0356ed6c66470b71768806c5 (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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
#!/usr/bin/env python
###############################################
#
# Copyright (C) 2012 Digia Plc
# For any questions to Digia, please use contact form at http://qt.digia.com
#
# $QT_BEGIN_LICENSE:LGPL$
# GNU Lesser General Public License Usage
# This file may be used under the terms of the GNU Lesser General Public
# License version 2.1 as published by the Free Software Foundation and
# appearing in the file LICENSE.LGPL included in the packaging of this
# file. Please review the following information to ensure the GNU Lesser
# General Public License version 2.1 requirements will be met:
# http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
#
# GNU General Public License Usage
# Alternatively, this file may be used under the terms of the GNU General
# Public License version 3.0 as published by the Free Software Foundation
# and appearing in the file LICENSE.GPL included in the packaging of this
# file. Please review the following information to ensure the GNU General
# Public License version 3.0 requirements will be met:
# http://www.gnu.org/copyleft/gpl.html.
#
# $QT_END_LICENSE$
#
# If you have questions regarding the use of this file, please use
# contact form at http://qt.digia.com
#
###############################################

import errno
import fnmatch
import os
import platform
import re
import shutil
import subprocess
from subprocess import PIPE, STDOUT
import sys
import stat
import tarfile
import urllib2
import zipfile

# need to include this for win platforms as long path names
# cause problems
if platform.system().lower().startswith('win'):
    import win32api

SCRIPT_ROOT_DIR         = ''
PLATFORM_SUFFIX         = 'unknown'
IS_UNIX_PLATFORM        = False
IS_LINUX_PLATFORM       = False
IS_SOLARIS_PLATFORM     = False
IS_MAC_PLATFORM         = False
IS_WIN_PLATFORM         = False
DEBUG_RPATH             = False
MAX_DEBUG_PRINT_LENGTH  = 10000


###############################
# function
###############################
def init_common_module(root_path):
    global SCRIPT_ROOT_DIR
    SCRIPT_ROOT_DIR = root_path
    set_platform_specific_data()


###############################
# function
###############################
class head_request(urllib2.Request):
    def get_method(self):
        return 'HEAD'

def is_content_url_valid(url):
    # check first if the url points to file on local file system
    if (os.path.isfile(url)):
        return True
    # throws error if url does not point to valid object
    result = False
    try:
        response = urllib2.urlopen(head_request(url))
        result = True
    except Exception:
        pass

    return result;


###############################
# function
###############################
CURRENT_DOWNLOAD_PERCENT = 0
def dlProgress(count, blockSize, totalSize):
    global CURRENT_DOWNLOAD_PERCENT
    percent = int(count*blockSize*100/totalSize)
    # produce only reasonable amount of prints into stdout
    if percent > CURRENT_DOWNLOAD_PERCENT:
        CURRENT_DOWNLOAD_PERCENT = percent
        sys.stdout.write("\r" + "     Downloading: %d%%" % percent)
        sys.stdout.flush()
    if count*blockSize >= totalSize:
        CURRENT_DOWNLOAD_PERCENT = 0
        print '\n'


###############################
# function
###############################
def set_platform_specific_data():
    global PLATFORM_SUFFIX
    global IS_UNIX_PLATFORM
    global IS_LINUX_PLATFORM
    global IS_SOLARIS_PLATFORM
    global IS_MAC_PLATFORM
    global IS_WIN_PLATFORM
    plat = platform.system().lower()
    if plat.startswith('win'):
        PLATFORM_SUFFIX = 'win'
        IS_WIN_PLATFORM = True
    elif plat.startswith('linux'):
        PLATFORM_SUFFIX = 'linux'
        IS_UNIX_PLATFORM = True
        IS_LINUX_PLATFORM = True
    elif plat.startswith('sun'):
        PLATFORM_SUFFIX = 'solaris'
        IS_UNIX_PLATFORM = True
        IS_SOLARIS_PLATFORM = True
    elif plat.startswith('darwin'):
        PLATFORM_SUFFIX = 'mac'
        IS_UNIX_PLATFORM = True
        IS_MAC_PLATFORM = True
    else:
        print '*** Unsupported platform, abort!'
        sys.exit(-1)


###############################
# function
###############################
def get_platform_suffix():
    return PLATFORM_SUFFIX


###############################
# function
###############################
def is_unix_platform():
    return IS_UNIX_PLATFORM

def is_linux_platform():
    return IS_LINUX_PLATFORM

def is_solaris_platform():
    return IS_SOLARIS_PLATFORM

def is_mac_platform():
    return IS_MAC_PLATFORM

def is_win_platform():
    return IS_WIN_PLATFORM


###############################
# function
###############################
def get_executable_suffix():
    if IS_WIN_PLATFORM:
        return '.exe'
    else:
        return ''


###############################
# function
###############################
def make_files_list(directory, rgxp):
    """Populate and return 'fileslist[]' with all files inside 'directory' matching 'regx'"""
    # if 'directory' is not a directory, exit with error
    if not os.path.isdir(directory):
        print '*** Error, Given path is not valid: ' + directory
        sys.exit(-1)
    regex = re.compile(rgxp)
    filelist = []
    for root, dirs, files in os.walk(directory):
        for name in files:
            if regex.search(name):
                path = os.path.join(root, name)
                filelist.append(path)

    return filelist[:]


###############################
# function
###############################
def findInSubdirectory(filename, subdirectory=''):
    if subdirectory:
        path = subdirectory
    else:
        path = os.getcwd()
    for root, dirs, names in os.walk(path):
        if filename in names:
            return os.path.join(root, filename)
    raise '*** Error! File not found!'


###############################
# function
###############################
def move_tree(srcdir, dstdir, pattern=None):
    # windows has length limit for path names so try to truncate them as much as possible
    global IS_WIN_PLATFORM
    if IS_WIN_PLATFORM:
        srcdir = win32api.GetShortPathName(srcdir)
        dstdir = win32api.GetShortPathName(dstdir)
    # dstdir must exist first
    srcnames = os.listdir(srcdir)
    for name in srcnames:
        srcfname = os.path.join(srcdir, name)
        dstfname = os.path.join(dstdir, name)
        if is_win_platform():
            if len(srcfname) > 255:
                print 'given srcfname length (' + len(srcfname) + ') too long for Windows: ' + srcfname
                sys.exit(-1)
            if len(dstfname) > 255:
                print 'given dstfname length (' + len(dstfname) + ') too long for Windows: ' + dstfname
                sys.exit(-1)
        if os.path.isdir(srcfname) and not os.path.islink(srcfname):
            os.mkdir(dstfname)
            move_tree(srcfname, dstfname)
        elif pattern is None or fnmatch.fnmatch(name, pattern):
            os.rename(srcfname, dstfname)


###############################
# function
###############################
def copy_tree(source_dir, dest_dir):
    # windows has length limit for path names so try to truncate them as much as possible
    if IS_WIN_PLATFORM:
        source_dir = win32api.GetShortPathName(source_dir)
        dest_dir = win32api.GetShortPathName(dest_dir)
    src_files = os.listdir(source_dir)
    for file_name in src_files:
        full_file_name = os.path.join(source_dir, file_name)
        if is_win_platform():
            if len(full_file_name) > 255:
                print 'given full_file_name length (' + len(full_file_name) + ') too long for Windows: ' + full_file_name
                sys.exit(-1)
        if (os.path.isdir(full_file_name)):
            create_dirs(dest_dir + os.sep + file_name)
            copy_tree(full_file_name, dest_dir + os.sep + file_name)
        if (os.path.isfile(full_file_name)):
            shutil.copy(full_file_name, dest_dir)


###############################
# function
###############################
def handle_remove_readonly(func, path, exc):
  excvalue = exc[1]
  if func in (os.rmdir, os.remove) and excvalue.errno == errno.EACCES:
      os.chmod(path, stat.S_IRWXU| stat.S_IRWXG| stat.S_IRWXO) # 0777
      func(path)
  else:
      raise

def remove_tree(source_dir):
    if IS_WIN_PLATFORM:
        if os.path.exists(source_dir):
            source_dir = win32api.GetShortPathName(source_dir)
            cmd_args = ['rmdir', source_dir, '/S', '/Q']
            do_execute_sub_process(cmd_args, SCRIPT_ROOT_DIR, True)
    else:
        shutil.rmtree(source_dir)


###############################
# function
###############################
# substitute all matches in files with replacement_string
def replace_in_files(filelist, regexp, replacement_string):
    regexp_compiled=re.compile(regexp)
    for xfile in filelist:
        replaceflag=0
        readlines=open(xfile,'r').readlines()
        listindex = -1
        for currentline in readlines:
            listindex = listindex + 1
            if regexp_compiled.search(currentline):
                # substitute
                f=re.sub(regexp,replacement_string,currentline)
                # update the whole file variable ('readlines')
                readlines[listindex] = f
                replaceflag=1
        # if some text was replaced overwrite the original file
        if replaceflag==1:
            # open the file for writting
            write_file=open(xfile,'w')
            # overwrite the file
            for line in readlines:
                write_file.write(line)
            # close the file
            write_file.close()


###############################
# function
###############################
def safe_config_key_fetch(conf, section, key):
    if not conf.has_section(section):
        return ''
    if not conf.has_option(section, key):
        return ''
    return config_section_map(conf, section)[key]


###############################
# function
###############################
def config_section_map(conf, section):
    dict1 = {}
    options = conf.options(section)
    for option in options:
        try:
            dict1[option] = conf.get(section, option)
            if dict1[option] == -1:
                print('skip: %s' % option)
        except:
            print('exception on %s!' % option)
            dict1[option] = None
    return dict1


###############################
# function
###############################
def dump_config(conf, name):
    # dump entire config file
    print '------------------------------'
    print '- Config: ' + name
    print '------------------------------'
    for section in conf.sections():
        print '[' + section + ']'
        for option in conf.options(section):
            print ' ', option, '=', conf.get(section, option)
    print '------------------------------'


###############################
# function
###############################
def create_dirs(path_to_be_created):
    if not os.path.exists(path_to_be_created):
        try:
            os.makedirs(path_to_be_created)
        except:
            print '*** Failed to create dir: ' + path_to_be_created
            sys.exit(-1)


###############################
# Function
###############################
def locate_executable(directory, file_name):
    for root, dirs, files in os.walk(directory):
        for basename in files:
            if fnmatch.fnmatch(basename, file_name):
                filename = os.path.join(root, basename)
                if is_executable(filename):
                    return filename


###############################
# Function
###############################
def is_executable(path):
    plat = platform.system().lower()
    if IS_WIN_PLATFORM:
        if path.endswith('.exe') or path.endswith('.com'):
            return True
    elif IS_LINUX_PLATFORM:
        return (re.search(r':.* ELF',
                          subprocess.Popen(['file', '-L', path],
                                           stdout=subprocess.PIPE).stdout.read())
                is not None)
    elif IS_SOLARIS_PLATFORM:
        return (re.search(r':.* ELF',
                          subprocess.Popen(['file', '-dh', path],
                                           stdout=subprocess.PIPE).stdout.read())
                is not None)
    elif IS_MAC_PLATFORM:
        return (re.search(r'executable',
                          subprocess.Popen(['file', path],
                                           stdout=subprocess.PIPE).stdout.read())
                is not None)
    else:
        print '*** Error, is_executable not implemented yet!'
        sys.exit(-1)

    return False


###############################
# Function
###############################
def requires_rpath(file_path):
    if IS_WIN_PLATFORM:
        return False
    elif IS_LINUX_PLATFORM or IS_SOLARIS_PLATFORM:
        base = os.path.basename(file_path)
        filename, ext = os.path.splitext(base)
        # filter out some files from search, TODO
        m = re.match('\.o|\.h|\.png|\.htm|\.html|\.qml|\.qrc|\.jpg|\.svg|\.pro|\.pri|\.desktop|\.sci|\.txt|\.qdoc', ext)
        if m:
            return False
        if filename.lower() == 'qmake':
            return False
        elif ext.lower() == '.so':
            return True
        else:
            return is_executable(file_path)
    elif IS_MAC_PLATFORM:
        return False
    else:
        print '*** Unsupported platform!'
        sys.exit(-1)

    return False


###############################
# Function
###############################
def pathsplit(p, rest=[]):
    (h,t) = os.path.split(p)
    if len(h) < 1: return [t]+rest
    if len(t) < 1: return [h]+rest
    return pathsplit(h,[t]+rest)

def commonpath(l1, l2, common=[]):
    if len(l1) < 1: return (common, l1, l2)
    if len(l2) < 1: return (common, l1, l2)
    if l1[0] != l2[0]: return (common, l1, l2)
    return commonpath(l1[1:], l2[1:], common+[l1[0]])

def calculate_relpath(p1, p2):
    (common,l1,l2) = commonpath(pathsplit(p1), pathsplit(p2))
    p = []
    if len(l1) > 0:
        tmp = '..' + os.sep
        p = [ tmp * len(l1) ]
    p = p + l2
    return os.path.join( *p )


##############################################################
# Calculate the relative RPath for the given file
##############################################################
def calculate_rpath(file_full_path, destination_lib_path):
    if not (os.path.isfile(file_full_path)):
        print '*** Not a valid file: ' + file_full_path
        sys.exit(-1)

    bin_path    = os.path.dirname(file_full_path)
    path_to_lib = os.path.abspath(destination_lib_path)
    full_rpath = ''
    if path_to_lib == bin_path:
        full_rpath = '$ORIGIN'
    else:
        rp = calculate_relpath(bin_path, path_to_lib)
        full_rpath = '$ORIGIN' + os.sep + rp

    if DEBUG_RPATH:
        print '        ----------------------------------------'
        print '         RPath target folder: ' + path_to_lib
        print '         Bin file:            ' + file_full_path
        print '         Calculated RPath:    ' + full_rpath

    return full_rpath


##############################################################
# Handle the RPath in the given component files
##############################################################
def handle_component_rpath(component_root_path, destination_lib_path):
    print '        @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'
    print '        Handle RPath'
    print ''
    print '        Component root path:  ' + component_root_path
    print '        Destination lib path: ' + destination_lib_path

    # initialize the file list
    fileslist = []
    # loop on all files
    for root, dirs, files in os.walk(component_root_path):
        for name in files:
            file_full_path = os.path.join(root, name)
            if not os.path.isdir(file_full_path) and not os.path.islink(file_full_path):
                if requires_rpath(file_full_path):
                    dst = os.path.normpath(component_root_path + os.sep + destination_lib_path)
                    rp = calculate_rpath(file_full_path, dst)
                    #print '         RPath value: [' + rp + '] for file: [' + file_full_path + ']'
                    cmd_args = ['chrpath', '-r', rp, file_full_path]
                    #force silent operation
                    do_execute_sub_process_get_std_out(cmd_args, SCRIPT_ROOT_DIR, True, False)


###############################
# function
###############################
def do_execute_sub_process(args, execution_path, abort_on_fail):
    print '      --------------------------------------------------------------------'
    print '      Executing:      [' + list_as_string(args) + ']'
    print '      Execution path: [' + execution_path + ']'
    try:
        os.chdir(execution_path)
        if IS_WIN_PLATFORM:
            theproc = subprocess.Popen(args, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=False)
        else:
            theproc = subprocess.Popen(args)
        output = theproc.communicate()[0]
        if theproc.returncode:
            output = output[len(output) - MAX_DEBUG_PRINT_LENGTH:] if len(output) > MAX_DEBUG_PRINT_LENGTH else output
            print output
            print '*** Execution failed with code: %s' % str(theproc.returncode)
            if abort_on_fail:
                sys.exit(-1)
        print '      --------------------------------------------------------------------'
    except Exception:
        print sys.exc_info()
        if abort_on_fail:
            sys.exit(-1)
        else:
            pass

    os.chdir(SCRIPT_ROOT_DIR)
    return theproc.returncode


###############################
# function
###############################
def do_execute_sub_process_2(args, execution_path, abort_on_fail):
    print '      --------------------------------------------------------------------'
    print '      Executing:      [' + args + ']'
    print '      Execution path: [' + execution_path + ']'
    try:
        os.chdir(execution_path)
        theproc = subprocess.Popen(args, shell=True)
        output = theproc.communicate()[0]
        if theproc.returncode:
            output = output[len(output) - MAX_DEBUG_PRINT_LENGTH:] if len(output) > MAX_DEBUG_PRINT_LENGTH else output
            print output
            print '*** Execution failed with code: %s' % str(theproc.returncode)
            if abort_on_fail:
                sys.exit(-1)
        print '      --------------------------------------------------------------------'
    except Exception:
        print sys.exc_info()
        if abort_on_fail:
            sys.exit(-1)
        else:
            pass

    os.chdir(SCRIPT_ROOT_DIR)
    return theproc.returncode


###############################
# function
###############################
def do_execute_sub_process_get_std_out(args, execution_path, abort_on_fail, print_debug=True):
    if print_debug:
        print '      --------------------------------------------------------------------'
        print '      Executing:      [' + list_as_string(args) + ']'
        print '      Execution path: [' + execution_path + ']'
    theproc = None
    output = ''
    try:
        os.chdir(execution_path)
        if IS_WIN_PLATFORM:
            theproc = subprocess.Popen(args, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=False)
        else:
            theproc = subprocess.Popen(args, shell=False, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
        output = theproc.communicate()[0]
        if theproc.returncode:
            output = output[len(output) - MAX_DEBUG_PRINT_LENGTH:] if len(output) > MAX_DEBUG_PRINT_LENGTH else output
            print output
            print '*** Execution failed with code: %s' % str(theproc.returncode)
            if abort_on_fail:
                sys.exit(-1)
        if print_debug:
            print '      --------------------------------------------------------------------'
    except Exception:
        print sys.exc_info()
        if abort_on_fail:
            sys.exit(-1)
        else:
            pass

    os.chdir(SCRIPT_ROOT_DIR)
    return output


###############################
# function
###############################
def clone_repository(repo_url, repo_branch_or_tag, destination_folder):
    print '--------------------------------------------------------------------'
    print 'Cloning repository: ' + repo_url
    print '        branch/tag: ' + repo_branch_or_tag
    print 'Dest:               ' + destination_folder
    print '--------------------------------------------------------------------'

    cmd_args = ['git', 'clone', '--depth', '0', repo_url, destination_folder]
    do_execute_sub_process(cmd_args, SCRIPT_ROOT_DIR, True)

    cmd_args = ['git', 'checkout', repo_branch_or_tag]
    do_execute_sub_process(cmd_args, destination_folder, True)


###############################
# function
###############################
def extract_file(path, to_directory='.'):
    cmd_args = []
    if path.endswith('.zip'):
        cmd_args = ['7z', 'x', path]
    elif path.endswith('.tar'):
        cmd_args = ['tar', '-xzf', path]
    elif path.endswith('.tar.gz') or path.endswith('.tgz'):
        cmd_args = ['tar', '-xzf', path]
    elif path.endswith('.tar.bz2') or path.endswith('.tbz'):
        cmd_args = ['tar', '-xzf', path]
    elif path.endswith('.7z'):
        cmd_args = ['7z', 'x', path]
        # 7z does not have silent operation so we do it the hard way....
        do_execute_sub_process_get_std_out(cmd_args, to_directory, False)
        return True
    else:
        print 'Did not extract the file! Not archived or no appropriate extractor was found: ' + path
        return False

    do_execute_sub_process(cmd_args, to_directory, True)
    return True

###############################
# function
###############################
def list_as_string(argument_list):
    output= ' '.join(argument_list)
    return output