|
| 1 | +""" |
| 2 | +Alternative take on the "automatically discovered repositories" concept |
| 3 | +that requires no threads, polling or inotify. Instead the filesystem is |
| 4 | +consulted whenever a repository name is looked up. |
| 5 | +
|
| 6 | +Since Path.exists() and Path.iterdir() are fairly quick filesystem |
| 7 | +operations, performance should be good for small to medium sites. |
| 8 | +FancyRepo() objects are cached. |
| 9 | +
|
| 10 | +Repositories are identified by the existence of a |
| 11 | +
|
| 12 | + <reponame>/git-daemon-export-ok |
| 13 | +
|
| 14 | +file (for compatibility with gitweb). You can customize this path using |
| 15 | +the export_ok_path parameter. Setting it to '.' will cause every |
| 16 | +subdirectory to be considered a git repository. |
| 17 | +
|
| 18 | +For large sites this approach may be hard on the filesystem when listing |
| 19 | +repositories, because the process of enumerating the git repositories |
| 20 | +causes the git-daemon-export-ok file to be checked in every repository. |
| 21 | +This can be mitigated by setting detect_removals to False. |
| 22 | +""" |
| 23 | + |
| 24 | +import collections.abc |
| 25 | +import functools |
| 26 | +import os |
| 27 | +import pathlib |
| 28 | + |
| 29 | +import klaus |
| 30 | +import klaus.repo |
| 31 | + |
| 32 | +_bad_names = frozenset([os.curdir, os.pardir]) |
| 33 | +_bad_chars = frozenset(['\0', os.sep, os.altsep]) |
| 34 | +_default_directory_suffixes = ['', '.git'] |
| 35 | + |
| 36 | + |
| 37 | +def coalesce(*args): |
| 38 | + """Return the first argument that is not None""" |
| 39 | + |
| 40 | + return next(arg for arg in args if arg is not None) |
| 41 | + |
| 42 | + |
| 43 | +class AutodetectingRepoDict(collections.abc.Mapping): |
| 44 | + """ |
| 45 | + Maintain a virtual read-only dictionary whose contents represent |
| 46 | + the presence of git repositories in the given root directory. |
| 47 | +
|
| 48 | + :param root: The path to a directory containing repositories, each |
| 49 | + a direct subdirectory of the root. |
| 50 | + :param namespace: A namespace that will be applied to all detected |
| 51 | + repositories. |
| 52 | + :param detect_removals: Detect if repositories have been removed. |
| 53 | + Defaults to True. Setting it to False can improve performance |
| 54 | + for repository listings in very large sites. |
| 55 | + :param export_ok_path: The filesystem path to check (relative to |
| 56 | + the candidate repository root) to see if it is a valid servable |
| 57 | + git repository. Defaults to 'git-daemon-export-ok'. Set to '.' |
| 58 | + if every directory is known to be a valid repository root. |
| 59 | + :param directory_suffixes: A list of suffixes that your git directories |
| 60 | + may have. The default is ['', '.git']. |
| 61 | + """ |
| 62 | + |
| 63 | + def __init__( |
| 64 | + self, |
| 65 | + root, |
| 66 | + namespace=None, |
| 67 | + detect_removals=None, |
| 68 | + export_ok_path=None, |
| 69 | + directory_suffixes=None, |
| 70 | + ): |
| 71 | + self._root = pathlib.Path(root) |
| 72 | + self._cache = {} |
| 73 | + self._namespace = namespace |
| 74 | + self._detect_removals = coalesce(detect_removals, True) |
| 75 | + self._export_ok_path = coalesce(export_ok_path, 'git-daemon-export-ok') |
| 76 | + # Use the keys of a dict in reverse order so that we can create a sort |
| 77 | + # of "poor man's splay tree": the suffixes are always tried in reverse |
| 78 | + # order. If a suffix was matched succesfully it is moved to the end by |
| 79 | + # removing and readding it so that it is tried as the first option for |
| 80 | + # the next repository. |
| 81 | + self._suffixes = dict.fromkeys( |
| 82 | + reversed(list(coalesce(directory_suffixes, _default_directory_suffixes))) |
| 83 | + ) |
| 84 | + |
| 85 | + def __getitem__(self, name): |
| 86 | + if ( |
| 87 | + not name |
| 88 | + or name.startswith('.') |
| 89 | + or name in _bad_names |
| 90 | + or not _bad_chars.isdisjoint(name) |
| 91 | + ): |
| 92 | + raise KeyError(name) |
| 93 | + |
| 94 | + if not self._detect_removals: |
| 95 | + # Try returning a cached version first, to avoid filesystem access |
| 96 | + try: |
| 97 | + return self._cache[name] |
| 98 | + except KeyError: |
| 99 | + pass |
| 100 | + |
| 101 | + for suffix in reversed(self._suffixes): |
| 102 | + # Bare git repositories may have a .git suffix on the directory name: |
| 103 | + path = self._root / (name + suffix) |
| 104 | + if (path / self._export_ok_path).exists(): |
| 105 | + # Reorder suffix test order on the assumption that most repos will |
| 106 | + # have the same suffix: |
| 107 | + del self._suffixes[suffix] |
| 108 | + self._suffixes[suffix] = None |
| 109 | + break |
| 110 | + else: |
| 111 | + self._cache.pop(name, None) |
| 112 | + raise KeyError(name) |
| 113 | + |
| 114 | + if self._detect_removals: |
| 115 | + try: |
| 116 | + return self._cache[name] |
| 117 | + except KeyError: |
| 118 | + pass |
| 119 | + |
| 120 | + repo = klaus.repo.FancyRepo(str(path), self._namespace) |
| 121 | + self._cache[name] = repo |
| 122 | + return repo |
| 123 | + |
| 124 | + def __iter__(self): |
| 125 | + def is_valid_repo(path): |
| 126 | + if not self._detect_removals and path.name in self._cache: |
| 127 | + return True |
| 128 | + return (path / self._export_ok_path).exists() |
| 129 | + |
| 130 | + suffixes = sorted(self._suffixes, key=len, reverse=True) |
| 131 | + |
| 132 | + def removesuffixes(string): |
| 133 | + for suffix in suffixes: |
| 134 | + attempt = string.removesuffix(suffix) |
| 135 | + if attempt != string: |
| 136 | + return attempt |
| 137 | + return string |
| 138 | + |
| 139 | + return ( |
| 140 | + removesuffixes(path.name) |
| 141 | + for path in self._root.iterdir() |
| 142 | + if is_valid_repo(path) |
| 143 | + ) |
| 144 | + |
| 145 | + def __len__(self): |
| 146 | + return sum(1 for _ in self) |
| 147 | + |
| 148 | + |
| 149 | +class AutodetectingRepoContainer(klaus.repo.BaseRepoContainer): |
| 150 | + """ |
| 151 | + RepoContainer based on AutodetectingRepoDict. |
| 152 | + See AutodetectingRepoDict for parameter descriptions. |
| 153 | + """ |
| 154 | + |
| 155 | + def __init__(self, repos_root, *args, **kwargs): |
| 156 | + super().__init__(repos_root) |
| 157 | + self.valid = AutodetectingRepoDict(repos_root, *args, **kwargs) |
| 158 | + |
| 159 | + |
| 160 | +def make_autodetecting_app( |
| 161 | + repos_root, |
| 162 | + *args, |
| 163 | + detect_removals=None, |
| 164 | + export_ok_path=None, |
| 165 | + directory_suffixes=None, |
| 166 | + **kwargs, |
| 167 | +): |
| 168 | + return klaus.make_app( |
| 169 | + repos_root, |
| 170 | + *args, |
| 171 | + repo_container_factory=functools.partial( |
| 172 | + AutodetectingRepoContainer, |
| 173 | + detect_removals=detect_removals, |
| 174 | + export_ok_path=export_ok_path, |
| 175 | + directory_suffixes=directory_suffixes, |
| 176 | + ), |
| 177 | + **kwargs, |
| 178 | + ) |
0 commit comments