def fork(self, user, repo_name, fork_name): """ Fork a reposotory Make a copy of the repository for yourself. usage: $cmd <repo name> <fork name> """ repo = self.get_repo_object(config.ADMIN, repo_name) if not repo.has_permissions(user.username, "r"): raise InvalidPermissions("You need read permissions for forking") fork_path = os.path.join(self.path_to_repos, self.real_path(fork_name)) if os.path.exists(fork_path): raise InvalidRepository("Repository '%s' already exists." % repo_name) shutil.copytree(repo.repo_path, fork_path) repo = self.klass(fork_path, config.ADMIN) repo.reset_permissions_to(user.username) for username, permission in self.default_permissions: repo.set_permissions(username, permission) repo.save() subssh.writeln("\n\n Forked repository '%s' to '%s' \n" % (repo_name, fork_name)) self.info(user, fork_name)
def ls(self, user, action=""): """ List repositories. usage: $cmd [mine] """ repos = [] for repo_in_fs in os.listdir(self.path_to_repos): try: repo = self.klass(os.path.join(self.path_to_repos, repo_in_fs), config.ADMIN) except InvalidRepository: continue else: repos.append(repo) if action == "mine": repos = [repo for repo in repos if repo.is_owner(user.username)] elif not action: repos = [repo for repo in repos if repo.has_permissions(user.username, 'r')] elif action: raise subssh.InvalidArguments("Unknown action '%s'" % action) for repo in sorted(repos, lambda a, b: cmp(a.name, b.name)): subssh.writeln(repo.name)
def list_keys(username): db = AuthorizedKeysDB(disable_lock=True) try: subuser = db.subusers[username] except KeyError: subssh.writeln("%s has no keys" % username) else: for i, (key, (type, comment)) in enumerate(subuser.pubkeys.items()): subssh.writeln("%s. %s key: %s %s" % (i+1, comment, type , key)) db.close()
def add_key(username, args, comment=""): if not username_pattern.search(username): subssh.writeln("User name \"%s\" contains restricted characters" % username) return 1 try: key = get_key_from_input(" ".join(args)) except (urllib2.HTTPError, IOError, OSError), e: if len(e.args) > 1: subssh.errln(e.args[1]) else: subssh.errln(e.args[0]) return 1
def show_doc(user, command): """ Show man page of a command. usage: man <command> """ try: doc_tmpl = active.cmds[command].__doc__ except KeyError: subssh.errln("Unkown command '%s'" % command) return 1 if doc_tmpl: subssh.writeln(tools.expand_subssh_vars(doc_tmpl, cmd=command)) else: subssh.writeln("'%s' has no doc string" % command)
def init(self, user, repo_name): """ Create new repository. usage: $cmd <repository name> """ if not subssh.safe_chars_only_pat.match(repo_name): subssh.errln("Bad repository name. Allowed characters: %s (regexp)" % subssh.safe_chars) return 1 repo_path = self.real_path(repo_name) self.create_repository(repo_path, user.username) self.copy_common_hooks(user, repo_name) subssh.writeln("\n\n Created new repository '%s' \n" % repo_name) self.info(user, repo_name)
def info(self, user, repo_name): """ Show information about repository. Shows repository paths, owner and permissions. usage: $cmd <repo name> """ repo = self.get_repo_object(config.ADMIN, repo_name) subssh.writeln() subssh.writeln("Owners: %s" % format_list(repo.get_owners())) subssh.writeln() subssh.writeln("Permissions:") for username, perm in repo.get_all_permissions(): subssh.writeln("%s = %s" %(username, perm), indent=4 ) subssh.writeln() if self.is_web_enabled(repo): subssh.writeln("Anonymous web view is enabled") else: subssh.writeln("Anonymous web view is disabled") subssh.writeln() subssh.writeln("Access:") urls = self.viewable_urls(user.username, repo) try: subssh.writeln("Read/Write %s" % urls['rw'], indent=4) except KeyError: pass try: subssh.writeln("Anonymous read %s" % urls['anonymous_read'], indent=4) except KeyError: pass try: subssh.writeln("Web view %s" % urls['webview'], indent=4) except KeyError: pass subssh.writeln()