def testFilesAsStartPaths(self):
     join = self.input_api.os_path.join
     self.input_api.os_path.isfile = lambda _: True
     input_files = [
         'a',
         'a.cc',
         'a.txt',
         join('foo', 'a'),
         join('foo', 'a.cc'),
         join('foo', 'a.txt'),
         join('third_party', 'a'),
         join('third_party', 'a.cc'),
         join('third_party', 'a.txt'),
         join('foo', 'third_party', 'a'),
         join('foo', 'third_party', 'a.cc'),
         join('foo', 'third_party', 'a.txt'),
     ]
     root_dir = os.path.sep + 'src'
     actual = copyright_scanner.FindFiles(self.input_api, root_dir,
                                          input_files, [''])
     self.assertEqual(['a.cc', join('foo', 'a.cc')], actual)
     actual = copyright_scanner.FindFiles(self.input_api, root_dir,
                                          input_files, ['third_party'])
     self.assertEqual(['a.cc', join('foo', 'a.cc')], actual)
     actual = copyright_scanner.FindFiles(self.input_api, root_dir,
                                          input_files, ['foo'])
     self.assertEqual(['a.cc'], actual)
     actual = copyright_scanner.FindFiles(self.input_api, root_dir,
                                          input_files,
                                          ['foo', 'third_party'])
     self.assertEqual(['a.cc'], actual)
     actual = copyright_scanner.FindFiles(self.input_api, root_dir,
                                          input_files,
                                          [join('foo', 'third_party')])
     self.assertEqual(['a.cc', join('foo', 'a.cc')], actual)
    def testDirAsStartPath(self):
        self.input_api.os_path.isfile = lambda _: False
        join = self.input_api.os_path.join
        normpath = self.input_api.os_path.normpath
        root_dir = os.path.sep + 'src'
        scan_from = '.'
        base_path = join(root_dir, scan_from)

        def mock_os_walk(path):
            return lambda _: [(join(base_path, path), [''],
                               ['a', 'a.cc', 'a.txt'])]

        self.input_api.os_walk = mock_os_walk('')
        actual = map(
            normpath,
            copyright_scanner.FindFiles(self.input_api, root_dir, [scan_from],
                                        ['']))
        self.assertEqual(['a.cc'], actual)

        self.input_api.os_walk = mock_os_walk('third_party')
        actual = map(
            normpath,
            copyright_scanner.FindFiles(self.input_api, root_dir, [scan_from],
                                        ['']))
        self.assertEqual([], actual)

        self.input_api.os_walk = mock_os_walk('foo')
        actual = map(
            normpath,
            copyright_scanner.FindFiles(self.input_api, root_dir, [scan_from],
                                        ['']))
        self.assertEqual([join('foo', 'a.cc')], actual)

        self.input_api.os_walk = mock_os_walk('foo')
        actual = map(
            normpath,
            copyright_scanner.FindFiles(self.input_api, root_dir, [scan_from],
                                        ['foo']))
        self.assertEqual([], actual)

        self.input_api.os_walk = mock_os_walk(join('foo', 'bar'))
        actual = map(
            normpath,
            copyright_scanner.FindFiles(self.input_api, root_dir, [scan_from],
                                        ['foo']))
        self.assertEqual([], actual)

        self.input_api.os_walk = mock_os_walk(join('foo', 'third_party'))
        actual = map(
            normpath,
            copyright_scanner.FindFiles(self.input_api, root_dir, [scan_from],
                                        ['']))
        self.assertEqual([], actual)

        self.input_api.os_walk = mock_os_walk(join('foo', 'third_party'))
        actual = map(
            normpath,
            copyright_scanner.FindFiles(self.input_api, root_dir, [scan_from],
                                        [join('foo', 'third_party')]))
        self.assertEqual([], actual)
Пример #3
0
def _CheckLicenseHeaders(excluded_dirs_list, whitelisted_files):
    """Checks that all files which are not in a listed third-party directory,
  and which do not use the standard Chromium license, are whitelisted.
  Args:
    excluded_dirs_list: The list of directories to exclude from scanning.
    whitelisted_files: The whitelist of files.
  Returns:
    ScanResult.Ok if all files with non-standard license headers are whitelisted
    and the whitelist contains no stale entries;
    ScanResult.Warnings if there are stale entries;
    ScanResult.Errors if new non-whitelisted entries found.
  """
    input_api = InputApi()
    files_to_scan = copyright_scanner.FindFiles(input_api, REPOSITORY_ROOT,
                                                ['.'], excluded_dirs_list)
    sharded_files_to_scan = _ShardList(files_to_scan, 2000)
    pool = multiprocessing.Pool()
    offending_files_chunks = pool.map_async(_FindCopyrightViolations,
                                            sharded_files_to_scan).get(999999)
    pool.close()
    pool.join()
    # Flatten out the result
    offending_files = \
      [item for sublist in offending_files_chunks for item in sublist]

    (unknown, missing,
     stale) = copyright_scanner.AnalyzeScanResults(input_api,
                                                   whitelisted_files,
                                                   offending_files)

    if unknown:
        print 'The following files contain a third-party license but are not in ' \
              'a listed third-party directory and are not whitelisted. You must ' \
              'add the following files to the whitelist.\n%s' % \
              '\n'.join(sorted(unknown))
    if missing:
        print 'The following files are whitelisted, but do not exist.\n%s' % \
            '\n'.join(sorted(missing))
    if stale:
        print 'The following files are whitelisted unnecessarily. You must ' \
              'remove the following files from the whitelist.\n%s' % \
              '\n'.join(sorted(stale))

    if unknown:
        code = ScanResult.Errors
    elif stale or missing:
        code = ScanResult.Warnings
    else:
        code = ScanResult.Ok

    problem_paths = sorted(set(unknown + missing + stale))
    return (code, problem_paths)