def open(url, desktop=None, wait=0): """ Open the 'url' in the current desktop's preferred file browser. If the optional 'desktop' parameter is specified then attempt to use that particular desktop environment's mechanisms to open the 'url' instead of guessing or detecting which environment is being used. Suggested values for 'desktop' are "standard", "KDE", "GNOME", "XFCE", "Mac OS X", "Windows" where "standard" employs a DESKTOP_LAUNCH environment variable to open the specified 'url'. DESKTOP_LAUNCH should be a command, possibly followed by arguments, and must have any special characters shell-escaped. The process identifier of the "opener" (ie. viewer, editor, browser or program) associated with the 'url' is returned by this function. If the process identifier cannot be determined, None is returned. An optional 'wait' parameter is also available for advanced usage and, if 'wait' is set to a true value, this function will wait for the launching mechanism to complete before returning (as opposed to immediately returning as is the default behaviour). """ # Decide on the desktop environment in use. desktop_in_use = use_desktop(desktop) if desktop_in_use == "standard": arg = "".join([os.environ["DESKTOP_LAUNCH"], subprocess.mkarg(url)]) return _run(arg, 1, wait) elif desktop_in_use == "Windows": # NOTE: This returns None in current implementations. return os.startfile(url) elif desktop_in_use == "KDE4": cmd = ["kioclient", "exec", url] elif desktop_in_use == "KDE": cmd = ["kfmclient", "exec", url] elif desktop_in_use == "GNOME": cmd = ["gnome-open", url] elif desktop_in_use == "XFCE": cmd = ["exo-open", url] elif desktop_in_use == "Mac OS X": cmd = ["open", url] elif desktop_in_use == "X11" and "BROWSER" in os.environ: cmd = [os.environ["BROWSER"], url] # Finish with an error where no suitable desktop was identified. else: raise OSError("Desktop '%s' not supported (neither DESKTOP_LAUNCH nor os.startfile could be used)" % desktop_in_use) return _run(cmd, 0, wait)
def open(url, desktop=None, wait=0): desktop_in_use = use_desktop(desktop) if desktop_in_use == "standard": arg = "".join([os.environ["DESKTOP_LAUNCH"], subprocess.mkarg(url)]) return _run(arg, 1, wait) elif desktop_in_use == "GNOME": cmd = ["gnome-open", url] elif desktop_in_use == "X11" and "BROWSER" in os.environ: cmd = [os.environ["BROWSER"], url] else: raise OSError("Desktop '%s' not supported (neither DESKTOP_LAUNCH nor os.startfile could be used)" % desktop_in_use) return _run(cmd, 0, wait)
def main(): import getopt import subprocess args = sys.argv[1:] try: opts, args = getopt.gnu_getopt(args, 'qph', ["quiet", "pty", "help"]) except getopt.GetoptError as e: usage(e) opt_quiet = False opt_pty = False for opt, val in opts: if opt in ('-h', '--help'): usage() if opt in ('-q', '--quiet'): opt_quiet = True if opt in ('-p', '--pty'): opt_pty = True if not args: usage() output = args.pop(0) if output[0] == '|': output = output[1:] from subprocess import Popen, PIPE p = Popen(output, shell=True, stdin=PIPE) output_fh = p.stdin else: output_fh = open(output, 'w') command = args if args else [os.environ.get('SHELL', '/bin/bash')] trap = UnitedStdTrap(usepty=opt_pty, transparent=not opt_quiet, tee=output_fh) try: os.system(command[0] + " ".join(subprocess.mkarg(arg) for arg in command[1:])) finally: trap.close() output_fh.close()
def _prepareCommand(self, command, username): j.logger.log('Attempt to run %s as user %s' % (command, username), 6) try: pwent = pwd.getpwnam(username) except KeyError: raise ValueError('The user %s can\'t be found on this system' % username) if not os.getuid() == 0: raise j.exceptions.RuntimeError('Can\'t execute as user when not running as root (UID 0)') subin = '/bin/su' if not j.sal.fs.exists(subin): raise j.exceptions.RuntimeError('%s not found on this system, I need it there' % subin) command = '%s --login --command %s %s' % (subin, subprocess.mkarg(command), username) return command
def quote_shell_arg(cmd): return subprocess.mkarg(cmd)