Exemple #1
0
def VerifyLatestAFDOFile(afdo_release_spec, buildroot, gs_context):
  """Verify that the latest AFDO profile for a release is suitable.

  Find the latest AFDO profile file for a particular release and check
  that it is not too stale. The latest AFDO profile name for a release
  can be found in a file in GS under the name
  latest-chrome-<arch>-<release>.afdo.

  Args:
    afdo_release_spec: architecture and release to find the latest AFDO
        profile for.
    buildroot: buildroot where AFDO data should be stored.
    gs_context: GS context to retrieve data.

  Returns:
    The first return value is the name of the AFDO profile file found. None
    otherwise.
    The second return value indicates whether the profile found is expired or
    not. False when no profile is found.
  """
  latest_afdo_url = GSURL_LATEST_CHROME_AFDO % afdo_release_spec

  # Check if latest-chrome-<arch>-<release>.afdo exists.
  try:
    latest_detail = gs_context.List(latest_afdo_url, details=True)
  except gs.GSNoSuchKey:
    logging.info('Could not find latest AFDO info file %s', latest_afdo_url)
    return None, False

  # Then get the name of the latest valid AFDO profile file.
  local_dir = AFDO_BUILDROOT_LOCAL % {'build_root': buildroot}
  latest_afdo_file = LATEST_CHROME_AFDO_FILE % afdo_release_spec
  latest_afdo_path = os.path.join(local_dir, latest_afdo_file)
  gs_context.Copy(latest_afdo_url, latest_afdo_path)

  cand = osutils.ReadFile(latest_afdo_path).strip()
  cand_build = int(cand.split('.')[2])
  curr_build = int(afdo_release_spec['build'])

  # Verify the AFDO profile file is not too stale.
  mod_date = latest_detail[0].creation_time
  curr_date = datetime.datetime.now()
  allowed_stale_days = datetime.timedelta(days=AFDO_ALLOWED_STALE_DAYS)
  if ((curr_date - mod_date) > allowed_stale_days and
      (curr_build - cand_build) > AFDO_ALLOWED_STALE_BUILDS):
    logging.info('Found latest AFDO info file %s but it is too old',
                 latest_afdo_url)
    return cand, True

  return cand, False
Exemple #2
0
def PatchKernelEbuild(filename, version):
  """Update the AFDO_PROFILE_VERSION string in the given kernel ebuild file.

  Args:
    filename: name of the ebuild
    version: e.g., [61, 9752, 0, 0]
  """
  contents = []
  for line in osutils.ReadFile(filename).splitlines():
    if re.match(KERNEL_PROFILE_MATCH_PATTERN, line):
      contents.append(KERNEL_PROFILE_WRITE_PATTERN % tuple(version) + '\n')
    else:
      contents.append(line + '\n')
  osutils.WriteFile(filename, contents, atomic=True)
    def testReadWrite(self):
        """Sanity check that the read and write method work properly."""
        packages1 = os.path.join(self.tempdir, 'Packages1')
        packages2 = os.path.join(self.tempdir, 'Packages2')

        # Set some data.
        pkg_index = binpkg.PackageIndex()
        pkg_index.header['A'] = 'B'
        pkg_index.packages = [
            {
                'CPV': 'foo/bar',
                'KEY': 'value'
            },
            {
                'CPV': 'cat/pkg',
                'KEY': 'also_value'
            },
        ]

        # Write the two package index files using each writing method.
        pkg_index.modified = True
        pkg_index.WriteFile(packages1)
        with open(packages2, 'w') as f:
            pkg_index.Write(f)

        # Make sure the two files are the same.
        fc1 = osutils.ReadFile(packages1)
        fc2 = osutils.ReadFile(packages2)
        self.assertEqual(fc1, fc2)

        # Make sure it parses out the same data we wrote.
        with open(packages1) as f:
            read_index = binpkg.PackageIndex()
            read_index.Read(f)

        self.assertDictEqual(pkg_index.header, read_index.header)
        self.assertCountEqual(pkg_index.packages, read_index.packages)
    def testLogStdoutToFile(self):
        # Make mox happy.
        log = os.path.join(self.tempdir, 'output')
        ret = cros_build_lib.RunCommand(['python', '-c', 'print "monkeys"'],
                                        log_stdout_to_file=log)
        self.assertEqual(osutils.ReadFile(log), 'monkeys\n')
        self.assertTrue(ret.output is None)
        self.assertTrue(ret.error is None)

        # Validate dumb api usage.
        ret = cros_build_lib.RunCommand(
            ['python', '-c', 'import sys;print "monkeys2"'],
            log_stdout_to_file=log,
            redirect_stdout=True)
        self.assertTrue(ret.output is None)
        self.assertEqual(osutils.ReadFile(log), 'monkeys2\n')

        os.unlink(log)
        ret = cros_build_lib.RunCommand(
            ['python', '-c', 'import sys;print >> sys.stderr, "monkeys3"'],
            log_stdout_to_file=log,
            redirect_stderr=True)
        self.assertEqual(ret.error, 'monkeys3\n')
        self.assertTrue(os.path.exists(log))
        self.assertEqual(os.stat(log).st_size, 0)

        os.unlink(log)
        ret = cros_build_lib.RunCommand([
            'python', '-u', '-c',
            'import sys;print "monkeys4"\nprint >> sys.stderr, "monkeys5"\n'
        ],
                                        log_stdout_to_file=log,
                                        combine_stdout_stderr=True)
        self.assertTrue(ret.output is None)
        self.assertTrue(ret.error is None)

        self.assertEqual(osutils.ReadFile(log), 'monkeys4\nmonkeys5\n')
def main(argv):
  if len(argv) < 4:
    Usage()
    sys.exit(1)

  # Avoid parsing most of argv because most of it is destined for
  # DepGraphGenerator/emerge rather than us. Extract what we need
  # without disturbing the rest.
  config_path = argv.pop()
  config = json.loads(osutils.ReadFile(config_path))
  overlay_dir = argv.pop()
  board = [x.split('=')[1] for x in argv if x.find('--board=') != -1]
  if board:
    ebuild_cmd = ['ebuild-%s' % board[0]]
    equery_cmd = ['equery-%s' % board[0]]
  else:
    ebuild_cmd = ['ebuild']
    equery_cmd = ['equery']

  use_sudo = not board

  # We want the toolchain to be quiet to avoid interfering with our output.
  depgraph_argv = ['--quiet', '--pretend', '--emptytree']

  # Defaults to rdeps, but allow command-line override.
  default_rootdeps_arg = ['--root-deps=rdeps']
  for arg in argv:
    if arg.startswith('--root-deps'):
      default_rootdeps_arg = []

  # Now, assemble the overall argv as the concatenation of the
  # default list + possible rootdeps-default + actual command line.
  depgraph_argv.extend(default_rootdeps_arg)
  depgraph_argv.extend(argv)

  deps = parallel_emerge.DepGraphGenerator()
  deps.Initialize(depgraph_argv)
  deps_tree, deps_info = deps.GenDependencyTree()
  deps_map = deps.GenDependencyGraph(deps_tree, deps_info)

  reporter = PatchReporter(config, overlay_dir, ebuild_cmd, equery_cmd,
                           sudo=use_sudo)
  observed = reporter.ObservePatches(deps_map)
  diff_count = reporter.ReportDiffs(observed)

  print('Packages analyzed: %d' % reporter.package_count)
  print('Patches observed: %d' % len(observed))
  print('Patches expected: %d' % len(reporter.patches.keys()))
  sys.exit(diff_count)
def ListWorkonPackagesInfo(board, host):
    """Find the specified workon packages for the specified board.

  Args:
    board: The board to look at. If host is True, this should be set to None.
    host: Whether to look at workon packages for the host.

  Returns a list of unique packages being worked on.
  """
    # Import portage late so that this script can be imported outside the chroot.
    # pylint: disable=W0404
    import portage.const
    packages = ListWorkonPackages(board, host)
    if not packages:
        return []
    results = {}
    install_root = '/' if host else '/build/%s' % board
    vdb_path = os.path.join(install_root, portage.const.VDB_PATH)
    buildroot, both = constants.SOURCE_ROOT, constants.BOTH_OVERLAYS
    for overlay in portage_utilities.FindOverlays(both, board, buildroot):
        for filename, projects in portage_utilities.GetWorkonProjectMap(
                overlay, packages):
            # chromeos-base/power_manager/power_manager-9999
            # cp = chromeos-base/power_manager
            # cpv = chromeos-base/power_manager-9999
            category, pn, p = portage_utilities.SplitEbuildPath(filename)
            cp = '%s/%s' % (category, pn)
            cpv = '%s/%s' % (category, p)

            # Get the time the package finished building. TODO(build): Teach Portage
            # to store the time the package started building and use that here.
            pkg_mtime_file = os.path.join(vdb_path, cpv, 'BUILD_TIME')
            try:
                pkg_mtime = int(osutils.ReadFile(pkg_mtime_file))
            except EnvironmentError as ex:
                if ex.errno != errno.ENOENT:
                    raise
                pkg_mtime = 0

            # Get the modificaton time of the ebuild in the overlay.
            src_ebuild_mtime = os.lstat(os.path.join(overlay,
                                                     filename)).st_mtime

            # Write info into the results dictionary, overwriting any previous
            # values. This ensures that overlays override appropriately.
            results[cp] = WorkonPackageInfo(cp, pkg_mtime, projects,
                                            src_ebuild_mtime)

    return results.values()
    def testCreateManifestRepo(self):
        """Test we can create a local git repository with a local manifest."""
        CONTENTS = 'manifest contents'

        src_manifest = os.path.join(self.tempdir, 'src_manifest')
        git_repo = os.path.join(self.tempdir, 'git_repo')
        dst_manifest = os.path.join(git_repo, 'default.xml')

        osutils.WriteFile(src_manifest, CONTENTS)
        repository.PrepManifestForRepo(git_repo, src_manifest)

        self.assertEqual(CONTENTS, osutils.ReadFile(dst_manifest))

        # This should fail if we don't have a valid Git repo. Not a perfect test.
        git.GetGitRepoRevision(git_repo)
Exemple #8
0
    def testManyBoardsBrokenArgs(self):
        """Tests that malformed args.gn files will be fixed in --boards."""
        self.SetupCommandMock(many_boards=True)
        for board in SDKFetcherMock.BOARDS:
            gn_args_file = os.path.join(self.chrome_src_dir, 'out_%s' % board,
                                        'Release', 'args.gn')
            osutils.WriteFile(gn_args_file, 'foo\nbar', makedirs=True)

        self.cmd_mock.inst.Run()

        for board in SDKFetcherMock.BOARDS:
            gn_args_file = os.path.join(self.chrome_src_dir, 'out_%s' % board,
                                        'Release', 'args.gn')
            self.assertTrue(
                osutils.ReadFile(gn_args_file).startswith('import'))
    def testRunGeneratorCmd(self):
        """Test the specialized command to run programs in chroot."""
        mock_result = cros_build_lib.CommandResult(stdout=b'foo output')
        run_mock = self.PatchObject(cros_build_lib,
                                    'run',
                                    return_value=mock_result)

        expected_cmd = ['cmd', 'bar', 'jo nes']
        gen = self._GetStdGenerator(work_dir=self.tempdir)
        gen._RunGeneratorCmd(expected_cmd)

        run_mock.assert_called_once_with(expected_cmd,
                                         stdout=True,
                                         enter_chroot=True,
                                         stderr=subprocess.STDOUT)

        self.assertIn(
            mock_result.output,
            osutils.ReadFile(os.path.join(self.tempdir, 'delta.log'),
                             mode='rb'))

        # Now run with squawk_wrap=true.
        run_mock = self.PatchObject(cros_build_lib,
                                    'run',
                                    return_value=mock_result)
        gen._RunGeneratorCmd(expected_cmd, squawk_wrap=True)

        run_mock.assert_called_once_with(expected_cmd,
                                         stdout=True,
                                         enter_chroot=True,
                                         stderr=subprocess.STDOUT)

        self.assertIn(
            mock_result.output,
            osutils.ReadFile(os.path.join(self.tempdir, 'delta.log'),
                             mode='rb'))
    def testProfileGeneration(self):
        """Tests that we generate the portage profile correctly."""
        # pylint: disable=protected-access
        overlay_dir = os.path.join(self.tempdir, 'overlays')
        sysroot_dir = os.path.join(self.tempdir, 'sysroot')
        overlays = [
            os.path.join(overlay_dir, letter) for letter in ('a', 'b', 'c')
        ]
        for o in overlays:
            osutils.SafeMakedirs(o)
        sysroot = sysroot_lib.Sysroot(sysroot_dir)

        sysroot.WriteConfig(
            sysroot_lib._DictToKeyValue({
                'ARCH': 'arm',
                'BOARD_OVERLAY': '\n'.join(overlays)
            }))

        sysroot._GenerateProfile()

        profile_link = os.path.join(sysroot.path, 'etc', 'portage',
                                    'make.profile')
        profile_parent = osutils.ReadFile(os.path.join(profile_link,
                                                       'parent')).splitlines()
        self.assertTrue(os.path.islink(profile_link))
        self.assertEqual(1, len(profile_parent))
        self.assertTrue(re.match('chromiumos:.*arm.*', profile_parent[0]))

        profile_dir = os.path.join(overlays[1], 'profiles', 'base')
        osutils.SafeMakedirs(profile_dir)

        sysroot._GenerateProfile()
        profile_parent = osutils.ReadFile(os.path.join(profile_link,
                                                       'parent')).splitlines()
        self.assertEqual(2, len(profile_parent))
        self.assertEqual(profile_dir, profile_parent[1])
    def _ReadPortNumber(self):
        """Read port number from file."""
        if not self.is_alive():
            raise DevServerStartupError('Devserver terminated unexpectedly!')

        try:
            timeout_util.WaitForReturnTrue(os.path.exists,
                                           func_args=[self.port_file],
                                           timeout=self.DEV_SERVER_TIMEOUT,
                                           period=5)
        except timeout_util.TimeoutError:
            self.terminate()
            raise DevServerStartupError('Devserver portfile does not exist!')

        self.port = int(osutils.ReadFile(self.port_file).strip())
Exemple #12
0
def GetParsedDeps(deps_file):
    """Returns the full parsed DEPS file dictionary, and merged deps.

  Arguments:
    deps_file: Path to the .DEPS.git file.

  Returns:
    An (x,y) tuple.  x is a dictionary containing the contents of the DEPS file,
    and y is a dictionary containing the result of merging unix and common deps.
  """
    deps = _LoadDEPS(osutils.ReadFile(deps_file, 'rU'))
    merged_deps = deps.get('deps', {})
    unix_deps = deps.get('deps_os', {}).get('unix', {})
    merged_deps = _MergeDeps(merged_deps, unix_deps)
    return deps, merged_deps
  def testCheckout(self):
    """Tests checkout with mocked out git."""
    self.committer.Checkout(['OWNERS'])

    self._assertCommand('git init')
    self._assertCommand('git remote add origin '
                        'https://chromium.googlesource.com/chromium/src.git')
    self._assertCommand('git config core.sparsecheckout true')
    self._assertCommand('git fetch --depth=1')
    self._assertCommand('git pull origin master')
    self._assertCommand('git checkout -B auto-commit-branch origin/master')
    self.assertEqual(
        osutils.ReadFile(os.path.join(self.tempdir, '.git', 'info',
                                      'sparse-checkout')),
        'OWNERS\ncodereview.settings\nWATCHLISTS')
Exemple #14
0
  def _StaleGnArgs(self, gn_args, gn_args_file_path):
    """Returns True if args.gn needs to be updated."""
    if not os.path.exists(gn_args_file_path):
      logging.warning('No args.gn file: %s', gn_args_file_path)
      return True

    new_gn_args = self._StripGnArgs(gn_args)
    old_gn_args = self._StripGnArgs(
        gn_helpers.FromGNArgs(osutils.ReadFile(gn_args_file_path)))
    if new_gn_args == old_gn_args:
      return False

    logging.warning('Stale args.gn file: %s', gn_args_file_path)
    self._LogArgsDiff(old_gn_args, new_gn_args)
    return True
Exemple #15
0
    def _FindUprevCandidateMock(files, allow_blacklisted=False):
        """Mock for the FindUprevCandidateMock function.

    Simplified implementation of FindUprevCandidate: consider an ebuild worthy
    of uprev if |allow_blacklisted| is set or the ebuild is not blacklisted.
    """
        for f in files:
            if (f.endswith('.ebuild')
                    and (not 'CROS_WORKON_BLACKLIST=1' in osutils.ReadFile(f)
                         or allow_blacklisted)):
                pkgdir = os.path.dirname(f)
                return _Package(
                    os.path.join(os.path.basename(os.path.dirname(pkgdir)),
                                 os.path.basename(pkgdir)))
        return None
Exemple #16
0
    def _ReadMetadataSizeFile(self):
        """Discover the metadata size.

    The payload generator creates the file containing the metadata size. So we
    read it and make sure it is a proper value.
    """
        if not os.path.isfile(self.metadata_size_file):
            raise Error('Metadata size file %s does not exist' %
                        self.metadata_size_file)

        try:
            self.metadata_size = int(
                osutils.ReadFile(self.metadata_size_file).strip())
        except ValueError:
            raise Error('Invalid metadata size %s' % self.metadata_size)
Exemple #17
0
    def _UploadNinjaLog(self, compiler_proxy_path):
        """Uploads .ninja_log file and its related metadata.

    This uploads the .ninja_log file generated by ninja to build Chrome.
    Also, it appends some related metadata at the end of the file following
    '# end of ninja log' marker.

    Args:
      compiler_proxy_path: Path to the compiler proxy, which will be contained
        in the metadata.

    Returns:
      The name of the uploaded file.
    """
        ninja_log_path = os.path.join(self._goma_log_dir, 'ninja_log')
        if not os.path.exists(ninja_log_path):
            logging.warning('ninja_log is not found: %s', ninja_log_path)
            return None
        ninja_log_content = osutils.ReadFile(ninja_log_path)

        try:
            st = os.stat(ninja_log_path)
            ninja_log_mtime = datetime.datetime.fromtimestamp(st.st_mtime)
        except OSError:
            logging.exception('Failed to get timestamp: %s', ninja_log_path)
            return None

        ninja_log_info = self._BuildNinjaInfo(compiler_proxy_path)

        # Append metadata at the end of the log content.
        ninja_log_content += '# end of ninja log\n' + json.dumps(
            ninja_log_info)

        # Aligned with goma_utils in chromium bot.
        pid = os.getpid()

        upload_ninja_log_path = os.path.join(
            self._goma_log_dir, 'ninja_log.%s.%s.%s.%d' %
            (getpass.getuser(), cros_build_lib.GetHostName(),
             ninja_log_mtime.strftime('%Y%m%d-%H%M%S'), pid))
        osutils.WriteFile(upload_ninja_log_path, ninja_log_content)
        uploaded_filename = os.path.basename(upload_ninja_log_path) + '.gz'
        self._gs_context.CopyInto(upload_ninja_log_path,
                                  self._remote_dir,
                                  filename=uploaded_filename,
                                  auto_compress=True,
                                  headers=self._headers)
        return uploaded_filename
    def SetupUser(self):
        """Propogate the user name<->id mapping from outside the chroot.

    Some unittests use getpwnam($USER), as does bash.  If the account
    is not registered in the sysroot, they get back errors.
    """
        MAGIC_GECOS = 'Added by your friendly platform test helper; do not modify'
        # This is kept in sync with what sdk_lib/make_chroot.sh generates.
        SDK_GECOS = 'ChromeOS Developer'

        user, uid, gid, home = self.GetNonRootAccount()
        if user == 'nobody':
            return

        passwd_db = os.path.join(self.sysroot, 'etc', 'passwd')
        with self.LockDb(passwd_db):
            data = osutils.ReadFile(passwd_db)
            accts = data.splitlines()
            for acct in accts:
                passwd = acct.split(':')
                if passwd[0] == user:
                    # Did the sdk make this account?
                    if passwd[4] == SDK_GECOS:
                        # Don't modify it (see below) since we didn't create it.
                        return

                    # Did we make this account?
                    if passwd[4] != MAGIC_GECOS:
                        raise RuntimeError(
                            'your passwd db (%s) has unmanaged acct %s' %
                            (passwd_db, user))

                    # Maybe we should see if it needs to be updated?  Like if they
                    # changed UIDs?  But we don't really check that elsewhere ...
                    return

            acct = '%(name)s:x:%(uid)s:%(gid)s:%(gecos)s:%(homedir)s:%(shell)s' % {
                'name': user,
                'uid': uid,
                'gid': gid,
                'gecos': MAGIC_GECOS,
                'homedir': home,
                'shell': '/bin/bash',
            }
            with open(passwd_db, 'a') as f:
                if data[-1] != '\n':
                    f.write('\n')
                f.write('%s\n' % acct)
def replicate_private_config(_build_targets, refs, chroot):
    """Replicate a private cros_config change to the corresponding public config.

  See uprev_versioned_package for args
  """
    package = 'chromeos-base/chromeos-config-bsp'

    if len(refs) != 1:
        raise ValueError('Expected exactly one ref, actual %s' % refs)

    # Expect a replication_config.jsonpb in the package root.
    package_root = _get_private_overlay_package_root(refs[0], package)
    replication_config_path = os.path.join(package_root,
                                           'replication_config.jsonpb')

    try:
        replication_config = json_format.Parse(
            osutils.ReadFile(replication_config_path),
            replication_config_pb2.ReplicationConfig())
    except IOError:
        raise ValueError('Expected ReplicationConfig missing at %s' %
                         replication_config_path)

    replication_lib.Replicate(replication_config)

    modified_files = [
        rule.destination_path
        for rule in replication_config.file_replication_rules
    ]

    # The generated platform C files are not easily filtered by replication rules,
    # i.e. JSON / proto filtering can be described by a FieldMask, arbitrary C
    # files cannot. Therefore, replicate and filter the JSON payloads, and then
    # generate filtered C files from the JSON payload.
    modified_files.extend(
        _generate_platform_c_files(replication_config, chroot))

    # Use the private repo's commit hash as the new version.
    new_private_version = refs[0].revision

    # modified_files should contain only relative paths at this point, but the
    # returned UprevVersionedPackageResult must contain only absolute paths.
    for i, modified_file in enumerate(modified_files):
        assert not os.path.isabs(modified_file)
        modified_files[i] = os.path.join(constants.SOURCE_ROOT, modified_file)

    return UprevVersionedPackageResult().add_result(new_private_version,
                                                    modified_files)
    def GetFullVersion(self, version):
        """Add the release branch to a ChromeOS platform version.

    Arguments:
      version: A ChromeOS platform number of the form XXXX.XX.XX, i.e.,
        3918.0.0.

    Returns:
      The version with release branch prepended.  I.e., R28-3918.0.0.
    """
        def DebugGsLs(*args, **kwargs):
            kwargs.setdefault('retries', 0)
            kwargs.setdefault('debug_level', logging.DEBUG)
            kwargs.setdefault('error_code_ok', True)
            return self.gs_ctx.LS(*args, **kwargs)

        assert not version.startswith('R')

        with self.misc_cache.Lookup(('full-version', version)) as ref:
            if ref.Exists(lock=True):
                return osutils.ReadFile(ref.path).strip()
            else:
                # First, assume crbug.com/230190 has been fixed, and we can just use
                # the bare version.  The 'ls' we do here is a relatively cheap check
                # compared to the pattern matching we do below.
                # TODO(rcui): Rename this function to VerifyVersion()
                lines = DebugGsLs(os.path.join(self.gs_base, version) +
                                  '/').output.splitlines()
                full_version = version
                if not lines:
                    # TODO(rcui): Remove this code when crbug.com/230190 is fixed.
                    lines = DebugGsLs(
                        os.path.join(self.gs_base, 'R*-%s' % version) +
                        '/').output.splitlines()
                    if not lines:
                        raise SDKError('Invalid version %s' % version)
                    real_path = lines[0]
                    if not real_path.startswith(self.gs_base):
                        raise AssertionError('%s does not start with %s' %
                                             (real_path, self.gs_base))
                    real_path = real_path[len(self.gs_base) + 1:]
                    full_version = real_path.partition('/')[0]
                    if not full_version.endswith(version):
                        raise AssertionError('%s does not end with %s' %
                                             (full_version, version))

                ref.AssignText(full_version)
                return full_version
Exemple #21
0
def _AdjustSymbolOffset(breakpad_file, offset):
    """Given a breakpad file, adjust the symbols by offset.

  Updates the file in place.

  Args:
    breakpad_file: File to read and update in place.
    offset: Integer to move symbols by.
  """
    logging.info('Adjusting symbols in %s with offset %d.', breakpad_file,
                 offset)

    # Keep newlines.
    lines = osutils.ReadFile(breakpad_file).splitlines(True)
    adjusted_lines = [_AdjustLineSymbolOffset(line, offset) for line in lines]
    osutils.WriteFile(breakpad_file, ''.join(adjusted_lines))
    def testUploadSymbols(self):
        """Upload a few files."""
        self.createSymbolFile('slim.sym', self.SLIM_CONTENT)
        self.createSymbolFile(os.path.join('nested', 'inner.sym'))
        self.createSymbolFile('fat.sym', self.FAT_CONTENT)

        result = upload_symbols.UploadSymbols(
            [self.data],
            'fake_url',
            'product',
            failed_list=self.failure_file,
            strip_cfi=len(self.SLIM_CONTENT) + 1)

        self.assertEquals(result, 0)
        self.assertEqual(self.urlopen_mock.call_count, 3)
        self.assertEquals(osutils.ReadFile(self.failure_file), '')
Exemple #23
0
 def testGenerateHtmlIndexFile(self):
     """Verifies GenerateHtmlIndex gives us something sane (input: file)"""
     index = os.path.join(self.tempdir, 'index.html')
     files = (
         'a.tgz',
         'b b b.txt',
         'c',
         'dalsdkjfasdlkf',
     )
     filelist = os.path.join(self.tempdir, 'listing')
     osutils.WriteFile(filelist, '\n'.join(files))
     commands.GenerateHtmlIndex(index, filelist)
     html = osutils.ReadFile(index)
     for f in files:
         # TODO(build): Use assertIn w/python-2.7.
         self.assertTrue('>%s</a>' % f in html)
Exemple #24
0
 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)
Exemple #25
0
 def Cat(self, path, **kwargs):
     """Returns the contents of a GS object."""
     kwargs.setdefault('redirect_stdout', True)
     if not PathIsGs(path):
         # gsutil doesn't support cat-ting a local path, so read it ourselves.
         try:
             return osutils.ReadFile(path)
         except Exception as e:
             if getattr(e, 'errno', None) == errno.ENOENT:
                 raise GSNoSuchKey('%s: file does not exist' % path)
             else:
                 raise GSContextException(str(e))
     elif self.dry_run:
         return ''
     else:
         return self.DoCommand(['cat', path], **kwargs).output
Exemple #26
0
    def testStoreMetadataSignatures(self):
        """Test how we store metadata signatures."""
        gen = self._GetStdGenerator(payload=self.delta_payload)
        metadata_signatures = (b'1' * 256, )
        encoded_metadata_signature = (
            'MTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMT'
            'ExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTEx'
            'MTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMT'
            'ExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTEx'
            'MTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMT'
            'ExMTExMTExMQ==')

        gen._StoreMetadataSignatures(metadata_signatures)

        self.assertEqual(osutils.ReadFile(gen.metadata_signature_file),
                         encoded_metadata_signature)
Exemple #27
0
    def testVersion(self):
        """Test the version property logic."""
        # Testing default value.
        self.assertEqual(0, self.chroot.GetVersion())

        # Test setting the version.
        self.chroot.SetVersion(5)
        self.assertEqual(5, self.chroot.GetVersion())
        self.assertEqual('5', osutils.ReadFile(self.version_file))

        # The current behavior is that outside processes writing to the file
        # does not affect our view after we've already read it. This shouldn't
        # generally be a problem since run_chroot_version_hooks should be the only
        # process writing to it.
        osutils.WriteFile(self.version_file, '10')
        self.assertEqual(5, self.chroot.GetVersion())
Exemple #28
0
    def testMainImportNoFilter(self):
        output = os.path.join(self.tempdir, 'output.json')
        cros_config_test_schema.Start(
            os.path.join(this_dir, 'test_data/cros_config_test_device.yaml'),
            None, output, None)
        json_dict = json.loads(osutils.ReadFile(output))
        json_obj = libcros_schema.GetNamedTuple(json_dict)
        self.assertEqual(2, len(json_obj.chromeos.devices))

        device = json_obj.chromeos.devices[0]
        self.assertEqual('nautilus', device.device_name)
        self.assertEqual(3, len(device.command_groups))

        device = json_obj.chromeos.devices[1]
        self.assertEqual('nautiluslte', device.device_name)
        self.assertEqual(4, len(device.command_groups))
    def _GetMetadata(self, version):
        """Return metadata (in the form of a dict) for a given version."""
        raw_json = None
        full_version = self.GetFullVersion(version)
        version_base = os.path.join(self.gs_base, full_version)
        with self.misc_cache.Lookup(
            (self.board, version, constants.METADATA_JSON)) as ref:
            if ref.Exists(lock=True):
                raw_json = osutils.ReadFile(ref.path)
            else:
                raw_json = self.gs_ctx.Cat(os.path.join(
                    version_base, constants.METADATA_JSON),
                                           debug_level=logging.DEBUG).output
                ref.AssignText(raw_json)

        return json.loads(raw_json)
Exemple #30
0
  def testCommitNewLKGM(self):
    """Tests that we can commit a new LKGM file."""
    self.old_lkgm = '999.0.0'
    self.rc.AddCmdResult(partial_mock.In('remote'), returncode=0,
                         side_effect=self._createOldLkgm)
    self.committer.CheckoutChrome()

    self.assertEqual(self.committer.lkgm_file, self.lkgm_file)

    self.committer.UpdateLKGM()
    self.committer.CommitNewLKGM()

    # Check the file was actually written out correctly.
    self.assertEqual(osutils.ReadFile(self.lkgm_file), self.committer._lkgm)
    self.assertCommandContains(['git', 'commit'])
    self.assertEqual(self.committer._old_lkgm, self.old_lkgm)