Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Adds a python formatter
  • Loading branch information
zanderso committed Jun 3, 2022
commit 88276ccb3e0fab940e5674eb615b53fcd11e8e74
11 changes: 11 additions & 0 deletions .style.yapf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[style]
based_on_style = yapf
# Defined in https://github.com/google/yapf/blob/20d0c8f1774cf3843f4032f3e9ab02338bf98c75/yapf/yapflib/style.py#L326
# Docs and full list of knobs:
# https://github.com/google/yapf#knobs
split_before_first_argument = true
blank_line_before_module_docstring = true
# dedent_closing_brackets is required by coalesce_brackets
dedent_closing_brackets = true
coalesce_brackets = true
each_dict_entry_on_separate_line = false
3 changes: 3 additions & 0 deletions DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ deps = {
'src/third_party/boringssl':
Var('github_git') + '/dart-lang/boringssl_gen.git' + '@' + Var('dart_boringssl_gen_rev'),

'src/third_party/yapf':
Var('github_git') + '/google/yapf' + '@' + '212c5b5ad8e172d2d914ae454c121c89cccbcb35',

'src/third_party/boringssl/src':
'https://boringssl.googlesource.com/boringssl.git' + '@' + Var('dart_boringssl_rev'),

Expand Down
119 changes: 110 additions & 9 deletions ci/bin/format.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,24 @@ enum MessageType {

enum FormatCheck {
clang,
gn,
java,
python,
whitespace,
gn,
}

FormatCheck nameToFormatCheck(String name) {
switch (name) {
case 'clang':
return FormatCheck.clang;
case 'gn':
return FormatCheck.gn;
case 'java':
return FormatCheck.java;
case 'python':
return FormatCheck.python;
case 'whitespace':
return FormatCheck.whitespace;
case 'gn':
return FormatCheck.gn;
default:
throw FormattingException('Unknown FormatCheck type $name');
}
Expand All @@ -69,12 +72,14 @@ String formatCheckToName(FormatCheck check) {
switch (check) {
case FormatCheck.clang:
return 'C++/ObjC';
case FormatCheck.gn:
return 'GN';
case FormatCheck.java:
return 'Java';
case FormatCheck.python:
return 'Python';
case FormatCheck.whitespace:
return 'Trailing whitespace';
case FormatCheck.gn:
return 'GN';
}
}

Expand Down Expand Up @@ -140,6 +145,15 @@ abstract class FormatChecker {
allFiles: allFiles,
messageCallback: messageCallback,
);
case FormatCheck.gn:
return GnFormatChecker(
processManager: processManager,
baseGitRef: baseGitRef,
repoDir: repoDir,
srcDir: srcDir,
allFiles: allFiles,
messageCallback: messageCallback,
);
case FormatCheck.java:
return JavaFormatChecker(
processManager: processManager,
Expand All @@ -149,17 +163,17 @@ abstract class FormatChecker {
allFiles: allFiles,
messageCallback: messageCallback,
);
case FormatCheck.whitespace:
return WhitespaceFormatChecker(
case FormatCheck.python:
return PythonFormatChecker(
processManager: processManager,
baseGitRef: baseGitRef,
repoDir: repoDir,
srcDir: srcDir,
allFiles: allFiles,
messageCallback: messageCallback,
);
case FormatCheck.gn:
return GnFormatChecker(
case FormatCheck.whitespace:
return WhitespaceFormatChecker(
processManager: processManager,
baseGitRef: baseGitRef,
repoDir: repoDir,
Expand Down Expand Up @@ -665,6 +679,93 @@ class GnFormatChecker extends FormatChecker {
}
}

/// Checks the format of any .py files using the "yapf" command.
class PythonFormatChecker extends FormatChecker {
PythonFormatChecker({
ProcessManager processManager = const LocalProcessManager(),
required String baseGitRef,
required Directory repoDir,
required Directory srcDir,
bool allFiles = false,
MessageCallback? messageCallback,
}) : super(
processManager: processManager,
baseGitRef: baseGitRef,
repoDir: repoDir,
srcDir: srcDir,
allFiles: allFiles,
messageCallback: messageCallback,
) {
yapfBin = File(path.join(
repoDir.absolute.path,
'tools',
'yapf.sh',
));
_yapfStyle = File(path.join(
repoDir.absolute.path,
'.style.yapf',
));
}

late final File yapfBin;
late final File _yapfStyle;

@override
Future<bool> checkFormatting() async {
message('Checking Python formatting...');
return (await _runYapfCheck(fixing: false)) == 0;
}

@override
Future<bool> fixFormatting() async {
message('Fixing Python formatting...');
await _runYapfCheck(fixing: true);
// The yapf script shouldn't fail when fixing errors.
return true;
}

Future<int> _runYapfCheck({required bool fixing}) async {
final List<String> filesToCheck = await getFileList(<String>['*.py']);

final List<String> cmd = <String>[
yapfBin.path,
'--style', _yapfStyle.path,
if (!fixing) '--diff',
if (fixing) '--in-place',
];
final List<WorkerJob> jobs = <WorkerJob>[];
for (final String file in filesToCheck) {
jobs.add(WorkerJob(<String>[...cmd, file]));
}
final ProcessPool yapfPool = ProcessPool(
processRunner: _processRunner,
printReport: namedReport('python format'),
);
final List<WorkerJob> completedJobs = await yapfPool.runToCompletion(jobs);
reportDone();
final List<String> incorrect = <String>[];
for (final WorkerJob job in completedJobs) {
if (job.result.exitCode == 1) {
incorrect.add(' ${job.command.last}\n${job.result.output}');
}
}
if (incorrect.isNotEmpty) {
final bool plural = incorrect.length > 1;
if (fixing) {
message('Fixed ${incorrect.length} python file${plural ? 's' : ''}'
' which ${plural ? 'were' : 'was'} formatted incorrectly.');
} else {
error('Found ${incorrect.length} python file${plural ? 's' : ''}'
' which ${plural ? 'were' : 'was'} formatted incorrectly:');
incorrect.forEach(stderr.writeln);
}
} else {
message('All python files formatted correctly.');
}
return incorrect.length;
}
}

@immutable
class _GrepResult {
const _GrepResult(this.file, [this.hits = const <String>[], this.lineNumbers = const <int>[]]);
Expand Down
1 change: 1 addition & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ LIBRARY: txt
ORIGIN: ../../../flutter/LICENSE
TYPE: LicenseType.bsd
FILE: ../../../flutter/.clang-tidy
FILE: ../../../flutter/.style.yapf
FILE: ../../../flutter/DEPS
FILE: ../../../flutter/assets/asset_manager.cc
FILE: ../../../flutter/assets/asset_manager.h
Expand Down
2 changes: 1 addition & 1 deletion ci/licenses_golden/tool_signature
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Signature: 7b0c7d76d9cfc331776ed8d447e52f67
Signature: 4d80570261409484c1813b3fe5282783

1 change: 1 addition & 0 deletions tools/licenses/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1844,6 +1844,7 @@ class _RepositoryRootThirdPartyDirectory extends _RepositoryGenericThirdPartyDir
&& entry.name != 'mockito' // only used by tests
&& entry.name != 'pymock' // presumably only used by tests
&& entry.name != 'pyyaml' // build-time dependency only
&& entry.name != 'yapf' // only used for code formatting
&& entry.name != 'android_embedding_dependencies' // testing framework for android
&& entry.name != 'yasm' // build-time dependency only
&& entry.name != 'binutils' // build-time dependency only
Expand Down
32 changes: 32 additions & 0 deletions tools/yapf.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash
# Copyright 2013 The Flutter Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Generates objc docs for Flutter iOS libraries.

set -e

# On Mac OS, readlink -f doesn't work, so follow_links traverses the path one
# link at a time, and then cds into the link destination and find out where it
# ends up.
#
# The function is enclosed in a subshell to avoid changing the working directory
# of the caller.
function follow_links() (
cd -P "$(dirname -- "$1")"
file="$PWD/$(basename -- "$1")"
while [[ -h "$file" ]]; do
cd -P "$(dirname -- "$file")"
file="$(readlink -- "$file")"
cd -P "$(dirname -- "$file")"
file="$PWD/$(basename -- "$file")"
done
echo "$file"
)

SCRIPT_DIR=$(follow_links "$(dirname -- "${BASH_SOURCE[0]}")")
SRC_DIR="$(cd "$SCRIPT_DIR/../.."; pwd -P)"
YAPF_DIR="$(cd "$SRC_DIR/third_party/yapf"; pwd -P)"

PYTHONPATH="$YAPF_DIR" python3 "$YAPF_DIR/yapf" "$@"