def get_repo(self, name): """ Return the repository identified as `name`. `name` may be a bytes string or unicode string. """ username, name = _split_path(name) name = decodefilename(name) # Check if user has permissions to access this path if username != self.username and not self.is_admin: raise AccessDeniedError(name) # Get the userid associated to the username. user_obj = self userid = self._userid if username != self.username: row = self._db.findone('users', username=username) if not row: raise DoesNotExistError(name) userid = row['userid'] user_obj = UserObject(self._store, row) # Search the repo with and without leading "/" row = (self._db.findone('repos', userid=userid, repopath=name) or self._db.findone('repos', userid=userid, repopath="/" + name) or self._db.findone('repos', userid=userid, repopath=name + "/") or self._db.findone( 'repos', userid=userid, repopath="/" + name + "/")) if not row: raise DoesNotExistError(name) return RepoObject(user_obj, row)
def test_init(self): e = FileError('some/path') self.assertEqual('some/path', str(e)) e = DoesNotExistError('some/path') self.assertEqual('some/path', str(e)) e = AccessDeniedError('some/path') self.assertEqual('some/path', str(e)) e = UnknownError('some/path') self.assertEqual('some/path', str(e))
def get_repo(self, name): """ Return the repository identified as `name`. `name` may be a bytes string or unicode string. """ username, name = split_path(name) # Check if user has permissions to access this path if username != self._username and not self.is_admin: raise AccessDeniedError(name) name = normpath(name) for r in self._db.get_repos(username): if name == normpath(encodefilename(r)): user_obj = self if username == self._username else UserObject( self._userdb, self._db, username) return RepoObject(user_obj, r) raise DoesNotExistError(name)
def get_repo(self, repopath): """ Return a repo object. """ assert isinstance(repopath, bytes) or isinstance(repopath, str) if isinstance(repopath, bytes): repopath = decodefilename(repopath) repopath = repopath.strip('/') # Search the repo with and without leading "/" row = (self._db.findone('repos', userid=self.userid, repopath=repopath) or self._db.findone( 'repos', userid=self.userid, repopath="/" + repopath) or self._db.findone( 'repos', userid=self.userid, repopath=repopath + "/") or self._db.findone( 'repos', userid=self.userid, repopath="/" + repopath + "/")) if not row: raise DoesNotExistError(self.userid, repopath) return RepoObject(self, row)
def get_repo_path(self, path): """ Return a the repository identified by the given `path`. """ username, path = split_path(path) # Check if user has permissions to access this path if username != self._username and not self.is_admin: raise AccessDeniedError(path) path = normpath(path) for r in self._db.get_repos(username): repo = normpath(encodefilename(r)) if path.startswith(repo): user_obj = self if username == self._username else UserObject( self._userdb, self._db, username) repo_obj = RepoObject(user_obj, r) path_obj = repo_obj.get_path(path[len(repo):]) return (repo_obj, path_obj) raise DoesNotExistError(path)
def get_repo(self, name, as_user=None): """ Return the repository identified as `name`. `name` should be <username>/<repopath> """ username, repopath = _split_path(name) repopath = decodefilename(repopath) # Check permissions as_user = as_user or self.app.currentuser assert as_user, "as_user or current user must be defined" if username != as_user.username and not as_user.is_admin: raise AccessDeniedError(name) # Get the userid associated to the username. user_obj = self.get_user(username) if not user_obj: raise DoesNotExistError(name) # Get the repo object. return user_obj.get_repo(repopath)
def get_repo_path(self, path): """ Return a the repository identified by the given `path`. """ assert isinstance(path, bytes) or isinstance(path, str) sep = b'/' if isinstance(path, bytes) else '/' path = path.strip(sep) + sep # Since we don't know which part of the "path" is the repopath, # we need to do multiple search. try: startpos = 0 while True: pos = path.index(sep, startpos) try: repo_obj = self.get_repo(path[:pos]) path_obj = repo_obj.get_path(path[pos + 1:]) return repo_obj, path_obj except DoesNotExistError: # continue looping startpos = pos + 1 except ValueError: raise DoesNotExistError(path)