Skip to content

Commit

Permalink
- change print to log and default log level to info
Browse files Browse the repository at this point in the history
- add verbose to print all log output (log level `finest`).
- use stack_trace.terse
  • Loading branch information
zoechigist committed Jun 8, 2015
1 parent 3c98396 commit 12e67df
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 42 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## 0.3.0+2
- change print to log and default log level to `info`
- add verbose to print all log output (log level `finest`).

## 0.3.0+1
- fix a bug with default version

Expand Down
46 changes: 35 additions & 11 deletions lib/src/cli.dart
Expand Up @@ -6,7 +6,24 @@ import 'package:unscripted/unscripted.dart';
import 'package:pub_semver/pub_semver.dart';
import 'package:bwu_dart_archive_downloader/dart_update.dart';

main([List<String> arguments]) =>
import 'package:logging/logging.dart' show Logger, Level;
import 'package:quiver_log/log.dart' show BASIC_LOG_FORMATTER, PrintAppender;
import 'package:stack_trace/stack_trace.dart' show Chain;

final _log = new Logger('bwu_dart_archive_downloader.src.cli');

void main(List<String> args) {
Logger.root.level = Level.INFO;
var appender = new PrintAppender(BASIC_LOG_FORMATTER);
appender.attachLogger(Logger.root);

Chain.capture(() => _main(args), onError: (error, stack) {
_log.shout(error);
_log.shout(stack.terse);
});
}

_main([List<String> arguments]) =>
new Script(DownloadCommandModel).execute(arguments);

class DDownScriptModel extends Object with DownloadCommandModel {
Expand Down Expand Up @@ -109,7 +126,14 @@ Extracts the ZIP archive top-level directory into the the "extractAs" directory.
Extracts the ZIP archive top-level directory into the the "extractAs" directory.''',
abbr: 't',
defaultsTo: '.') //
String extractTo}) async {
String extractTo, //
@Flag(help: '''
Verbose logging output.''', defaultsTo: false, negatable: true) //
bool verbose}) async {
if (verbose) {
Logger.root.level = Level.FINEST;
}

final downloader = new DartArchiveDownloader(outputDirectory == '.'
? io.Directory.current
: new io.Directory(outputDirectory));
Expand All @@ -119,7 +143,7 @@ Extracts the ZIP archive top-level directory into the the "extractAs" directory.
if (channels.isEmpty) {
throw 'Channel "${channel}" isn\'t supported or recognized.';
} else {
print('Using channel "${channels.first.value}".');
_log.fine('Using channel "${channels.first.value}".');
}

String versionDirectory;
Expand All @@ -141,15 +165,15 @@ Extracts the ZIP archive top-level directory into the the "extractAs" directory.
if (artifacts.isEmpty) {
throw 'Artifact "${artifact}" isn\'t supported or recognized.';
} else {
print('Using artifact "${artifacts.first.value}".');
_log.fine('Using artifact "${artifacts.first.value}".');
}

final Iterable<FileAddition> fileAdditions =
FileAddition.values.where((v) => v.value.startsWith(fileAddition));
if (fileAdditions.isEmpty) {
throw 'FileAddition "${fileAddition}" isn\'t supported or recognized.';
} else {
print('Using file addition "${fileAdditions.first.value}".');
_log.fine('Using file addition "${fileAdditions.first.value}".');
}

Iterable<Platform> platforms;
Expand All @@ -160,7 +184,7 @@ Extracts the ZIP archive top-level directory into the the "extractAs" directory.
if (platforms.isEmpty) {
throw 'Platform "${platform}" isn\'t supported or recognized.';
} else {
print('Using platform "${platforms.first.value}".');
_log.fine('Using platform "${platforms.first.value}".');
}
}

Expand All @@ -169,7 +193,7 @@ Extracts the ZIP archive top-level directory into the the "extractAs" directory.
case DownloadArtifact.apiDocs:
downloadFile = ApiDocsFile.dartApiDocsZip;
if (filename != null) {
print('Ignoring parameter filename ("${filename}").');
_log.fine('Ignoring parameter filename ("${filename}").');
}
break;
case DownloadArtifact.dartium:
Expand All @@ -178,7 +202,7 @@ Extracts the ZIP archive top-level directory into the the "extractAs" directory.
if (fileConstructors.isEmpty) {
throw 'File "${filename}" isn\'t supported or recognized.';
} else {
print('Using download file "${fileConstructors.first}".');
_log.fine('Using download file "${fileConstructors.first}".');
}
downloadFile = DartiumFile.values[fileConstructors.first](
platforms.first,
Expand All @@ -187,7 +211,7 @@ Extracts the ZIP archive top-level directory into the the "extractAs" directory.
case DownloadArtifact.dartiumAndroid:
downloadFile = DartiumAndroidFile.contentShell;
if (filename != null) {
print('Ignoring parameter filename ("${filename}").');
_log.fine('Ignoring parameter filename ("${filename}").');
}
break;
case DownloadArtifact.eclipseUpdate:
Expand All @@ -200,13 +224,13 @@ Extracts the ZIP archive top-level directory into the the "extractAs" directory.
downloadFile = new SdkFile.dartSdk(platforms.first,
debug: debugBuild, fileAddition: fileAdditions.first);
if (filename != null) {
print('Ignoring parameter filename ("${filename}").');
_log.fine('Ignoring parameter filename ("${filename}").');
}
break;
case DownloadArtifact.version:
downloadFile = VersionFile.version;
if (filename != null) {
print('Ignoring parameter filename ("${filename}").');
_log.fine('Ignoring parameter filename ("${filename}").');
}
break;
default:
Expand Down
22 changes: 13 additions & 9 deletions lib/src/dart_archive_downloader.dart
Expand Up @@ -3,14 +3,18 @@ library bwu_dart_archive_downloader.src.dart_archive_downloader;
import 'dart:async' show Future, Stream;
import 'dart:io' as io;
import 'dart:convert' show JSON;
import 'package:path/path.dart' as path;
import 'package:http/http.dart' as http;
import 'package:logging/logging.dart' show Logger;
import 'package:path/path.dart' as path;
import 'package:pub_semver/pub_semver.dart';

const baseUri = 'http://gsdview.appspot.com/dart-archive/channels/';
const apiAuthority = 'www.googleapis.com';
const apiPath = '/storage/v1/b/dart-archive/o';

final _log =
new Logger('bwu_dart_archive_downloader.src.dart_archive_downloader');

/// Contains the information gathered from the `VERSION` file in the download
/// directory.
class VersionInfo {
Expand Down Expand Up @@ -96,7 +100,7 @@ class DartArchiveDownloader {
_downloadFile = new io.File(
path.join(_downloadDirectory.path, uri.pathSegments.last));
}
print('download "${uri.toString()}" to "${_downloadFile.path}".');
_log.fine('download "${uri.toString()}" to "${_downloadFile.path}".');

final http.StreamedResponse response = await request.send();
if (response.statusCode != 200) {
Expand Down Expand Up @@ -154,7 +158,7 @@ class DartArchiveDownloader {
bool hasMorePages = true;
Map query = {'prefix': 'channels/${channel.value}', 'delimiter': '/'};
List<String> versions = <String>[];
print('Fetch versions from ${channel.value}.');
_log.fine('Fetch versions from ${channel.value}.');
int currentPage = 1;

while (hasMorePages) {
Expand All @@ -164,7 +168,7 @@ class DartArchiveDownloader {
query.remove('pageToken');
}
Uri uri = new Uri.https(apiAuthority, apiPath, query);
print(' page: ${currentPage++}');
_log.fine(' page: ${currentPage++}');
final response = JSON.decode((await http.get(uri)).body);
token = response['nextPageToken'];
if (token == null || token.isEmpty) {
Expand All @@ -175,7 +179,7 @@ class DartArchiveDownloader {
.map((e) =>
e.split('/').where((e) => e != null && e.isNotEmpty).last)
.where(_isNotWeirdOrInvalidVersion));
// print('${token}, ${response['prefixes'].first}');
// _log.fine('${token}, ${response['prefixes'].first}');
}

versions.sort((k, v) => _descendingVersionComparer(k, v) * -1);
Expand All @@ -194,7 +198,7 @@ class DartArchiveDownloader {
Future<String> findVersion(
DownloadChannel channel, Version semanticVersion) async {
final versions = await getVersions(channel);
print('${versions.length - 2} versions found');
_log.fine('${versions.length - 2} versions found');
return _findVersion(
channel, semanticVersion, versions, 1, versions.length - 1);
}
Expand All @@ -204,14 +208,14 @@ class DartArchiveDownloader {
if (start == end) {
final VersionInfo versionInfo =
await _getVersionInfo(channel, versions[start]);
print('check pos ${start}: ${versionInfo.semanticVersion}');
_log.fine('check pos ${start}: ${versionInfo.semanticVersion}');
return versions[start];
}
final mid = start + ((end - start) ~/ 2);
final VersionInfo versionInfo =
await _getVersionInfo(channel, versions[mid]);
// print('start: ${start} - end: ${end} - mid: ${mid} - version: ${versionInfo.semanticVersion}');
print('check pos ${mid}: ${versionInfo.semanticVersion}');
// _log.fine('start: ${start} - end: ${end} - mid: ${mid} - version: ${versionInfo.semanticVersion}');
_log.fine('check pos ${mid}: ${versionInfo.semanticVersion}');

if (semanticVersion == versionInfo.semanticVersion) {
return versions[mid];
Expand Down
25 changes: 14 additions & 11 deletions lib/src/dart_update.dart
@@ -1,12 +1,15 @@
library bwu_dart_archive_downloader.src.dart_update;

import 'dart:async' show Future, Stream;
import 'dart:io' as io;
import 'dart:convert' show JSON;
import 'package:path/path.dart' as path;
import 'dart:io' as io;
import 'package:archive/archive.dart';
import 'package:bwu_dart_archive_downloader/bwu_dart_archive_downloader.dart';
export 'package:bwu_dart_archive_downloader/bwu_dart_archive_downloader.dart';
import 'package:logging/logging.dart' show Logger;
import 'package:path/path.dart' as path;

final _log = new Logger('bwu_dart_archive_downloader.src.dart_update');

// TODO(zoechi) investigate this API
// https://www.googleapis.com/storage/v1/b/dart-archive/o?prefix=channels/stable/release/&delimiter=/
Expand Down Expand Up @@ -93,7 +96,7 @@ class DartUpdate {
files.where((f) => f != null).forEach((f) => print(f.path));

await Future.wait(pendingDownloads);
print('Downloads finished');
_log.info('Downloads finished');
//}

}
Expand Down Expand Up @@ -178,7 +181,7 @@ String getZipRootDirectory(io.File archive) {
/// directory of the Zip archive)
Future installArchive(io.File archive, io.Directory installDirectory,
{String replaceRootDirectoryName}) async {
print(
_log.info(
'Extract "${archive.absolute.path}" to "${installDirectory.absolute.path}".');
final io.Process process = await io.Process.start('unzip', [
archive.absolute.path,
Expand Down Expand Up @@ -207,23 +210,23 @@ Future installArchive(io.File archive, io.Directory installDirectory,
// } else {
// filename = (entity as ZipEntryImpl).name;
// }
// print(filename);
// _log.fine(filename);
//
// if (entity.isDirectory) {
// final dir =
// new io.Directory(path.join(installDirectory.path, filename))
// ..createSync(recursive: true);
// print('created: ${dir.path}');
// _log.info('created: ${dir.path}');
// } else {
// final newFile =
// new io.File(path.join(installDirectory.path, filename))
// ..createSync(recursive: true);
//
// await newFile.openWrite().addStream((entity as ZipEntry).content());
// print('created: ${newFile.path}');
// _log.info('created: ${newFile.path}');
// }
// } else {
// print(entity.runtimeType);
// _log.fine(entity.runtimeType);
// }
// }
//// final archive = loadArchiveContent(sdkArchive);
Expand All @@ -233,16 +236,16 @@ Future installArchive(io.File archive, io.Directory installDirectory,
//// final dir = new io.Directory(
//// path.join(_options.installDirectory.path, filename))
//// ..createSync(recursive: true);
//// print('created: ${dir.path}');
//// _log.info('created: ${dir.path}');
//// } else {
//// List<int> data = file.content;
//// final newFile =
//// new io.File(path.join(_options.installDirectory.path, filename))
//// ..createSync(recursive: true)
//// ..writeAsBytesSync(data);
//// print('create: ${newFile.path}');
//// _log.info('create: ${newFile.path}');
//// }
//// }
// print('Install "${installDirectory.path}" completed');
// _log.info('Install "${installDirectory.path}" completed');
// }
}
2 changes: 1 addition & 1 deletion pubspec.yaml
@@ -1,6 +1,6 @@
name: bwu_dart_archive_downloader
author: Günter Zöchbauer <guenter@gzoechbauer.com>
version: 0.3.0+1
version: 0.3.0+2
description: Typed API for the site where Dart artifacts like SDK, Dartium, ContentShell, ChromeDriver and API docs can be downloaded.
homepage: https://github.com/bwu-dart/bwu_dart_archive_downloader
environment:
Expand Down
25 changes: 15 additions & 10 deletions test/dart_sdk_download_test.dart
Expand Up @@ -2,9 +2,12 @@
library bwu_dart_archive_downloader.test.dart_sdk_download;

import 'dart:io' as io;
import 'package:test/test.dart';
import 'package:bwu_dart_archive_downloader/src/dart_update.dart';
import 'package:logging/logging.dart' show Logger;
import 'package:pub_semver/pub_semver.dart';
import 'package:test/test.dart';

final _log = new Logger(' bwu_dart_archive_downloader.test.dart_sdk_download');

main() {
group('download SDK', () {
Expand All @@ -28,9 +31,9 @@ main() {
..backupDirectory = new io.Directory('temp/backup'));

await updater.update();
print('done');
_log.fine('done');
},
skip: 'just for manual testing',
// skip: 'just for manual testing',
timeout: const Timeout(const Duration(seconds: 500)));

test('download version be/raw/131727', () async {
Expand All @@ -42,9 +45,9 @@ main() {
version: '131727');
final io.File file = await downloader.downloadFile(uri);
expect(file.existsSync(), isTrue);
print('done');
_log.fine('done');
},
skip: 'just for manual testing',
// skip: 'just for manual testing',
timeout: const Timeout(const Duration(seconds: 500)));

test('download version be/raw/ Version 1.2.0-edge.32698', () async {
Expand All @@ -58,9 +61,9 @@ main() {
Platform.getFromSystemPlatform(prefer64bit: true)), version: version);
final io.File file = await downloader.downloadFile(uri);
expect(file.existsSync(), isTrue);
print('done');
_log.fine('done');
},
skip: 'just for manual testing',
// skip: 'just for manual testing',
timeout: const Timeout(const Duration(seconds: 500)));

test('download version stable/release/ Version 1.2.0', () async {
Expand All @@ -74,9 +77,9 @@ main() {
Platform.getFromSystemPlatform(prefer64bit: true)), version: version);
final io.File file = await downloader.downloadFile(uri);
expect(file.existsSync(), isTrue);
print('done');
_log.fine('done');
},
skip: 'just for manual testing',
// skip: 'just for manual testing',
timeout: const Timeout(const Duration(seconds: 500)));
});

Expand All @@ -91,5 +94,7 @@ main() {
expect(versions, contains('131727'));
expect(versions, isNot(contains('raw')));
}, timeout: const Timeout(const Duration(seconds: 100)));
}, skip: true);
}
// ,skip: true
);
}

0 comments on commit 12e67df

Please sign in to comment.