def testPatchKernelEbuild(self):
   before = [
       'The following line contains the version:',
       'AFDO_PROFILE_VERSION="R63-9901.21-1506581597"', 'It should be changed.'
   ]
   after = [
       'The following line contains the version:',
       'AFDO_PROFILE_VERSION="R12-3456.78-9876543210"', 'It should be changed.'
   ]
   tf = os.path.join(self.tempdir, 'test.ebuild')
   osutils.WriteFile(tf, '\n'.join(before))
   afdo.PatchKernelEbuild(tf, [12, 3456, 78, 9876543210])
   x = osutils.ReadFile(tf).splitlines()
   self.assertEqual(after, x)
    def PerformStage(self):
        version_info = self._run.GetVersionInfo()
        build_version = [
            int(x) for x in version_info.VersionString().split('.')
        ]
        chrome_version = int(version_info.chrome_branch)
        target_version = [chrome_version] + build_version
        profile_versions = afdo.GetAvailableKernelProfiles()
        candidates = sorted(afdo.FindKernelEbuilds())
        expire_soon = set()
        not_found = set()
        expired = set()
        for candidate, kver in candidates:
            profile_version = None
            if kver in afdo.KERNEL_SKIP_AFDO_UPDATE:
                continue
            if kver in profile_versions:
                profile_version = afdo.FindLatestProfile(
                    target_version, profile_versions[kver])
            if not profile_version:
                not_found.add(kver)
                continue
            if afdo.ProfileAge(
                    profile_version) > afdo.KERNEL_ALLOWED_STALE_DAYS:
                expired.add('%s: %s' % (kver, profile_version))
                continue
            if afdo.ProfileAge(profile_version) > afdo.KERNEL_WARN_STALE_DAYS:
                expire_soon.add('%s: %s' % (kver, profile_version))

            afdo.PatchKernelEbuild(candidate, profile_version)
            # If the *-9999.ebuild is not the last entry in its directory, Manifest
            # will contain an unused line for previous profile which is still fine.
            if candidate.endswith('-9999.ebuild'):
                afdo.UpdateManifest(path_util.ToChrootPath(candidate))
        afdo.CommitIfChanged(afdo.KERNEL_EBUILD_ROOT,
                             'Update profiles and manifests for Kernel.')

        if not_found or expired:
            raise afdo.NoValidProfileFound(
                'Cannot find AutoFDO profiles: %s or expired: %s' %
                (not_found, expired))

        if expire_soon:
            self._WarnSheriff(expire_soon)