def files_in_archive(self, force_refresh=False):
if self._files_in_archive and not force_refresh:
return self._files_in_archive
cmd = [ self.cmd_path, '--list', '-C', self.dest ]
if self.zipflag:
cmd.append(self.zipflag)
if self.opts:
cmd.extend([ '--show-transformed-names' ] + self.opts)
if self.excludes:
cmd.extend([ '--exclude=' + quote(f) for f in self.excludes ])
cmd.extend([ '-f', self.src ])
rc, out, err = self.module.run_command(cmd, cwd=self.dest, environ_update=dict(LANG='C', LC_ALL='C', LC_MESSAGES='C'))
if rc != 0:
raise UnarchiveError('Unable to list files in the archive')
for filename in out.splitlines():
# Compensate for locale-related problems in gtar output (octal unicode representation) #11348
# filename = filename.decode('string_escape')
filename = codecs.escape_decode(filename)[0]
if filename and filename not in self.excludes:
self._files_in_archive.append(to_native(filename))
return self._files_in_archive
python类escape_decode()的实例源码
def decode(self, input, final=False):
return codecs.escape_decode(input, self.errors)[0]
def decode(self, input, final=False):
return codecs.escape_decode(input, self.errors)[0]
def unescape(value):
return codecs.escape_decode(value)[0].decode('utf-8')
def decode(self, input, final=False):
return codecs.escape_decode(input, self.errors)[0]
def decode(self, input, final=False):
return codecs.escape_decode(input, self.errors)[0]
def decode(self, input, final=False):
return codecs.escape_decode(input, self.errors)[0]
def decode(self, input, final=False):
return codecs.escape_decode(input, self.errors)[0]
def raw_bytes(raw):
"""Make a bytes object out of a raw string.
This is analogous to raw_unicode, but processes byte escape characters
to produce a bytes object.
"""
return codecs.escape_decode(raw)[0]
def decode(self, input, final=False):
return codecs.escape_decode(input, self.errors)[0]
def decode(self, input, final=False):
return codecs.escape_decode(input, self.errors)[0]
def decode(self, input, final=False):
return codecs.escape_decode(input, self.errors)[0]
def decode(self, input, final=False):
return codecs.escape_decode(input, self.errors)[0]
def decode(self, input, final=False):
return codecs.escape_decode(input, self.errors)[0]
def decode(self, input, final=False):
return codecs.escape_decode(input, self.errors)[0]
def assemble_from_fragments(src_path, delimiter=None, compiled_regexp=None, ignore_hidden=False):
''' assemble a file from a directory of fragments '''
tmpfd, temp_path = tempfile.mkstemp()
tmp = os.fdopen(tmpfd, 'wb')
delimit_me = False
add_newline = False
for f in sorted(os.listdir(src_path)):
if compiled_regexp and not compiled_regexp.search(f):
continue
fragment = u"%s/%s" % (src_path, f)
if not os.path.isfile(fragment) or (ignore_hidden and os.path.basename(fragment).startswith('.')):
continue
fragment_content = open(fragment, 'rb').read()
# always put a newline between fragments if the previous fragment didn't end with a newline.
if add_newline:
tmp.write(b('\n'))
# delimiters should only appear between fragments
if delimit_me:
if delimiter:
# un-escape anything like newlines
delimiter = codecs.escape_decode(delimiter)[0]
tmp.write(delimiter)
# always make sure there's a newline after the
# delimiter, so lines don't run together
if delimiter[-1] != b('\n'):
tmp.write(b('\n'))
tmp.write(fragment_content)
delimit_me = True
if fragment_content.endswith(b('\n')):
add_newline = False
else:
add_newline = True
tmp.close()
return temp_path
def _assemble_from_fragments(self, src_path, delimiter=None, compiled_regexp=None, ignore_hidden=False):
''' assemble a file from a directory of fragments '''
tmpfd, temp_path = tempfile.mkstemp()
tmp = os.fdopen(tmpfd, 'wb')
delimit_me = False
add_newline = False
for f in (to_text(p, errors='surrogate_or_strict') for p in sorted(os.listdir(src_path))):
if compiled_regexp and not compiled_regexp.search(f):
continue
fragment = u"%s/%s" % (src_path, f)
if not os.path.isfile(fragment) or (ignore_hidden and os.path.basename(fragment).startswith('.')):
continue
fragment_content = open(self._loader.get_real_file(fragment), 'rb').read()
# always put a newline between fragments if the previous fragment didn't end with a newline.
if add_newline:
tmp.write(b'\n')
# delimiters should only appear between fragments
if delimit_me:
if delimiter:
# un-escape anything like newlines
delimiter = codecs.escape_decode(delimiter)[0]
tmp.write(delimiter)
# always make sure there's a newline after the
# delimiter, so lines don't run together
if delimiter[-1] != b'\n':
tmp.write(b'\n')
tmp.write(fragment_content)
delimit_me = True
if fragment_content.endswith(b'\n'):
add_newline = False
else:
add_newline = True
tmp.close()
return temp_path
def decode(self, input, final=False):
return codecs.escape_decode(input, self.errors)[0]
def load_string(self):
orig = self.readline()
rep = orig[:-1]
for q in (b'"', b"'"): # double or single quote
if rep.startswith(q):
if not rep.endswith(q):
raise ValueError("insecure string pickle")
rep = rep[len(q):-len(q)]
break
else:
raise ValueError("insecure string pickle: %r" % orig)
self.append(codecs.escape_decode(rep)[0]
.decode(self.encoding, self.errors))
def test_empty(self):
self.assertEqual(codecs.escape_decode(""), ("", 0))
def decode(self, input, final=False):
return codecs.escape_decode(input, self.errors)[0]
def decode(self, input, final=False):
return codecs.escape_decode(input, self.errors)[0]
def decode(self, input, final=False):
return codecs.escape_decode(input, self.errors)[0]
def decode(self, input, final=False):
return codecs.escape_decode(input, self.errors)[0]
def decode(self, input, final=False):
return codecs.escape_decode(input, self.errors)[0]
def decode(self, input, final=False):
return codecs.escape_decode(input, self.errors)[0]
def decode(self, input, final=False):
return codecs.escape_decode(input, self.errors)[0]
def test_empty(self):
self.assertEqual(codecs.escape_decode(""), ("", 0))
def test_raw(self):
decode = codecs.escape_decode
for b in range(256):
b = chr(b)
if b != '\\':
self.assertEqual(decode(b + '0'), (b + '0', 2))
def test_escape(self):
decode = codecs.escape_decode
check = coding_checker(self, decode)
check(b"[\\\n]", b"[]")
check(br'[\"]', b'["]')
check(br"[\']", b"[']")
check(br"[\\]", br"[\]")
check(br"[\a]", b"[\x07]")
check(br"[\b]", b"[\x08]")
check(br"[\t]", b"[\x09]")
check(br"[\n]", b"[\x0a]")
check(br"[\v]", b"[\x0b]")
check(br"[\f]", b"[\x0c]")
check(br"[\r]", b"[\x0d]")
check(br"[\7]", b"[\x07]")
check(br"[\8]", br"[\8]")
check(br"[\78]", b"[\x078]")
check(br"[\41]", b"[!]")
check(br"[\418]", b"[!8]")
check(br"[\101]", b"[A]")
check(br"[\1010]", b"[A0]")
check(br"[\501]", b"[A]")
check(br"[\x41]", b"[A]")
check(br"[\X41]", br"[\X41]")
check(br"[\x410]", b"[A0]")
for b in range(256):
b = chr(b)
if b not in '\n"\'\\abtnvfr01234567x':
check('\\' + b, '\\' + b)