Welcome to mirror list, hosted at ThFree Co, Russian Federation.

util.py « util « bockbuild - github.com/mono/bockbuild.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 96233cc311de7e240d4d0617a66a7fdc0c8b9671 (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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
import re
import glob
import os
import sys
import subprocess
import fileinput
import inspect
import time
import difflib
import shutil
import tarfile
import hashlib
import stat
from datetime import datetime,timedelta
import functools

# from
# https://svn.blender.org/svnroot/bf-blender/trunk/blender/build_files/scons/tools/bcolors.py


class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

class exit_codes:
    NOTSET = -1
    SUCCESS = 0
    FAILURE = 1

class config:
    trace = False
    filter = None  # function name/package name filter for trace() and test()
    test = False
    iterative = False  # FIXME: this needs a bit more work
    quiet = None
    never_rebuild = False
    verbose = False
    protected_git_repos = [] # we do not allow modifying behavior on our profile repo or bockbuild repo.
    absolute_root = None # there is no file resolution beneath this path. Displayed paths are shortened by omitting this segment.
    state_root = None
    exit_code = exit_codes.NOTSET
    artifact_lifespan_days = 7

class CommandException (Exception):  # shell command failure

    def __init__(self, message, cwd=None):
        if cwd is None:
            cwd = os.getcwd()
        Exception.__init__(self, '%s: %s (path: %s)' %
                           (get_caller(), message, cwd))
        verbose(message)


# internal/unexpected issue, treat as unrecoverable
class BockbuildException (Exception):

    def __init__(self, message):
        Exception.__init__(self, message)


class Logger:
    last_header = None
    print_color = False
    monkeywrench = False



def get_caller(skip=0, get_dump=False):
    # this whole thing fails if we're not in a valid directory
    try:
        cwd = os.getcwd()
    except OSError as e:
        return '~could not get caller (current directory not valid)~'

    stack = inspect.stack(3)
    if len(inspect.stack()) < 3 + skip:
        return 'top'
    output = None
    last_caller = None
    for record in stack[2 + skip:]:
        caller = record[3]
        frame = record[0]

        try:
            if 'self' in frame.f_locals:
                try:
                    output = '%s->%s' % (frame.f_locals['self'].name, caller)
                except Exception as e:
                    pass
                finally:
                    if output is None:
                        output = '%s->%s' % (
                            frame.f_locals['self'].__class__.__name__, caller)
            else:
                last_caller = caller
            if get_dump:
                output = output + "\n" + \
                    "\t".join(dump(frame.f_locals['self'], 'self'))

        except Exception as e:
            pass
        if output is not None:
            return output

    if output is None:
        return last_caller


def assert_exists(path):
    if not os.path.exists(path):
        error('assert_exists failed: ' + os.path.basename(path))


def loginit(message):
    if os.getenv('BUILD_REVISION') is not None:  # MonkeyWrench
        print '@MonkeyWrench: SetSummary:<h3>%s</h3>' % message
        Logger.monkeywrench = True
    elif sys.stdout.isatty():
        Logger.print_color = True
    logprint('** %s **' % message, bcolors.BOLD)
    print


def colorprint(message, color):
    message = str(message).replace (os.path.join(config.absolute_root,''), '%s@%s' % (bcolors.BOLD, bcolors.ENDC))
    if Logger.print_color:
        print '%s%s%s' % (color, message, bcolors.ENDC)
    else:
        print message


def logprint(message, color, summary=False, header=None, trace=False):
    if isinstance(message, str):
        lines = message.split('\n')
    elif isinstance(message, dict):
        lines = list()
        for k in message.keys():
            lines.append('%s : %s' % (k, message[k]))
    else:  # assume iterable
        lines = message

    if config.quiet == True and trace == False:
        return
    if summary:
        if Logger.monkeywrench:
            for line in lines:
                print '@MonkeyWrench: AddSummary:<p>%s</p>' % line
            return

    if header != Logger.last_header:
        Logger.last_header = header
        print
        if header is not None:
            colorprint('%s:' % header, color)

    for line in lines:
        output = ''
        if header is not None:
            output = '\t'
        output = output + '%s' % line.rstrip('\r\n')
        colorprint(output, color)


def title(message, summary=True):
    logprint('\n** %s **\n' % message, bcolors.HEADER, summary)


def info(message, 	summary=True):
    logprint(message, '--\t' + bcolors.OKGREEN, summary, header= None)


def progress(message):
    logprint(message, bcolors.OKBLUE, header=get_caller())


def verbose(message):
    if not config.verbose and not config.trace:
        return
    logprint(message, bcolors.OKBLUE, header=get_caller())


def warn(message):
    if isinstance(message, str):
        message = '%s %s' % ('(bockbuild warning)', message)
    logprint(message, bcolors.FAIL, header=get_caller())

def finish (exit_code):
    if exit_code > config.exit_code:
        config.exit_code = exit_code
    sys.exit(config.exit_code)

def error(message, more_output=False):
    config.trace = False
    if isinstance(message, str):
        message = '%s %s' % ('(bockbuild error)', message)
    logprint(message, bcolors.FAIL, header=get_caller(), summary=True)
    if not more_output:
        finish(exit_codes.FAILURE)

def trace(message, skip=0):
    if config.trace == False:
        return

    caller = get_caller(skip)

    if config.filter is not None and config.filter not in caller:
        return

    logprint(message, bcolors.FAIL, summary=False, header=caller, trace=True)


def test(func):
    if config.test == False:
        return
    caller = get_caller()

    if config.filter is not None and config.filter not in caller:
        return

    if func() == False:
        error('Test ''%s'' failed.' % func.__name__)

def retry(fn, attempts = 3, delay = 5):
    def decorator(*args, **kwargs):
        result = None
        for x in range(attempts):
            try:
                 result = fn (*args, **kwargs)
                 break
            except CommandException as e:
                if x == attempts -1:
                    raise BockbuildException (e)
                info(str(e))
                info('Retrying <%s> in %s secs...' % (fn.__name__, delay))
                time.sleep(delay)
        return result
    return decorator

def ensure_dir(d, purge=False):
    trace('ensuring:' + d)
    if os.path.exists(d):
        if not purge:
            return

        verbose('Nuking %s' % d)
        unprotect_dir(d, recursive=True)
        delete(d)

    os.makedirs(d)

def first_existing (paths):
    for p in paths:
        if os.path.exists (p): return p
    error ('None of these paths were found: %s' % paths)

# quick and dirty assuming they have the same name/paths
def identical_files(first, second):
    hash1 = hashlib.sha1(open(first).read()).hexdigest()
    hash2 = hashlib.sha1(open(second).read()).hexdigest()

    return hash1 == hash2


def md5(path):
    return hashlib.md5(open(path).read()).hexdigest()


def compare_text(new, old):
    difflines = [line for line in difflib.context_diff(old, new, n=0)]
    if len(difflines) > 0:
        changes = [line.rstrip('\r\n') for line in difflines if line.startswith(
            ('+ ', '- ', '! '))]
        return changes
    else:
        return None


def is_changed(new, file, show_diff=True):
    orig = []

    if os.path.exists(file):
        with open(file) as input:
            orig = [line.rstrip('\r\n') for line in input.readlines()]

    else:
        return len(new) > 0

    diff = compare_text(new, orig)

    if diff is not None:
        if show_diff:
            map(lambda x: info(x), diff)
        return True
    else:
        return False

def is_expired (path, age_cutoff_days):
    artifact_age_days = (datetime.utcnow() - datetime.utcfromtimestamp(os.path.getmtime(path))).days
    return artifact_age_days > age_cutoff_days

def get_filetype(path):
    # the env variables are to work around a issue with OS X and 'file':
    # https://trac.macports.org/ticket/38771
    return backtick('LC_CTYPE=C LANG=C file -b "%s"' % path)[0]

# http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python


def which(program):
    def is_exe(fpath):
        return os.path.exists(fpath) and os.access(fpath, os.X_OK)

    def ext_candidates(fpath):
        yield fpath
        for ext in os.environ.get("PATHEXT", "").split(os.pathsep):
            yield fpath + ext

    fpath, fname = os.path.split(program)
    if fpath:
        if is_exe(program):
            return program
    else:
        for path in os.environ["PATH"].split(os.pathsep):
            exe_file = os.path.join(path, program)
            for candidate in ext_candidates(exe_file):
                if is_exe(candidate):
                    return candidate

    return None

def parse_rootdir(result, cwd):
    # http://stackoverflow.com/a/18339166
    if os.path.basename(result) == '.git': # normal repo
        return os.path.dirname(result)
    elif result == '.':
        return cwd
    else:
        return result


def find_git(self, echo=False):
    git_bin = which('git')
    if not git_bin:
        error('git not found in PATH')

    @retry
    def git_operation(self, args, cwd, hazard = False, allow_fail = False, singleline_output = False, options = None, allow_nonrootdir = False):
        try:
            cwd = os.path.realpath(cwd)
            (exit, out, err) = run(git_bin, ['rev-parse', '--show-toplevel'], cwd)
            if len(out) > 0:
                root = out
            else:
                (exit, out, err) = run(git_bin, ['rev-parse', '--git-dir'], cwd)
                root = parse_rootdir(out, cwd)
        except:
            raise
        if root != cwd and not allow_nonrootdir:
            error ('Git operations allowed only on the root directory of the repo (root: %s cwd: %s)' % (root, cwd))
        if hazard:
            root = git_rootdir (self, cwd)
            assert_modifiable_repo (root)
        try:
            fullargs = args.split(' ')
            if options:
                if not isinstance(options, list):
                    error ('options argument must be a list')
                fullargs = fullargs + options
            (exit, out, err) = run(git_bin, fullargs, cwd)
        except CommandException:
            if allow_fail:
                return None
            else:
                raise

        lines = out.split('\n')
        if singleline_output:
            if len(lines) > 1:
                error ('Single line output expected from git. Received the following:\n%s' % out)
            else:
                return lines[0]

        return lines

    self.git = git_operation.__get__(self, self.__class__)
    self.git_bin = git_bin


def assert_git_dir(self):
    try:
        self.git('rev-parse HEAD')
    except:
        error('Attempted git action in non-git directory')

def assert_modifiable_repo(cwd):
    if cwd in config.protected_git_repos:
        raise BockbuildException ('Hazardous Git operation attempt at protected path: %s' % cwd)

is_modifiable_repo = lambda x: os.path.realpath(x) not in config.protected_git_repos

def git_get_revision(self, cwd):
    return self.git('rev-parse HEAD', cwd)[0]


def git_get_branch(self, cwd):
    return self.git('symbolic-ref -q --short HEAD', cwd, allow_fail = True, singleline_output = True)

def git_is_dirty(self, cwd):
    return 'dirty' in git_shortid (self, cwd)


def git_patch(self, dir, patch):
    self.git('diff > %s' % patch)


def git_shortid(self, cwd):
    branch = git_get_branch(self, cwd)
    short_rev = self.git('describe --abbrev --always --dirty', cwd)[0]
    if branch is None:
        return short_rev
    else:
        return '%s@%s' % (branch, short_rev)

def git_rootdir(self, cwd):
    # http://stackoverflow.com/a/18339166
    result = self.git('rev-parse --show-toplevel', cwd, allow_nonrootdir=True, singleline_output=True)
    if len(result) > 0:
        return result
    else:
        result = self.git('rev-parse --git-dir', cwd, allow_nonrootdir=True, singleline_output=True)
        return parse_rootdir(result)

def git_isrootdir(self, cwd):
    try:
        return git_rootdir (self, cwd) == cwd
    except:
        error('git_isrootdir')



def git_get_commit_msg(self, cwd):
    return self.git('show -s --format=%B HEAD', cwd)[0]

def protect_dir(path, recursive=False):
    if not os.path.isdir(path):
        error('only safe for dirs: %s' % path)

    os.chmod(path, stat.S_IRUSR | stat.S_IXUSR)

    if recursive:
        for root, subdirs, filelist in os.walk(path):
            protect_dir(root, recursive=False)


def unprotect_dir(path, recursive=False):
    if not os.path.isdir(path):
        error('only safe for dirs: %s' % path)

    os.chmod(path, stat.S_IRUSR | stat.S_IXUSR | stat.S_IWUSR |
             stat.S_IXGRP | stat.S_IRGRP | stat.S_IXOTH | stat.S_IROTH)

    if recursive:
        for root, subdirs, filelist in os.walk(path):
            if not os.path.islink(root):
                unprotect_dir(root, recursive=False)

# wrap around shutil.rmtree, which is unreliable. Sometimes a few attempts
# do the trick...


def delete(path):
    trace('deleting %s' % path)
    if not os.path.isabs(path):
        raise BockbuildException('Relative paths are not allowed: %s' % path)

    if not os.path.lexists(path):
        raise CommandException('Invalid path to rm: %s' % path)

    if os.getcwd() == path:
        raise BockbuildException(
            'Will not delete current directory: %s' % path)

    # get the dir out of the way so that we don't have to deal with
    # inconsistent state if we fail
    if os.path.isfile(path):
        os.remove(path)
        return

    # directory removal
    if os.path.islink(path):
        os.unlink(path)
        return

    orig_path = path
    unprotect_dir(path, recursive=True)
    path = path + '.deleting'
    if os.path.exists(path):
        delete(path)

    shutil.move(orig_path, path)
    for x in range(1, 5):
        try:
            if os.path.isfile(path) or os.path.islink(path):
                os.remove(path)
            elif os.path.isdir(path):
                shutil.rmtree(path, ignore_errors=False)
        except OSError as e:
            pass
        finally:
            if not os.path.exists(path):
                break
        warn('retrying delete of %s' % path)
        # try to sabotage whoever else is writing in the directory...
        protect_dir(path, recursive=True)
        time.sleep(1)
        unprotect_dir(path, recursive=True)

    if os.path.exists(path):
        error('Deleting failed: %s' % orig_path)

def link_dir(link_path, dest_path):
    if not os.path.isdir (dest_path):
        error ('Not found or not a directory: %s' % dest_path)
    if os.path.lexists(link_path):
        delete(link_path)
    os.symlink(dest_path, link_path)

def merge_trees(src, dst, delete_src=True):
    if not os.path.isdir(src) or not os.path.isdir(dst):
        raise Exception('"%s" or "%s" are not both directories ' % (src, dst))
    run_shell('rsync -a --ignore-existing %s/* %s' % (src, dst), False)
    if delete_src:
        delete(src)


def iterate_dir(dir, with_links=False, with_dirs=False, summary=False):
    x = 0
    links = 0
    dirs = 0

    for root, subdirs, filelist in os.walk(dir):
        dirs = dirs + 1
        if with_dirs:
            yield root
        for file in filelist:
            path = os.path.join(root, file)
            if os.path.islink(path):
                links = links + 1
                if with_links:
                    yield path
                continue
            x = x + 1
            yield path

    if summary:
        info("%s: %s files, %s dirs, %s symlinks" %
             (os.path.relpath(dir, os.getcwd()), x, dirs, links))


def zip(src, archive):
    x = 0
    # thanks to http://stackoverflow.com/a/17080988

    pwd = os.getcwd()

    try:
        os.chdir(src)
        with tarfile.open(archive, "w:gz") as zip:
            for path in iterate_dir(src, with_links=True, with_dirs=True, summary=False):
                relpath = os.path.relpath(path, src)
                zip.add(relpath, recursive=False)
                x = x + 1
    finally:
        os.chdir(pwd)


def unzip(archive, dst):
    if os.path.exists(dst):
        raise Exception('unzip: Destination should not exist: %s' % dst)

    pwd = os.getcwd()
    relroot = os.path.abspath(os.path.join(dst, os.pardir))

    try:
        os.chdir(relroot)
        with tarfile.open(archive) as zip:
            zip.extractall(dst)
    except:
        if os.path.exists(archive):
            delete(archive)
        if os.path.exists(dst):
            delete(dst)
        raise
    finally:
        os.chdir(pwd)


def dump(self, name):
    for k in self.__dict__.keys():
        if isinstance(self.__dict__[k], (str, list, tuple, dict, bool, int)) and not k.startswith('_'):
            yield '%s.%s = "%s"\n' % (name, k, self.__dict__[k])


def expand_macros(node, vars, extra_vars='active_profile'):
    def sub_macro(m):
        type = m.groups()[0]
        expr = m.groups()[1]
        if type == '%':
            expr = 'self.' + expr

        resolved = False
        for var in [vars, extra_vars]:
            try:
                o = eval(expr, {}, {'self': var})
                resolved = True
                break
            except:
                pass
        if not resolved:
            raise Exception("'%s' could not be resolved in string '%s'" %
                  (m.groups()[1], node))
        if o is None:
            return ''
        elif isinstance(o, (list, tuple)):
            return ' '.join(o)
        return str(o)

    if hasattr(node, '__dict__'):
        for k, v in node.__dict__.iteritems():
            if not k.startswith('_'):
                node.__dict__[k] = expand_macros(v, vars)
    elif isinstance(node, dict):
        for k, v in node.iteritems():
            node[k] = expand_macros(v, vars)
    elif isinstance(node, (list, tuple)):
        for i, v in enumerate(node):
            node[i] = expand_macros(v, vars)
    elif isinstance(node, str):
        orig_node = node
        iters = 0
        while True:
            v = re.sub('(?<!\\\)([%$]){([^}]+)}',
                       sub_macro, node)
            if v == node:
                break
            iters += 1
            if iters >= 500:
                sys.exit('Too many macro substitutions, possible recursion:'
                         '\'%s\'' % orig_node)
            node = v

    return node


def replace_in_file(filename, word_dic):
    rc = re.compile('|'.join(map(re.escape, word_dic)))

    def translate(match):
        return word_dic[match.group(0)]
    for line in fileinput.FileInput(filename, inplace=1):
        print rc.sub(translate, line)


def run(cmd, args, cwd, env=None):
    trace('@%s %s' % (cmd, args))

    if not isinstance(cmd, str):
        error('cmd argument must be a string')
    if not isinstance(args, list):
        error('args argument must be a list')
    cmd_list = [cmd] + args
    try:
        proc = subprocess.Popen(cmd_list, shell=False, cwd=cwd,
                                env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    except Exception as e:
        error(str(e))

    stdout, stderr = proc.communicate()
    exit_code = proc.returncode

    if not exit_code == 0:
        raise CommandException('"%s" failed, error code %s\nstderr:\n%s' % (
            cmd + str(args), exit_code, stderr), cwd=cwd)

    return (exit_code, stdout[:-1], stderr)


def run_shell(cmd, print_cmd=False, cwd=None, fatal=True):
    if print_cmd:
        print '++', cmd
    if not print_cmd:
        trace(cmd)
    proc = subprocess.Popen(cmd, shell=True, bufsize=-1, cwd=cwd)
    exit_code = proc.wait()
    if not exit_code == 0:
        msg = '"%s" failed, error code %s' % (cmd, exit_code)
        if fatal:
            raise CommandException(msg, cwd)
        else:
            warn (msg)


def backtick(cmd, print_cmd=False, echo=False):
    if print_cmd or echo:
        print '``', cmd
    if not print_cmd:
        trace('``' + cmd)
    proc = subprocess.Popen(cmd, shell=True, bufsize=-1,
                            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = proc.communicate()

    exit_code = proc.returncode

    if echo:
        info(stdout)
        if len(stderr) > 0:
            warn(stderr)

    if not exit_code == 0:
        raise CommandException('"%s" failed, error code %s\nstderr:\n%s' % (
            cmd, exit_code, stderr), os.getcwd())

    return stdout.split('\n')


def get_host():
    search_paths = ['/usr/share', '/usr/local/share']
    am_config_guess = []
    for path in search_paths:
        am_config_guess.extend(glob.glob(os.path.join(
            path, os.path.join('automake*', 'config.guess'))))
    for config_guess in am_config_guess:
        config_sub = os.path.join(os.path.dirname(config_guess), 'config.sub')
        if os.access(config_guess, os.X_OK) and os.access(config_sub, os.X_OK):
            return backtick('%s %s' % (config_sub, backtick(config_guess)[0]))[0]
    return 'python-%s' % os.name


def get_cpu_count():
    try:
        return os.sysconf('SC_NPROCESSORS_CONF')
    except:
        return 1