Ejemplo n.º 1
0
    def create_default_user_config(self):
        user_config_dir  = UserInfo.config_folder()
        user_config_file = os.path.join(user_config_dir, "config.json")

        default_config = json.loads(AppConfig.DEFAULT_CONFIG)
        default_config["maintainer-info"] = UserInfo.maintainer_info()

        for app in default_config.get("apps", {}).values():
            secret_key = base64.encodestring(os.urandom(32)).decode("utf-8")
            app\
                .setdefault("appconfig", {})\
                .setdefault("SECRET_KEY", secret_key)
        #end for

        try:
            os.makedirs(user_config_dir, exist_ok=True)
            with open(user_config_file, "w", encoding="utf-8") as f, \
                    self._lock_file(f) as f:
                os.fchmod(f.fileno(), 0o0600)
                json.dump(default_config, f, ensure_ascii=False, indent=4)
        except Exception as e:
            raise BoltError(
                "failed to store '{}': {}".format(user_config_file, str(e))
            )

        return default_config
Ejemplo n.º 2
0
    def refresh(self,
                releases=False,
                mirrors=False,
                overwrite_existing=False,
                **kwargs):
        items_to_fetch = []

        if releases:
            items_to_fetch.append("releases")
        if mirrors:
            items_to_fetch.append("mirrors")

        for item in items_to_fetch:
            os.makedirs(UserInfo.config_folder(), exist_ok=True)

            filename = "{}.json".format(item)
            src_url = '/'.join([self.base_url, filename])
            data = self._fetch_json(src_url)
            dest_file = os.path.join(UserInfo.config_folder(), filename)

            # Run the appropriate merge helper.
            getattr(self, "_merge_{}".format(item))(
                data, dest_file, overwrite_existing=overwrite_existing)
Ejemplo n.º 3
0
    def load_user_config(self):
        config = None

        user_config_file = os.path.join(UserInfo.config_folder(),
                                        "config.json")

        if os.path.exists(user_config_file):
            with open(user_config_file, "r", encoding="utf-8") as f, \
                    self._lock_file(f) as f:
                config = json.load(f)
        else:
            config = self.create_default_user_config()

        return config
Ejemplo n.º 4
0
    def _load_json_file(self, which):
        result = collections.OrderedDict()

        json_file = os.path.join(UserInfo.config_folder(),
                                 "{}.json".format(which))

        if not os.path.exists(json_file):
            self.refresh(**{which: True})

        try:
            with open(json_file, "r", encoding="utf-8") as f, \
                    self._lock_file(f) as f:
                result = json.load(f,
                                   object_pairs_hook=collections.OrderedDict)
            #end with
        except DistroInfoError:
            raise
        except Exception as e:
            raise DistroInfoError("error loading '{}': {}".format(
                json_file, str(e)))

        return result