Example #1
0
def project(request, pytestconfig):

    project_dir = get_populus_option(cmdline_option="--populus-project",
                                     ini_option="populus_project",
                                     environ_var="PYTEST_POPULUS_PROJECT",
                                     pytestconfig=pytestconfig,
                                     default=pytestconfig.args[0])

    if not os.path.exists(get_json_config_file_path(project_dir)):
        raise FileNotFoundError(
            "No populus project found for testing in {project_dir}".format(
                project_dir=project_dir))

    contracts = request.config.cache.get(CACHE_KEY_CONTRACTS, None)
    mtime = request.config.cache.get(CACHE_KEY_MTIME, None)

    project = Project(project_dir, create_config_file=True)

    project.fill_contracts_cache(contracts, mtime)
    request.config.cache.set(
        CACHE_KEY_CONTRACTS,
        normalize_object_for_json(project.compiled_contract_data),
    )
    request.config.cache.set(
        CACHE_KEY_MTIME,
        get_latest_mtime(project.get_all_source_file_paths()),
    )

    return project
Example #2
0
    def __init__(self, project_dir=None, user_config_file_path=None):

        self._reset_configs_cache()
        if project_dir is None:
            self.project_dir = os.getcwd()
        else:
            self.project_dir = os.path.abspath(project_dir)

        # user config
        if user_config_file_path is not None:
            if not os.path.exists(user_config_file_path):
                raise FileNotFoundError(
                    "No populus configuration file found at specified location: "
                    "`{0}`".format(user_config_file_path))
            self.user_config_file_path = user_config_file_path
        else:
            self.user_config_file_path = get_user_default_config_path()

        # project config
        config_file_path = get_json_config_file_path(self.project_dir)

        if os.path.exists(config_file_path):
            self.config_file_path = config_file_path
        else:
            self.config_file_path = get_default_config_path()

        self.load_config()
Example #3
0
    def __init__(self,
                 project_dir=None,
                 user_config_file_path=None):

        self._reset_configs_cache()
        if project_dir is None:
            self.project_dir = os.getcwd()
        else:
            self.project_dir = os.path.abspath(project_dir)

        # user config
        if user_config_file_path is not None:
            if not os.path.exists(user_config_file_path):
                raise FileNotFoundError(
                    "No populus configuration file found at specified location: "
                    "`{0}`".format(user_config_file_path)
                )
            self.user_config_file_path = user_config_file_path
        else:
            self.user_config_file_path = get_user_default_config_path()

        # project config
        config_file_path = get_json_config_file_path(self.project_dir)

        if os.path.exists(config_file_path):
            self.config_file_path = config_file_path
        else:
            self.config_file_path = get_default_config_path()

        self.load_config()
Example #4
0
    def __init__(self,
                 project_dir=None,
                 user_config_file_path=None,
                 create_config_file=False):  # noqa: E501

        self._reset_configs_cache()
        if project_dir is None:
            self.project_dir = os.getcwd()
        else:
            self.project_dir = os.path.abspath(project_dir)

        # user config
        self.user_config_file_path = user_config_file_path
        if self.user_config_file_path is None:
            self.user_config_file_path = get_user_json_config_file_path()
            if not check_if_user_json_config_file_exists():
                user_config_path = get_user_json_config_file_path()
                user_defaults_path = get_user_default_config_path()
                ensure_path_exists(os.path.dirname(user_config_path))
                shutil.copyfile(user_defaults_path, user_config_path)

        # legacy config
        legacy_path = get_legacy_json_config_file_path(self.project_dir)
        if os.path.exists(legacy_path):
            self.legacy_config_path = legacy_path
            warnings.warn(
                "Found legacy config file at {legacy_path}".format(
                    legacy_path=legacy_path), PopulusResourceWarning)

        # project config
        self.config_file_path = get_json_config_file_path(self.project_dir)
        if not os.path.exists(self.config_file_path):
            if create_config_file:
                defaults_path = get_default_config_path()
                shutil.copyfile(defaults_path, self.config_file_path)
            else:
                if self.legacy_config_path is not None:
                    msg = "No project config file found at {project_dir}, but a legacy config exists. Try to upgrade"  # noqa: E501
                else:
                    msg = "No project config file found at {project_dir}"
                raise FileNotFoundError(
                    msg.format(project_dir=self.project_dir))

        self.load_config()
Example #5
0
def upgrade_configs(project_dir, logger, to_version):
    """upgrade project and the user config file"""

    user_config_file_path = get_user_json_config_file_path()
    if os.path.exists(user_config_file_path):
        user_config = load_config(user_config_file_path)
        current_user_config_version = int(user_config['version'])

        if current_user_config_version < int(LATEST_VERSION):
            upgraded_user_config = upgrade_user_config(user_config, to_version)
            if upgrade_config:
                write_config(upgraded_user_config, user_config_file_path)
            else:
                os.remove(user_config_file_path)

    legacy_config_file_path = get_legacy_json_config_file_path(project_dir)
    project_config_file_path = get_json_config_file_path(project_dir)

    if os.path.exists(legacy_config_file_path):
        legacy_config = load_config(legacy_config_file_path)
        legacy_version = legacy_config['version']

        if int(legacy_version) > int(LAST_NO_USER_CONFIG_VERSION):
            raise KeyError(
                "Unkown legacy version {legacy_version} at {legacy_config}".
                format(legacy_version=legacy_version,
                       legacy_config=legacy_config))
        elif int(legacy_version) < int(LAST_NO_USER_CONFIG_VERSION):
            upgraded_legacy_config = upgrade_config(
                legacy_config, ConfigContext.LEGACY,
                LAST_NO_USER_CONFIG_VERSION)
            write_config(upgraded_legacy_config, legacy_config_file_path)

    has_project_config = os.path.exists(project_config_file_path)
    has_legacy_config = os.path.exists(legacy_config_file_path)

    if has_project_config or has_legacy_config:
        if has_project_config:
            project_config = load_config(project_config_file_path)
        elif has_legacy_config:
            project_config = load_config(legacy_config_file_path)
        else:
            raise Exception("Invariant")
        project_config_version = int(project_config['version'])

        if project_config_version < int(LATEST_VERSION):
            upgraded_project_config = upgrade_config(
                project_config,
                ConfigContext.USER,
                to_version,
            )
            if upgraded_project_config:
                write_config(upgraded_project_config, project_config_file_path)
            elif has_project_config:
                os.remove(project_config_file_path)

    if os.path.exists(
            legacy_config_file_path) and to_version in KNOWN_USER_VERSIONS:
        os.rename(
            legacy_config_file_path,
            "{0}.backup".format(legacy_config_file_path),
        )
Example #6
0
def upgrade_configs(project_dir, logger, to_version):
    """upgrade project and the user config file"""

    user_config_file_path = get_user_json_config_file_path()
    if os.path.exists(user_config_file_path):
        user_config = load_config(user_config_file_path)
        current_user_config_version = int(user_config['version'])

        if current_user_config_version < int(LATEST_VERSION):
            upgraded_user_config = upgrade_user_config(user_config, to_version)
            if upgrade_config:
                write_config(upgraded_user_config, user_config_file_path)
            else:
                os.remove(user_config_file_path)

    legacy_config_file_path = get_legacy_json_config_file_path(project_dir)
    project_config_file_path = get_json_config_file_path(project_dir)

    if os.path.exists(legacy_config_file_path):
        legacy_config = load_config(legacy_config_file_path)
        legacy_version = legacy_config['version']

        if int(legacy_version) > int(LAST_NO_USER_CONFIG_VERSION):
            raise KeyError(
                "Unkown legacy version {legacy_version} at {legacy_config}".format(
                    legacy_version=legacy_version,
                    legacy_config=legacy_config
                )
            )
        elif int(legacy_version) < int(LAST_NO_USER_CONFIG_VERSION):
            upgraded_legacy_config = upgrade_config(
                legacy_config,
                ConfigContext.LEGACY,
                LAST_NO_USER_CONFIG_VERSION
            )
            write_config(upgraded_legacy_config, legacy_config_file_path)

    has_project_config = os.path.exists(project_config_file_path)
    has_legacy_config = os.path.exists(legacy_config_file_path)

    if has_project_config or has_legacy_config:
        if has_project_config:
            project_config = load_config(project_config_file_path)
        elif has_legacy_config:
            project_config = load_config(legacy_config_file_path)
        else:
            raise Exception("Invariant")
        project_config_version = int(project_config['version'])

        if project_config_version < int(LATEST_VERSION):
            upgraded_project_config = upgrade_config(
                project_config,
                ConfigContext.USER,
                to_version,
            )
            if upgraded_project_config:
                write_config(upgraded_project_config, project_config_file_path)
            elif has_project_config:
                os.remove(project_config_file_path)

    if os.path.exists(legacy_config_file_path) and to_version in KNOWN_USER_VERSIONS:
        os.rename(
            legacy_config_file_path,
            "{0}.backup".format(legacy_config_file_path),
        )