PoserPlainZipExtrator.py
1    from __future__ import print_function
2    from zipfile import ZipFile, is_zipfile
3    from tempfile import gettempdir
4    from shutil import copyfileobj
5    
6    import os, re
7    import wx
8    
9    try:
10       import poser
11   except ImportError:
12       from PoserLibs import POSER_FAKE as poser
13   
14   SCENE = poser.Scene()
15   RUNTIME = poser.Libraries()
16   TEMPPATH = os.path.join(gettempdir(), "TEMP_ZIP")
17   ZIP_EXTRACTION_DIR = globals().setdefault("ZIP_EXTRACTION_DIR", TEMPPATH)
18   ZIP_FOLDER = globals().setdefault("ZIP_FOLDER", "")
19   
20   TO_CORRECT = "runtime libraries geometries scenes textures " \
21                "camera character collections face hair hand light materials pose".split()
22   
23   
24   def extract_from_zip(zip2workwith, install_path=TEMPPATH, to_correct=TO_CORRECT):
25       overwrite = None
26   
27       install_path_list = re.split("[\\/:]+", install_path)
28       if is_zipfile(zip2workwith):
29           with ZipFile(zip2workwith, "r") as zfile:
30               for info in (inf for inf in zfile.infolist() if inf.file_size > 0):
31                   names = old = re.split(r"[\\/:]+", info.filename)
32                   for idx, n in enumerate(names):
33                       if n.lower() in to_correct:
34                           names[idx] = n.capitalize()
35                   fname = "/".join(install_path_list + names)
36                   path, filename = fname.rsplit("/", 1)
37                   if not os.path.exists(path):
38                       try:
39                           os.makedirs(path)
40                       except Exception:
41                           raise ("Can't create Path '%s'" % path)
42   
43                   if not os.path.isdir(path):
44                       raise IOError("'%s' exists, but is not a directory." % path)
45   
46                   if os.path.exists(fname):
47                       if overwrite is None:
48                           with wx.MessageDialog(None, message="Overwrite existing files?",
49                                                 caption="File '%s' exists" % filename,
50                                                 style=wx.YES_NO | wx.ICON_WARNING) as dlg:
51                               overwrite = dlg.ShowModal() == wx.OK
52                       if not overwrite:
53                           continue
54   
55                   with zfile.open(info.filename, "r") as z_in:
56                       with open(fname, "w") as z_out:
57                           copyfileobj(z_in, z_out)
58   
59   
60   if __name__ == "__main__":
61       aborted = False
62       with wx.FileDialog(None, "Select a zip file to install",
63                          wildcard="ZIP files (*.zip)|*.zip|All files (*)|*",
64                          defaultDir=ZIP_FOLDER,
65                          style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as dlg:
66           if dlg.ShowModal() == wx.ID_OK:
67               path = dlg.GetPath()
68   
69               with wx.SingleChoiceDialog(None,
70                                          message="Path to install to",
71                                          caption="Select install path for '%s'" % os.path.basename(path),
72                                          choices=poser.Libraries() + [ZIP_EXTRACTION_DIR,
73                                                                       "[Select a path]"]) \
74                       as dlg:
75                   if dlg.ShowModal() == wx.ID_OK:
76                       install_path = dlg.GetStringSelection()
77                       if install_path[0] == "[":
78                           install_path = wx.DirSelector(message="Select a path to install to")
79                       if install_path is not None:
80                           extract_from_zip(path, install_path)
81                           ZIP_EXTRACTION_DIR = install_path
82                           with wx.MessageDialog(None,
83                                                 message="To path '%s'" % install_path,
84                                                 caption="Zip Extracted",
85                                                 style=wx.ICON_INFORMATION) as dlg:
86                               dlg.ShowModal()
87                       else:
88                           aborted = True
89                   else:
90                       aborted = True
91               ZIP_FOLDER = os.path.dirname(path)
92           else:
93               aborted = True
94   
95       if aborted:
96           with wx.MessageDialog(None,
97                                 message="Aborted.",
98                                 caption="Zip Extraction",
99                                 style=wx.ICON_HAND) as dlg:
100              dlg.ShowModal()
101