Ejemplo n.º 1
0
    def __init__(
        self,
        path: Union[Path, str],
        jot_types: Type[JotType],
        config: Optional[CaskadeConfig] = None,
    ):
        self.casks = {}
        self.jot_types = jot_types
        self.data_locations = {}
        self.datalinks = defaultdict(dict)
        self.check_points = []
        self.dir = ensure_path(path).absolute()
        self.config = config
        if not self.dir.exists():
            self.dir.mkdir(mode=0o0700, parents=True)
            self.caskade_id = Rake.build_new(RootSchema.CASKADE)
            if config is None:
                self.config = CaskadeConfig(origin=self.caskade_id)
            else:
                config.origin = self.caskade_id
                self.config = config
            self._etc_dir().mkdir(mode=0o0700, parents=True)
            self.private_key = RSA2048().generate_private_key()
            self.public_key = self.private_key.public_key()
            pem = self._rsa_pem()
            pem.touch(0o600)
            pem.write_bytes(self.private_key.private_bytes())
            pem.chmod(0o600)
            dump_jsonable(self._config_file(), self.config)
            self.cask_ids = []
            self._set_active(
                CaskFile(self, CaskId(self.caskade_id, 0), CaskType.ACTIVE))
            self.active.create_file()
        else:
            assert self.dir.is_dir()
            self.private_key = RSA2048().load_private_key(
                self._rsa_pem().read_bytes())
            self.public_key = self.private_key.public_key()
            self.config = load_jsonable(self._config_file(), CaskadeConfig)
            self.caskade_id = self.config.origin

            for fpath in self.dir.iterdir():
                file = CaskFile.by_file(self, fpath)
                if file is not None and self.is_file_belong(file):
                    self.casks[file.cask_id] = file
            self.cask_ids = sorted(self.casks.keys(), reverse=True)
            assert len(self.cask_ids)
            self.casks[self.cask_ids[0]].read_file(
                check_point_collector=self.check_points)
            for k in self.cask_ids[1:]:
                self.casks[k].read_file(
                    check_point_collector=self.check_points)
        self.config.validate_config()
Ejemplo n.º 2
0
 def match(self, path):
     path = ensure_path(path)
     if self.root in path.parents:
         rel_path = path.relative_to(self.root)
         return rel_path.match(self.pattern)
     return False
Ejemplo n.º 3
0
 def __init__(self, cur_dir, pattern):
     self.root = ensure_path(cur_dir)
     self.pattern = pattern
Ejemplo n.º 4
0
 def match(self, path):
     path = ensure_path(path)
     return any(pm.match(path) for pm in self.all_matches)
Ejemplo n.º 5
0
 def __init__(self, dir: Union[Path, str]):
     self.dir = ensure_path(dir)
     self.caskade = BaseCaskade(self._hash_tree())
Ejemplo n.º 6
0
 def from_file(file: Union[str, Path]) -> "Cake":
     return Cake.from_stream(ensure_path(file).open("rb"))