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, service, family=None): self.service = service self.family = family self._pending = set() if service.process_timeout is not None: self.process_timeout = service.process_timeout if not service.environment: self._procenv = Environment() else: self._procenv = service.environment if not service.exec_args: raise ChParameterError( "No command or arguments provided for service") # If the service is enabled, assure we check for the presence of the executable now. This is # to catch any start-up situations (such as cron jobs without their executables being present). # However, we don't check this if a service is disabled. self._orig_executable = service.exec_args[0] if service.enabled: self._try_to_enable()
def test_expand8(self): "Test conditional expansion" env = Environment(from_env=ENV8).expanded() #printdict(env, compare = RESULT8) envstr = canonical(env) #print('RESULT8 = "' + envstr + '"') self.assertEqual(envstr, RESULT8)
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 test_expand6(self): "Try self-referential expansions" enva = Environment(ENV4a, CONFIG4a) self.assertEqual( canonical(enva.expanded()), "[('ADMINVAR1', 'user'), ('ADMINVAR2', ''), ('ADMINVAR3', 'admin'), ('PATH', '/usr/local/bin:/bin'), ('THEREPATH', '/there')]" ) envb = Environment(enva) self.assertEqual( canonical(envb.expanded()), "[('ADMINVAR1', 'user'), ('ADMINVAR2', ''), ('ADMINVAR3', 'admin'), ('PATH', '/usr/local/bin:/bin'), ('THEREPATH', '/there')]" ) envc = Environment(envb, CONFIG4c) self.assertEqual( canonical(envc.expanded()), "[('ADMINVAR1', 'user'), ('ADMINVAR2', ''), ('ADMINVAR3', 'admin'), ('MISCPATH', '/mislibs'), ('PATH', '/usr/python/bin:/usr/local/bin:/bin'), ('PYAGAIN', '/mislibs:/pythonlibs:'), ('PYPATH', '/pythonlibs:'), ('THEREPATH', '/mislibs:/there')]" )
def test_expand6(self): "Try self-referential expansions" enva = Environment(ENV4a, CONFIG4a) self.assertEqual(canonical(enva.expanded()), "[('ADMINVAR1', 'user'), ('ADMINVAR2', ''), ('ADMINVAR3', 'admin'), ('PATH', '/usr/local/bin:/bin'), ('THEREPATH', '/there')]") envb = Environment(enva) self.assertEqual(canonical(envb.expanded()), "[('ADMINVAR1', 'user'), ('ADMINVAR2', ''), ('ADMINVAR3', 'admin'), ('PATH', '/usr/local/bin:/bin'), ('THEREPATH', '/there')]") envc = Environment(envb, CONFIG4c) self.assertEqual(canonical(envc.expanded()), "[('ADMINVAR1', 'user'), ('ADMINVAR2', ''), ('ADMINVAR3', 'admin'), ('MISCPATH', '/mislibs'), ('PATH', '/usr/python/bin:/usr/local/bin:/bin'), ('PYAGAIN', '/mislibs:/pythonlibs:'), ('PYPATH', '/pythonlibs:'), ('THEREPATH', '/mislibs:/there')]")
def __init__(self, initdict, name = "MAIN", env = None, settings = None): self.name = name if settings: for sd in self._settings_defaults: if sd not in initdict: val = settings.get(sd) if val is not None: setattr(self, sd, val) for k,v in initdict.items(): setattr(self, k, v) # User names always have .xxx qualifier because of schema restrictions. Otherwise, it's a user # defined name subject to restrictions. splitname = self.name.rsplit('.', 1) if len(splitname) == 2 and splitname[0] == splitname[0].upper(): raise ChParameterError("all-uppercase names such as '{0}' are reserved for the system.".format(self.name)) # UID and GID are expanded according to the incoming environment, # since the new environment depends upon these. if env: env.expand_attributes(self, 'uid', 'gid') uid = self.get('uid') gid = self.get('gid') if gid is not None and uid is None: raise Exception("cannot specify 'gid' without 'uid'") # We can now use 'self' as our config, with all defaults. env = self.environment = Environment(env, uid=uid, gid=gid, config=self, resolve_xid = not self.get('optional', False)) self.augment_environment(env) if self._expand_these: env.expand_attributes(self, *self._expand_these) for attr,func in self._typecheck.items(): getattr(self, '_typecheck_'+func)(attr) self.post_init()
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 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 get_environment(self): if not self._env: self._env = Environment(config=self.get_settings(), uid=self.uid, gid=self.gid) return self._env
def test_expand7(self): "Test some self-referential anomalies" env = Environment(ENV7, CONFIG7a).expanded() envstr = canonical(env) #print('RESULT7 = "' + envstr + '"') self.assertEqual(envstr, RESULT7)
def test_expand4(self): env = Environment(from_env=ENV4).expanded() #printdict(env) envstr = canonical(env) #print('RESULT4 = "' + envstr + '"') self.assertEqual(envstr, RESULT4)
def test_expand3(self): env = Environment(from_env=ENV3).expanded() #printdict(env, compare = RESULT3) envstr = canonical(env) #print('RESULT3 = "' + canonical(env, True) + '"') self.assertEqual(envstr, RESULT3)
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