def test_expand5(self): "Try simple expansion" env = Environment(from_env = ENV4).expanded() self.assertEqual(env.expand("hello $(UBERNEST)"), "hello nest:inside/usr/garyw and /usr/garyw/apps and:/usr/garyw/apps/theap") self.assertEqual(env.expand("hello $(MAYBE5) and $(MAYBE4)"), "hello blach/foo and /usr/garyw/foo") self.assertEqual(env.expand("hello $(MAYBE5:+$(MAYBE5)b) and $(MAYBE41)"), "hello blach/foob and $(MAYBE41)") self.assertEqual(env.expand("hello $(MAYBE5:+$(MAYBE5)b) and $(MAYBE41:-gone$(MAYBE4))"), "hello blach/foob and gone/usr/garyw/foo")
def test_expand5(self): "Try simple expansion" env = Environment(from_env=ENV4).expanded() self.assertEqual( env.expand("hello $(UBERNEST)"), "hello nest:inside/usr/garyw and /usr/garyw/apps and:/usr/garyw/apps/theap" ) self.assertEqual(env.expand("hello $(MAYBE5) and $(MAYBE4)"), "hello blach/foo and /usr/garyw/foo") self.assertEqual( env.expand("hello $(MAYBE5:+$(MAYBE5)b) and $(MAYBE41)"), "hello blach/foob and $(MAYBE41)") self.assertEqual( env.expand( "hello $(MAYBE5:+$(MAYBE5)b) and $(MAYBE41:-gone$(MAYBE4))"), "hello blach/foob and gone/usr/garyw/foo")
def __init__(self, options): self.sockname = options['--socket'] if not self.sockname: self.sockname = "/tmp/sdnotify-proxy-{0}.sock".format(os.getpid()) self.proxy_enabled = parent_socket and not options['--noproxy'] if options['--wait-stop']: self.wait_mode = 'stop' elif options['--wait-ready']: self.wait_mode = 'ready' if options['--timeout'] and self.wait_mode: self.timeout = float(options['--timeout']) self.verbose = options['--verbose'] # Modify original environment os.environ['NOTIFY_SOCKET'] = self.sockname # Set up the environment, reparse the options, build the final command Environment.set_parse_parameters('%', '{') env = Environment() env['PID'] = str(os.getpid()) env['SOCKET_ARGS'] = options['--template'] or DEFAULT_TEMPLATE if parent_socket: env['ORIG_NOTIFY_SOCKET'] = parent_socket env = env.expanded() self.proc_args = shlex.split( env.expand(' '.join( maybe_quote(arg) for arg in [options['COMMAND']] + options['ARGS']))) self.listener = NotifyListener(self.sockname, onNotify=self.notify_received, onClose=self._parent_closed) loop.add_signal_handler(signal.SIGTERM, self._got_sig) loop.add_signal_handler(signal.SIGINT, self._got_sig) proctitle = '[sdnotify-exec]' try: from setproctitle import setproctitle setproctitle(proctitle) except ImportError: pass
def __init__(self, options): self.sockname = options['--socket'] if not self.sockname: self.sockname = "/tmp/sdnotify-proxy-{0}.sock".format(os.getpid()) self.proxy_enabled = parent_socket and not options['--noproxy'] if options['--wait-stop']: self.wait_mode = 'stop' elif options['--wait-ready']: self.wait_mode = 'ready' if options['--timeout'] and self.wait_mode: self.timeout = float(options['--timeout']) self.verbose = options['--verbose'] # Modify original environment os.environ['NOTIFY_SOCKET'] = self.sockname # Set up the environment, reparse the options, build the final command Environment.set_parse_parameters('%', '{') env = Environment() env['PID'] = str(os.getpid()) env['SOCKET_ARGS'] = options['--template'] or DEFAULT_TEMPLATE if parent_socket: env['ORIG_NOTIFY_SOCKET'] = parent_socket env = env.expanded() self.proc_args = shlex.split(env.expand(' '.join(maybe_quote(arg) for arg in [options['COMMAND']] + options['ARGS']))) self.listener = NotifyListener(self.sockname, onNotify = self.notify_received, onClose = self._parent_closed) loop.add_signal_handler(signal.SIGTERM, self._got_sig) loop.add_signal_handler(signal.SIGINT, self._got_sig) proctitle = '[sdnotify-exec]' try: from setproctitle import setproctitle setproctitle(proctitle) except ImportError: pass
def main_entry(): options = docopt(__doc__, version=VERSION_MESSAGE) files = options['FILE'] start = options['--xprefix'] braces = options['--xgrouping'] if braces: if any([b not in '{([' for b in braces]): print( "error: --xgrouping can accept one or more of '{{', '[', or '(' only. Not this: '{0}'." .format(braces)) exit(1) # Enable or disable, but don't cache them if enabled Environment.set_backtick_expansion(bool(options['--shell-enable']), False) Environment.set_parse_parameters(start, braces) env = Environment() # Support stdin/stdout behavior if '-' is the only file specified on the command line if '-' in files: if len(files) > 1: print( "error: '-' for stdin/stdout cannot be combined with other filename arguments" ) exit(1) sys.stdout.write(env.expand(sys.stdin.read())) sys.stdout.flush() exit(0) if len(files) < 2: print("error: must include two or more filename arguments") exit(1) destdir = os.path.abspath(files[-1]) destfile = None if os.path.isdir(destdir): if not os.access(destdir, os.W_OK | os.X_OK): print("error: directory {0} exists but is not writable".format( destdir)) st = options['--strip'] if st: files = [(f, os.path.basename(f).rstrip(st)) for f in files[:-1]] else: files = [(f, os.path.basename(f)) for f in files[:-1]] check_canwrite([os.path.join(destdir, p[1]) for p in files], options['--overwrite']) elif len(files) != 2: print( "error: destination is not a directory and more than 2 files specified" ) exit(1) else: destfile = files[1] files = [(files[0], files[0])] check_canwrite([destfile], options['--overwrite']) # files is now a list of pairs [(source, dest-basename), ...] for curpair in files: if not os.path.exists(curpair[0]): print("error: file does not exist, {0}".format(curpair[0])) exit(1) if not os.access(curpair[0], os.R_OK): print("error: file is not readable, {0}".format(curpair[0])) exit(1) for curpair in files: if not destfile: destfile = os.path.join(destdir, curpair[1]) try: oldstat = os.stat(curpair[0]) oldf = open(curpair[0], 'r') except Exception as ex: print("error: cannot open input file {0}: {1}".format( curpair[0], ex)) exit(1) try: newf = open(destfile, 'w') except Exception as ex: print("error: cannot open output file {0}: {1}".format( destfile, ex)) exit(1) newf.write(env.expand(oldf.read())) oldf.close() newf.close() if options['--archive']: # ATTEMPT to retain permissions try: os.chown(destfile, oldstat.st_uid, oldstat.st_gid) except PermissionError: # Try them separately. User first, then group. try: os.chown(destfile, oldstat.st_uid, -1) except PermissionError: pass try: os.chown(destfile, -1, oldstat.st_gid) except PermissionError: pass try: os.chmod(destfile, oldstat.st_mode) except PermissionError: pass try: os.utime(destfile, times=(oldstat.st_atime, oldstat.st_mtime)) except PermissionError: pass if options['--verbose']: print("envcp {0} {1}".format(curpair[0], destfile)) destfile = None
def main_entry(): options = docopt(__doc__, version=VERSION_MESSAGE) files = options['FILE'] start = options['--xprefix'] braces = options['--xgrouping'] if braces: if any([b not in '{([' for b in braces]): print("error: --xgrouping can accept one or more of '{{', '[', or '(' only. Not this: '{0}'.".format(braces)) exit(1) # Enable or disable, but don't cache them if enabled Environment.set_backtick_expansion(bool(options['--shell-enable']), False) Environment.set_parse_parameters(start, braces) env = Environment() # Support stdin/stdout behavior if '-' is the only file specified on the command line if '-' in files: if len(files) > 1: print("error: '-' for stdin/stdout cannot be combined with other filename arguments") exit(1) sys.stdout.write(env.expand(sys.stdin.read())) sys.stdout.flush() exit(0) if len(files) < 2: print("error: must include two or more filename arguments") exit(1) destdir = os.path.abspath(files[-1]); destfile = None if os.path.isdir(destdir): if not os.access(destdir, os.W_OK|os.X_OK): print("error: directory {0} exists but is not writable".format(destdir)) st = options['--strip'] if st: files = [(f, os.path.basename(f).rstrip(st)) for f in files[:-1]] else: files = [(f, os.path.basename(f)) for f in files[:-1]] check_canwrite([os.path.join(destdir, p[1]) for p in files], options['--overwrite']) elif len(files) != 2: print("error: destination is not a directory and more than 2 files specified") exit(1) else: destfile = files[1] files = [(files[0], files[0])] check_canwrite([destfile], options['--overwrite']) # files is now a list of pairs [(source, dest-basename), ...] for curpair in files: if not os.path.exists(curpair[0]): print("error: file does not exist, {0}".format(curpair[0])) exit(1) if not os.access(curpair[0], os.R_OK): print("error: file is not readable, {0}".format(curpair[0])) exit(1) for curpair in files: if not destfile: destfile = os.path.join(destdir, curpair[1]) try: oldstat = os.stat(curpair[0]) oldf = open(curpair[0], 'r') except Exception as ex: print("error: cannot open input file {0}: {1}".format(curpair[0], ex)) exit(1) try: newf = open(destfile, 'w') except Exception as ex: print("error: cannot open output file {0}: {1}".format(destfile, ex)) exit(1) newf.write(env.expand(oldf.read())) oldf.close() newf.close() if options['--archive']: # ATTEMPT to retain permissions try: os.chown(destfile, oldstat.st_uid, oldstat.st_gid); except PermissionError: # Try them separately. User first, then group. try: os.chown(destfile, oldstat.st_uid, -1); except PermissionError: pass try: os.chown(destfile, -1, oldstat.st_gid); except PermissionError: pass try: os.chmod(destfile, oldstat.st_mode); except PermissionError: pass try: os.utime(destfile, times=(oldstat.st_atime, oldstat.st_mtime)) except PermissionError: pass if options['--verbose']: print("envcp {0} {1}".format(curpair[0], destfile)) destfile = None