File: detect_nonascii_sourcefiles.py

package info (click to toggle)
simdjson 4.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,936 kB
  • sloc: cpp: 171,612; ansic: 19,122; sh: 1,126; python: 842; makefile: 47; ruby: 25; javascript: 13
file content (36 lines) | stat: -rwxr-xr-x 1,020 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/python3

import sys

def verifyContent(f,filename):
  linenumber=-999
  line=''
  try:
    for linenumber, line in enumerate(f):
      try:
        ascii=line.encode('ascii')
      except UnicodeEncodeError as e:
        #print(f"a: found problem {e} at line {linenumber+1} in {filename}:")
        print(f"Found problem at line {linenumber+1} in {filename}:")
        print(line.rstrip())
        for col, char in enumerate(line.encode('utf-8')):
          if char>=127:
             offender=char
             offendingcol=col
             break
        print(" "*offendingcol + "^")
        print(f"Column {offendingcol+1} contains 0x{offender:02X}")
        sys.exit(1)

  except UnicodeDecodeError as e:
    print(f"Could not open {filename} as utf-8, it can't be ascii.")
    sys.exit(1)



for filename in sys.argv[1:]:
  with open(filename,encoding='utf-8') as f:
    #print(f"file {filename} was possible to open as utf-8")
    verifyContent(f,filename)
  print("all files were found to be ascii.")