~ubuntu-branches/ubuntu/trusty/plainbox-provider-checkbox/trusty

« back to all changes in this revision

Viewing changes to bin/optical_read_test

  • Committer: Package Import Robot
  • Author(s): Zygmunt Krynicki
  • Date: 2014-04-07 19:00:31 UTC
  • mfrom: (3.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20140407190031-rf836grml6oilfyt
Tags: 0.4-1
* New upstream release. List of bugfixes:
  https://launchpad.net/plainbox-provider-checkbox/14.04/0.4
* debian/watch: look for new releases on launchpad
* debian/rules: stop using pybuild and use manage.py
  {i18n,build,install,validate} instead. This also drops dependency on
  python3-distutils-extra and replaces that with intltool
* debian/control: drop X-Python3-Version
* debian/control: make plainbox-provider-checkbox depend on python and
  python2.7 (for some scripts) rather than suggesting them.
* debian/upstream/signing-key.asc: Use armoured gpg keys to avoid having to
  keep binary files in Debian packaging. Also, replace that with my key
  since I made the 0.3 release upstream.
* debian/source/lintian-overrides: add an override for warning about no
  source for flash movie with reference to a bug report that discusses that
  issue.
* debian/source/include-binaries: drop (no longer needed)
* debian/patches: drop (no longer needed)
* debian/plainbox-provider-checkbox.lintian-overrides: drop (no longer
  needed)
* Stop being a python3 module, move to from DPMT to PAPT

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python3
 
2
 
 
3
import os
 
4
import sys
 
5
import posixpath
 
6
import filecmp
 
7
import shutil
 
8
 
 
9
from argparse import ArgumentParser
 
10
from subprocess import Popen, PIPE
 
11
 
 
12
DEFAULT_DIR = '/tmp/checkbox.optical'
 
13
DEFAULT_DEVICE_DIR = 'device'
 
14
DEFAULT_IMAGE_DIR = 'image'
 
15
 
 
16
 
 
17
CDROM_ID = '/lib/udev/cdrom_id'
 
18
 
 
19
 
 
20
def _command(command, shell=True):
 
21
    proc = Popen(command,
 
22
                   shell=shell,
 
23
                   stdout=PIPE,
 
24
                   stderr=PIPE
 
25
                   )
 
26
    return proc
 
27
 
 
28
 
 
29
def _command_out(command, shell=True):
 
30
    proc = _command(command, shell)
 
31
    return proc.communicate()[0].strip()
 
32
 
 
33
 
 
34
def compare_tree(source, target):
 
35
    for dirpath, dirnames, filenames in os.walk(source):
 
36
        #if both tree are empty return false
 
37
        if dirpath == source and dirnames == [] and filenames == []:
 
38
            return False
 
39
        for name in filenames:
 
40
            file1 = os.path.join(dirpath, name)
 
41
            file2 = file1.replace(source, target, 1)
 
42
            if os.path.isfile(file1) and not os.path.islink(file1):
 
43
                if filecmp.cmp(file1, file2):
 
44
                    continue
 
45
                else:
 
46
                    return False
 
47
            else:
 
48
                continue
 
49
    return True
 
50
 
 
51
 
 
52
def read_test(device):
 
53
    passed = False
 
54
    device_dir = os.path.join(DEFAULT_DIR, DEFAULT_DEVICE_DIR)
 
55
    image_dir = os.path.join(DEFAULT_DIR, DEFAULT_IMAGE_DIR)
 
56
 
 
57
    for dir in (device_dir, image_dir):
 
58
        if posixpath.exists(dir):
 
59
            shutil.rmtree(dir)
 
60
    os.makedirs(device_dir)
 
61
 
 
62
    try:
 
63
        _command("umount %s" % device).communicate()
 
64
        mount = _command("mount -o ro %s %s" % (device, device_dir))
 
65
        mount.communicate()
 
66
        if mount.returncode != 0:
 
67
            print("Unable to mount %s to %s" % 
 
68
                    (device, device_dir), file=sys.stderr)
 
69
            return False
 
70
 
 
71
        file_copy = _command("cp -dpR %s %s" % (device_dir, image_dir))
 
72
        file_copy.communicate()
 
73
        if file_copy.returncode != 0:
 
74
            print("Failed to copy files from %s to %s" % 
 
75
                    (device_dir, image_dir), file=sys.stderr)
 
76
            return False
 
77
        if compare_tree(device_dir, image_dir):
 
78
            passed = True
 
79
    except:
 
80
        print("File Comparison failed while testing %s" % device, 
 
81
                file=sys.stderr)
 
82
        passed = False
 
83
    finally:
 
84
        _command("umount %s" % device_dir).communicate(3)
 
85
        for dir in (device_dir, image_dir):
 
86
            if posixpath.exists(dir):
 
87
                shutil.rmtree(dir)
 
88
 
 
89
    if passed:
 
90
        print("File Comparison passed (%s)" % device)
 
91
    
 
92
    return passed
 
93
 
 
94
 
 
95
def get_capabilities(device):
 
96
    cmd = "%s %s" % (CDROM_ID, device)
 
97
    capabilities = _command_out(cmd)
 
98
    return capabilities
 
99
 
 
100
 
 
101
def main():
 
102
    tests = []
 
103
    return_values = []
 
104
 
 
105
    parser = ArgumentParser()
 
106
    parser.add_argument("device", nargs='+',
 
107
                        help=('Specify an optical device or list of devices '
 
108
                              'such as /dev/cdrom'))
 
109
    args = parser.parse_args()
 
110
 
 
111
    if os.geteuid() != 0:
 
112
        parser.error("ERROR: Must be root to run this script.")
 
113
 
 
114
    for device in args.device:
 
115
 
 
116
        capabilities = get_capabilities(device)
 
117
        if not capabilities:
 
118
            print("Unable to get capabilities of %s" % device, file=sys.stderr)
 
119
            return 1
 
120
        for capability in capabilities.decode().split('\n'):
 
121
            if capability[:3] == 'ID_':
 
122
                cap = capability[3:-2]
 
123
                if cap == 'CDROM' or cap == 'CDROM_DVD':
 
124
                    tests.append('read')
 
125
 
 
126
        for test in set(tests):
 
127
            print("Testing %s on %s ... " % (test, device), file=sys.stdout)
 
128
            tester = "%s_test" % test
 
129
            return_values.append(globals()[tester](device))
 
130
    
 
131
    return False in return_values
 
132
 
 
133
if __name__ == "__main__":
 
134
    sys.exit(main())