Provides utility functions for filenames. FileInfo provides easy access to the components of a file's path relative to the project root.
| 1700 | |
| 1701 | |
| 1702 | class FileInfo: |
| 1703 | """Provides utility functions for filenames. |
| 1704 | |
| 1705 | FileInfo provides easy access to the components of a file's path |
| 1706 | relative to the project root. |
| 1707 | """ |
| 1708 | |
| 1709 | def __init__(self, filename): |
| 1710 | self._filename = filename |
| 1711 | |
| 1712 | def FullName(self): |
| 1713 | """Make Windows paths like Unix.""" |
| 1714 | return os.path.abspath(self._filename).replace("\\", "/") |
| 1715 | |
| 1716 | def RepositoryName(self): |
| 1717 | r"""FullName after removing the local path to the repository. |
| 1718 | |
| 1719 | If we have a real absolute path name here we can try to do something smart: |
| 1720 | detecting the root of the checkout and truncating /path/to/checkout from |
| 1721 | the name so that we get header guards that don't include things like |
| 1722 | "C:\\Documents and Settings\\..." or "/home/username/..." in them and thus |
| 1723 | people on different computers who have checked the source out to different |
| 1724 | locations won't see bogus errors. |
| 1725 | """ |
| 1726 | fullname = self.FullName() |
| 1727 | |
| 1728 | if os.path.exists(fullname): |
| 1729 | project_dir = os.path.dirname(fullname) |
| 1730 | |
| 1731 | # If the user specified a repository path, it exists, and the file is |
| 1732 | # contained in it, use the specified repository path |
| 1733 | if _repository: |
| 1734 | repo = FileInfo(_repository).FullName() |
| 1735 | root_dir = project_dir |
| 1736 | while os.path.exists(root_dir): |
| 1737 | # allow case insensitive compare on Windows |
| 1738 | if os.path.normcase(root_dir) == os.path.normcase(repo): |
| 1739 | return os.path.relpath(fullname, root_dir).replace("\\", "/") |
| 1740 | one_up_dir = os.path.dirname(root_dir) |
| 1741 | if one_up_dir == root_dir: |
| 1742 | break |
| 1743 | root_dir = one_up_dir |
| 1744 | |
| 1745 | if os.path.exists(os.path.join(project_dir, ".svn")): |
| 1746 | # If there's a .svn file in the current directory, we recursively look |
| 1747 | # up the directory tree for the top of the SVN checkout |
| 1748 | root_dir = project_dir |
| 1749 | one_up_dir = os.path.dirname(root_dir) |
| 1750 | while os.path.exists(os.path.join(one_up_dir, ".svn")): |
| 1751 | root_dir = os.path.dirname(root_dir) |
| 1752 | one_up_dir = os.path.dirname(one_up_dir) |
| 1753 | |
| 1754 | prefix = os.path.commonprefix([root_dir, project_dir]) |
| 1755 | return fullname[len(prefix) + 1 :] |
| 1756 | |
| 1757 | # Not SVN <= 1.6? Try to find a git, hg, or svn top level directory by |
| 1758 | # searching up from the current path. |
| 1759 | root_dir = current_dir = os.path.dirname(fullname) |
no outgoing calls
no test coverage detected