Fred Drake | 72fbd82 | 2000-10-06 16:36:48 +0000 | [diff] [blame] | 1 | """Utility class and function to get information about the CVS repository |
| 2 | based on checked-out files. |
| 3 | """ |
| 4 | |
| 5 | import os |
| 6 | |
| 7 | |
| 8 | def get_repository_list(paths): |
| 9 | d = {} |
| 10 | for name in paths: |
| 11 | if os.path.isfile(name): |
| 12 | dir = os.path.dirname(name) |
| 13 | else: |
| 14 | dir = name |
| 15 | rootfile = os.path.join(name, "CVS", "Root") |
| 16 | root = open(rootfile).readline().strip() |
| 17 | if not d.has_key(root): |
| 18 | d[root] = RepositoryInfo(dir), [name] |
| 19 | else: |
| 20 | d[root][1].append(name) |
| 21 | return d.values() |
| 22 | |
| 23 | |
| 24 | class RepositoryInfo: |
| 25 | """Record holding information about the repository we want to talk to.""" |
| 26 | cvsroot_path = None |
| 27 | branch = None |
| 28 | |
| 29 | # type is '', ':ext', or ':pserver:' |
| 30 | type = "" |
| 31 | |
| 32 | def __init__(self, dir=None): |
| 33 | if dir is None: |
| 34 | dir = os.getcwd() |
| 35 | dir = os.path.join(dir, "CVS") |
| 36 | root = open(os.path.join(dir, "Root")).readline().strip() |
| 37 | if root.startswith(":pserver:"): |
| 38 | self.type = ":pserver:" |
| 39 | root = root[len(":pserver:"):] |
| 40 | elif ":" in root: |
| 41 | if root.startswith(":ext:"): |
| 42 | root = root[len(":ext:"):] |
| 43 | self.type = ":ext:" |
| 44 | self.repository = root |
| 45 | if ":" in root: |
| 46 | host, path = root.split(":", 1) |
| 47 | self.cvsroot_path = path |
| 48 | else: |
| 49 | self.cvsroot_path = root |
| 50 | fn = os.path.join(dir, "Tag") |
| 51 | if os.path.isfile(fn): |
| 52 | self.branch = open(fn).readline().strip()[1:] |
| 53 | |
| 54 | def get_cvsroot(self): |
| 55 | return self.type + self.repository |
| 56 | |
| 57 | _repository_dir_cache = {} |
| 58 | |
| 59 | def get_repository_file(self, path): |
| 60 | filename = os.path.abspath(path) |
| 61 | if os.path.isdir(path): |
| 62 | dir = path |
| 63 | join = 0 |
| 64 | else: |
| 65 | dir = os.path.dirname(path) |
| 66 | join = 1 |
| 67 | try: |
| 68 | repodir = self._repository_dir_cache[dir] |
| 69 | except KeyError: |
| 70 | repofn = os.path.join(dir, "CVS", "Repository") |
| 71 | repodir = open(repofn).readline().strip() |
| 72 | repodir = os.path.join(self.cvsroot_path, repodir) |
| 73 | self._repository_dir_cache[dir] = repodir |
| 74 | if join: |
| 75 | fn = os.path.join(repodir, os.path.basename(path)) |
| 76 | else: |
| 77 | fn = repodir |
| 78 | return fn[len(self.cvsroot_path)+1:] |
| 79 | |
| 80 | def __repr__(self): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 81 | return "<RepositoryInfo for %r>" % self.get_cvsroot() |