2017-06-05 16:02:39 +03:00
|
|
|
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
|
2015-11-18 17:16:17 +02:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2014-06-07 13:34:31 +03:00
|
|
|
|
2015-05-23 18:36:11 +03:00
|
|
|
from os import chmod
|
2014-06-07 13:34:31 +03:00
|
|
|
from os.path import join, splitext
|
|
|
|
from tarfile import open as tarfile_open
|
|
|
|
from time import mktime
|
|
|
|
from zipfile import ZipFile
|
|
|
|
|
2015-05-18 19:22:42 +03:00
|
|
|
import click
|
2014-06-07 13:34:31 +03:00
|
|
|
|
2016-01-24 16:45:04 +02:00
|
|
|
from platformio import app, util
|
2014-06-07 13:34:31 +03:00
|
|
|
from platformio.exception import UnsupportedArchiveType
|
|
|
|
|
|
|
|
|
|
|
|
class ArchiveBase(object):
|
|
|
|
|
|
|
|
def __init__(self, arhfileobj):
|
|
|
|
self._afo = arhfileobj
|
|
|
|
|
|
|
|
def get_items(self):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def extract_item(self, item, dest_dir):
|
|
|
|
self._afo.extract(item, dest_dir)
|
|
|
|
self.after_extract(item, dest_dir)
|
|
|
|
|
|
|
|
def after_extract(self, item, dest_dir):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class TARArchive(ArchiveBase):
|
|
|
|
|
|
|
|
def __init__(self, archpath):
|
|
|
|
ArchiveBase.__init__(self, tarfile_open(archpath))
|
|
|
|
|
|
|
|
def get_items(self):
|
|
|
|
return self._afo.getmembers()
|
|
|
|
|
|
|
|
|
|
|
|
class ZIPArchive(ArchiveBase):
|
|
|
|
|
|
|
|
def __init__(self, archpath):
|
|
|
|
ArchiveBase.__init__(self, ZipFile(archpath))
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def preserve_permissions(item, dest_dir):
|
|
|
|
attrs = item.external_attr >> 16L
|
|
|
|
if attrs:
|
|
|
|
chmod(join(dest_dir, item.filename), attrs)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def preserve_mtime(item, dest_dir):
|
2015-05-23 18:36:11 +03:00
|
|
|
util.change_filemtime(
|
2014-06-07 13:34:31 +03:00
|
|
|
join(dest_dir, item.filename),
|
2016-08-03 23:38:20 +03:00
|
|
|
mktime(list(item.date_time) + [0] * 3))
|
2014-06-07 13:34:31 +03:00
|
|
|
|
|
|
|
def get_items(self):
|
|
|
|
return self._afo.infolist()
|
|
|
|
|
|
|
|
def after_extract(self, item, dest_dir):
|
|
|
|
self.preserve_permissions(item, dest_dir)
|
|
|
|
self.preserve_mtime(item, dest_dir)
|
|
|
|
|
|
|
|
|
|
|
|
class FileUnpacker(object):
|
|
|
|
|
|
|
|
def __init__(self, archpath, dest_dir="."):
|
|
|
|
self._archpath = archpath
|
|
|
|
self._dest_dir = dest_dir
|
|
|
|
self._unpacker = None
|
|
|
|
|
|
|
|
_, archext = splitext(archpath.lower())
|
|
|
|
if archext in (".gz", ".bz2"):
|
|
|
|
self._unpacker = TARArchive(archpath)
|
|
|
|
elif archext == ".zip":
|
|
|
|
self._unpacker = ZIPArchive(archpath)
|
|
|
|
|
|
|
|
if not self._unpacker:
|
|
|
|
raise UnsupportedArchiveType(archpath)
|
|
|
|
|
|
|
|
def start(self):
|
2016-01-24 16:45:04 +02:00
|
|
|
if app.is_disabled_progressbar():
|
2015-05-18 19:22:42 +03:00
|
|
|
click.echo("Unpacking...")
|
|
|
|
for item in self._unpacker.get_items():
|
2014-06-07 13:34:31 +03:00
|
|
|
self._unpacker.extract_item(item, self._dest_dir)
|
2015-05-18 19:22:42 +03:00
|
|
|
else:
|
2016-08-03 23:38:20 +03:00
|
|
|
items = self._unpacker.get_items()
|
|
|
|
with click.progressbar(items, label="Unpacking") as pb:
|
2015-05-18 19:22:42 +03:00
|
|
|
for item in pb:
|
|
|
|
self._unpacker.extract_item(item, self._dest_dir)
|
2014-06-07 13:34:31 +03:00
|
|
|
return True
|