@@ 1250-1352 (lines=103) @@ | ||
1247 | pass |
|
1248 | ||
1249 | ||
1250 | class FileInfo(object): |
|
1251 | """Provides utility functions for filenames. |
|
1252 | ||
1253 | FileInfo provides easy access to the components of a file's path |
|
1254 | relative to the project root. |
|
1255 | """ |
|
1256 | ||
1257 | def __init__(self, filename): |
|
1258 | self._filename = filename |
|
1259 | ||
1260 | def FullName(self): |
|
1261 | """Make Windows paths like Unix.""" |
|
1262 | return os.path.abspath(self._filename).replace('\\', '/') |
|
1263 | ||
1264 | def RepositoryName(self): |
|
1265 | r"""FullName after removing the local path to the repository. |
|
1266 | ||
1267 | If we have a real absolute path name here we can try to do something smart: |
|
1268 | detecting the root of the checkout and truncating /path/to/checkout from |
|
1269 | the name so that we get header guards that don't include things like |
|
1270 | "C:\Documents and Settings\..." or "/home/username/..." in them and thus |
|
1271 | people on different computers who have checked the source out to different |
|
1272 | locations won't see bogus errors. |
|
1273 | """ |
|
1274 | fullname = self.FullName() |
|
1275 | ||
1276 | if os.path.exists(fullname): |
|
1277 | project_dir = os.path.dirname(fullname) |
|
1278 | ||
1279 | # If the user specified a repository path, it exists, and the file is |
|
1280 | # contained in it, use the specified repository path |
|
1281 | if _repository: |
|
1282 | repo = FileInfo(_repository).FullName() |
|
1283 | root_dir = project_dir |
|
1284 | while os.path.exists(root_dir): |
|
1285 | # allow case insensitive compare on Windows |
|
1286 | if os.path.normcase(root_dir) == os.path.normcase(repo): |
|
1287 | return os.path.relpath(fullname, root_dir).replace('\\', '/') |
|
1288 | one_up_dir = os.path.dirname(root_dir) |
|
1289 | if one_up_dir == root_dir: |
|
1290 | break |
|
1291 | root_dir = one_up_dir |
|
1292 | ||
1293 | if os.path.exists(os.path.join(project_dir, ".svn")): |
|
1294 | # If there's a .svn file in the current directory, we recursively look |
|
1295 | # up the directory tree for the top of the SVN checkout |
|
1296 | root_dir = project_dir |
|
1297 | one_up_dir = os.path.dirname(root_dir) |
|
1298 | while os.path.exists(os.path.join(one_up_dir, ".svn")): |
|
1299 | root_dir = os.path.dirname(root_dir) |
|
1300 | one_up_dir = os.path.dirname(one_up_dir) |
|
1301 | ||
1302 | prefix = os.path.commonprefix([root_dir, project_dir]) |
|
1303 | return fullname[len(prefix) + 1:] |
|
1304 | ||
1305 | # Not SVN <= 1.6? Try to find a git, hg, or svn top level directory by |
|
1306 | # searching up from the current path. |
|
1307 | root_dir = current_dir = os.path.dirname(fullname) |
|
1308 | while current_dir != os.path.dirname(current_dir): |
|
1309 | if (os.path.exists(os.path.join(current_dir, ".git")) or |
|
1310 | os.path.exists(os.path.join(current_dir, ".hg")) or |
|
1311 | os.path.exists(os.path.join(current_dir, ".svn"))): |
|
1312 | root_dir = current_dir |
|
1313 | current_dir = os.path.dirname(current_dir) |
|
1314 | ||
1315 | if (os.path.exists(os.path.join(root_dir, ".git")) or |
|
1316 | os.path.exists(os.path.join(root_dir, ".hg")) or |
|
1317 | os.path.exists(os.path.join(root_dir, ".svn"))): |
|
1318 | prefix = os.path.commonprefix([root_dir, project_dir]) |
|
1319 | return fullname[len(prefix) + 1:] |
|
1320 | ||
1321 | # Don't know what to do; header guard warnings may be wrong... |
|
1322 | return fullname |
|
1323 | ||
1324 | def Split(self): |
|
1325 | """Splits the file into the directory, basename, and extension. |
|
1326 | ||
1327 | For 'chrome/browser/browser.cc', Split() would |
|
1328 | return ('chrome/browser', 'browser', '.cc') |
|
1329 | ||
1330 | Returns: |
|
1331 | A tuple of (directory, basename, extension). |
|
1332 | """ |
|
1333 | ||
1334 | googlename = self.RepositoryName() |
|
1335 | project, rest = os.path.split(googlename) |
|
1336 | return (project,) + os.path.splitext(rest) |
|
1337 | ||
1338 | def BaseName(self): |
|
1339 | """File base name - text after the final slash, before the final period.""" |
|
1340 | return self.Split()[1] |
|
1341 | ||
1342 | def Extension(self): |
|
1343 | """File extension - text following the final period, includes that period.""" |
|
1344 | return self.Split()[2] |
|
1345 | ||
1346 | def NoExtension(self): |
|
1347 | """File has no source file extension.""" |
|
1348 | return '/'.join(self.Split()[0:2]) |
|
1349 | ||
1350 | def IsSource(self): |
|
1351 | """File has a source file extension.""" |
|
1352 | return _IsSourceExtension(self.Extension()[1:]) |
|
1353 | ||
1354 | ||
1355 | def _ShouldPrintError(category, confidence, linenum): |
@@ 1250-1352 (lines=103) @@ | ||
1247 | pass |
|
1248 | ||
1249 | ||
1250 | class FileInfo(object): |
|
1251 | """Provides utility functions for filenames. |
|
1252 | ||
1253 | FileInfo provides easy access to the components of a file's path |
|
1254 | relative to the project root. |
|
1255 | """ |
|
1256 | ||
1257 | def __init__(self, filename): |
|
1258 | self._filename = filename |
|
1259 | ||
1260 | def FullName(self): |
|
1261 | """Make Windows paths like Unix.""" |
|
1262 | return os.path.abspath(self._filename).replace('\\', '/') |
|
1263 | ||
1264 | def RepositoryName(self): |
|
1265 | r"""FullName after removing the local path to the repository. |
|
1266 | ||
1267 | If we have a real absolute path name here we can try to do something smart: |
|
1268 | detecting the root of the checkout and truncating /path/to/checkout from |
|
1269 | the name so that we get header guards that don't include things like |
|
1270 | "C:\Documents and Settings\..." or "/home/username/..." in them and thus |
|
1271 | people on different computers who have checked the source out to different |
|
1272 | locations won't see bogus errors. |
|
1273 | """ |
|
1274 | fullname = self.FullName() |
|
1275 | ||
1276 | if os.path.exists(fullname): |
|
1277 | project_dir = os.path.dirname(fullname) |
|
1278 | ||
1279 | # If the user specified a repository path, it exists, and the file is |
|
1280 | # contained in it, use the specified repository path |
|
1281 | if _repository: |
|
1282 | repo = FileInfo(_repository).FullName() |
|
1283 | root_dir = project_dir |
|
1284 | while os.path.exists(root_dir): |
|
1285 | # allow case insensitive compare on Windows |
|
1286 | if os.path.normcase(root_dir) == os.path.normcase(repo): |
|
1287 | return os.path.relpath(fullname, root_dir).replace('\\', '/') |
|
1288 | one_up_dir = os.path.dirname(root_dir) |
|
1289 | if one_up_dir == root_dir: |
|
1290 | break |
|
1291 | root_dir = one_up_dir |
|
1292 | ||
1293 | if os.path.exists(os.path.join(project_dir, ".svn")): |
|
1294 | # If there's a .svn file in the current directory, we recursively look |
|
1295 | # up the directory tree for the top of the SVN checkout |
|
1296 | root_dir = project_dir |
|
1297 | one_up_dir = os.path.dirname(root_dir) |
|
1298 | while os.path.exists(os.path.join(one_up_dir, ".svn")): |
|
1299 | root_dir = os.path.dirname(root_dir) |
|
1300 | one_up_dir = os.path.dirname(one_up_dir) |
|
1301 | ||
1302 | prefix = os.path.commonprefix([root_dir, project_dir]) |
|
1303 | return fullname[len(prefix) + 1:] |
|
1304 | ||
1305 | # Not SVN <= 1.6? Try to find a git, hg, or svn top level directory by |
|
1306 | # searching up from the current path. |
|
1307 | root_dir = current_dir = os.path.dirname(fullname) |
|
1308 | while current_dir != os.path.dirname(current_dir): |
|
1309 | if (os.path.exists(os.path.join(current_dir, ".git")) or |
|
1310 | os.path.exists(os.path.join(current_dir, ".hg")) or |
|
1311 | os.path.exists(os.path.join(current_dir, ".svn"))): |
|
1312 | root_dir = current_dir |
|
1313 | current_dir = os.path.dirname(current_dir) |
|
1314 | ||
1315 | if (os.path.exists(os.path.join(root_dir, ".git")) or |
|
1316 | os.path.exists(os.path.join(root_dir, ".hg")) or |
|
1317 | os.path.exists(os.path.join(root_dir, ".svn"))): |
|
1318 | prefix = os.path.commonprefix([root_dir, project_dir]) |
|
1319 | return fullname[len(prefix) + 1:] |
|
1320 | ||
1321 | # Don't know what to do; header guard warnings may be wrong... |
|
1322 | return fullname |
|
1323 | ||
1324 | def Split(self): |
|
1325 | """Splits the file into the directory, basename, and extension. |
|
1326 | ||
1327 | For 'chrome/browser/browser.cc', Split() would |
|
1328 | return ('chrome/browser', 'browser', '.cc') |
|
1329 | ||
1330 | Returns: |
|
1331 | A tuple of (directory, basename, extension). |
|
1332 | """ |
|
1333 | ||
1334 | googlename = self.RepositoryName() |
|
1335 | project, rest = os.path.split(googlename) |
|
1336 | return (project,) + os.path.splitext(rest) |
|
1337 | ||
1338 | def BaseName(self): |
|
1339 | """File base name - text after the final slash, before the final period.""" |
|
1340 | return self.Split()[1] |
|
1341 | ||
1342 | def Extension(self): |
|
1343 | """File extension - text following the final period, includes that period.""" |
|
1344 | return self.Split()[2] |
|
1345 | ||
1346 | def NoExtension(self): |
|
1347 | """File has no source file extension.""" |
|
1348 | return '/'.join(self.Split()[0:2]) |
|
1349 | ||
1350 | def IsSource(self): |
|
1351 | """File has a source file extension.""" |
|
1352 | return _IsSourceExtension(self.Extension()[1:]) |
|
1353 | ||
1354 | ||
1355 | def _ShouldPrintError(category, confidence, linenum): |