Python shutil.make_archive() Examples

The following are 30 code examples for showing how to use shutil.make_archive(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module shutil , or try the search function .

Example 1
Project: gftools   Author: googlefonts   File: gftools-qa.py    License: 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 2
Project: gltf-blender-importer   Author: ksons   File: make_package.py    License: MIT License 6 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 3
Project: Lector   Author: BasioMeusPuga   File: mobi.py    License: 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 4
Project: ScanSSD   Author: MaliParag   File: IOUevaluater.py    License: 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 5
Project: tcex   Author: ThreatConnect-Inc   File: package.py    License: 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 6
Project: DICAT   Author: aces   File: dicom_anonymizer_methods.py    License: 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 7
Project: chimera   Author: AmitMY   File: open_nmt.py    License: 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 8
Project: FontSelector_blender_addon   Author: samytichadou   File: export_favorites.py    License: 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 9
Project: pfio   Author: pfnet   File: test_context.py    License: 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 10
Project: superpaper   Author: hhannine   File: make-windows-release.py    License: 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 11
Project: rafiki   Author: nginyc   File: cifar.py    License: 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 12
Project: rafiki   Author: nginyc   File: load_folder_format.py    License: 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 13
Project: stash   Author: ywangd   File: test_getstash.py    License: 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 14
Project: snips-nlu   Author: snipsco   File: processing_unit.py    License: 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
Project: SecPi   Author: SecPi   File: worker.py    License: GNU General Public License v3.0 5 votes vote down vote up
def prepare_data(self):
		try:
			if os.listdir(self.data_directory): # check if there are any files available
				shutil.make_archive("%s/%s" % (self.zip_directory, config.get('pi_id')), "zip", self.data_directory)
				logging.info("Created ZIP file")
				return True
			else:
				logging.info("No data to zip")
				return False
		except OSError as oe:
			self.post_err("Pi with id '%s' wasn't able to prepare data for manager:\n%s" % (config.get('pi_id'), oe))
			logging.error("Wasn't able to prepare data for manager: %s" % oe)

	# Remove all the data that was created during the alarm, unlink == remove 
Example 16
Project: firmanal   Author: kyechou   File: extractor.py    License: MIT License 5 votes vote down vote up
def _check_rootfs(self):
        """
        If this file contains a known filesystem type, extract it.
        """

        if not self.get_rootfs_status():
            for module in binwalk.scan(self.item, "-e", "-r", "-y",
                                       "filesystem", signature=True,
                                       quiet=True):
                for entry in module.results:
                    self.printf(">>>> %s" % entry.description)
                    break

                if module.extractor.directory:
                    unix = Extractor.io_find_rootfs(module.extractor.directory)

                    if not unix[0]:
                        self.printf(">>>> Extraction failed!")
                        return False

                    self.printf(">>>> Found Linux filesystem in %s!" % unix[1])
                    if self.output:
                        shutil.make_archive(self.output, "gztar",
                                            root_dir=unix[1])
                    else:
                        self.extractor.do_rootfs = False
                    return True
        return False 
Example 17
Project: ConvLab   Author: ConvLab   File: analysis.py    License: MIT License 5 votes vote down vote up
def analyze_trial(trial_spec, session_metrics_list):
    '''Analyze trial and save data, then return metrics'''
    info_prepath = trial_spec['meta']['info_prepath']
    # calculate metrics
    trial_metrics = calc_trial_metrics(session_metrics_list, info_prepath)
    # plot graphs
    viz.plot_trial(trial_spec, trial_metrics)
    # zip files
    if util.get_lab_mode() == 'train':
        predir, _, _, _, _, _ = util.prepath_split(info_prepath)
        shutil.make_archive(predir, 'zip', predir)
        logger.info(f'All trial data zipped to {predir}.zip')
    return trial_metrics 
Example 18
Project: ConvLab   Author: ConvLab   File: analysis.py    License: MIT License 5 votes vote down vote up
def analyze_experiment(spec, trial_data_dict):
    '''Analyze experiment and save data'''
    info_prepath = spec['meta']['info_prepath']
    util.write(trial_data_dict, f'{info_prepath}_trial_data_dict.json')
    # calculate experiment df
    experiment_df = calc_experiment_df(trial_data_dict, info_prepath)
    # plot graph
    viz.plot_experiment(spec, experiment_df, METRICS_COLS)
    # zip files
    predir, _, _, _, _, _ = util.prepath_split(info_prepath)
    shutil.make_archive(predir, 'zip', predir)
    logger.info(f'All experiment data zipped to {predir}.zip')
    return experiment_df 
Example 19
Project: Radium   Author: mehulj94   File: Radiumkeylogger.py    License: Apache License 2.0 5 votes vote down vote up
def ZipAttachments(f_name):
    arch_name = "C:\Users\Public\Intel\Logs\\" + f_name + "Attachments"
    files = os.listdir(dir_zip)

    try:
        shutil.make_archive(arch_name, 'zip', dir_zip)
    except Exception as e:
        pass

    for j in range(len(files)):
        try:
            os.remove(dir_zip + "\\" + files[j])
        except Exception as e:
            print e

#Function to take screenshot 
Example 20
Project: dcc   Author: amimo   File: dcc.py    License: Apache License 2.0 5 votes vote down vote up
def archive_compiled_code(project_dir):
    outfile = make_temp_file('-dcc')
    outfile = shutil.make_archive(outfile, 'zip', project_dir)
    return outfile 
Example 21
Project: macro_pack   Author: sevagas   File: ppt_gen.py    License: Apache License 2.0 5 votes vote down vote up
def _injectCustomUi(self):
        customUIfile = utils.randomAlpha(8)+".xml" # Generally something like customUI.xml
        customUiContent = \
"""<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="AutoOpen" ></customUI>"""       
        relationShipContent =  \
"""<?xml version="1.0" encoding="UTF-8"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail" Target="docProps/thumbnail.jpeg"/><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="ppt/presentation.xml"/><Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/><Relationship Id="%s" Type="http://schemas.microsoft.com/office/2007/relationships/ui/extensibility" Target="/customUI/%s" /></Relationships>""" \
 % ("rId5", customUIfile)
        generatedFile = self.outputFilePath
        #  0 copy file to temp dir
        fileCopy = shutil.copy2(generatedFile, self.workingPath)
        # 1 extract zip file in temp working dir
        zipDir = os.path.join(self.workingPath, "zip")
        zipTest = ZipFile(fileCopy)
        zipTest.extractall(zipDir)
        # 2 Set customUi
        customUiDir = os.path.join(zipDir, "customUI")
        if not os.path.exists(customUiDir):
                os.makedirs(customUiDir)
        customUiFile =   os.path.join(customUiDir, customUIfile)      
        with open (customUiFile, "w") as f:
                f.write(customUiContent)
        # 3 Set relationships
        relsFile = os.path.join(zipDir, "_rels", ".rels")
        with open (relsFile, "w") as f:
            f.write(relationShipContent)
        # 3 Recreate archive
        shutil.make_archive(os.path.join(self.workingPath,"rezipped_archive"), format="zip", root_dir=os.path.join(self.workingPath, "zip")) 
        # 4 replace file
        os.remove(generatedFile)
        shutil.copy2(os.path.join(self.workingPath,"rezipped_archive.zip"), generatedFile) 
Example 22
Project: nodevectors   Author: VHRanger   File: embedders.py    License: MIT License 5 votes vote down vote up
def save(self, filename: str):
        """
        Saves model to a custom file format
        
        filename : str
            Name of file to save. Don't include filename extensions
            Extensions are added automatically
        
        File format is a zipfile with joblib dump (pickle-like) + dependency metata
        Metadata is checked on load.
        
        Includes validation and metadata to avoid Pickle deserialization gotchas
        See here Alex Gaynor PyCon 2014 talk "Pickles are for Delis"
            for more info on why we introduce this additional check
        """
        if '.zip' in filename:
            raise UserWarning("The file extension '.zip' is automatically added"
                + " to saved models. The name will have redundant extensions")
        sysverinfo = sys.version_info
        meta_data = {
            "python_": f'{sysverinfo[0]}.{sysverinfo[1]}',
            "skl_": sklearn.__version__[:-2],
            "pd_": pd.__version__[:-2],
            "csrg_": cg.__version__[:-2]
        }
        with tempfile.TemporaryDirectory() as temp_dir:
            joblib.dump(self, os.path.join(temp_dir, self.f_model), compress=True)
            with open(os.path.join(temp_dir, self.f_mdata), 'w') as f:
                json.dump(meta_data, f)
            filename = shutil.make_archive(filename, 'zip', temp_dir) 
Example 23
Project: readux   Author: ecds   File: export.py    License: MIT License 5 votes vote down vote up
def website_zip(self):
        '''Package up a Jekyll site created by :meth:`website` as a zip file
        for easy download.

        :return: :class:`tempfile.NamedTemporaryFile` temporary zip file
        '''
        export_dir = self.generate_website()

        # create a tempfile to hold a zip file of the site
        # (using tempfile for automatic cleanup after use)
        webzipfile = tempfile.NamedTemporaryFile(
            suffix='.zip',
            prefix='%s_annotated_site_' % self.manifest.id)
        shutil.make_archive(
            # name of the zipfile to create without .zip
            os.path.splitext(webzipfile.name)[0],
            'zip',  # archive format; could also do tar
            export_dir
        )
        logger.debug('Jekyll site web export zipfile for %s is %s',
                     self.manifest.id, webzipfile.name)
        # clean up temporary files
        shutil.rmtree(export_dir)
        # NOTE: method has to return the tempfile itself, or else it will
        # get cleaned up when the reference is destroyed
        return webzipfile 
Example 24
Project: a2ml   Author: augerai   File: local_fsclient.py    License: Apache License 2.0 5 votes vote down vote up
def archive_folder(self, path_src, format):
        shutil.make_archive(path_src, format, path_src) 
Example 25
Project: funfuzz   Author: MozillaSecurity   File: s3cache.py    License: Mozilla Public License 2.0 5 votes vote down vote up
def compressAndUploadDirTarball(self, directory, tarball_path):  # pylint: disable=invalid-name,missing-param-doc
        # pylint: disable=missing-type-doc
        """Compress a directory into a bz2 tarball and upload it to S3."""
        print("Creating archive...")
        shutil.make_archive(directory, "bztar", directory)
        self.uploadFileToS3(tarball_path) 
Example 26
Project: streamalert   Author: airbnb   File: package.py    License: Apache License 2.0 5 votes vote down vote up
def create(self):
        """Create a Lambda deployment package .zip file."""
        LOGGER.info('Creating package for %s', self.package_name)

        if os.path.exists(self.temp_package_path):
            shutil.rmtree(self.temp_package_path)

        # Copy all of the default package files
        self._copy_files(self.DEFAULT_PACKAGE_FILES)

        # Copy in any user-specified files
        self._copy_user_config_files()

        if not self._resolve_libraries():
            LOGGER.error('Failed to install necessary libraries')
            return False

        # Zip it all up
        # Build these in the top-level of the terraform directory as streamalert.zip
        result = shutil.make_archive(
            os.path.join(TERRAFORM_FILES_PATH, self.package_name),
            'zip',
            self.temp_package_path
        )

        LOGGER.info('Successfully created package: %s', result)

        # Remove temp files
        shutil.rmtree(self.temp_package_path)

        return result 
Example 27
Project: Montreal-Forced-Aligner   Author: MontrealCorpusTools   File: models.py    License: MIT License 5 votes vote down vote up
def dump(self, sink, archive_fmt=FORMAT):
        """
        Write archive to disk, and return the name of final archive
        """
        return make_archive(sink, archive_fmt,
                            *os.path.split(self.dirname)) 
Example 28
Project: bc18-scaffold   Author: battlecode   File: gui.py    License: MIT License 5 votes vote down vote up
def upload_scrim_server(return_args):
    cwd = os.getcwd()
    if 'NODOCKER' in os.environ:
        os.chdir('..')
    else:
        os.chdir('/player')
    os.chdir(return_args['file_name'])
    zip_file_name = os.path.abspath(os.path.join('../',
        return_args['file_name']))

    shutil.make_archive(zip_file_name, 'zip', '.')
    if not zip_file_name.endswith('.zip'):
        zip_file_name += '.zip'

    os.chdir(cwd)
    username = return_args['username']
    password = return_args['password']
    req = get_token(username, password)
    if req.status_code != 200:
        print("Error authenticating.")
        return "Error authenticating."

    token = json.loads(req.text)['access_token']
    headers = {}
    headers['Authorization'] = 'Bearer ' + token
    data = {}
    data['label'] = return_args['player']
    with open(zip_file_name, 'rb') as image_file:
        encoded_string = base64.b64encode(image_file.read())
    data['src'] = encoded_string
    res =  requests.post("https://battlecode.org/apis/submissions", headers=headers, data=data)
    return "success" 
Example 29
Project: oggm   Author: OGGM   File: test_utils.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def make_fake_zipdir(dir_path, fakefile=None, base_dir=None,
                     archiv='zip', extension='.zip'):
    """Creates a directory with a file in it if asked to, then compresses it"""
    utils.mkdir(dir_path)
    if fakefile:
        touch(os.path.join(dir_path, fakefile))
    shutil.make_archive(dir_path, archiv, dir_path, base_dir)
    return dir_path + extension 
Example 30
Project: torc   Author: rascal999   File: __init__.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def execute_tool(self, job, context):
        # Always check any tools provided by
        # community members
        print("Running")
        print("Execute string " + self.tool['execute_string'])
        proc = Popen(self.tool['execute_string'], stdout=PIPE, stderr=PIPE, shell=True)

        decode_locale = lambda s: s.decode(getlocale()[1])
        self.tool_stdout, self.tool_stderr = map(decode_locale, proc.communicate())

        # Callback / pause from here
        return_code = proc.returncode
        # Zip resulting directory - This is crap because when doing assessment
        # this will be run at each tool execution.
        zip_file = os.path.dirname(os.path.abspath(__file__)) + \
            "/" + job['target_name'] + '_' + str(job['id'])
        shutil.make_archive(zip_file, 'zip', job['output_dir'])
        # TODO This could be dodgy
        shutil.rmtree(job['output_dir'])

        # Update completed and return_code field in db
        con = sqlite3.connect(os.path.dirname(os.path.abspath(__file__)) + '/assets.db')
        cur = con.cursor()
        if context == 'tool':
            cur.execute("UPDATE tool_jobs SET executed = 1, return_code = ?, zip_file = ? WHERE id = ?",(str(return_code),str(zip_file),str(job['id'])))
            con.commit()
        if context == 'assessment':
            # Pull and check for 0
            cur.execute("SELECT return_code FROM assessment_jobs WHERE id = ? AND return_code == 0",(str(job['id'])))
            data = cur.fetchall()
            if len(data) == 0 or data[0][0] == 0:
                cur.execute("UPDATE assessment_jobs SET executed = 1, return_code = ?, zip_file = ? WHERE id = ?",(str(return_code),str(zip_file),str(job['id'])))
                con.commit()

        # Close connection
        if con:
            con.close()