File data/vifm-media-osx added (mode: 100755) (index 000000000..90b499f33) |
|
1 |
|
#!/usr/bin/env python |
|
2 |
|
# -*- coding: utf-8 -*- |
|
3 |
|
"""vifm mediaprg for Mac OSX""" |
|
4 |
|
|
|
5 |
|
from __future__ import print_function |
|
6 |
|
|
|
7 |
|
import argparse |
|
8 |
|
import plistlib |
|
9 |
|
import subprocess |
|
10 |
|
import sys |
|
11 |
|
|
|
12 |
|
DISKUTIL="diskutil" |
|
13 |
|
|
|
14 |
|
def make_parser(): |
|
15 |
|
"""Return arparse.ArgumentParser instance""" |
|
16 |
|
parser = argparse.ArgumentParser( |
|
17 |
|
description=__doc__, # printed with -h/--help |
|
18 |
|
# Don't mess with format of description |
|
19 |
|
formatter_class=argparse.RawDescriptionHelpFormatter, |
|
20 |
|
# To have --help print defaults with trade-off it changes |
|
21 |
|
# formatting, use: ArgumentDefaultsHelpFormatter |
|
22 |
|
) |
|
23 |
|
subparsers = parser.add_subparsers(help='command') |
|
24 |
|
|
|
25 |
|
parser_list = subparsers.add_parser('list', help='list mounted media') |
|
26 |
|
parser_list.set_defaults(func=list) |
|
27 |
|
|
|
28 |
|
parser_mount = subparsers.add_parser('mount', help='mount a device') |
|
29 |
|
parser_mount.add_argument("device", metavar="device", type=str, nargs=1, |
|
30 |
|
help="device to mount") |
|
31 |
|
parser_mount.set_defaults(func=mount) |
|
32 |
|
|
|
33 |
|
parser_unmount = subparsers.add_parser('unmount', help='unmount given mount point') |
|
34 |
|
parser_unmount.add_argument("path", metavar="path", type=str, nargs=1, |
|
35 |
|
help="path to unmount") |
|
36 |
|
parser_unmount.set_defaults(func=unmount) |
|
37 |
|
|
|
38 |
|
return parser |
|
39 |
|
|
|
40 |
|
def list(args): |
|
41 |
|
"""List media""" |
|
42 |
|
try: |
|
43 |
|
# Use '-plist' to get XML output |
|
44 |
|
output = subprocess.check_output( |
|
45 |
|
[DISKUTIL, "list", "-plist"]) |
|
46 |
|
except subprocess.CalledProcessError: |
|
47 |
|
print("Failed to execute '{}'".format(DISKUTIL), file=sys.stderr) |
|
48 |
|
return(1) |
|
49 |
|
root = plistlib.readPlistFromString(output) |
|
50 |
|
for disk in root["AllDisksAndPartitions"]: |
|
51 |
|
# By experimentation, these seem to be root and other uninteresting |
|
52 |
|
# disks |
|
53 |
|
if disk["Content"] == "GUID_partition_scheme": |
|
54 |
|
continue |
|
55 |
|
if "Partitions" in disk: |
|
56 |
|
for partition in disk["Partitions"]: |
|
57 |
|
print_disk_or_partition(partition) |
|
58 |
|
else: |
|
59 |
|
print_disk_or_partition(disk) |
|
60 |
|
return(0) |
|
61 |
|
|
|
62 |
|
def print_disk_or_partition(node): |
|
63 |
|
"""Given a PList node, print the associated disk or partition""" |
|
64 |
|
print("device=/dev/" + node["DeviceIdentifier"]) |
|
65 |
|
try: |
|
66 |
|
print("label=" + node["VolumeName"]) |
|
67 |
|
except KeyError: |
|
68 |
|
pass |
|
69 |
|
try: |
|
70 |
|
print("mount-point=" + node["MountPoint"]) |
|
71 |
|
except KeyError: |
|
72 |
|
# Unmounted disk |
|
73 |
|
pass |
|
74 |
|
|
|
75 |
|
def mount(args): |
|
76 |
|
"""Mount a device""" |
|
77 |
|
device = args.device[0] |
|
78 |
|
try: |
|
79 |
|
subprocess.check_call([DISKUTIL, "mount", device]) |
|
80 |
|
except OSError: |
|
81 |
|
print("Failed to execute {}".format(DISKUTIL), file=sys.stderr) |
|
82 |
|
return(1) |
|
83 |
|
except subprocess.CalledProcessError: |
|
84 |
|
# Trust diskutil to have output a useful error message |
|
85 |
|
return(1) |
|
86 |
|
return(0) |
|
87 |
|
|
|
88 |
|
def unmount(args): |
|
89 |
|
"""Unmount given mount point""" |
|
90 |
|
path = args.path[0] |
|
91 |
|
try: |
|
92 |
|
subprocess.check_call([DISKUTIL, "unmount", path]) |
|
93 |
|
except OSError: |
|
94 |
|
print("Failed to execute {}".format(DISKUTIL), file=sys.stderr) |
|
95 |
|
return(1) |
|
96 |
|
except subprocess.CalledProcessError: |
|
97 |
|
# Trust diskutil to have output a useful error message |
|
98 |
|
return(1) |
|
99 |
|
return(0) |
|
100 |
|
|
|
101 |
|
def main(argv=None): |
|
102 |
|
parser = make_parser() |
|
103 |
|
args = parser.parse_args(argv if argv else sys.argv[1:]) |
|
104 |
|
return(args.func(args)) |
|
105 |
|
|
|
106 |
|
if __name__ == "__main__": |
|
107 |
|
sys.exit(main()) |