Repository navigation
Expand file tree
/
Copy pathutilities.py
More file actions
239 lines (207 loc) · 8.74 KB
/
Copy pathutilities.py
File metadata and controls
239 lines (207 loc) · 8.74 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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
# Print an error message
#
# Note: if e is NOT None, the message should not end with a dot, since a ':' will be appended
#
# e: if e is None, show no error details
# caption: must always exist (is used from code to show a readable message)
# exitCode: if exitCode is None, do not exit, just show the error
# Print an error message
#
# Note: if e is NOT None, the message should not end with a dot, since a ':' will be appended
#
# e: if e is None, show no error details
# caption: must always exist (is used from code to show a readable message)
# exitCode: if exitCode is None, do not exit, just show the error
def errorHandler(e, caption, exitCode=1):
if hasattr(e, 'strerror'):
msg = e.strerror
else:
msg = e
if e is not None:
# show error details, and in red
# show error details, and in red
print('\x1b[31;20m{}:'.format(caption), msg, '\x1b[0m', file=sys.stderr)
else:
# show no error details, and in cyan
print('\x1b[36;20m{}\x1b[0m'.format(caption), file=sys.stderr)
# show no error details, and in cyan
print('\x1b[36;20m{}\x1b[0m'.format(caption), file=sys.stderr)
if exitCode is not None:
sys.exit(exitCode)
import os
from pathlib import Path
def get_version(caption = 'v'):
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'VERSION'), 'r') as version_file:
return caption + version_file.read().strip()
def is_docker():
cgroup = Path('/proc/self/cgroup')
return Path('/.dockerenv').is_file() or cgroup.is_file() and 'docker' in cgroup.read_text()
import base64
# https://stackoverflow.com/questions/12776679/imap-folder-path-encoding-imap-utf-7-for-python/45787169#45787169
def b64padanddecode(b):
"""Decode unpadded base64 data"""
b+=(-len(b)%4)*'=' #base64 padding (if adds '===', no valid padding anyway)
return base64.b64decode(b,altchars='+,',validate=True).decode('utf-16-be')
def imaputf7decode(s):
"""Decode a string encoded according to RFC2060 aka IMAP UTF7.
Minimal validation of input, only works with trusted data"""
lst=s.split('&')
out=lst[0]
for e in lst[1:]:
u,a=e.split('-',1) #u: utf16 between & and 1st -, a: ASCII chars folowing it
if u=='' : out+='&'
else: out+=b64padanddecode(u)
out+=a
return out
def imaputf7encode(s):
""""Encode a string into RFC2060 aka IMAP UTF7"""
s=s.replace('&','&-')
iters=iter(s)
unipart=out=''
for c in s:
if 0x20<=ord(c)<=0x7f :
if unipart!='' :
out+='&'+base64.b64encode(unipart.encode('utf-16-be')).decode('ascii').rstrip('=')+'-'
unipart=''
out+=c
else : unipart+=c
if unipart!='' :
out+='&'+base64.b64encode(unipart.encode('utf-16-be')).decode('ascii').rstrip('=')+'-'
return out
import re
import hashlib
def createReliableMessageId(message_id, data):
if message_id and len(message_id.strip()) > 10:
msg_id_safe = re.sub(r'[^a-zA-Z0-9_\-\.() ]+', '', message_id.strip())
else:
try:
# imaplib FETCH returns a list like [ (b'1 (BODY[] {n}', raw_message), b')' ]
# -> the raw bytes are data[0][1], NOT data[1] (which is the closing paren)
if type(data) is list:
raw = None
for part in data:
if isinstance(part, tuple) and len(part) == 2:
raw = part[1]
break
if raw is None:
raw = data[0]
msg_id_safe = hashlib.sha224(raw).hexdigest()
else:
msg_id_safe = hashlib.sha224(data).hexdigest()
except Exception as e:
errorHandler(e, '- FAILED: Creating message ID', exitCode=None)
raise e
return msg_id_safe
def createReliableFoldername(message_id, data):
# 255 is the max filename length on all systems
if message_id and len(message_id.strip()) < 255:
foldername = re.sub(r'[^a-zA-Z0-9_\-\.() ]+', '', message_id.strip())
else:
try:
# imaplib FETCH returns a list like [ (b'1 (BODY[] {n}', raw_message), b')' ]
# -> the raw bytes are data[0][1], NOT data[1] (which is the closing paren)
if type(data) is list:
raw = None
for part in data:
if isinstance(part, tuple) and len(part) == 2:
raw = part[1]
break
if raw is None:
raw = data[0]
foldername = hashlib.sha224(raw).hexdigest()
else:
foldername = hashlib.sha224(data).hexdigest()
except Exception as e:
errorHandler(e, '- FAILED: Creating folder name', exitCode=None)
raise e
return foldername
def hasTTY():
return sys.stdin and sys.stdin.isatty()
import configparser
#import os
class DollarInterpolation(configparser.ExtendedInterpolation):
"""ExtendedInterpolation for the imapbox config.
- `${name}` resolves `name` from the unnamed section
(`allow_unnamed_section=True`), i.e. the key=value block before the
first `[section]` header.
- `${section:name}` resolves `name` from the given `section`.
- `${env:name}` resolves `name` from an `[env]` section if present,
otherwise from the environment (`os.environ`).
- Every other `$` is kept literally: `$$` stays `$$`, and `$${...}`
writes a literal `${...}` instead of being interpolated.
"""
def _interpolate_some(self, parser, option, accum, rest, section, map,
depth):
rawval = parser.get(section, option, raw=True, fallback=rest)
if depth > configparser.MAX_INTERPOLATION_DEPTH:
raise configparser.InterpolationDepthError(option, section, rawval)
while rest:
p = rest.find("$")
if p < 0:
accum.append(rest)
return
if p > 0:
accum.append(rest[:p])
rest = rest[p:]
# p is no longer used
c = rest[1:2]
if c == "$":
if rest[2:3] == "{":
accum.append("${")
rest = rest[3:]
else:
accum.append("$$")
rest = rest[2:]
elif c == "{":
m = self._KEYCRE.match(rest)
if m is None:
raise configparser.InterpolationSyntaxError(
option, section,
"bad interpolation variable reference %r" % rest)
path = m.group(1).split(':')
rest = rest[m.end():]
sect = section
opt = option
try:
if len(path) == 1:
opt = parser.optionxform(path[0])
v = parser.get(configparser.UNNAMED_SECTION, opt,
raw=True)
elif len(path) == 2:
sect = path[0]
opt = parser.optionxform(path[1])
if sect == 'env':
if parser.has_option('env', opt):
v = parser.get('env', opt, raw=True)
elif path[1] in os.environ:
v = os.environ[path[1]]
elif path[1].upper() in os.environ:
v = os.environ[path[1].upper()]
else:
raise configparser.InterpolationMissingOptionError(
option, section, rawval,
":".join(path))
else:
v = parser.get(sect, opt, raw=True)
else:
raise configparser.InterpolationSyntaxError(
option, section,
"More than one ':' found: %r" % (rest,))
except (KeyError, configparser.NoSectionError,
configparser.NoOptionError):
raise configparser.InterpolationMissingOptionError(
option, section, rawval, ":".join(path)) from None
if v is None:
continue
if "$" in v:
self._interpolate_some(parser, opt, accum, v, sect,
dict(parser.items(sect, raw=True)),
depth + 1)
else:
accum.append(v)
else:
accum.append("$")
rest = rest[1:]