예제 #1
0
 def sync_env(self) -> None:
     """
     Synchronize the object's value with the environment variable TF_VAR_<name>
     """
     NAME = "TF_VAR_DOCKER_DROPLET_" + self.name.upper()
     if self.value:
         environ.putenv(NAME, self.value)
     else:
         self.value = environ.get(NAME, None)
예제 #2
0
    def __enter__(self):
        self.old_path = old_path = environ.get("PATH", None)
        self.p = save_pkg_resources_state()
        _ = self.p.__enter__()  # noqa: F841
        self.m = save_modules()
        _ = self.m.__enter__()  # noqa: F841
        self.e = CleanEnv()
        self.e.__enter__()
        if old_path:
            first_colon = old_path.index(":")
            first_part = old_path[:first_colon]
            if first_part.endswith("venv/bin"):
                replacement_path = old_path[first_colon + 1:]
                environ.putenv("PATH", replacement_path)
        if self.venv_path is False:
            # if isinstance(args, list):
            #     if args[0] == "python3":
            #         args[0] = "/usr/bin/python3"
            #     elif args[0] == "pip3":
            #         args[0] = "/usr/bin/pip3"
            #     elif args[0] == "python":
            #         args[0] = "/usr/bin/python"
            #     elif args[0] == "pip":
            #         args[0] = "/usr/bin/pip"
            # elif isinstance(args, str):
            #     if args.startswith("python3 "):
            #         args = args.replace("python3", "/usr/bin/python3", 1)
            #     elif args.startswith("python "):
            #         args = args.replace("python", "/usr/bin/python", 1)
            #     elif args.startswith("pip3 "):
            #         args = args.replace("pip3", "/usr/bin/pip3", 1)
            #     elif args.startswith("pip "):
            #         args = args.replace("pip", "/usr/bin/pip", 1)
            pass
        else:
            venv_parent = path.dirname(self.venv_path)
            activate_location = path.join(self.venv_path, "bin", "activate")

            cmd2 = ". {} && echo ~~MARKER~~ && set".format(activate_location)
            env = (subprocess.Popen(
                cmd2, shell=True, cwd=venv_parent,
                stdout=subprocess.PIPE).stdout.read().decode(
                    "utf-8").splitlines())
            marker = False
            new_envs = {}
            for e in env:
                if marker:
                    e = e.strip().split("=", 1)
                    if len(e) > 1:
                        name = str(e[0]).upper()
                        if name in ("IFS", "OPTIND"):
                            continue
                        else:
                            new_envs[name] = e[1].lstrip("'").rstrip("'")
                elif e.strip() == "~~MARKER~~":
                    marker = True
            environ.update(new_envs)
예제 #3
0
 def __exit__(self, exc_type, exc_val, exc_tb):
     if self.e is not None:
         self.e.__exit__(exc_type, exc_val, exc_tb)
     if self.old_path is not None:
         environ.putenv("PATH", self.old_path)
     if self.m is not None:
         self.m.__exit__(exc_type, exc_val, exc_tb)
     if self.p is not None:
         self.p.__exit__(exc_type, exc_val, exc_tb)
예제 #4
0
 def __exit__(self, exc_type, exc_val, exc_tb):
     for p in self.params_to_clean:
         old_val = self.old_vals.get(p, None)
         if old_val is not None:
             environ.putenv(p, old_val)
     for p in self.params_to_save:
         old_val = self.old_vals.get(p, None)
         if old_val is not None:
             environ.putenv(p, old_val)
예제 #5
0
    def config(self, config_path):
        """
        Load configurations from config files.

        :param config_path: Path to the config folder.
        """
        environ.putenv('TZ', 'UTC')

        # Set server to run on localhost and port 80
        self.server_config = {
            'server.socket_host': '0.0.0.0',
            'server.socket_port': 80,
            'request.show_tracebacks': False
        }

        # Set dispatcher to be a RESTful method dispatcher
        self.route_config = {
            '/': {
                'request.dispatch':
                cherrypy.dispatch.MethodDispatcher(),
                'tools.sessions.on':
                True,
                'tools.encode.encoding':
                'utf-8',
                'tools.encode.on':
                True,
                'tools.response_headers.on':
                True,
                'tools.response_headers.headers':
                [('Content-Type', 'text/plain;charset=utf-8')],
                'response.timeout':
                10
            }
        }

        # Set default responses
        responses = ['The sky is blue.', 'Water is wet.', 'Rocks are hard.']

        # Try read configuration to replace default responses.
        try:
            fp = open(path.join(config_path, 'responses.json'),
                      'r',
                      encoding='utf8')
            responses = loads(fp.read())
            fp.close()
            assert isinstance(responses, list)
        except (FileNotFoundError, JSONDecodeError, AssertionError):
            warn('No responses config loaded, using defaults.', Warning)
        self.responses = responses