1 parent
47f58ff45f
commit
f99a94d685
2 files changed
+98
-55
No files matched your search
@@ -33,7 +33,7 @@ similar to the C interface provided by UnRAR. There is also a
|
|||||||
higher level interface which makes some common operations easier.
|
higher level interface which makes some common operations easier.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__version__ = '0.99.2'
|
__version__ = '0.99.3'
|
||||||
|
|
||||||
try:
|
try:
|
||||||
WindowsError
|
WindowsError
|
||||||
|
|||||||
+97
-54
@@ -33,115 +33,158 @@ from rar_exceptions import *
|
|||||||
class UnpackerNotInstalled(Exception): pass
|
class UnpackerNotInstalled(Exception): pass
|
||||||
|
|
||||||
rar_executable_cached = None
|
rar_executable_cached = None
|
||||||
|
rar_executable_version = None
|
||||||
|
|
||||||
def call_unrar(params):
|
def call_unrar(params):
|
||||||
"Calls rar/unrar command line executable, returns stdout pipe"
|
"Calls rar/unrar command line executable, returns stdout pipe"
|
||||||
global rar_executable_cached
|
global rar_executable_cached
|
||||||
if rar_executable_cached is None:
|
if rar_executable_cached is None:
|
||||||
for command in ('unrar', 'rar', os.path.join(os.path.dirname(__file__), 'unrar')):
|
for command in ('unrar', 'rar'):
|
||||||
try:
|
try:
|
||||||
subprocess.Popen([command], stdout = subprocess.PIPE)
|
subprocess.Popen([command], stdout=subprocess.PIPE)
|
||||||
rar_executable_cached = command
|
rar_executable_cached = command
|
||||||
break
|
break
|
||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
if rar_executable_cached is None:
|
if rar_executable_cached is None:
|
||||||
raise UnpackerNotInstalled("No suitable RAR unpacker installed")
|
raise UnpackerNotInstalled("No suitable RAR unpacker installed")
|
||||||
|
|
||||||
assert type(params) == list, "params must be list"
|
assert type(params) == list, "params must be list"
|
||||||
args = [rar_executable_cached] + params
|
args = [rar_executable_cached] + params
|
||||||
try:
|
try:
|
||||||
gc.disable() # See http://bugs.python.org/issue1336
|
gc.disable() # See http://bugs.python.org/issue1336
|
||||||
return subprocess.Popen(args, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
|
return subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||||
finally:
|
finally:
|
||||||
gc.enable()
|
gc.enable()
|
||||||
|
|
||||||
class RarFileImplementation(object):
|
class RarFileImplementation(object):
|
||||||
|
|
||||||
def init(self, password = None):
|
def init(self, password=None):
|
||||||
|
global rar_executable_version
|
||||||
self.password = password
|
self.password = password
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
stdoutdata, stderrdata = self.call('v', []).communicate()
|
stdoutdata, stderrdata = self.call('v', []).communicate()
|
||||||
|
|
||||||
for line in stderrdata.splitlines():
|
for line in stderrdata.splitlines():
|
||||||
if line.strip().startswith("Cannot open"):
|
if line.strip().startswith("Cannot open"):
|
||||||
raise FileOpenError
|
raise FileOpenError
|
||||||
if line.find("CRC failed") >= 0:
|
if line.find("CRC failed")>=0:
|
||||||
raise IncorrectRARPassword
|
raise IncorrectRARPassword
|
||||||
accum = []
|
accum = []
|
||||||
source = iter(stdoutdata.splitlines())
|
source = iter(stdoutdata.splitlines())
|
||||||
line = ''
|
line = ''
|
||||||
while not (line.startswith('Comment:') or line.startswith('Pathname/Comment')):
|
while not (line.startswith('UNRAR')):
|
||||||
if line.strip().endswith('is not RAR archive'):
|
|
||||||
raise InvalidRARArchive
|
|
||||||
line = source.next()
|
line = source.next()
|
||||||
while not line.startswith('Pathname/Comment'):
|
signature = line
|
||||||
accum.append(line.rstrip('\n'))
|
# The code below is mighty flaky
|
||||||
|
# and will probably crash on localized versions of RAR
|
||||||
|
# but I see no safe way to rewrite it using a CLI tool
|
||||||
|
if signature.startswith("UNRAR 4"):
|
||||||
|
rar_executable_version = 4
|
||||||
|
while not (line.startswith('Comment:') or line.startswith('Pathname/Comment')):
|
||||||
|
if line.strip().endswith('is not RAR archive'):
|
||||||
|
raise InvalidRARArchive
|
||||||
|
line = source.next()
|
||||||
|
while not line.startswith('Pathname/Comment'):
|
||||||
|
accum.append(line.rstrip('\n'))
|
||||||
|
line = source.next()
|
||||||
|
if len(accum):
|
||||||
|
accum[0] = accum[0][9:] # strip out "Comment:" part
|
||||||
|
self.comment = '\n'.join(accum[:-1])
|
||||||
|
else:
|
||||||
|
self.comment = None
|
||||||
|
elif signature.startswith("UNRAR 5"):
|
||||||
|
rar_executable_version = 5
|
||||||
line = source.next()
|
line = source.next()
|
||||||
if len(accum):
|
while not line.startswith('Archive:'):
|
||||||
accum[0] = accum[0][9:]
|
if line.strip().endswith('is not RAR archive'):
|
||||||
self.comment = '\n'.join(accum[:-1])
|
raise InvalidRARArchive
|
||||||
|
accum.append(line.rstrip('\n'))
|
||||||
|
line = source.next()
|
||||||
|
if len(accum):
|
||||||
|
self.comment = '\n'.join(accum[:-1]).strip()
|
||||||
|
else:
|
||||||
|
self.comment = None
|
||||||
else:
|
else:
|
||||||
self.comment = None
|
raise UnpackerNotInstalled("Unsupported RAR version, expected 4.x or 5.x, found: "
|
||||||
|
+ signature.split(" ")[1])
|
||||||
|
|
||||||
|
|
||||||
def escaped_password(self):
|
def escaped_password(self):
|
||||||
return '-' if self.password == None else self.password
|
return '-' if self.password == None else self.password
|
||||||
|
|
||||||
|
|
||||||
def call(self, cmd, options = [], files = []):
|
def call(self, cmd, options=[], files=[]):
|
||||||
options2 = options + ['p' + self.escaped_password()]
|
options2 = options + ['p'+self.escaped_password()]
|
||||||
soptions = ['-' + x for x in options2]
|
soptions = ['-'+x for x in options2]
|
||||||
return call_unrar([cmd] + soptions + ['--', self.archiveName] + files)
|
return call_unrar([cmd]+soptions+['--',self.archiveName]+files)
|
||||||
|
|
||||||
def infoiter(self):
|
def infoiter(self):
|
||||||
|
|
||||||
stdoutdata, stderrdata = self.call('v', ['c-']).communicate()
|
command = "v" if rar_executable_version == 4 else "l"
|
||||||
|
stdoutdata, stderrdata = self.call(command, ['c-']).communicate()
|
||||||
|
|
||||||
for line in stderrdata.splitlines():
|
for line in stderrdata.splitlines():
|
||||||
if line.strip().startswith("Cannot open"):
|
if line.strip().startswith("Cannot open"):
|
||||||
raise FileOpenError
|
raise FileOpenError
|
||||||
|
|
||||||
accum = []
|
accum = []
|
||||||
source = iter(stdoutdata.splitlines())
|
source = iter(stdoutdata.splitlines())
|
||||||
line = ''
|
line = ''
|
||||||
while not line.startswith('--------------'):
|
while not line.startswith('-----------'):
|
||||||
if line.strip().endswith('is not RAR archive'):
|
if line.strip().endswith('is not RAR archive'):
|
||||||
raise InvalidRARArchive
|
raise InvalidRARArchive
|
||||||
if line.find("CRC failed") >= 0:
|
if line.startswith("CRC failed") or line.startswith("Checksum error"):
|
||||||
raise IncorrectRARPassword
|
raise IncorrectRARPassword
|
||||||
line = source.next()
|
line = source.next()
|
||||||
line = source.next()
|
line = source.next()
|
||||||
i = 0
|
i = 0
|
||||||
re_spaces = re.compile(r"\s+")
|
re_spaces = re.compile(r"\s+")
|
||||||
while not line.startswith('--------------'):
|
if rar_executable_version == 4:
|
||||||
accum.append(line)
|
while not line.startswith('-----------'):
|
||||||
if len(accum) == 2:
|
accum.append(line)
|
||||||
|
if len(accum)==2:
|
||||||
|
data = {}
|
||||||
|
data['index'] = i
|
||||||
|
# asterisks mark password-encrypted files
|
||||||
|
data['filename'] = accum[0].strip().lstrip("*") # asterisks marks password-encrypted files
|
||||||
|
fields = re_spaces.split(accum[1].strip())
|
||||||
|
data['size'] = int(fields[0])
|
||||||
|
attr = fields[5]
|
||||||
|
data['isdir'] = 'd' in attr.lower()
|
||||||
|
data['datetime'] = time.strptime(fields[3]+" "+fields[4], '%d-%m-%y %H:%M')
|
||||||
|
data['comment'] = None
|
||||||
|
yield data
|
||||||
|
accum = []
|
||||||
|
i += 1
|
||||||
|
line = source.next()
|
||||||
|
elif rar_executable_version == 5:
|
||||||
|
while not line.startswith('-----------'):
|
||||||
|
fields = line.strip().lstrip("*").split()
|
||||||
data = {}
|
data = {}
|
||||||
data['index'] = i
|
data['index'] = i
|
||||||
data['filename'] = accum[0].strip()
|
data['filename'] = " ".join(fields[4:])
|
||||||
info = re_spaces.split(accum[1].strip())
|
data['size'] = int(fields[1])
|
||||||
data['size'] = int(info[0])
|
attr = fields[0]
|
||||||
attr = info[5]
|
|
||||||
data['isdir'] = 'd' in attr.lower()
|
data['isdir'] = 'd' in attr.lower()
|
||||||
data['datetime'] = time.strptime(info[3] + " " + info[4], '%d-%m-%y %H:%M')
|
data['datetime'] = time.strptime(fields[2]+" "+fields[3], '%d-%m-%y %H:%M')
|
||||||
data['comment'] = None
|
data['comment'] = None
|
||||||
yield data
|
yield data
|
||||||
accum = []
|
|
||||||
i += 1
|
i += 1
|
||||||
line = source.next()
|
line = source.next()
|
||||||
|
|
||||||
|
|
||||||
def read_files(self, checker):
|
def read_files(self, checker):
|
||||||
res = []
|
res = []
|
||||||
for info in self.infoiter():
|
for info in self.infoiter():
|
||||||
checkres = checker(info)
|
checkres = checker(info)
|
||||||
if checkres == True and not info.isdir:
|
if checkres==True and not info.isdir:
|
||||||
pipe = self.call('p', ['inul'], [info.filename]).stdout
|
pipe = self.call('p', ['inul'], [info.filename]).stdout
|
||||||
res.append((info, pipe.read()))
|
res.append((info, pipe.read()))
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def extract(self, checker, path, withSubpath, overwrite):
|
def extract(self, checker, path, withSubpath, overwrite):
|
||||||
res = []
|
res = []
|
||||||
command = 'x'
|
command = 'x'
|
||||||
@@ -151,7 +194,7 @@ class RarFileImplementation(object):
|
|||||||
if overwrite:
|
if overwrite:
|
||||||
options.append('o+')
|
options.append('o+')
|
||||||
else:
|
else:
|
||||||
options.append('o-')
|
options.append('o-')
|
||||||
if not path.endswith(os.sep):
|
if not path.endswith(os.sep):
|
||||||
path += os.sep
|
path += os.sep
|
||||||
names = []
|
names = []
|
||||||
@@ -159,17 +202,17 @@ class RarFileImplementation(object):
|
|||||||
checkres = checker(info)
|
checkres = checker(info)
|
||||||
if type(checkres) in [str, unicode]:
|
if type(checkres) in [str, unicode]:
|
||||||
raise NotImplementedError("Condition callbacks returning strings are deprecated and only supported in Windows")
|
raise NotImplementedError("Condition callbacks returning strings are deprecated and only supported in Windows")
|
||||||
if checkres == True and not info.isdir:
|
if checkres==True and not info.isdir:
|
||||||
names.append(info.filename)
|
names.append(info.filename)
|
||||||
res.append(info)
|
res.append(info)
|
||||||
names.append(path)
|
names.append(path)
|
||||||
proc = self.call(command, options, names)
|
proc = self.call(command, options, names)
|
||||||
stdoutdata, stderrdata = proc.communicate()
|
stdoutdata, stderrdata = proc.communicate()
|
||||||
if stderrdata.find("CRC failed") >= 0:
|
if stderrdata.find("CRC failed")>=0 or stderrdata.find("Checksum error")>=0:
|
||||||
raise IncorrectRARPassword
|
raise IncorrectRARPassword
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def destruct(self):
|
def destruct(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
Reference in new issue
Block a user