#!/usr/bin/env python3
import sys
import subprocess
from tempfile import NamedTemporaryFile as TMPFile

fp = TMPFile()
def change_usb_state(vm, action, devices):
    fp.seek(0)
    fp.truncate(0)
    fp.write("""<hostdev mode='subsystem' type='usb'>
                    <source>
                    <vendor id='0x{}'/>
                    <product id='0x{}'/>
                    </source>
                </hostdev>""".format(*devices.split(':')).encode())
    fp.flush()
    return subprocess.call(['doas','virsh', action+'-device', vm, fp.name])


def run_usb_command(vm_name, action, *devices):
    for device in devices:
        fail = change_usb_state(vm_name, action, device)
        if fail: print(f"[VMusb] {action}ing {devices} on {vm_name} failed")

def main(arguments):
    if len(arguments) <3 and arguments[1] not in ['attach','detach']:
        print(f"[VMusb] invalid action: {arguments}")
        return 1
    run_usb_command(*arguments)
    return 0
    
if __name__ == "__main__":
    main(sys.argv[1::])

a3e8bd18215fa2a355b80203ed8010802d48f739
<mask token>


def main(arguments):
    if len(arguments) < 3 and arguments[1] not in ['attach', 'detach']:
        print(f'[VMusb] invalid action: {arguments}')
        return 1
    run_usb_command(*arguments)
    return 0


<mask token>

<mask token>


def change_usb_state(vm, action, devices):
    fp.seek(0)
    fp.truncate(0)
    fp.write(
        """<hostdev mode='subsystem' type='usb'>
                    <source>
                    <vendor id='0x{}'/>
                    <product id='0x{}'/>
                    </source>
                </hostdev>"""
        .format(*devices.split(':')).encode())
    fp.flush()
    return subprocess.call(['doas', 'virsh', action + '-device', vm, fp.name])


def run_usb_command(vm_name, action, *devices):
    for device in devices:
        fail = change_usb_state(vm_name, action, device)
        if fail:
            print(f'[VMusb] {action}ing {devices} on {vm_name} failed')


def main(arguments):
    if len(arguments) < 3 and arguments[1] not in ['attach', 'detach']:
        print(f'[VMusb] invalid action: {arguments}')
        return 1
    run_usb_command(*arguments)
    return 0


if __name__ == '__main__':
    main(sys.argv[1:])

<mask token>
fp = TMPFile()


def change_usb_state(vm, action, devices):
    fp.seek(0)
    fp.truncate(0)
    fp.write(
        """<hostdev mode='subsystem' type='usb'>
                    <source>
                    <vendor id='0x{}'/>
                    <product id='0x{}'/>
                    </source>
                </hostdev>"""
        .format(*devices.split(':')).encode())
    fp.flush()
    return subprocess.call(['doas', 'virsh', action + '-device', vm, fp.name])


def run_usb_command(vm_name, action, *devices):
    for device in devices:
        fail = change_usb_state(vm_name, action, device)
        if fail:
            print(f'[VMusb] {action}ing {devices} on {vm_name} failed')


def main(arguments):
    if len(arguments) < 3 and arguments[1] not in ['attach', 'detach']:
        print(f'[VMusb] invalid action: {arguments}')
        return 1
    run_usb_command(*arguments)
    return 0


if __name__ == '__main__':
    main(sys.argv[1:])

import sys
import subprocess
from tempfile import NamedTemporaryFile as TMPFile
fp = TMPFile()


def change_usb_state(vm, action, devices):
    fp.seek(0)
    fp.truncate(0)
    fp.write(
        """<hostdev mode='subsystem' type='usb'>
                    <source>
                    <vendor id='0x{}'/>
                    <product id='0x{}'/>
                    </source>
                </hostdev>"""
        .format(*devices.split(':')).encode())
    fp.flush()
    return subprocess.call(['doas', 'virsh', action + '-device', vm, fp.name])


def run_usb_command(vm_name, action, *devices):
    for device in devices:
        fail = change_usb_state(vm_name, action, device)
        if fail:
            print(f'[VMusb] {action}ing {devices} on {vm_name} failed')


def main(arguments):
    if len(arguments) < 3 and arguments[1] not in ['attach', 'detach']:
        print(f'[VMusb] invalid action: {arguments}')
        return 1
    run_usb_command(*arguments)
    return 0


if __name__ == '__main__':
    main(sys.argv[1:])

#!/usr/bin/env python3
import sys
import subprocess
from tempfile import NamedTemporaryFile as TMPFile

fp = TMPFile()
def change_usb_state(vm, action, devices):
    fp.seek(0)
    fp.truncate(0)
    fp.write("""<hostdev mode='subsystem' type='usb'>
                    <source>
                    <vendor id='0x{}'/>
                    <product id='0x{}'/>
                    </source>
                </hostdev>""".format(*devices.split(':')).encode())
    fp.flush()
    return subprocess.call(['doas','virsh', action+'-device', vm, fp.name])


def run_usb_command(vm_name, action, *devices):
    for device in devices:
        fail = change_usb_state(vm_name, action, device)
        if fail: print(f"[VMusb] {action}ing {devices} on {vm_name} failed")

def main(arguments):
    if len(arguments) <3 and arguments[1] not in ['attach','detach']:
        print(f"[VMusb] invalid action: {arguments}")
        return 1
    run_usb_command(*arguments)
    return 0
    
if __name__ == "__main__":
    main(sys.argv[1::])
