forked from kovidgoyal/calibre
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck.py
More file actions
145 lines (115 loc) · 4.49 KB
/
check.py
File metadata and controls
145 lines (115 loc) · 4.49 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python
__license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
import errno
import hashlib
import json
import os
import subprocess
from setup import Command, build_cache_dir, dump_json, edit_file
class Message:
def __init__(self, filename, lineno, msg):
self.filename, self.lineno, self.msg = filename, lineno, msg
def __str__(self):
return f'{self.filename}:{self.lineno}: {self.msg}'
def checkable_python_files(SRC):
for dname in ('odf', 'calibre'):
for x in os.walk(os.path.join(SRC, dname)):
for f in x[-1]:
y = os.path.join(x[0], f)
if (f.endswith('.py') and f not in (
'dict_data.py', 'unicodepoints.py', 'krcodepoints.py',
'jacodepoints.py', 'vncodepoints.py', 'zhcodepoints.py') and
'prs500/driver.py' not in y) and not f.endswith('_ui.py'):
yield y
class Check(Command):
description = 'Check for errors in the calibre source code'
CACHE = 'check.json'
def get_files(self):
yield from checkable_python_files(self.SRC)
for x in os.walk(self.j(self.d(self.SRC), 'recipes')):
for f in x[-1]:
f = self.j(x[0], f)
if f.endswith('.recipe'):
yield f
for x in os.walk(self.j(self.SRC, 'pyj')):
for f in x[-1]:
f = self.j(x[0], f)
if f.endswith('.pyj'):
yield f
if self.has_changelog_check:
yield self.j(self.d(self.SRC), 'Changelog.txt')
def read_file(self, f):
with open(f, 'rb') as f:
return f.read()
def file_hash(self, f):
try:
return self.fhash_cache[f]
except KeyError:
self.fhash_cache[f] = ans = hashlib.sha1(self.read_file(f)).hexdigest()
return ans
def is_cache_valid(self, f, cache):
return cache.get(f) == self.file_hash(f)
@property
def cache_file(self):
return self.j(build_cache_dir(), self.CACHE)
def save_cache(self, cache):
dump_json(cache, self.cache_file)
def file_has_errors(self, f):
ext = os.path.splitext(f)[1]
if ext in {'.py', '.recipe'}:
p2 = subprocess.Popen(['ruff', 'check', f])
return p2.wait() != 0
if ext == '.pyj':
p = subprocess.Popen(['rapydscript', 'lint', f])
return p.wait() != 0
if ext == '.yaml':
p = subprocess.Popen(['python', self.j(self.wn_path, 'whats_new.py'), f])
return p.wait() != 0
def run(self, opts):
self.fhash_cache = {}
cache = {}
self.wn_path = os.path.expanduser('~/work/srv/main/static')
self.has_changelog_check = os.path.exists(self.wn_path)
try:
with open(self.cache_file, 'rb') as f:
cache = json.load(f)
except OSError as err:
if err.errno != errno.ENOENT:
raise
dirty_files = tuple(f for f in self.get_files() if not self.is_cache_valid(f, cache))
try:
for i, f in enumerate(dirty_files):
self.info('\tChecking', f)
if self.file_has_errors(f):
self.info('%d files left to check' % (len(dirty_files) - i - 1))
edit_file(f)
if self.file_has_errors(f):
raise SystemExit(1)
cache[f] = self.file_hash(f)
finally:
self.save_cache(cache)
def report_errors(self, errors):
for err in errors:
self.info('\t\t', str(err))
def clean(self):
try:
os.remove(self.cache_file)
except OSError as err:
if err.errno != errno.ENOENT:
raise
class UpgradeSourceCode(Command):
description = 'Upgrade python source code'
def run(self, opts):
files = []
for f in os.listdir(os.path.dirname(os.path.abspath(__file__))):
q = os.path.join('setup', f)
if f.endswith('.py') and f not in ('linux-installer.py',) and not os.path.isdir(q):
files.append(q)
for path in checkable_python_files(self.SRC):
q = path.replace(os.sep, '/')
if '/metadata/sources/' in q or '/store/stores/' in q:
continue
files.append(q)
subprocess.call(['pyupgrade', '--py38-plus'] + files)