History

checkkconfigsymbols.py: port to Python 3

Python 2 is slowly dying, so port the script to Python 3.

Valentin Rothberg committed 10 years ago

Commit 7c5227a1 parent f175ba11 file · +17 −16Browse files

Showing the changes to scripts (1 of 1 file). Show all

Unified · Split

scripts/checkkconfigsymbols.py+17−16
@@ -1,4 +1,4 @@
1#!/usr/bin/env python21#!/usr/bin/env python3
2 2
3"""Find Kconfig symbols that are referenced but not defined."""3"""Find Kconfig symbols that are referenced but not defined."""
4 4
@@ -128,9 +128,9 @@ def main(): · 123 unmodified lines
128 if opts.sim and not opts.commit and not opts.diff:128 if opts.sim and not opts.commit and not opts.diff:
129 sims = find_sims(opts.sim, opts.ignore)129 sims = find_sims(opts.sim, opts.ignore)
130 if sims:130 if sims:
131 print "%s: %s" % (yel("Similar symbols"), ', '.join(sims))131 print("%s: %s" % (yel("Similar symbols"), ', '.join(sims)))
132 else:132 else:
133 print "%s: no similar symbols found" % yel("Similar symbols")133 print("%s: no similar symbols found" % yel("Similar symbols"))
134 sys.exit(0)134 sys.exit(0)
135 135
136 # dictionary of (un)defined symbols136 # dictionary of (un)defined symbols
@@ -183,28 +183,28 @@ def main(): · 46 unmodified lines
183 183
184 # now print the output184 # now print the output
185 for feature in sorted(undefined):185 for feature in sorted(undefined):
186 print red(feature)186 print(red(feature))
187 187
188 files = sorted(undefined.get(feature))188 files = sorted(undefined.get(feature))
189 print "%s: %s" % (yel("Referencing files"), ", ".join(files))189 print("%s: %s" % (yel("Referencing files"), ", ".join(files)))
190 190
191 sims = find_sims(feature, opts.ignore, defined)191 sims = find_sims(feature, opts.ignore, defined)
192 sims_out = yel("Similar symbols")192 sims_out = yel("Similar symbols")
193 if sims:193 if sims:
194 print "%s: %s" % (sims_out, ', '.join(sims))194 print("%s: %s" % (sims_out, ', '.join(sims)))
195 else:195 else:
196 print "%s: %s" % (sims_out, "no similar symbols found")196 print("%s: %s" % (sims_out, "no similar symbols found"))
197 197
198 if opts.find:198 if opts.find:
199 print "%s:" % yel("Commits changing symbol")199 print("%s:" % yel("Commits changing symbol"))
200 commits = find_commits(feature, opts.diff)200 commits = find_commits(feature, opts.diff)
201 if commits:201 if commits:
202 for commit in commits:202 for commit in commits:
203 commit = commit.split(" ", 1)203 commit = commit.split(" ", 1)
204 print "\t- %s (\"%s\")" % (yel(commit[0]), commit[1])204 print("\t- %s (\"%s\")" % (yel(commit[0]), commit[1]))
205 else:205 else:
206 print "\t- no commit found"206 print("\t- no commit found")
207 print # new line207 print() # new line
208 208
209 209
210def yel(string):210def yel(string):
@@ -225,7 +225,8 @@ def execute(cmd): · 14 unmodified lines
225 """Execute %cmd and return stdout. Exit in case of error."""225 """Execute %cmd and return stdout. Exit in case of error."""
226 try:226 try:
227 cmdlist = cmd.split(" ")227 cmdlist = cmd.split(" ")
228 stdout = subprocess.check_output(cmdlist, stderr=STDOUT, shell=False)228 stdout = subprocess.check_output(cmdlist, stderr=subprocess.STDOUT, shell=False)
229 stdout = stdout.decode(errors='replace')
229 except subprocess.CalledProcessError as fail:230 except subprocess.CalledProcessError as fail:
230 exit("Failed to execute %s\n%s" % (cmd, fail))231 exit("Failed to execute %s\n%s" % (cmd, fail))
231 return stdout232 return stdout
@@ -256,7 +257,7 @@ def get_head(): · 24 unmodified lines
256 257
257def partition(lst, size):258def partition(lst, size):
258 """Partition list @lst into eveni-sized lists of size @size."""259 """Partition list @lst into eveni-sized lists of size @size."""
259 return [lst[i::size] for i in xrange(size)]260 return [lst[i::size] for i in range(size)]
260 261
261 262
262def init_worker():263def init_worker():
@@ -350,7 +351,7 @@ def check_symbols_helper(pool, ignore): · 87 unmodified lines
350 351
351 # inverse mapping of referenced_features to dict(feature: [files])352 # inverse mapping of referenced_features to dict(feature: [files])
352 inv_map = dict()353 inv_map = dict()
353 for _file, features in referenced_features.iteritems():354 for _file, features in referenced_features.items():
354 for feature in features:355 for feature in features:
355 inv_map[feature] = inv_map.get(feature, set())356 inv_map[feature] = inv_map.get(feature, set())
356 inv_map[feature].add(_file)357 inv_map[feature].add(_file)
@@ -388,7 +389,7 @@ def parse_source_file(sfile): · 31 unmodified lines
388 if not os.path.exists(sfile):389 if not os.path.exists(sfile):
389 return references390 return references
390 391
391 with open(sfile, "r") as stream:392 with open(sfile, "r", encoding='utf-8', errors='replace') as stream:
392 lines = stream.readlines()393 lines = stream.readlines()
393 394
394 for line in lines:395 for line in lines:
@@ -437,7 +438,7 @@ def parse_kconfig_file(kfile): · 42 unmodified lines
437 if not os.path.exists(kfile):438 if not os.path.exists(kfile):
438 return defined, references439 return defined, references
439 440
440 with open(kfile, "r") as stream:441 with open(kfile, "r", encoding='utf-8', errors='replace') as stream:
441 lines = stream.readlines()442 lines = stream.readlines()
442 443
443 for i in range(len(lines)):444 for i in range(len(lines)):

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t