Index: vws ================================================================== --- vws +++ vws @@ -9,13 +9,17 @@ import select import errno import re import os import os.path +import fnmatch +import shlex +import shutil import sys import time import pwd +import grp VERSION = 0.8 def find_vm(name): """ Search and return VM directory """ search_path = [os.path.join(pwd.getpwuid(os.getuid()).pw_dir, "VWs"), @@ -41,13 +45,13 @@ except IOError as ex: if ex.errno == errno.ECONNREFUSED: # virtal machine is not running return None raise ex - readfd, dummy_w, dummy_x = select.select([sock], [], [], 0.1) + readfd, _, _ = select.select([sock], [], [], 0.1) if sock in readfd: - dummy_greeting = sock.recv(1024) + _ = sock.recv(1024) return sock def send_command(sock, command): """ Sends monitor command to given socket and returns answer """ if sock is None: @@ -228,20 +232,23 @@ def make_start_cmdline(options): """ Append options passed from vws commandline to start script commandline """ - arg = "" + arg = [] if options.cdrom: - arg = " -cdrom " + os.path.abspath(options.cdrom[0]) + arg.append("-cdrom") + arg.append(os.path.abspath(options.cdrom[0])) if options.snapshot: - arg = arg+" -snapshot" + arg.append("-snapshot") if options.args: - arg = arg + " " + "".join(options.args) + arg.append( "".join(options.args)) if options.password: os.environ["SPICE_PASSWORD"] = options.password[0] - return arg + if arg: + return shlex.join(arg) + return "" def cmd_start(options): """ vws start """ if not "DISPLAY" in os.environ: # If cannot start GUI just don't do it. options.gui = False @@ -250,11 +257,11 @@ cwd = os.getcwd() os.chdir(options.dir) # Check for snapshot snapshot_id = check_for_snapshot(options.dir) if snapshot_id is not None: - arg = arg + " -loadvm " + snapshot_id + arg = arg + " " + shlex.join(["-loadvm", snapshot_id]) # Check for correct brige name try: os.stat("monitor") except FileNotFoundError: # We cannot find monitor socket. So this machine might be @@ -316,11 +323,11 @@ eol = answer.endswith("\n") sys.stdout.flush() elif options.sock in readfd: print("UNSOLICITED MESSAGE %" + options.sock.recv(1000).decode("utf-8").rstrip()) - eol = True + eol = True except KeyboardInterrupt: if not eol: print("") eol = True print("Keyboard interrupt") @@ -478,16 +485,15 @@ if "mac" in vminfo and not "ip" in vminfo: if vminfo["mac"] in arp_data: vminfo["ip"] = arp_data[vminfo["mac"]] else: vminfo["ip"] = "-" -def all_vms(patterns=["*"]): +def all_vms(patterns=("*",)): """ Returns list of tuples vmname, vmtype, directory for all vms """ - import fnmatch search_path = [("private", os.path.join(pwd.getpwuid(os.getuid()).pw_dir, "VWs")), ("shared", config.get("directories", "SharedVMs")), ("autostart", config.get("directories", "AutostartVMs"))] vmlist = [] @@ -530,18 +536,16 @@ if not count: sys.exit(1) def cmd_screenshot(options): """ vws screenshot """ - from os.path import abspath - filename = abspath(options.filename) + filename = os.path.abspath(options.filename) print(send_command(options.sock, "screendump " + filename)) def cmd_record(options): """ vws record """ - from os.path import abspath - filename = abspath(options.filename) + filename = os.path.abspath(options.filename) print(send_command(options.sock, "wavcapture " + filename)) def cmd_stoprecord(options): """ vws stoprecord """ answer = send_command(options.sock, "info capture") @@ -710,11 +714,10 @@ def cmd_shutdown(options): """ Search for all running machines and stops all of them """ dirlist = [config.get("directories", "AutostartVMs"), config.get("directories", "SharedVms")] if os.getresuid()[1] == 0: - import grp dirlist += map(lambda x: os.path.expanduser("~"+x)+"/VWs", grp.getgrnam(config.get("permissions", "vm_group")).gr_mem) else: dirlist.append(os.path.expanduser("~")+"/VWs") @@ -845,20 +848,20 @@ if parsed_args.localtime: options["rtc"] = "-rtc base=localtime,clock=host \\\n" if os.path.exists(machinedir): if os.path.exists(os.path.join(machinedir, "start")): - raise OSError("Virtual Worstation %s already exists" % + exc = OSError("Virtual Worstation %s already exists" % parsed_args.machine) else: - raise OSError("Cannot create VW directory, " + + exc = OSError("Cannot create VW directory, " + "something on the way") + raise exc # Creating directory for VM os.makedirs(machinedir, dirmode) parsed_args.dir = machinedir if parsed_args.shared: - import grp gid = grp.getgrnam(config.get("permissions", "vm_group")).gr_gid uid = os.getuid() os.chown(machinedir, uid, gid) if config.getboolean("permissions", "setgid_vm"): os.chmod(machinedir, 0o2775) @@ -940,12 +943,14 @@ 'setgid_vm':'yes'}}) def read_config(conf): """ Read configration files """ if os.getuid() != 0: + home = pwd.getpwuid(os.getuid()).pw_dir conf.read(['/etc/vws.conf', - os.path.join(pwd.getpwuid(os.getuid()).pw_dir, '.vwsrc')]) + os.path.join(home, ".config", "vws", "vws.conf"), + os.path.join(home, '.vwsrc')]) else: conf.read(['/etc/vws.conf']) def main(): """ Parse an arguments and execute everything """ global config @@ -1090,11 +1095,10 @@ try: cmd_create(parsed_args) except Exception as ex: # pylint: disable=broad-except print(str(ex), file=sys.stderr) if hasattr(parsed_args, "dir"): - import shutil shutil.rmtree(parsed_args.dir) sys.exit(1) sys.exit(0) if parsed_args.command is None: