~javier.collado/checkbox-certification/story525_backport_offline

« back to all changes in this revision

Viewing changes to scripts/optical_read_test

  • Committer: Marc Tardif
  • Date: 2011-04-01 20:40:56 UTC
  • Revision ID: marc.tardif@canonical.com-20110401204056-2qrax6b1gtriqqbc
BackportedĀ move-stuff-to-compat.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
 
3
 
import os
4
 
import sys
5
 
import posixpath
6
 
import filecmp
7
 
import shutil
8
 
 
9
 
from subprocess import Popen, PIPE
10
 
 
11
 
DEFAULT_DIR = '/tmp/checkbox.optical'
12
 
DEFAULT_DEVICE_DIR = 'device'
13
 
DEFAULT_IMAGE_DIR = 'image'
14
 
 
15
 
 
16
 
CDROM_ID = '/lib/udev/cdrom_id'
17
 
 
18
 
def _command(command, shell=True):
19
 
    proc = Popen(command,
20
 
                   shell=shell,
21
 
                   stdout=PIPE,
22
 
                   stderr=PIPE
23
 
                   )
24
 
    return proc
25
 
 
26
 
def _command_out(command, shell=True):
27
 
    proc = _command(command, shell)
28
 
    return proc.communicate()[0].strip()
29
 
 
30
 
def compare_tree(source, target):
31
 
    for dirpath, dirnames, filenames in os.walk(source):
32
 
        #if both tree are empty return false
33
 
        if dirpath == source and dirnames == [] and filenames == []:
34
 
            return False
35
 
        for name in filenames:
36
 
            file1 = os.path.join(dirpath, name)
37
 
            file2 = file1.replace(source, target, 1)
38
 
            if os.path.isfile(file1) and not os.path.islink(file1):
39
 
                if filecmp.cmp(file1, file2):
40
 
                    continue
41
 
                else:
42
 
                    return False
43
 
            else:
44
 
                continue
45
 
    return True
46
 
 
47
 
def read_test(device):
48
 
    passed = False
49
 
    device_dir = os.path.join(DEFAULT_DIR, DEFAULT_DEVICE_DIR)
50
 
    image_dir = os.path.join(DEFAULT_DIR, DEFAULT_IMAGE_DIR)
51
 
 
52
 
    for dir in (device_dir, image_dir):
53
 
        if posixpath.exists(dir):
54
 
            shutil.rmtree(dir)
55
 
    os.makedirs(device_dir)
56
 
 
57
 
    try:
58
 
        _command("umount %s" % device).communicate()
59
 
        mount = _command("mount -o ro %s %s" % (device, device_dir))
60
 
        mount.communicate()
61
 
        if mount.returncode != 0:
62
 
            return False
63
 
 
64
 
        _command("cp -dpR %s %s" % (device_dir, image_dir)).communicate()
65
 
        if compare_tree(device_dir, image_dir):
66
 
            passed = True
67
 
    except:
68
 
        passed = False
69
 
    finally:
70
 
        _command("umount %s" % device_dir).communicate(3)
71
 
        for dir in (device_dir, image_dir):
72
 
            if posixpath.exists(dir):
73
 
                shutil.rmtree(dir)
74
 
 
75
 
    if passed:
76
 
        print "Passed"
77
 
    else:
78
 
        print "Failed"
79
 
    return passed
80
 
 
81
 
 
82
 
def get_capabilities(device):
83
 
    cmd = "%s %s" % (CDROM_ID, device)
84
 
    capabilities = _command_out(cmd)
85
 
    return capabilities.split('\n')
86
 
 
87
 
def main(args):
88
 
    tests = []
89
 
    return_values = []
90
 
 
91
 
    for device in args:
92
 
        capabilities = get_capabilities(device)
93
 
        for capability in capabilities:
94
 
            if capability[:3] == 'ID_':
95
 
                cap = capability[3:-2]
96
 
                if cap == 'CDROM' or cap == 'CDROM_DVD':
97
 
                    tests.append('read')
98
 
 
99
 
        for test in set(tests):
100
 
            sys.stdout.write("Testing %s on %s ... " % (test, device))
101
 
            tester = "%s_test" % test
102
 
            return_values.append(globals()[tester](device))
103
 
            sys.stdout.flush()
104
 
 
105
 
    return False in return_values
106
 
 
107
 
if __name__ == "__main__":
108
 
    sys.exit(main(sys.argv[1:]))
109