Skip to content

Commit 9569691

Browse files
committed
Refactor code in tidy.py
- Replace wildcard import with explicit import of `check_license` - Move more logic outside of the `try` block. - Group all helper functions together. - Define `interesting_exts` and `uninteresting_files` at start of file (with the rest of the constant declarations).
1 parent f1eebb8 commit 9569691

File tree

1 file changed

+37
-36
lines changed

1 file changed

+37
-36
lines changed

src/etc/tidy.py

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import subprocess
1414
import re
1515
import os
16-
from licenseck import *
16+
from licenseck import check_license
1717
import snapshot
1818

1919
err = 0
@@ -22,13 +22,8 @@
2222
tab_flag = "ignore-tidy-tab"
2323
linelength_flag = "ignore-tidy-linelength"
2424

25-
# Be careful to support Python 2.4, 2.6, and 3.x here!
26-
config_proc = subprocess.Popen(["git", "config", "core.autocrlf"],
27-
stdout=subprocess.PIPE)
28-
result = config_proc.communicate()[0]
29-
30-
true = "true".encode('utf8')
31-
autocrlf = result.strip() == true if result is not None else False
25+
interesting_files = ['.rs', '.py', '.js', '.sh', '.c', '.h']
26+
uninteresting_files = ['miniz.c', 'jquery', 'rust_android_dummy']
3227

3328

3429
def report_error_name_no(name, no, s):
@@ -51,6 +46,34 @@ def do_license_check(name, contents):
5146
if not check_license(name, contents):
5247
report_error_name_no(name, 1, "incorrect license")
5348

49+
50+
def update_counts(current_name):
51+
global file_counts
52+
global count_other_linted_files
53+
54+
_, ext = os.path.splitext(current_name)
55+
56+
if ext in interesting_files:
57+
file_counts[ext] += 1
58+
else:
59+
count_other_linted_files += 1
60+
61+
62+
def interesting_file(f):
63+
if any(x in f for x in uninteresting_files):
64+
return False
65+
66+
return any(os.path.splitext(f)[1] == ext for ext in interesting_files)
67+
68+
69+
# Be careful to support Python 2.4, 2.6, and 3.x here!
70+
config_proc = subprocess.Popen(["git", "config", "core.autocrlf"],
71+
stdout=subprocess.PIPE)
72+
result = config_proc.communicate()[0]
73+
74+
true = "true".encode('utf8')
75+
autocrlf = result.strip() == true if result is not None else False
76+
5477
current_name = ""
5578
current_contents = ""
5679
check_tab = True
@@ -63,30 +86,16 @@ def do_license_check(name, contents):
6386

6487
src_dir = sys.argv[1]
6588

66-
try:
67-
count_lines = 0
68-
count_non_blank_lines = 0
69-
count_other_linted_files = 0
70-
71-
interesting_files = ['.rs', '.py', '.js', '.sh', '.c', '.h']
72-
73-
file_counts = {ext: 0 for ext in interesting_files}
74-
75-
def update_counts(current_name):
76-
global file_counts
77-
global count_other_linted_files
78-
79-
_, ext = os.path.splitext(current_name)
89+
count_lines = 0
90+
count_non_blank_lines = 0
91+
count_other_linted_files = 0
8092

81-
if ext in interesting_files:
82-
file_counts[ext] += 1
83-
else:
84-
count_other_linted_files += 1
93+
file_counts = {ext: 0 for ext in interesting_files}
8594

86-
all_paths = set()
95+
all_paths = set()
8796

97+
try:
8898
for (dirpath, dirnames, filenames) in os.walk(src_dir):
89-
9099
# Skip some third-party directories
91100
skippable_dirs = {
92101
'src/jemalloc',
@@ -105,14 +114,6 @@ def update_counts(current_name):
105114
if any(d in dirpath for d in skippable_dirs):
106115
continue
107116

108-
def interesting_file(f):
109-
if "miniz.c" in f \
110-
or "jquery" in f \
111-
or "rust_android_dummy" in f:
112-
return False
113-
114-
return any(os.path.splitext(f)[1] == ext for ext in interesting_files)
115-
116117
file_names = [os.path.join(dirpath, f) for f in filenames
117118
if interesting_file(f)
118119
and not f.endswith("_gen.rs")

0 commit comments

Comments
 (0)