2020# You should have received a copy of the GNU Lesser General Public License along
2121# with this program; if not, see <http://www.gnu.org/licenses/>.
2222
23- ''' Shutdown a qube '''
23+ """ Shutdown a qube"""
2424
2525from __future__ import print_function
2626
3333import qubesadmin .exc
3434
3535parser = qubesadmin .tools .QubesArgumentParser (
36- description = __doc__ , vmname_nargs = '+' )
36+ description = __doc__ , vmname_nargs = "+"
37+ )
3738
38- parser .add_argument ('--wait' ,
39- action = 'store_true' , default = False ,
40- help = 'wait for the VMs to shut down' )
39+ parser .add_argument (
40+ "--wait" ,
41+ action = "store_true" ,
42+ default = False ,
43+ help = "wait for the VMs to shut down" ,
44+ )
4145
42- parser .add_argument ('--timeout' ,
43- action = 'store' , type = float ,
46+ parser .add_argument (
47+ "--timeout" ,
48+ action = "store" ,
49+ type = float ,
4450 default = 60 ,
45- help = 'timeout after which domains are killed when using --wait'
46- ' (default: %(default)d)' )
51+ help = "timeout after which domains are killed when using --wait"
52+ " (default: %(default)d)" ,
53+ )
4754
4855parser .add_argument (
49- '--force' ,
50- action = 'store_true' , default = False ,
51- help = 'shut down even if other qubes depend on this one (e.g. as NetVM'
52- ' or AudioVM); does not affect how the qube itself is shut down;'
53- ' use with caution' )
56+ "--force" ,
57+ action = "store_true" ,
58+ default = False ,
59+ help = "shut down even if other qubes depend on this one (e.g. as NetVM"
60+ " or AudioVM); does not affect how the qube itself is shut down;"
61+ " use with caution" ,
62+ )
5463
5564parser .add_argument (
56- '--dry-run' ,
57- action = 'store_true' , dest = 'dry_run' , default = False ,
58- help = 'don\' t really shutdown or kill the domains; useful with --wait' )
65+ "--dry-run" ,
66+ action = "store_true" ,
67+ dest = "dry_run" ,
68+ default = False ,
69+ help = "don't really shutdown or kill the domains; useful with --wait" ,
70+ )
5971
6072
6173def failed_domains (vms ):
62- ''' Find the domains that have not successfully been shut down'''
74+ """ Find the domains that have not successfully been shut down"""
6375
6476 # DispVM might have been deleted before we check them, so NA is acceptable.
65- return [vm for vm in vms
66- if not (vm .get_power_state () == 'Halted'
67- or (vm .klass == 'DispVM' and vm .get_power_state () == 'NA' ))]
77+ return [
78+ vm
79+ for vm in vms
80+ if not (
81+ vm .get_power_state () == "Halted"
82+ or (vm .klass == "DispVM" and vm .get_power_state () == "NA" )
83+ )
84+ ]
85+
6886
6987def main (args = None , app = None ): # pylint: disable=missing-docstring
7088 args = parser .parse_args (args , app = app )
@@ -86,14 +104,15 @@ def main(args=None, app=None): # pylint: disable=missing-docstring
86104 pass
87105 except qubesadmin .exc .QubesException as e :
88106 if not args .wait :
89- vm .log .error (' Shutdown error: {}' .format (e ))
107+ vm .log .error (" Shutdown error: {}" .format (e ))
90108 shutdown_failed .add (vm )
91109 if not args .wait :
92110 if shutdown_failed :
93111 parser .error_runtime (
94- 'Failed to shut down: ' +
95- ', ' .join (vm .name for vm in shutdown_failed ),
96- len (shutdown_failed ))
112+ "Failed to shut down: "
113+ + ", " .join (vm .name for vm in shutdown_failed ),
114+ len (shutdown_failed ),
115+ )
97116 return
98117 awaiting = remaining_domains - shutdown_failed
99118 remaining_domains = shutdown_failed
@@ -103,16 +122,21 @@ def main(args=None, app=None): # pylint: disable=missing-docstring
103122
104123 try :
105124 # pylint: disable=no-member
106- loop .run_until_complete (asyncio .wait_for (
107- qubesadmin .events .utils .wait_for_domain_shutdown (
108- awaiting ), args .timeout ))
125+ loop .run_until_complete (
126+ asyncio .wait_for (
127+ qubesadmin .events .utils .wait_for_domain_shutdown (awaiting ),
128+ args .timeout ,
129+ )
130+ )
109131 except (TimeoutError , asyncio .TimeoutError ):
110132 if not args .dry_run :
111133 current_vms = failed_domains (awaiting )
112134 if current_vms :
113135 args .app .log .info (
114- 'Killing remaining qubes: {}'
115- .format (', ' .join ([str (vm ) for vm in current_vms ])))
136+ "Killing remaining qubes: {}" .format (
137+ ", " .join ([str (vm ) for vm in current_vms ])
138+ )
139+ )
116140 for vm in current_vms :
117141 try :
118142 vm .kill ()
@@ -126,10 +150,10 @@ def main(args=None, app=None): # pylint: disable=missing-docstring
126150 failed = failed_domains (args .domains )
127151 if failed :
128152 parser .error_runtime (
129- ' Failed to shut down: ' +
130- ', ' . join ( vm . name for vm in failed ),
131- len ( failed ) )
153+ " Failed to shut down: " + ", " . join ( vm . name for vm in failed ),
154+ len ( failed ),
155+ )
132156
133157
134- if __name__ == ' __main__' :
158+ if __name__ == " __main__" :
135159 sys .exit (main ())
0 commit comments