-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvboxssdelete.py
103 lines (80 loc) · 3.72 KB
/
vboxssdelete.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import os
import re
import shlex
import subprocess
import clr
clr.AddReference('System.Windows.Forms')
# .NET imports
import wpf
import System
from System.Windows import Application, Controls, Forms, Window
class VBoxSSDelete(Window):
vbmpaths = ["C:\Program Files\Oracle\VirtualBox\VBoxManage.exe",
"C:\Program Files (x86)\Oracle\VirtualBox\VBoxManage.exe",
"C:\Program Files\VirtualBox\VBoxManage.exe",
"C:\Program Files (x86)\VirtualBox\VBoxManage.exe" ]
def __init__(self):
wpf.LoadComponent(self, 'vboxssdelete.xaml')
self.VBoxManageButton.Click += self.VBoxManageButtonClick
self.DeleteButton.Click += self.DeleteButtonClick
self.VMListBox.SelectionChanged += self.VMListBoxChange
for path in self.vbmpaths:
if os.path.exists(path):
self.VBoxManageTextBox.Text = path
break
self.LoadVMList()
def LoadVMList(self):
try:
self.VMListBox.Items.Clear()
cmd = '"%s" list vms' % self.VBoxManageTextBox.Text
vmslist = subprocess.check_output(cmd)
for line in str.splitlines(vmslist):
# vboxmanage list vms result is 'machine_name {uuid}'
(name, uuid) = shlex.split(line)
lbi = Controls.ListBoxItem()
lbi.Content = name
self.VMListBox.Items.Add(lbi)
except Exception as err:
raise err
def LoadSSList(self, name):
"""
Get list of snapshots for given machine name/uuid
"""
try:
self.SSListBox.Items.Clear()
cmd = '"%s" showvminfo "%s"' % (self.VBoxManageTextBox.Text, name)
cmdoutput = subprocess.check_output(cmd)
found_snapshot_string = False
for line in str.splitlines(cmdoutput):
if line.strip() == "":
continue
if re.match("Snapshots:", line):
found_snapshot_string = True
continue
if found_snapshot_string:
mobj = re.match("\s*Name: (.*) \(UUID: (.*)\)", line)
if mobj:
lbi = Controls.ListBoxItem()
lbi.Content = mobj.group(1)
self.SSListBox.Items.Add(lbi)
# no more snapshots found, exit loop
else:
break
except Exception as err:
raise err
def VBoxManageButtonClick(self, sender, args):
ofd = Forms.OpenFileDialog()
ofd.CheckFileExists = True
if ofd.ShowDialog() == Forms.DialogResult.OK:
self.VBoxManageTextBox.Text = ofd.FileName
def VMListBoxChange(self, sender, args):
self.LoadSSList(self.VMListBox.SelectedItem.Content)
def DeleteButtonClick(self, sender, args):
for item in self.SSListBox.SelectedItems:
cmd = '"%s" snapshot "%s" delete "%s"' % (self.VBoxManageTextBox.Text,
self.VMListBox.SelectedItem.Content,
item.Content)
self.StatusTextBox.Text += "Deleting snapshot with command command [%s]\n" % (cmd)
cmdoutput = subprocess.check_output(cmd)
self.StatusTextBox.Text += cmdoutput + "\n"
self.LoadSSList(self.VMListBox.SelectedItem.Content)