1
1
#!/usr/bin/env python
2
- # python_lint.py - Runs flake8 linting over the repository ------*- python -*-
3
- #
2
+
4
3
# This source file is part of the Swift.org open source project
5
4
#
6
- # Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5
+ # Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
7
6
# Licensed under Apache License v2.0 with Runtime Library Exception
8
7
#
9
8
# See https://swift.org/LICENSE.txt for license information
10
9
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
11
10
#
12
11
13
- from __future__ import print_function
12
+
13
+ """
14
+ Utility script used to run the flake8 linter over all the project Python
15
+ sources.
16
+ """
17
+
18
+
19
+ from __future__ import absolute_import , print_function , unicode_literals
14
20
15
21
import os
16
22
import subprocess
17
23
import sys
18
24
19
25
20
- def lint (arguments , verbose = True ):
21
- flake8_result = subprocess .call (
22
- [sys .executable , "-c" , "import flake8; import flake8_import_order" ]
23
- )
24
- if flake8_result != 0 :
25
- if verbose :
26
- print ("""
26
+ __all__ = [
27
+ 'lint' ,
28
+ ]
29
+
30
+
31
+ # -----------------------------------------------------------------------------
32
+ # Constants
33
+
34
+ _UTILS_DIR = os .path .abspath (os .path .dirname (__file__ ))
35
+ _PROJECT_DIR = os .path .dirname (_UTILS_DIR )
36
+
37
+ _REQUIRED_PACKAGES = [
38
+ 'flake8' ,
39
+ 'flake8-import-order' ,
40
+ ]
41
+
42
+ _INSTALL_FLAKE8_MESSAGE = """
27
43
The flake8 and flake8-import-order Python packages are required for linting,
28
44
but these were not found on your system.
29
45
@@ -32,25 +48,49 @@ def lint(arguments, verbose=True):
32
48
python -m pip install flake8
33
49
python -m pip install flake8-import-order
34
50
35
- For more help, see http://flake8.pycqa.org.""" )
51
+ For more help, see http://flake8.pycqa.org.
52
+ """
36
53
37
- # We should be returning `flake8_result` from here. However,
38
- # some Python files lint themselves using embedded doctests,
39
- # which causes CI smoke tests to fail because the Linux nodes
40
- # do not have these modules installed.
54
+
55
+ # -----------------------------------------------------------------------------
56
+ # Helpers
57
+
58
+ def _is_package_installed (name ):
59
+ """Runs the pip command to check if a package is installed.
60
+ """
61
+
62
+ command = [
63
+ sys .executable ,
64
+ '-m' , 'pip' ,
65
+ 'show' , '--quiet' ,
66
+ name ,
67
+ ]
68
+
69
+ with open (os .devnull , 'w' ) as devnull :
70
+ status = subprocess .call (command , stderr = devnull )
71
+
72
+ return not status
73
+
74
+
75
+ # -----------------------------------------------------------------------------
76
+
77
+ def lint (args , verbose = False ):
78
+ all_packages_installed = all ([
79
+ _is_package_installed (name )
80
+ for name in _REQUIRED_PACKAGES
81
+ ])
82
+
83
+ if not all_packages_installed :
84
+ if verbose :
85
+ print (_INSTALL_FLAKE8_MESSAGE )
41
86
42
87
return 0
43
88
44
- utils_directory = os .path .dirname (os .path .abspath (__file__ ))
45
- parent_directory = os .path .dirname (utils_directory )
46
- linting_result = subprocess .call (
47
- [sys .executable , "-m" , "flake8" ] + arguments ,
48
- cwd = parent_directory ,
49
- universal_newlines = True
50
- )
51
- return linting_result
89
+ return subprocess .call (
90
+ [sys .executable , '-m' , 'flake8' ] + args ,
91
+ cwd = _PROJECT_DIR ,
92
+ universal_newlines = True )
52
93
53
94
54
95
if __name__ == '__main__' :
55
- linting_result = lint (sys .argv [1 :])
56
- sys .exit (linting_result )
96
+ sys .exit (lint (sys .argv [1 :], verbose = True ))
0 commit comments