~jocave/checkbox/hybrid-amd-gpu-mods

« back to all changes in this revision

Viewing changes to checkbox-old/scripts/block_device_resource

  • Committer: Tarmac
  • Author(s): Brendan Donegan
  • Date: 2013-06-03 11:12:58 UTC
  • mfrom: (2154.2.1 bug1185759)
  • Revision ID: tarmac-20130603111258-1b3m5ydvkf1accts
"[r=zkrynicki][bug=1185759][author=brendan-donegan] automatic merge by tarmac"

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python3
 
2
 
 
3
import os
 
4
import re
 
5
from glob import glob
 
6
 
 
7
rootdir_pattern = re.compile('^.*?/devices')
 
8
 
 
9
def device_state(name):
 
10
    """
 
11
    Follow pmount policy to determine whether a device is removable or internal.
 
12
    """
 
13
    with open('/sys/block/%s/device/block/%s/removable' % (name, name)) as f:
 
14
        if f.read(1) == '1':
 
15
            return 'removable'
 
16
 
 
17
    path = rootdir_pattern.sub('', os.readlink('/sys/block/%s' % name))
 
18
    hotplug_buses = ("usb", "ieee1394", "mmc", "pcmcia", "firewire")
 
19
    for bus in hotplug_buses:
 
20
        if os.path.exists('/sys/bus/%s' % bus):
 
21
            for device_bus in os.listdir('/sys/bus/%s/devices' % bus):
 
22
                device_link = rootdir_pattern.sub('', os.readlink(
 
23
                    '/sys/bus/%s/devices/%s' % (bus, device_bus)))
 
24
                if re.search(device_link, path):
 
25
                    return 'removable'
 
26
 
 
27
    return 'internal'
 
28
 
 
29
 
 
30
def usb_support(name, version):
 
31
    """
 
32
    Check the USB specification number for both hub port and device
 
33
    """
 
34
    path = rootdir_pattern.sub('', os.readlink('/sys/block/%s' % name))
 
35
 
 
36
    # Remove the usb config.interface part of the path
 
37
    m = re.match('((.*usb\d+).*\/)\d-[\d\.:\-]+\/.*', path)
 
38
    if m:
 
39
        device_path = m.group(1)
 
40
        hub_port_path = m.group(2)
 
41
 
 
42
        # Check the highest version of USB the device supports
 
43
        with open('/sys/devices/%s/version' %device_path) as f:
 
44
            if float(f.readline()) < version:
 
45
                return 'unsupported'
 
46
 
 
47
        # Check the highest version of USB the hub supports
 
48
        with open('/sys/devices/%s/version' %hub_port_path) as f:
 
49
            if float(f.readline()) < version:
 
50
                return 'unsupported'
 
51
 
 
52
        return 'supported'
 
53
 
 
54
    return 'unsupported'
 
55
 
 
56
 
 
57
for path in glob('/sys/block/*/device'):
 
58
    name = re.sub('.*/(.*?)/device', '\g<1>', path)
 
59
    state = device_state(name)
 
60
    usb2 = usb_support(name, 2.00)
 
61
    usb3 = usb_support(name, 3.00)
 
62
    # FIXME: Remove leading block device name when the requirements
 
63
    # checking code in Checkbox allows it
 
64
    print("""\
 
65
%(name)s_state: %(state)s
 
66
%(name)s_usb2: %(usb2)s
 
67
%(name)s_usb3: %(usb3)s
 
68
""" % {"name": name,
 
69
       "state": state,
 
70
       "usb2": usb2,
 
71
       "usb3": usb3})