Provides utility functions for filenames. FileInfo provides easy access to the components of a file's path relative to the project root.
| 1100 | |
| 1101 | |
| 1102 | class FileInfo(object): |
| 1103 | """Provides utility functions for filenames. |
| 1104 | |
| 1105 | FileInfo provides easy access to the components of a file's path |
| 1106 | relative to the project root. |
| 1107 | """ |
| 1108 | |
| 1109 | def __init__(self, filename): |
| 1110 | self._filename = filename |
| 1111 | |
| 1112 | def FullName(self): |
| 1113 | """Make Windows paths like Unix.""" |
| 1114 | return os.path.abspath(self._filename).replace('\\', '/') |
| 1115 | |
| 1116 | def RepositoryName(self): |
| 1117 | """FullName after removing the local path to the repository. |
| 1118 | |
| 1119 | If we have a real absolute path name here we can try to do something smart: |
| 1120 | detecting the root of the checkout and truncating /path/to/checkout from |
| 1121 | the name so that we get header guards that don't include things like |
| 1122 | "C:\Documents and Settings\..." or "/home/username/..." in them and thus |
| 1123 | people on different computers who have checked the source out to different |
| 1124 | locations won't see bogus errors. |
| 1125 | """ |
| 1126 | fullname = self.FullName() |
| 1127 | |
| 1128 | if os.path.exists(fullname): |
| 1129 | project_dir = os.path.dirname(fullname) |
| 1130 | |
| 1131 | if os.path.exists(os.path.join(project_dir, ".svn")): |
| 1132 | # If there's a .svn file in the current directory, we recursively look |
| 1133 | # up the directory tree for the top of the SVN checkout |
| 1134 | root_dir = project_dir |
| 1135 | one_up_dir = os.path.dirname(root_dir) |
| 1136 | while os.path.exists(os.path.join(one_up_dir, ".svn")): |
| 1137 | root_dir = os.path.dirname(root_dir) |
| 1138 | one_up_dir = os.path.dirname(one_up_dir) |
| 1139 | |
| 1140 | prefix = os.path.commonprefix([root_dir, project_dir]) |
| 1141 | return fullname[len(prefix) + 1:] |
| 1142 | |
| 1143 | # Not SVN <= 1.6? Try to find a git, hg, or svn top level directory by |
| 1144 | # searching up from the current path. |
| 1145 | root_dir = current_dir = os.path.dirname(fullname) |
| 1146 | while current_dir != os.path.dirname(current_dir): |
| 1147 | if (os.path.exists(os.path.join(current_dir, ".git")) or |
| 1148 | os.path.exists(os.path.join(current_dir, ".hg")) or |
| 1149 | os.path.exists(os.path.join(current_dir, ".svn"))): |
| 1150 | root_dir = current_dir |
| 1151 | current_dir = os.path.dirname(current_dir) |
| 1152 | |
| 1153 | if (os.path.exists(os.path.join(root_dir, ".git")) or |
| 1154 | os.path.exists(os.path.join(root_dir, ".hg")) or |
| 1155 | os.path.exists(os.path.join(root_dir, ".svn"))): |
| 1156 | prefix = os.path.commonprefix([root_dir, project_dir]) |
| 1157 | return fullname[len(prefix) + 1:] |
| 1158 | |
| 1159 | # Don't know what to do; header guard warnings may be wrong... |
no outgoing calls
no test coverage detected