-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.py
More file actions
267 lines (206 loc) · 7.69 KB
/
utils.py
File metadata and controls
267 lines (206 loc) · 7.69 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
"""
Miscellaneous utilities.
These are mostly used in templates, for presentation purposes.
"""
from typing import Sequence
from types import ModuleType
import base64
import functools
import mimetypes
import string
import inspect
import sys
import time
import os
import os.path
import git
try:
import pygments
from pygments import highlight
from pygments import lexers
from pygments.formatters import HtmlFormatter
_html_formatter = HtmlFormatter(
encoding="utf-8",
cssclass="source_code",
linenos="table",
anchorlinenos=True,
lineanchors="line",
)
@functools.lru_cache
def can_colorize(s: str) -> bool:
"""True if we can colorize the string, False otherwise."""
# Pygments can take a huge amount of time with long files, or with
# very long lines; these are heuristics to try to avoid those
# situations.
if len(s) > (512 * 1024):
return False
# If any of the first 5 lines is over 300 characters long, don't
# colorize.
start = 0
for i in range(5):
pos = s.find("\n", start)
if pos == -1:
break
if pos - start > 300:
return False
start = pos + 1
return True
@functools.lru_cache
def colorize_diff(s: str) -> str:
lexer = lexers.DiffLexer(encoding="utf-8")
formatter = HtmlFormatter(encoding="utf-8", cssclass="source_code")
return highlight(s, lexer, formatter)
@functools.lru_cache
def colorize_blob(fname, s: str) -> str:
# Explicit import to enable type checking, otherwise mypy gets confused
# because pygments is defined as a generic module | None.
import pygments.lexer
lexer: pygments.lexer.Lexer | pygments.lexer.LexerMeta
try:
lexer = lexers.guess_lexer_for_filename(fname, s, encoding="utf-8")
except lexers.ClassNotFound:
# Only try to guess lexers if the file starts with a shebang,
# otherwise it's likely a text file and guess_lexer() is prone to
# make mistakes with those.
if s.startswith("#!"):
try:
lexer = lexers.guess_lexer(s[:80], encoding="utf-8")
except lexers.ClassNotFound:
pass
else:
lexer = lexers.TextLexer(encoding="utf-8")
return highlight(s, lexer, _html_formatter)
except ImportError:
@functools.lru_cache
def can_colorize(s: str) -> bool:
"""True if we can colorize the string, False otherwise."""
return False
@functools.lru_cache
def colorize_diff(s: str) -> str:
raise RuntimeError("colorize_diff() called without pygments support")
@functools.lru_cache
def colorize_blob(fname, s: str) -> str:
raise RuntimeError("colorize_blob() called without pygments support")
try:
import markdown
def can_markdown(repo: git.Repo, fname: str) -> bool:
"""True if we can process file through markdown, False otherwise."""
if not repo.info.embed_markdown:
return False
return fname.endswith(".md")
class RewriteLocalLinks(markdown.treeprocessors.Treeprocessor):
"""Rewrites relative links to files, to match git-arr's links.
A link of "[example](a/file.md)" will be rewritten such that it links to
"a/f=file.md.html".
Note that we're already assuming a degree of sanity in the HTML, so we
don't re-check that the path is reasonable.
"""
def run(self, root):
for child in root:
if child.tag == "a":
self.rewrite_href(child)
# Continue recursively.
self.run(child)
def rewrite_href(self, tag):
"""Rewrite an <a>'s href."""
target = tag.get("href")
if not target:
return
if "://" in target or target.startswith("/"):
return
head, tail = os.path.split(target)
new_target = os.path.join(head, "f=" + tail + ".html")
tag.set("href", new_target)
class RewriteLocalLinksExtension(markdown.Extension):
def extendMarkdown(self, md):
md.treeprocessors.register(
RewriteLocalLinks(), "RewriteLocalLinks", 1000
)
_md_extensions: Sequence[str | markdown.Extension] = [
"markdown.extensions.fenced_code",
"markdown.extensions.tables",
RewriteLocalLinksExtension(),
]
@functools.lru_cache
def markdown_blob(s: str) -> str:
return markdown.markdown(s, extensions=_md_extensions)
except ImportError:
def can_markdown(repo: git.Repo, fname: str) -> bool:
"""True if we can process file through markdown, False otherwise."""
return False
@functools.lru_cache
def markdown_blob(s: str) -> str:
raise RuntimeError("markdown_blob() called without markdown support")
def shorten(s: str, width=60):
if len(s) < 60:
return s
return s[:57] + "..."
def can_embed_image(repo: git.Repo, fname: str) -> bool:
"""True if we can embed image file in HTML, False otherwise."""
if not repo.info.embed_images:
return False
return ("." in fname) and (
fname.split(".")[-1].lower() in ["jpg", "jpeg", "png", "gif"]
)
def embed_image_blob(fname: str, image_data: bytes) -> str:
mimetype = mimetypes.guess_type(fname)[0]
b64img = base64.b64encode(image_data).decode("ascii")
return '<img style="max-width:100%;" src="data:{0};base64,{1}" />'.format(
mimetype, b64img
)
@functools.lru_cache
def is_binary(b: bytes):
# Git considers a blob binary if NUL in first ~8KB, so do the same.
return b"\0" in b[:8192]
@functools.lru_cache
def hexdump(s: bytes):
graph = string.ascii_letters + string.digits + string.punctuation + " "
b = s.decode("latin1")
offset = 0
while b:
t = b[:16]
hexvals = ["%.2x" % ord(c) for c in t]
text = "".join(c if c in graph else "." for c in t)
yield offset, " ".join(hexvals[:8]), " ".join(hexvals[8:]), text
offset += 16
b = b[16:]
def log_timing(*log_args):
"Decorator to log how long a function call took."
if not os.environ.get("GIT_ARR_DEBUG"):
return lambda f: f
def log_timing_decorator(f):
argspec = inspect.getfullargspec(f)
idxs = [argspec.args.index(arg) for arg in log_args]
@functools.wraps(f)
def wrapper(*args, **kwargs):
start = time.time()
result = f(*args, **kwargs)
end = time.time()
f_args = [args[i] for i in idxs]
sys.stderr.write(
"%.4fs %s %s\n" % (end - start, f.__name__, " ".join(f_args))
)
return result
return wrapper
return log_timing_decorator
try:
import xattr
def set_xattr_oid(path: str, oid: str):
"""Set the xattr 'user.git-arr.oid' on the given path."""
try:
xattr.setxattr(path, "user.git-arr.oid", oid.encode("utf-8"))
except OSError as e:
print(f"{path}: error writing xattr: {e}")
def get_xattr_oid(path: str) -> str:
"""Get the xattr 'user.git-arr.oid' from the given path."""
try:
return xattr.getxattr(path, "user.git-arr.oid").decode("utf-8")
except OSError as e:
return ""
except ImportError:
def set_xattr_oid(path: str, oid: str):
"""Set the xattr 'user.git-arr.oid' on the given path."""
pass
def get_xattr_oid(path: str) -> str:
"""Get the xattr 'user.git-arr.oid' from the given path."""
return ""