Add cache command to sky_tools with subcommands for populating/clearing

This adds the following commands to sky_tools:
  sky_tools cache clear: Nukes all local artifacts in the cache
  sky_tools cache populate: Populates the cache with all known artifacts

This is useful both to fix busted caches and to make sure that the cache is
fully populated so that subsequent operations can proceed without needing
network access.
This commit is contained in:
James Robinson
2015-09-22 15:19:25 -07:00
parent 0758c5922f
commit f735604aca
3 changed files with 104 additions and 12 deletions

View File

@@ -7,6 +7,7 @@ import 'dart:io';
import 'package:args/args.dart';
import 'package:logging/logging.dart';
import 'package:sky_tools/src/build.dart';
import 'package:sky_tools/src/cache.dart';
import 'package:sky_tools/src/common.dart';
import 'package:sky_tools/src/init.dart';
import 'package:sky_tools/src/install.dart';
@@ -92,6 +93,7 @@ void main(List<String> args) {
for (CommandHandler handler in [
new BuildCommandHandler(),
new CacheCommandHandler(),
new InitCommandHandler(),
new InstallCommandHandler(),
new RunMojoCommandHandler(),

View File

@@ -43,33 +43,42 @@ class ArtifactStore {
}
Future<Directory> _cacheDir() async {
String cacheDirPath = path.join(packageRoot, 'sky_tools', 'cache', 'sky_engine', engineRevision);
Directory cacheDir = new Directory(cacheDirPath);
Directory cacheDir = new Directory(path.join(packageRoot, 'sky_tools', 'cache'));
if (!await cacheDir.exists()) {
await cacheDir.create(recursive: true);
}
return cacheDir;
}
Future<Directory> _engineSpecificCacheDir() async {
Directory cacheDir = await _cacheDir();
Directory engineSpecificDir = new Directory(path.join(cacheDir.path, 'sky_engine', engineRevision));
if (!await engineSpecificDir.exists()) {
await engineSpecificDir.create(recursive: true);
}
return engineSpecificDir;
}
// Whether the artifact needs to be marked as executable on disk.
bool _needsToBeExecutable(Artifact artifact) {
return artifact == Artifact.FlutterCompiler;
}
Future<String> getPath(Artifact artifact) async {
Directory cacheDir = await _cacheDir();
Directory cacheDir = await _engineSpecificCacheDir();
String category, name;
if (artifact == Artifact.FlutterCompiler) {
category = 'shell';
name = 'sky_snapshot';
} else if (artifact == Artifact.SkyViewerMojo) {
category = 'viewer';
name = 'sky_viewer.mojo';
} else {
// Unknown artifact.
return '';
switch (artifact) {
case Artifact.FlutterCompiler:
category = 'shell';
name = 'sky_snapshot';
break;
case Artifact.SkyViewerMojo:
category = 'viewer';
name = 'sky_viewer.mojo';
break;
}
File cachedFile = new File(path.join(cacheDir.path, name));
@@ -84,4 +93,17 @@ class ArtifactStore {
}
return cachedFile.path;
}
Future clear() async {
Directory cacheDir = await _cacheDir();
_logging.fine('Clearing cache directory ${cacheDir.path}');
await cacheDir.delete(recursive: true);
}
Future populate() async {
for (Artifact artifact in Artifact.values) {
_logging.fine('Populating cache with $artifact');
await getPath(artifact);
}
}
}

View File

@@ -0,0 +1,68 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
library sky_tools.cache;
import 'dart:async';
import 'package:args/args.dart';
import 'package:logging/logging.dart';
import 'artifacts.dart';
import 'common.dart';
final Logger _logging = new Logger('sky_tools.cache');
class CacheCommandHandler extends CommandHandler {
CacheCommandHandler() : super('cache', 'Manages sky_tools\' cache of binary artifacts.');
ArgParser get parser {
ArgParser parser = new ArgParser();
parser.addFlag('help', abbr: 'h', negatable: false);
parser.addOption('package-root', defaultsTo: 'packages');
ArgParser clearParser = parser.addCommand('clear');
clearParser.addFlag('help', abbr: 'h', negatable: false);
ArgParser populateParser = parser.addCommand('populate');
populateParser.addFlag('help', abbr: 'h', negatable: false);
return parser;
}
Future<int> _clear(String packageRoot, ArgResults results) async {
if (results['help']) {
print('Clears all artifacts from the cache.');
print(parser.usage);
return 0;
}
ArtifactStore artifacts = new ArtifactStore(packageRoot);
await artifacts.clear();
return 0;
}
Future<int> _populate(String packageRoot, ArgResults results) async {
if (results['help']) {
print('Populates the cache with all known artifacts.');
print(parser.usage);
return 0;
}
ArtifactStore artifacts = new ArtifactStore(packageRoot);
await artifacts.populate();
return 0;
}
@override
Future<int> processArgResults(ArgResults results) async {
if (results['help'] || results.command == null) {
print(parser.usage);
return 0;
}
if (results.command.name == 'clear') {
return _clear(results['package-root'], results.command);
} else if (results.command.name == 'populate') {
return _populate(results['package-root'], results.command);
} else {
_logging.severe('Unknown cache command \"${results.command.name}\"');
return 2;
}
}
}