9
from argparse import ArgumentParser
10
from subprocess import Popen, PIPE
12
DEFAULT_DIR = '/tmp/checkbox.optical'
13
DEFAULT_DEVICE_DIR = 'device'
14
DEFAULT_IMAGE_DIR = 'image'
17
CDROM_ID = '/lib/udev/cdrom_id'
20
def _command(command, shell=True):
29
def _command_out(command, shell=True):
30
proc = _command(command, shell)
31
return proc.communicate()[0].strip()
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 == []:
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):
52
def read_test(device):
54
device_dir = os.path.join(DEFAULT_DIR, DEFAULT_DEVICE_DIR)
55
image_dir = os.path.join(DEFAULT_DIR, DEFAULT_IMAGE_DIR)
57
for dir in (device_dir, image_dir):
58
if posixpath.exists(dir):
60
os.makedirs(device_dir)
63
_command("umount %s" % device).communicate()
64
mount = _command("mount -o ro %s %s" % (device, device_dir))
66
if mount.returncode != 0:
67
print("Unable to mount %s to %s" %
68
(device, device_dir), file=sys.stderr)
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)
77
if compare_tree(device_dir, image_dir):
80
print("File Comparison failed while testing %s" % device,
84
_command("umount %s" % device_dir).communicate(3)
85
for dir in (device_dir, image_dir):
86
if posixpath.exists(dir):
90
print("File Comparison passed (%s)" % device)
95
def get_capabilities(device):
96
cmd = "%s %s" % (CDROM_ID, device)
97
capabilities = _command_out(cmd)
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()
111
if os.geteuid() != 0:
112
parser.error("ERROR: Must be root to run this script.")
114
for device in args.device:
116
capabilities = get_capabilities(device)
118
print("Unable to get capabilities of %s" % device, file=sys.stderr)
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':
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))
131
return False in return_values
133
if __name__ == "__main__":