pyobjc / build-support / run_tests.py

  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
#!/usr/bin/env python3
"""
A script that builds and tests the entire pyobjc tree
in a number of virtual environments:

- python 2.6
- python 2.7
- python 3.1
- python 3.2

- all in 32-bit, 3-way

- with command-line arguments to select specific 
  combinations

Assumptions:
- there are python frameworks for the various architectures: DbgPython-VARIANT.framework
- those frameworks contain distribute and virtualenv

"""
import sys
sys.dont_write_bytecode = True

import distribute_setup
distribute_setup.use_setuptools()

import pkg_resources
# Use Jinja2 for templating because that's the
# only one that supports Python3 at this time.
pkg_resources.require('Jinja2')

import getopt, os, shutil, logging, subprocess, time
from topsort import topological_sort

from jinja2 import Template

gBaseDir = '.'
gIndexTemplate = os.path.join(gBaseDir, 'templates', 'index.html')
gTestResults = os.path.join(gBaseDir, "testresults")


gUsage = """\
run_tests.py [-a archs] [--archs=archs] [-v versions] [--versions,versions]

archs:    32-bit,3-way (values separated by commas)
versions: 2.6,2.7,3.1,3.2    (values seperated by commas)
"""

gBaseDir = os.path.dirname(os.path.abspath(__file__))
gRootDir = os.path.dirname(gBaseDir)
gTestResults = os.path.join(gBaseDir, "testresults")

gFrameworkNameTemplate="DbgPython-{archs}"

gVersions=["3.2", "2.7", "3.1", "2.6"]
gArchs=["32-bit", "3-way"]

gArchMap={
    '3-way': ['ppc', 'i386', 'x86_64'],
    '32-bit': ['ppc', 'i386'],
    'intel': ['i386', 'x86_64'],
}


def supports_arch_command(version):

    # In virtualenvs both 2.6 and 2.7 support
    # the 'arch' command because virtualenv
    # copies the real interpreter into the 
    # virtualenv.
    return True

    # This code is true for the python 
    # interpreter outside of virtual environments:
    """
    major, minor = map(int, version.split('.'))
    if major == 2:
        return minor >= 7
    else:
        return minor >= 2
    """

def main():
    logging.basicConfig(level=logging.DEBUG)

    try:
        opts, args = getopt.getopt(sys.argv[1:], 'a:v:h?', ["help", "archs=", "versions="])
    except getopt.error as msg:
        print(msg, file=sys.stderr)
        print(gUsage, file=sys.stderr)
        sys.exit(1)

    if args:
        print("Additional arguments", file=sys.stderr)
        print(gUsage, file=sys.stderr)
        sys.exit(1)

    versions=gVersions
    archs=gArchs

    for k, v in opts:
        if k in ('-?', '-h', '--help'):
            print(gUsage)
            sys.exit(0)
        elif k in ['-a', '--archs']:
            archs=v.split(',')

            for v in archs:
                if v not in gArchs:
                    print("Unsupported architecture: {0}".format(v),
                            file=sys.stderr)
                    sys.exit(1)

        elif k in ['-v', '--versions']:
            versions=v.split(',')

            for v in versions:
                if v not in gVersions:
                    print("Unsupported Python version: {0}".format(v),
                            file=sys.stderr)
                    sys.exit(1)
        else:
            print("ERROR: Unhandled script option: {0}".format(k),
                    file=sys.stderr)
            sys.exit(2)

    for ver in versions:
        for arch in archs:
            run_tests(ver, arch)

    gen_summary(versions, archs)

def detect_frameworks():
    """
    Returns a list of framework wrappers in the order they should
    be build in.
    """
    topdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

    frameworks = []
    partial_order = []

    for subdir in os.listdir(topdir):
        if not subdir.startswith('pyobjc-framework-'): continue

        setup = os.path.join(topdir, subdir, 'setup.py')

        requires = None
        with open(setup) as fp:
            for ln in fp:
                if requires is None:
                    if ln.strip().startswith('install_requires'):
                        requires = []
                else:
                    if ln.strip().startswith(']'):
                        break

                    dep = ln.strip()[1:-1]
                    if dep.startswith('pyobjc-framework'):
                        dep = dep.split('>')[0]
                        requires.append(dep)

        frameworks.append(subdir)
        for dep in requires:
            partial_order.append((dep, subdir))

    frameworks = topological_sort(frameworks, partial_order)
    return frameworks



def run_tests(version, archs):

    lg = logging.getLogger("run_tests")

    p = subprocess.Popen(["xcode-select", "-print-path" ],
        stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

    stdout, _ = p.communicate()
    path = stdout.strip()
    path = os.path.join(path.decode('UTF-8'), "Library", "PrivateFrameworks")
    ib_env = {
        'DYLD_FRAMEWORK_PATH': path,
    }
    del path
    

    lg.info("Run tests for Python %s with archs %s", version, archs)

    subdir = os.path.join(gBaseDir, "virtualenvs", "{0}--{1}".format(version, archs))
    if os.path.exists(subdir):
        lg.debug("Remove existing virtualenv")
        shutil.rmtree(subdir)

    if not os.path.exists(os.path.dirname(subdir)):
        os.mkdir(os.path.dirname(subdir))

    resultdir = os.path.join(gTestResults, "{0}--{1}".format(version, archs))
    if os.path.exists(resultdir):
        lg.debug("Remove existing results directory")
        shutil.rmtree(resultdir)

    if not os.path.exists(os.path.dirname(resultdir)):
        os.mkdir(os.path.dirname(resultdir))

    base_python = "/Library/Frameworks/{0}.framework/Versions/{1}/bin/python".format(
            gFrameworkNameTemplate.format(archs=archs, version=version), version)
    if version[0] == '3':
        base_python += '3'

    if os.path.exists(base_python + "-all"):
        base_python = base_python + "-all"

    lg.debug("Interpreter: %s", base_python)

    if not os.path.exists(base_python):
        lg.warning("No python installation for Python %r %r", version, archs)
        raise RuntimeError(base_python)


    lg.debug("Create virtualenv in %s", subdir)
    if version[0] == '2':
        p = subprocess.Popen([
            base_python,
            "-mvirtualenv",
            subdir])
    else:
        p = subprocess.Popen([
            base_python,
            "-mvirtualenv3",
            subdir])

    xit = p.wait()
    if xit != 0:
        lg.warning("Cannot create virtualenv in %s", subdir)
        raise RuntimeError(subdir)

    lg.debug("Install dependencies")
    pass # Nothing to do here


    lg.debug("Install base packages")
    if version[0] == '3':
        python = os.path.join(subdir, "bin", "python3")
    else:
        python = os.path.join(subdir, "bin", "python")
    # There are circular dependencies w.r.t. testing the Cocoa and Quartz wrappers,
    # install pyobjc-core, pyobjc-framework-Cocoa and pyobjc-framework-Quartz
    # to ensure we avoid those problems.
    for pkg in ["pyobjc-core", "pyobjc-framework-Cocoa", "pyobjc-framework-Quartz"]:
        if not os.path.exists(os.path.join(resultdir, pkg)):
            os.makedirs(os.path.join(resultdir, pkg))

        pkgroot = os.path.join(gRootDir, pkg)
        pkgbuild = os.path.join(pkgroot, "build")
        if os.path.exists(pkgbuild):
            lg.debug("Remove build directory for %s", pkg)
            shutil.rmtree(pkgbuild)
        
        lg.info("Install %s into %s", pkg, os.path.basename(subdir))
        p = subprocess.Popen([
            python, "setup.py", "install"],
            cwd=pkgroot, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

        stdout, _ = p.communicate()

        with open(os.path.join(resultdir, pkg, "pre-build-stdout.txt"), "wb") as fd:
            fd.write(stdout)

        xit = p.wait()
        if xit != 0:
            lg.warning("Install %s failed", pkg)
            raise RuntimeError(pkg)

    lg.debug("Start testing cycle")
    for pkg in ["pyobjc-core"] + detect_frameworks():
        if not os.path.exists(os.path.join(resultdir, pkg)):
            os.makedirs(os.path.join(resultdir, pkg))

        pkgroot = os.path.join(gRootDir, pkg)

        if pkg == "pyobjc-framework-InterfaceBuilderKit":
            env = os.environ.copy()
            env.update(ib_env)
            
        else:
            env = os.environ

        pkgbuild = os.path.join(pkgroot, "build")
        if os.path.exists(pkgbuild):
            lg.debug("Remove build directory for %s", pkg)
            shutil.rmtree(pkgbuild)

        lg.debug("Build %s for %s", pkg, os.path.basename(subdir))
        p = subprocess.Popen([
            python, "setup.py", "install"],
            cwd=pkgroot, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

        stdout, _ = p.communicate()
        with open(os.path.join(resultdir, pkg, "build-stdout.txt"), "wb") as fd:
            fd.write(stdout)

        xit = p.wait()
        if xit != 0:
            print(stdout)
            lg.warning("Build %s failed", pkg)
            #raise RuntimeError(pkg)
            continue

        # TODO: 
        # - For python2.7/3.2: use `arch` to run tests with all architectures
        # - For python2.6/3.1: run tests using 'python-32' and 'python-64' 
        #   when those are available

        if supports_arch_command(version):
            for a in gArchMap[archs]:
                lg.info("Test %s for %s (%s)", pkg, os.path.basename(subdir), a)
                p = subprocess.Popen([
                    '/usr/bin/arch', '-' + a,
                    python, "setup.py", "test"],
                    cwd=pkgroot, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env)
                stdout, _ = p.communicate()

                with open(os.path.join(resultdir, pkg, "test-stdout-{0}.txt".format(a)), "wb") as fd:
                    fd.write(stdout)

                status = stdout.splitlines()
                if status[-1].startswith(b'['):
                    status = status[-2]
                else:
                    status = status[-1]
                status = status.decode('UTF-8')
                lg.info("Test %s for %s (%s): %s", pkg, os.path.basename(subdir), a, status)

                xit = p.wait()
                if xit != 0:
                    lg.warning("Test %s failed", pkg)

                with open(os.path.join(resultdir, pkg, "test-stdout-{0}.exit".format(a)), "w") as fd:
                    fd.write(str(xit))
                
        else:
            lg.debug("Test %s for %s", pkg, os.path.basename(subdir))
            p = subprocess.Popen([
                python, "setup.py", "test"],
                cwd=pkgroot, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
                env=env)
            stdout, _ = p.communicate()

            with open(os.path.join(resultdir, pkg, "test-stdout.txt"), "wb") as fd:
                fd.write(stdout)

            status = stdout.splitlines()[-1]
            lg.info("Test %s for %s: %s", pkg, os.path.basename(subdir), status)

            xit = p.wait()
            if xit != 0:
                lg.warning("Test %s failed", pkg)
                continue
                #raise RuntimeError(pkg)

        
        lg.debug("Install %s into %s", pkg, os.path.basename(subdir))
        p = subprocess.Popen([
            python, "setup.py", "install"],
            cwd=pkgroot)

        xit = p.wait()
        if xit != 0:
            lg.warning("Install %s failed", pkg)
            raise RuntimeError(pkg)

       

def parse_tests(inputfile):
    result = {
        'test_pass':  0,
        'test_fail':  0,
        'test_error': 0,
    }
    last_line = ''
    with open(inputfile) as stream:
        for ln in stream:
            if not ln.startswith('['):
                last_line = ln
            ln = ln.rstrip()
            if ln.endswith('... ok'):
                result['test_pass'] += 1
            elif ln.endswith('... FAIL'):
                result['test_fail'] += 1
            elif ln.endswith('... ERROR'):
                result['test_error'] += 1

            elif 'Fatal Python error' in ln:
                # Interpreter aborted itself, 
                # treat this as a test error
                result['test_error'] += 1


    if not last_line.split()[0].isupper():
        # The last line of the output should
        # be the unittest status line, which
        # starts with an uppercase word. 
        # Not having it is an error (and tends
        # to be caused by interpreter crashes)
        result['test_error'] += 1

    result['class_pass'] = ''
    result['class_fail'] = ''
    result['class_error'] = ''
    if result['test_pass'] == 0  \
        and result['test_fail'] == 0 \
        and result['test_error'] == 0:
            result['class_pass'] = 'error'
    if result['test_fail']:
        result['class_fail'] = 'warning'
    if result['test_error']:
        result['class_error'] = 'error'

    if result['test_error'] + result['test_fail'] == 0:
        result['class_pass'] = 'ok'
                
    return result

def parse_build(inputfile):
    result = {
        'build_warnings': 0,
        'build_errors':   0,
    }
    with open(inputfile) as stream:
        for ln in stream:
            if 'warning: no directories found matching' in ln:
                # Completely harmless warning
                continue


            if 'error:' in ln:
                result['build_errors'] += 1

            elif ln.startswith('../../libxml2-src/') or \
                    ln.startswith('libffi-src'):
                # Ignore warnings in 3th-party libraries (pyobjc-core)
                continue


            elif 'is deprecated' in ln and 'objc/Protocol.h' in ln:
                # Pyobjc-core uses deprecated access methods for protocols
                # in 32-bit mode. 
                continue

            elif 'is deprecated' in ln:
                # Ignore all deprecation errors for now, framework wrappers
                # also trigger these when they contain manual wrappers for
                # deprecated APIs.
                continue

            elif 'warning:' in ln:
                result['build_warnings'] += 1

    result['class_warnings'] = ''
    result['class_errors'] = ''
    if result['build_warnings']:
        result['class_warnings'] = 'warning'
    if result['build_errors']:
        result['class_errors'] = 'error'

    if result['build_warnings'] + result['build_errors'] == 0:
        result['class_warnings'] = 'ok'
        result['class_errors'] = 'ok'

    return result

def get_svnversion():
    p = subprocess.Popen([
        'svnversion',
        ], cwd='..', stdout=subprocess.PIPE)
    stdout, _ = p.communicate()
    xit = p.wait()
    if xit != 0:
        raise RuntimeError(xit)

    return stdout.decode('UTF-8')

def get_svnurl():
    p = subprocess.Popen([
        'svn', 'info', '.'
        ], cwd='..', stdout=subprocess.PIPE)
    stdout, _ = p.communicate()
    xit = p.wait()
    if xit != 0:
        raise RuntimeError(xit)

    for ln in stdout.splitlines():
        if ln.startswith(b'URL'):
            return ln.split(None, 1)[1].decode('UTF-8')

def get_osx_version():
    p = subprocess.Popen([
        'sw_vers',
        ], cwd='..', stdout=subprocess.PIPE)
    stdout, _ = p.communicate()
    xit = p.wait()
    if xit != 0:
        raise RuntimeError(xit)

    r = {}
    for ln in stdout.splitlines():
        k, v = ln.decode('UTF-8').split(':', 1)
        r[k.strip()] = v.strip()

    return "{ProductName} {ProductVersion} ({BuildVersion})".format(**r)

def gen_summary(report_versions, report_archs):
    with open(gIndexTemplate) as fp:
        tmpl = Template(fp.read())

    svn={}
    svn['revision'] = get_svnversion()
    svn['url'] = get_svnurl()

    osx={}
    osx['version'] = get_osx_version()

    versions = {}

    for subdir in os.listdir(gTestResults):
        if subdir == 'index.html': continue
        version, style = subdir.split('--')

        if version not in report_versions: continue
        if style not in report_archs: continue

        versions[(version, style)] = modules = []

        for mod in os.listdir(os.path.join(gTestResults, subdir)):
            moddir = os.path.join(gTestResults, subdir, mod)
            info = parse_build(os.path.join(moddir, 'build-stdout.txt'))
            info['name'] = mod
            modules.append(info)
            info['archs'] = []
            info['class'] = None

            if info['build_errors']:
                info['class'] = 'error'
            #elif info['build_warnings']:
                #info['class'] = 'warning'

            for fn in os.listdir(moddir):
                if not fn.startswith('test'): continue
                if fn.endswith('exit'): continue

                if fn == 'test-stdout.txt':
                    a = 'all'

                else:
                    a = fn.split('-')[-1].split('.')[0]

                info['archs'].append(a)
                info[a] =  parse_tests(os.path.join(moddir, fn))

                with open (os.path.join(moddir, fn)[:-3] + 'exit') as fd:
                    info[a]['exit'] = fd.read().strip()


                if info[a]['test_fail'] and (info['class'] is None):
                    info['class'] = 'warning'

                if info[a]['test_error']:
                    info['class'] = 'error'

                if info[a]['exit'] != 0:
                    info['class'] = 'crash'


            if info['class'] is None:
                info['class'] = 'ok'

    with open(os.path.join(gTestResults, 'index.html'), 'w') as fp:
        fp.write(tmpl.render(
            svn=svn,
            osx=osx,
            versions=versions,
            sorted=sorted,
            timestamp=time.ctime(),
            ))

if __name__ == "__main__":
    try:
        main()
    except RuntimeError:
        sys.exit(1)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.