summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKai Huuhko <kai.huuhko@gmail.com>2013-12-01 08:03:58 +0200
committerKai Huuhko <kai.huuhko@gmail.com>2013-12-01 08:03:58 +0200
commitcf60175c24936b4a04fe8c545b3c2c9ab5a97938 (patch)
treea25b6a1304767d5c024b9ae27559d1c6f2025824
Initial commit for python-unarHEADmaster
-rw-r--r--.gitignore25
-rwxr-xr-xUnAr.py117
-rwxr-xr-xtest.py19
3 files changed, 161 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..95a85b8
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,25 @@
1*.py[co]
2MANIFEST
3
4# Packages
5*.egg
6*.egg-info
7dist
8build
9eggs
10parts
11bin
12var
13sdist
14develop-eggs
15.installed.cfg
16
17# Installer logs
18pip-log.txt
19
20# Unit test / coverage reports
21.coverage
22.tox
23
24# Translations
25*.mo
diff --git a/UnAr.py b/UnAr.py
new file mode 100755
index 0000000..a6ba7a3
--- /dev/null
+++ b/UnAr.py
@@ -0,0 +1,117 @@
1import os
2import json
3import subprocess
4
5class Archive(object):
6
7 def __init__(self,
8 filename,
9 password=None,
10 encoding=None,
11 password_encoding=None,
12 no_recursion=False
13 ):
14
15 self.filename = filename
16 self.password = password
17 self.encoding = encoding
18 self.password_encoding = password_encoding
19
20 self.info = json.loads(subprocess.check_output(["lsar", "-j", filename]))
21
22 self.contents = []
23
24 if "lsarError" in self.info:
25 print("Error {} while opening the archive.".format(info["lsarError"]))
26 sys.exit()
27
28 for c in self.info["lsarContents"]:
29 if c.get("XADIsDirectory", None) == 1:
30 d = Directory(self, c)
31 self.contents.append(d)
32 else:
33 f = File(self, c)
34 self.contents.append(f)
35
36 def extract(self,
37 output_directory=None,
38 force_overwrite=False,
39 force_rename=False,
40 force_skip=False,
41 force_directory=False,
42 no_directory=False,
43 indexes=False,
44 no_recursion=False,
45 copy_time=False,
46 files=None,
47 ):
48
49 if isinstance(files, (str,unicode)):
50 files = [files,]
51 elif isinstance(files, int):
52 files = str(files)
53 files = [files,]
54
55 s = ["unar", ]
56 if output_directory is not None:
57 s.append("-output-directory")
58 s.append("{}".format(output_directory))
59 if force_overwrite:
60 s.append("-force-overwrite")
61 if force_rename:
62 s.append("-force-rename")
63 if force_skip:
64 s.append("force-skip")
65 if force_directory:
66 s.append("-force-directory")
67 if no_directory:
68 s.append("-no-directory")
69 if self.password is not None:
70 s.append("-password {}".format(self.password))
71 if self.encoding is not None:
72 s.append("-encoding {}".format(self.encoding))
73 if self.password_encoding is not None:
74 s.append("-password-encoding {}".format(self.password_encoding))
75 if indexes:
76 s.append("-indexes")
77 if no_recursion:
78 s.append("-no-recursion")
79 if copy_time:
80 s.append("-copy-time")
81 s.append(self.filename)
82 if files:
83 s += files
84
85 output = subprocess.call(s)
86
87class Directory(object):
88 def __init__(self, archive, handle):
89 self.archive = archive
90 self.handle = handle
91 self.idx = handle.get("XADIndex", 0)
92 self.name = handle.get("XADFileName", "")
93
94 def extract(self, *args, **kwargs):
95 self.archive.extract(*args, files=self.idx, indexes=True, **kwargs)
96
97class File(object):
98 def __init__(self, a, f):
99 self.archive = archive
100 self.handle = handle
101 self.idx = handle.get("XADIndex", 0)
102 self.name = handle.get("XADFileName", "")
103 self.size = handle.get("XADFileSize", 0)
104 self.compressed_size = handle.get("XADCompressedSize", 0)
105
106 def __enter__(self):
107 self.extract(output_directory="/tmp", force_overwrite=True, no_directory=True)
108 self.tmp_path = os.path.join("/tmp", self.name)
109 return self.tmp_path
110
111 def __exit__(self, exc_type, exc_val, exc_tb):
112 os.remove(self.tmp_path)
113 return False
114
115 def extract(self, *args, **kwargs):
116 self.archive.extract(*args, files=self.idx, indexes=True, **kwargs)
117
diff --git a/test.py b/test.py
new file mode 100755
index 0000000..bf4b0a3
--- /dev/null
+++ b/test.py
@@ -0,0 +1,19 @@
1#!/usr/bin/python
2
3import sys
4
5from UnAr import Archive, File
6
7if __name__ == "__main__":
8 if len(sys.argv) < 2:
9 print("No file specified.")
10 sys.exit()
11
12 cmd = " ".join(sys.argv[1:])
13
14 ar = Archive(cmd)
15 for f in ar.contents:
16 if isinstance(f, File) and f.name.endswith(".txt"):
17 with f as p:
18 with open(p, "r") as myfile:
19 print(myfile.read())