]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - misc/build_helpers/test_mac_diskimage.py
7b7aaecb04eb7c2720cc893549a8e43a10a8df7b
[tahoe-lafs/tahoe-lafs.git] / misc / build_helpers / test_mac_diskimage.py
1 # This script uses hdiutil to attach a dmg (whose name is derived from the
2 # appname and the version number passed in), asserts that it attached as
3 # expected, cd's into the mounted filesystem, executes "$appname
4 # --version-and-path", and checks whether the output of --version-and-path is
5 # right.
6
7 # If all of the paths listed therein are loaded from within the current PWD
8 # then it exits with code 0.
9
10 # If anything goes wrong then it exits with non-zero (failure).  This is to
11 # check that the Mac OS "DMG" (disk image) package that gets built is correctly
12 # loading all of its packages from inside the image.
13
14 # Here is an example output from --version-and-path:
15
16 # allmydata-tahoe: 1.4.1-r3916 (/home/zooko/playground/allmydata/tahoe/trunk/trunk/src), foolscap: 0.4.1 (/usr/local/lib/python2.6/dist-packages/foolscap-0.4.1-py2.6.egg), pycryptopp: 0.5.10 (/home/zooko/playground/allmydata/tahoe/trunk/trunk/support/lib/python2.6/site-packages/pycryptopp-0.5.10-py2.6-linux-x86_64.egg), zfec: 1.4.2 (/usr/local/lib/python2.6/dist-packages/zfec-1.4.2-py2.6-linux-x86_64.egg), Twisted: 8.2.0-r26987 (/usr/local/lib/python2.6/dist-packages/Twisted-8.2.0_r26987-py2.6-linux-x86_64.egg), Nevow: 0.9.32 (/home/zooko/playground/allmydata/tahoe/trunk/trunk/support/lib/python2.6/site-packages/Nevow-0.9.32-py2.6.egg), zope.interface: 3.4.0 (/usr/lib/python2.6/dist-packages), python: 2.6.2 (/usr/bin/python), platform: Linux-Ubuntu_9.04-x86_64-64bit_ELF (None), sqlite: 3.6.10 (unknown), simplejson: 2.0.1 (/usr/local/lib/python2.6/dist-packages/simplejson-2.0.1-py2.6-linux-x86_64.egg), argparse: 0.8.0 (/usr/local/lib/python2.6/dist-packages/argparse-0.8.0-py2.6.egg), pyOpenSSL: 0.7 (/home/zooko/playground/allmydata/tahoe/trunk/trunk/support/lib/python2.6/site-packages/pyOpenSSL-0.7-py2.6-linux-x86_64.egg), pyutil: 1.3.30 (/usr/local/lib/python2.6/dist-packages/pyutil-1.3.30-py2.6.egg), zbase32: 1.1.1 (/usr/local/lib/python2.6/dist-packages/zbase32-1.1.1-py2.6.egg), setuptools: 0.6c12dev (/home/zooko/playground/allmydata/tahoe/trunk/trunk/support/lib/python2.6/site-packages/setuptools-0.6c12dev.egg), pysqlite: 2.4.1 (/usr/lib/python2.6/sqlite3)
17
18 import os, re, subprocess, time
19
20 def test_mac_diskimage(appname, version):
21     """ Return True on success, raise exception on failure. """
22     assert isinstance(appname, basestring), appname
23     assert isinstance(version, basestring), version
24     DMGNAME='mac/'+appname+'-'+version+'.dmg'
25
26     cmd = ['hdiutil', 'attach', DMGNAME]
27     attachit = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
28     rc = attachit.wait()
29     if rc != 0:
30         raise Exception("FAIL: hdiutil returned non-zero exit code: %r from command: %r" % (rc, cmd,))
31
32     stderrtxt = attachit.stderr.read()
33     if stderrtxt:
34         raise Exception("FAIL: hdiutil said something on stderr: %r" % (stderrtxt,))
35     stdouttxt = attachit.stdout.read()
36     mo = re.search("^(/[^ ]+)\s+Apple_HFS\s+(/Volumes/.*)$", stdouttxt, re.UNICODE|re.MULTILINE)
37     if not mo:
38         raise Exception("FAIL: hdiutil said something on stdout that didn't match our expectations: %r" % (stdouttxt,))
39     DEV=mo.group(1)
40     MOUNTPOINT=mo.group(2)
41
42     callitpid = None
43     try:
44         basedir = MOUNTPOINT + '/' + appname + '.app/Contents/Resources'
45
46         os.chdir(basedir)
47
48         cmd = ['../MacOS/' + appname, '--version-and-path']
49         callit = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
50         callitpid = callit.pid
51         assert callitpid
52         deadline = time.time() + 2 # If it takes longer than 2 seconds to do this then it fails.
53         while True:
54             rc = callit.poll()
55             if rc is not None:
56                 break
57             if time.time() > deadline:
58                 os.kill(callitpid, 15)
59                 raise Exception("FAIL: it took longer than 2 seconds to invoke $appname --version-and-path. stdout: %s, stderr: %s" % (callit.stdout.read(), callit.stderr.read()))
60             time.sleep(0.05)
61
62         if rc != 0:
63             raise Exception("FAIL: $appname --version-and-path returned non-zero exit code: %r" % (rc,))
64
65         stdouttxt = callit.stdout.read()
66
67         PKG_VER_PATH_RE=re.compile("(\S+): (\S+) \((.+?)\), ", re.UNICODE)
68
69         for mo in PKG_VER_PATH_RE.finditer(stdouttxt):
70             if not mo.group(3).startswith(basedir):
71                 raise Exception("FAIL: found package not loaded from basedir (%s); package was: %s" % (basedir, mo.groups(),))
72
73         return True # success!
74     finally:
75         if callitpid:
76             os.kill(callitpid, 9)
77             os.waitpid(callitpid, 0)
78         subprocess.call(['hdiutil', 'detach', '-Force', DEV])