Python shutil.make_archive() Examples

The following are 30 code examples of shutil.make_archive(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module shutil , or try the search function .
Example #1
Source File: make_package.py    From gltf-blender-importer with MIT License 8 votes vote down vote up
def make_package(suffix=None):
    this_dir = os.path.dirname(os.path.abspath(__file__))
    dist_dir = os.path.join(this_dir, 'dist')

    if not os.path.exists(dist_dir):
        os.makedirs(dist_dir)

    with tempfile.TemporaryDirectory() as tmpdir:
        shutil.copytree(
            os.path.join(this_dir, 'addons', 'io_scene_gltf_ksons'),
            os.path.join(tmpdir, 'io_scene_gltf_ksons'),
            ignore=shutil.ignore_patterns('__pycache__'))

        zip_name = 'io_scene_gltf_ksons'
        if suffix:
            zip_name += '-' + suffix

        shutil.make_archive(
            os.path.join('dist', zip_name),
            'zip',
            tmpdir) 
Example #2
Source File: make-windows-release.py    From superpaper with MIT License 6 votes vote down vote up
def make_portable(dst_path):
    portpath = os.path.join(dst_path, "superpaper-portable")
    portres = os.path.join(portpath, "superpaper/resources")
    portprof = os.path.join(portpath, "profiles")
    portexec = os.path.join(portpath, "superpaper")
    # copy resources
    copy_tree(os.path.join(SRCPATH, "resources"), portres)
    # copy profiles
    copy_tree(os.path.join(SRCPATH, "profiles-win"), portprof)
    # copy exe-less structure to be used by innosetup
    copy_tree(portpath, INNO_STUB)

    # copy executable
    shutil.copy2("./dist/superpaper.exe", portexec)
    # zip it
    shutil.make_archive(portpath, 'zip', dst_path, "superpaper-portable") 
Example #3
Source File: export_favorites.py    From FontSelector_blender_addon with GNU General Public License v3.0 6 votes vote down vote up
def fontselector_export_favorites(filepath, export_mode, zip, favorites_list, fake_user_list, context) :
    export_path = absolute_path(filepath)
    
    # create folder
    if not os.path.isdir(export_path) :
        os.makedirs(export_path)
    
    # copy fonts
    if export_mode in {"BOTH", "FAVORITES"} :
        for filepath in favorites_list :
            newpath = os.path.join(export_path, os.path.basename(filepath))
            shutil.copy2(filepath, newpath)
            shutil.copystat(filepath, newpath)
    if export_mode in {"BOTH", "FAKE_USER"} :
        for filepath in fake_user_list :
            newpath = os.path.join(export_path, os.path.basename(filepath))
            shutil.copy2(filepath, newpath)
            shutil.copystat(filepath, newpath)

    # create zip archive
    if zip :
        shutil.make_archive(export_path, 'zip', export_path)
        shutil.rmtree(export_path)
 
    return {'FINISHED'} 
Example #4
Source File: cifar.py    From rafiki with Apache License 2.0 6 votes vote down vote up
def _write_dataset(images, labels, label_to_index, out_dataset_path, limit):
    if limit is not None:
        print('Limiting dataset to {} samples...'.format(limit))
        images = images[:limit]
        labels = labels[:limit]

    with tempfile.TemporaryDirectory() as d:
        # Create images.csv in temp dir for dataset
        # For each (image, label), save image as .png and add row to images.csv
        # Show a progress bar in the meantime
        images_csv_path = os.path.join(d, 'images.csv')
        n = len(images)
        with open(images_csv_path, mode='w') as f:
            writer = csv.DictWriter(f, fieldnames=['path', 'class'])
            writer.writeheader()
            for (i, image, label) in tqdm(zip(range(n), images, labels), total=n, unit='images'):
                image_name = '{}-{}.png'.format(label, i)
                image_path = os.path.join(d, image_name)
                pil_image = Image.fromarray(image, mode='RGB')
                pil_image.save(image_path)
                writer.writerow({ 'path': image_name, 'class': label_to_index[label] })

        # Zip and export folder as dataset
        out_path = shutil.make_archive(out_dataset_path, 'zip', d)
        os.rename(out_path, out_dataset_path) # Remove additional trailing `.zip` 
Example #5
Source File: load_folder_format.py    From rafiki with Apache License 2.0 6 votes vote down vote up
def _write_dataset(pil_images, labels, out_dataset_path):
    with tempfile.TemporaryDirectory() as d:
        # Create images.csv in temp dir for dataset
        # For each (image, label), save image as .png and add row to images.csv
        # Show a progress bar in the meantime
        images_csv_path = os.path.join(d, 'images.csv')
        n = len(pil_images)
        with open(images_csv_path, mode='w') as f:
            writer = csv.DictWriter(f, fieldnames=['path', 'class'])
            writer.writeheader()
            for (i, pil_image, label) in tqdm(zip(range(n), pil_images, labels), total=n, unit='images'):
                image_name = '{}-{}.png'.format(label, i)
                image_path = os.path.join(d, image_name)
                pil_image.save(image_path)
                writer.writerow({ 'path': image_name, 'class': label })

        # Zip and export folder as dataset
        out_path = shutil.make_archive(out_dataset_path, 'zip', d)
        os.rename(out_path, out_dataset_path) # Remove additional trailing `.zip` 
Example #6
Source File: open_nmt.py    From chimera with MIT License 6 votes vote down vote up
def pre_process(self):
        save_data = temp_dir()

        train_src, train_tgt = self.train_data
        dev_src, dev_tgt = self.dev_data

        if self.features:
            train_src = list(map(add_features, train_src))
            dev_src = list(map(add_features, dev_src))

        run_param('preprocess.py', {
            "train_src": save_temp(train_src),
            "train_tgt": save_temp(train_tgt),
            "valid_src": save_temp(dev_src),
            "valid_tgt": save_temp(dev_tgt),
            "save_data": save_data + "data",
            "dynamic_dict": None  # This will add a dynamic-dict parameter
        })

        data_zip = shutil.make_archive(base_name=temp_name(), format="gztar", root_dir=save_data)

        f = open(data_zip, "rb")
        bin_data = f.read()
        f.close()
        return bin_data 
Example #7
Source File: test_getstash.py    From stash with MIT License 6 votes vote down vote up
def create_stash_zipfile(self):
        """
        Create a github-like zipfile from this source and return the path.
        :return: path to zipfile
        :rtype: str
        """
        tp = self.get_new_tempdir(create=True)
        toplevel_name = "stash-testing"
        toplevel = os.path.join(tp, toplevel_name)
        zipname = "{}.zip".format(toplevel)
        zippath = os.path.join(tp, zipname)
        zippath_wo_ext = os.path.splitext(zippath)[0]
        sourcepath = self.get_source_path()
        shutil.copytree(sourcepath, toplevel)
        shutil.make_archive(zippath_wo_ext, "zip", tp, toplevel_name)
        return zippath 
Example #8
Source File: dicom_anonymizer_methods.py    From DICAT with GNU General Public License v3.0 6 votes vote down vote up
def zip_dicom(directory):
    """
    Function that zip a directory.

    :param directory: path to the directory to zip
     :type directory: str

    :return: archive -> path to the created zip file
     :rtype: str

    """

    archive = directory + '.zip'

    if (os.listdir(directory) == []):
        sys.exit(
            "The directory " + directory + " is empty and will not be zipped.")
    else:
        shutil.make_archive(directory, 'zip', directory)

    if (os.path.exists(archive)):
        shutil.rmtree(directory)
        return archive
    else:
        sys.exit(archive + " could not be created.") 
Example #9
Source File: package.py    From tcex with Apache License 2.0 6 votes vote down vote up
def zip_file(self, app_path, app_name, tmp_path):
        """Zip the App with tcex extension.

        Args:
            app_path (str): The path of the current project.
            app_name (str): The name of the App.
            tmp_path (str): The temp output path for the zip.
        """
        # zip build directory
        zip_file = os.path.join(app_path, self.args.outdir, app_name)
        zip_file_zip = f'{zip_file}.zip'
        zip_file_tcx = f'{zip_file}.tcx'
        shutil.make_archive(zip_file, 'zip', tmp_path, app_name)
        shutil.move(zip_file_zip, zip_file_tcx)
        self._app_packages.append(zip_file_tcx)
        # update package data
        self.package_data['package'].append({'action': 'App Package:', 'output': zip_file_tcx}) 
Example #10
Source File: IOUevaluater.py    From ScanSSD with MIT License 6 votes vote down vote up
def archive_iou_txt(username, task_id, sub_id,userpath):

    inputdir=os.path.join(userpath,'iouEval_stats')
    
    if not os.path.exists(inputdir):
        print('No txt file is generated for IOU evaluation')
        pass
    
    dest_uploader = 'IOU_stats_archive'
    dest_uploader = os.path.join(userpath, dest_uploader)

    if not os.path.exists(dest_uploader):
        os.makedirs(dest_uploader)

    zip_file_name = '/' + task_id + '_' + sub_id
    shutil.make_archive(dest_uploader + zip_file_name, 'zip', inputdir)

    # return '/media/' + dest_uploader 
Example #11
Source File: test_context.py    From pfio with MIT License 6 votes vote down vote up
def test_fs_detection_on_container_posix(self):
        # Create a container for testing
        zip_file_name = "test"
        zip_file_path = zip_file_name + ".zip"
        posix_file_path = "file://" + zip_file_path

        # in the zip, the leading slash will be removed
        file_name_zip = self.tmpfile_path.lstrip('/')

        shutil.make_archive(zip_file_name, "zip", base_dir=self.tmpdir.name)

        with pfio.open_as_container(posix_file_path) as container:
            with container.open(file_name_zip, "r") as f:
                self.assertEqual(
                    f.read(), self.test_string_str)

        pfio.remove(zip_file_path) 
Example #12
Source File: mobi.py    From Lector with GNU General Public License v3.0 6 votes vote down vote up
def read_book(self):
        with HidePrinting():
            KindleUnpack.unpackBook(self.filename, self.extract_path)

        epub_filename = os.path.splitext(
            os.path.basename(self.filename))[0] + '.epub'
        self.epub_filepath = os.path.join(
            self.extract_path, 'mobi8', epub_filename)

        if not os.path.exists(self.epub_filepath):
            zip_dir = os.path.join(self.extract_path, 'mobi7')
            zip_file = os.path.join(
                self.extract_path, epub_filename)
            self.epub_filepath = shutil.make_archive(zip_file, 'zip', zip_dir)

        self.book = EPUB(self.epub_filepath, self.temp_dir) 
Example #13
Source File: gftools-qa.py    From gftools with Apache License 2.0 6 votes vote down vote up
def post_to_github(self, url):
        """Zip and post the check results as a comment to the github
        issue or pr."""
        report_zip = shutil.make_archive(self.out, "zip", self.out)
        uuid = str(uuid4())
        zip_url = self._post_media_to_gfr([report_zip], uuid)

        url_split = url.split("/")
        repo_slug = "{}/{}".format(url_split[3], url_split[4])
        pull = url_split[-1] if "pull" in url else None

        fontbakery_report = os.path.join(self.out, "Fontbakery", "report.md")
        if os.path.isfile(fontbakery_report):
            with open(fontbakery_report, "r") as fb:
                msg = "{}\n\n## Diff images: [{}]({})".format(
                    fb.read(), os.path.basename(zip_url[0]), zip_url[0]
                )
        else:
            msg = "## Diff images: [{}]({})".format(
                os.path.basename(zip_url[0]), zip_url[0]
            )
        self._post_gh_msg(msg, repo_slug, pull) 
Example #14
Source File: processing_unit.py    From snips-nlu with Apache License 2.0 6 votes vote down vote up
def to_byte_array(self):
        """Serialize the :class:`ProcessingUnit` instance into a bytearray

        This method persists the processing unit in a temporary directory, zip
        the directory and return the zipped file as binary data.

        Returns:
            bytearray: the processing unit as bytearray data
        """
        cleaned_unit_name = _sanitize_unit_name(self.unit_name)
        with temp_dir() as tmp_dir:
            processing_unit_dir = tmp_dir / cleaned_unit_name
            self.persist(processing_unit_dir)
            archive_base_name = tmp_dir / cleaned_unit_name
            archive_name = archive_base_name.with_suffix(".zip")
            shutil.make_archive(
                base_name=str(archive_base_name), format="zip",
                root_dir=str(tmp_dir), base_dir=cleaned_unit_name)
            with archive_name.open(mode="rb") as f:
                processing_unit_bytes = bytearray(f.read())
        return processing_unit_bytes 
Example #15
Source File: dapp_browser.py    From shadowlands with MIT License 5 votes vote down vote up
def _create_archive(self):
        dapp_path = Path(self.dapp.config._sl_dapp_path).joinpath(self.dapp.dapp_name)
        # Remove all cached bytecode, leaving only the code
        pycaches = dapp_path.glob("**/__pycache__")
        for cache in pycaches:
            shutil.rmtree(str(cache))

        archive_path = Path("/tmp").joinpath(self.dapp.dapp_name)
        shutil.make_archive(str(archive_path), 'zip',  self.dapp.config._sl_dapp_path, self.dapp.dapp_name)
        self.dapp.digest = filehasher(str(archive_path)+".zip")   
        self.dapp.add_sl_frame(AskClipboardFrame(self.dapp, 3, 65, title="Archive is in /tmp.  Copy Sha256 digest to clipboard?")) 
Example #16
Source File: WakewordManager.py    From ProjectAlice with GNU General Public License v3.0 5 votes vote down vote up
def _prepareHotword(self, path: Path) -> tuple:
		wakewordName = path.name
		zipPath = path.parent / (wakewordName + '.zip')

		self.logInfo(f'Cleaning up {wakewordName}')
		if zipPath.exists():
			zipPath.unlink()

		self.logInfo(f'Packing wakeword {wakewordName}')
		shutil.make_archive(base_name=zipPath.with_suffix(''), format='zip', root_dir=str(path))

		return wakewordName, zipPath 
Example #17
Source File: submaker_updatezip.py    From inception with GNU General Public License v3.0 5 votes vote down vote up
def make(self, updatePkgDir):
        keys_name = self.getValue("keys")
        signingKeys = self.getMaker().getConfig().getKeyConfig(keys_name) if keys_name else None
        updateBinaryKey, updateBinary = self.getTargetBinary("update-binary")
        assert updateBinary, "%s is not set" % updateBinaryKey

        if keys_name:
            assert signingKeys, "update.keys is '%s' but __config__.host.keys.%s is not set" % (keys_name, keys_name)
            signingKeys = signingKeys["private"], signingKeys["public"]

        shutil.copy(updateBinary, os.path.join(updatePkgDir, "META-INF/com/google/android/update-binary"))
        updateZipPath = updatePkgDir + "/../"
        updateZipPath += "update_unsigned" if signingKeys else "update"
        shutil.make_archive(updateZipPath, "zip", updatePkgDir)
        updateZipPath += ".zip"

        if signingKeys:
            javaKey, javaPath = self.getHostBinary("java")
            signApkKey, signApkPath = self.getHostBinary("signapk")

            assert signApkPath, "%s is not set" % signApkKey

            assert os.path.exists(signApkPath), "'%s' from %s does not exist" % (signApkPath, signApkKey)
            assert os.path.exists(javaPath), "'%s' from %s does not exist" % (javaPath, javaKey)

            signApk = SignApk(javaPath, signApkPath)
            targetPath =  updatePkgDir + "/../" + InceptionConstants.OUT_NAME_UPDATE
            signApk.sign(updateZipPath, targetPath, signingKeys[0], signingKeys[1])
            updateZipPath = targetPath

        return updateZipPath 
Example #18
Source File: ddc_v3_unbiased.py    From Deep-Drug-Coder with MIT License 5 votes vote down vote up
def save(self, model_name):
        """
        Save model in a zip file.
        """

        with tempfile.TemporaryDirectory() as dirpath:

            # Save the Keras model
            self.model.save(dirpath + "/model.h5")

            # Exclude unpicklable and unwanted attributes
            excl_attr = [
                "_DDC__mode",  # excluded because it is always identified within self.__init__()
                "_DDC__train_gen",  # unpicklable
                "_DDC__valid_gen",  # unpicklable
                "_DDC__sample_model",  # unpicklable
                "_DDC__multi_sample_model",  # unpicklable
                "_DDC__model",
            ]  # unpicklable

            # Cannot deepcopy self.__dict__ because of Keras' thread lock so this is
            # bypassed by popping and re-inserting the unpicklable attributes
            to_add = {}
            # Remove unpicklable attributes
            for attr in excl_attr:
                to_add[attr] = self.__dict__.pop(attr, None)

            # Pickle metadata, i.e. almost everything but the Keras models and generators
            pickle.dump(self.__dict__, open(dirpath + "/metadata.pickle", "wb"))

            # Zip directory with its contents
            shutil.make_archive(model_name, "zip", dirpath)

            # Finally, re-load the popped elements for the model to be usable
            for attr in excl_attr:
                self.__dict__[attr] = to_add[attr]

            print("Model saved.") 
Example #19
Source File: repackage.py    From apk-channelization with MIT License 5 votes vote down vote up
def package_channel_apk(raw_axml_data, channel, raw_filename, out, temp):
    newapk_name = raw_filename+'-'+channel+'-unsigned'
    newapk = os.path.join(out, newapk_name+'.apk')
    if os.path.isfile(newapk):
        os.remove(newapk) # remove old apk
    print 'creating unsigned apk :', newapk
    # clone a new buffer
    cloned_buffer = bytearray(len(raw_axml_data))
    cloned_buffer[:] = raw_axml_data
    replace_axml_string(cloned_buffer, _CHANNEL_PLACE_HOLDER, channel)
    temp_manifest = os.path.join(temp, _ANDROID_MANIFEST_XML)
    with open(temp_manifest, 'wb') as f:
        #print 'writing channel %s to AndroidManifest.xml' %channel
        f.write(cloned_buffer)
    temp_raw = os.path.join(temp, 'res/raw')
    if os.path.exists(temp_raw):
    	shutil.move(temp_raw, out)
    tempzip_name = os.path.join(out, newapk_name)
    tempzip = tempzip_name+'.zip'
    if os.path.exists(tempzip):
       os.remove(tempzip)
    #print 'creating channel archive', tempzip
    shutil.make_archive(tempzip_name, 'zip', temp)
    out_raw = os.path.join(out, 'raw')
    mZipFile = zipfile.ZipFile(tempzip, "a")
    for file in os.listdir(out_raw):
		full_path = os.path.join(out_raw, file);
		if os.path.isfile(full_path):
			mZipFile.write(full_path, "res\\raw\\" + file, zipfile.ZIP_STORED )
    mZipFile.close()
    os.rename(tempzip, newapk)
    #print 'renamed to ', newapk
    return newapk 
Example #20
Source File: files.py    From janeway with GNU Affero General Public License v3.0 5 votes vote down vote up
def zip_article_files(files, article_folders=False):
    """
    Zips up files that are related to an article.
    :param files: A list or queryset of File objects that have article_ids
    :param article_folders: Boolean, if true splits files into folders with
    article name.
    :return: strings path of the zip file, zip file name
    """
    file_name = '{0}.zip'.format(uuid4())

    # Copy files into a temp dir
    _dir = os.path.join(settings.BASE_DIR, 'files/temp', str(uuid4()))
    os.makedirs(_dir, 0o775)

    for file in files:
        if file.article_id:
            if article_folders:
                folder_name = '{id} - {title}'.format(id=file.article_id, title=strip_tags(file.article.title))
                article_dir = os.path.join(_dir, folder_name)
                if not os.path.exists(article_dir):
                    os.makedirs(article_dir, 0o775)
                shutil.copy(file.self_article_path(), article_dir)
            else:
                shutil.copy(file.self_article_path(), _dir)

    zip_path = '{dir}.zip'.format(dir=_dir)

    shutil.make_archive(_dir, 'zip', _dir)
    shutil.rmtree(_dir)
    return zip_path, file_name 
Example #21
Source File: node_control_tool.py    From indy-node with Apache License 2.0 5 votes vote down vote up
def _create_backup(self, src_ver: str):
        logger.debug('Creating backup for {}'.format(src_ver))
        shutil.make_archive(self._backup_name(src_ver),
                            self.backup_format, self.backup_target) 
Example #22
Source File: zip_client.py    From gokart with MIT License 5 votes vote down vote up
def make_archive(self) -> None:
        pass 
Example #23
Source File: upload_manager.py    From cloudify-manager with Apache License 2.0 5 votes vote down vote up
def _prepare_and_process_doc(self,
                                 file_server_root,
                                 archive_target_path,
                                 **kwargs):
        plugins = []
        caravan_ = kwargs['caravan_instance']
        for wgn_path, _ in caravan_:
            files_dir = os.path.dirname(wgn_path)
            archive_path = shutil.make_archive(
                os.path.join(caravan_.root_dir, os.path.basename(files_dir)),
                'zip',
                files_dir)

            try:
                new_plugin, _ = \
                    super(UploadedCaravanManager,
                          self)._prepare_and_process_doc(
                        str(uuid.uuid4()),
                        file_server_root,
                        archive_path,
                        **kwargs
                    )
                plugins.append((new_plugin, files_dir))
            except manager_exceptions.ConflictError:
                pass

        return plugins 
Example #24
Source File: zip_client.py    From gokart with MIT License 5 votes vote down vote up
def make_archive(self) -> None:
        [base, extension] = os.path.splitext(self._file_path)
        shutil.make_archive(base_name=base, format=extension[1:], root_dir=self._temporary_directory) 
Example #25
Source File: utils.py    From rasa_core with Apache License 2.0 5 votes vote down vote up
def zip_folder(folder):
    """Create an archive from a folder."""
    import shutil

    zipped_path = tempfile.NamedTemporaryFile(delete=False)
    zipped_path.close()

    # WARN: not thread save!
    return shutil.make_archive(zipped_path.name, str("zip"), folder) 
Example #26
Source File: gcs_zip_client.py    From gokart with MIT License 5 votes vote down vote up
def make_archive(self) -> None:
        extension = os.path.splitext(self._file_path)[1]
        shutil.make_archive(base_name=self._temporary_directory, format=extension[1:], root_dir=self._temporary_directory)
        self._client.put(self._temporary_file_path(), self._file_path) 
Example #27
Source File: file_interface.py    From piqa with Apache License 2.0 5 votes vote down vote up
def archive(self):
        if self._mode == 'embed' or self._mode == 'embed_context':
            shutil.make_archive(self._context_emb_dir, 'zip', self._context_emb_dir)
            shutil.rmtree(self._context_emb_dir)

        if self._mode == 'embed' or self._mode == 'embed_question':
            shutil.make_archive(self._question_emb_dir, 'zip', self._question_emb_dir)
            shutil.rmtree(self._question_emb_dir) 
Example #28
Source File: s3_zip_client.py    From gokart with MIT License 5 votes vote down vote up
def make_archive(self) -> None:
        extension = os.path.splitext(self._file_path)[1]
        shutil.make_archive(base_name=self._temporary_directory, format=extension[1:], root_dir=self._temporary_directory)
        self._client.put(self._temporary_file_path(), self._file_path) 
Example #29
Source File: build.py    From gauge-python with MIT License 5 votes vote down vote up
def create_zip():
    wd = cwd
    if os.path.exists(DEPLOY):
        shutil.rmtree(DEPLOY)
    copy_files(wd)
    output_file = PLUGIN_FILE_TEMPLATE.format(get_version())
    shutil.make_archive(output_file, ZIP, DEPLOY)
    shutil.rmtree(DEPLOY)
    if os.path.exists(BIN):
        shutil.rmtree(BIN)
    os.mkdir(BIN)
    plugin_zip = '{0}.zip'.format(output_file)
    shutil.move(plugin_zip, BIN)
    print('Zip file created.')
    return plugin_zip 
Example #30
Source File: crawler.py    From spidy with GNU General Public License v3.0 5 votes vote down vote up
def zip_saved_files(out_file_name, directory):
    """
    Creates a .zip file in the current directory containing all contents of dir, then empties.
    """
    shutil.make_archive(str(out_file_name), 'zip', directory)  # Zips files
    shutil.rmtree(directory)  # Deletes folder
    makedirs(directory)  # Creates empty folder of same name
    write_log('SAVE', 'Zipped documents to {0}.zip'.format(out_file_name))


########
# INIT #
########