Python tempfile.mkdtemp() Examples

The following are 30 code examples of tempfile.mkdtemp(). 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 tempfile , or try the search function .
Example #1
Source File: demo.py    From svviz with MIT License 33 votes vote down vote up
def downloadDemo(which):
    try:
        downloadDir = tempfile.mkdtemp()
        archivePath = "{}/svviz-data.zip".format(downloadDir)

        # logging.info("Downloading...")
        downloadWithProgress("http://svviz.github.io/svviz/assets/examples/{}.zip".format(which), archivePath)
        
        logging.info("Decompressing...")
        archive = zipfile.ZipFile(archivePath)
        archive.extractall("{}".format(downloadDir))

        if not os.path.exists("svviz-examples"):
            os.makedirs("svviz-examples/")

        shutil.move("{temp}/{which}".format(temp=downloadDir, which=which), "svviz-examples/")
    except Exception as e:
        print("error downloading and decompressing example data: {}".format(e))
        return False

    if not os.path.exists("svviz-examples"):
        print("error finding example data after download and decompression")
        return False
    return True 
Example #2
Source File: test_config.py    From Paradrop with Apache License 2.0 6 votes vote down vote up
def test_revert_config():
    """
    Test the revertConfig function
    """
    from paradrop.core.config import osconfig

    # Need to make a writable location for our config files.
    settings.UCI_CONFIG_DIR = tempfile.mkdtemp()
    settings.UCI_BACKUP_DIR = tempfile.mkdtemp()

    update = UpdateObject({'name': 'test'})
    update.old = None
    update.new = MagicMock()

    osconfig.revertConfig(update, "network")

    # Clean up our config dir
    pdos.remove(settings.UCI_CONFIG_DIR)
    pdos.remove(settings.UCI_BACKUP_DIR) 
Example #3
Source File: export.py    From svviz with MIT License 6 votes vote down vote up
def convertSVG(insvg, outformat, converter):
    outdir = tempfile.mkdtemp()
    inpath = "{}/original.svg".format(outdir)
    infile = open(inpath, "w")
    infile.write(insvg)
    infile.flush()
    infile.close()

    outpath = "{}/converted.{}".format(outdir, outformat)

    if converter == "webkittopdf":
        exportData = _convertSVG_webkitToPDF(inpath, outpath, outformat)
    elif converter == "librsvg":
        exportData = _convertSVG_rsvg_convert(inpath, outpath, outformat)
    elif converter == "inkscape":
        exportData = _convertSVG_inkscape(inpath, outpath, outformat)

    return exportData 
Example #4
Source File: insertsizes.py    From svviz with MIT License 6 votes vote down vote up
def plotInsertSizeDistribution(isd, sampleName, dataHub):
    try:
        from rpy2 import robjects as ro

        d = tempfile.mkdtemp()
        filename = os.path.join(d, sampleName)

        if not filename.endswith(".png"):
            filename += ".png"

        ro.r.png(filename, res=250, width=1200, height=1200)

        alleles = ["alt", "ref", "amb"]
        others = [[len(chosenSet) for chosenSet in dataHub.samples[sampleName].chosenSets(allele)] for allele in alleles]
        plotting.ecdf([isd.insertSizes]+others, ["average"]+alleles, xlab="Insert size (bp)", main=sampleName, legendWhere="bottomright", lwd=2)
        
        ro.r["dev.off"]()

        data = open(filename).read()
        return data
    except ImportError:
        return None 
Example #5
Source File: lambda_function_builder.py    From sqs-s3-logger with Apache License 2.0 6 votes vote down vote up
def build_package():
    build_dir = tempfile.mkdtemp(prefix='lambda_package_')
    install_packages(build_dir, REQUIRED_PACKAGES)
    for f in REQUIRED_FILES:
        shutil.copyfile(
            src=os.path.join(module_path, f),
            dst=os.path.join(build_dir, f)
        )

    out_file = os.path.join(
        tempfile.mkdtemp(prefix='lambda_package_built'),
        'sqs_s3_logger_lambda_{}.zip'.format(datetime.datetime.now().isoformat())
    )
    LOGGER.info('Creating a function package file at {}'.format(out_file))

    archive(build_dir, out_file)
    return out_file 
Example #6
Source File: projectfilefolderhandle.py    From CAMISIM with Apache License 2.0 6 votes vote down vote up
def __init__(self, tmp_dir, output_dir, time_stamp=None, logfile=None, verbose=True, debug=False):
		"""
		Constructor

		@param tmp_dir: Directory for temporary data
		@type tmp_dir: str | unicode
		@param output_dir: Directory where final data will be placed
		@type output_dir: str | unicode
		@param time_stamp: timestamp as string
		@type time_stamp: str | unicode
		@param logfile: file | FileIO | StringIO | basestring
		@param verbose: Not verbose means that only warnings and errors will be past to stream
		@type verbose: bool
		@param debug: Display debug messages
		@type debug: bool
		"""
		assert isinstance(tmp_dir, basestring)
		assert isinstance(output_dir, basestring)
		assert time_stamp is None or isinstance(time_stamp, basestring)
		self._tmp_dir = tempfile.mkdtemp(dir=tmp_dir)
		self._directory_output = output_dir
		self._time_stamp = time_stamp
		if time_stamp is None:
			self._time_stamp = datetime.datetime.fromtimestamp(time.time()).strftime('%Y.%m.%d_%H.%M.%S')
		super(ProjectFileFolderHandle, self).__init__(logfile, verbose, debug) 
Example #7
Source File: generator_utils_test.py    From fine-lm with MIT License 6 votes vote down vote up
def testGetOrGenerateTxtVocab(self):
    data_dir = tempfile.mkdtemp(dir=self.get_temp_dir())
    test_file = os.path.join(self.get_temp_dir(), "test.txt")
    with tf.gfile.Open(test_file, "w") as outfile:
      outfile.write("a b c\n")
      outfile.write("d e f\n")
    # Create a vocab over the test file.
    vocab1 = generator_utils.get_or_generate_txt_vocab(
        data_dir, "test.voc", 20, test_file)
    self.assertTrue(tf.gfile.Exists(os.path.join(data_dir, "test.voc")))
    self.assertIsNotNone(vocab1)

    # Append a new line to the test file which would change the vocab if
    # the vocab were not being read from file.
    with tf.gfile.Open(test_file, "a") as outfile:
      outfile.write("g h i\n")
    vocab2 = generator_utils.get_or_generate_txt_vocab(
        data_dir, "test.voc", 20, test_file)
    self.assertTrue(tf.gfile.Exists(os.path.join(data_dir, "test.voc")))
    self.assertIsNotNone(vocab2)
    self.assertEqual(vocab1.dump(), vocab2.dump()) 
Example #8
Source File: validate_submission.py    From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def main(args):
  print_in_box('Validating submission ' + args.submission_filename)
  random.seed()
  temp_dir = args.temp_dir
  delete_temp_dir = False
  if not temp_dir:
    temp_dir = tempfile.mkdtemp()
    logging.info('Created temporary directory: %s', temp_dir)
    delete_temp_dir = True
  validator = validate_submission_lib.SubmissionValidator(temp_dir,
                                                          args.use_gpu)
  if validator.validate_submission(args.submission_filename,
                                   args.submission_type):
    print_in_box('Submission is VALID!')
  else:
    print_in_box('Submission is INVALID, see log messages for details')
  if delete_temp_dir:
    logging.info('Deleting temporary directory: %s', temp_dir)
    subprocess.call(['rm', '-rf', temp_dir]) 
Example #9
Source File: test_testing.py    From calmjs with GNU General Public License v2.0 5 votes vote down vote up
def test_mock_tempfile(self):
        mock_tempfile = MockTempfile()
        mock_tempfile.mkdtemp()
        self.assertEqual(mock_tempfile.count, 1)
        self.assertTrue(exists(mock_tempfile.dirs[0]))
        # If this is NOT true, we probably left tmpdirs around.
        self.assertTrue(mock_tempfile.dirs[0].startswith(self.tmpdir))
        mock_tempfile.cleanup()
        self.assertFalse(exists(mock_tempfile.dirs[0])) 
Example #10
Source File: tmpdirs.py    From delocate with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, suffix="", prefix=template, dir=None):
        self.name = mkdtemp(suffix, prefix, dir)
        self._closed = False 
Example #11
Source File: test_testing.py    From calmjs with GNU General Public License v2.0 5 votes vote down vote up
def test_mkdtemp_not_test(self):
        with self.assertRaises(TypeError):
            mkdtemp(object)
        self.assertEqual(self.mock_tempfile.count, 0) 
Example #12
Source File: test_testing.py    From calmjs with GNU General Public License v2.0 5 votes vote down vote up
def setUp(self):
        self.tmpdir = realpath(tempfile.mkdtemp())
        tempfile.tempdir = self.tmpdir 
Example #13
Source File: test_gluon_gpu.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 5 votes vote down vote up
def test_symbol_block_fp16():
    # Test case to verify if initializing the SymbolBlock from a model with params
    # other than fp32 param dtype.

    # 1. Load a resnet model, cast it to fp16 and export
    tmp = tempfile.mkdtemp()
    tmpfile = os.path.join(tmp, 'resnet34_fp16')
    ctx = mx.gpu(0)

    net_fp32 = mx.gluon.model_zoo.vision.resnet34_v2(pretrained=True, ctx=ctx, root=tmp)
    net_fp32.cast('float16')
    net_fp32.hybridize()
    data = mx.nd.zeros((1,3,224,224), dtype='float16', ctx=ctx)
    net_fp32.forward(data)
    net_fp32.export(tmpfile, 0)

    # 2. Load the saved model and verify if all the params are loaded correctly.
    # and choose one of the param to verify the type if fp16.
    sm = mx.sym.load(tmpfile + '-symbol.json')
    inputs = mx.sym.var('data', dtype='float16')
    net_fp16 = mx.gluon.SymbolBlock(sm, inputs)
    net_fp16.collect_params().load(tmpfile + '-0000.params', ctx=ctx)
    # 3. Get a conv layer's weight parameter name. Conv layer's weight param is
    # expected to be of dtype casted, fp16.
    for param_name in net_fp16.params.keys():
        if 'conv' in param_name and 'weight' in param_name:
            break
    assert np.dtype(net_fp16.params[param_name].dtype) == np.dtype(np.float16) 
Example #14
Source File: test_testing.py    From calmjs with GNU General Public License v2.0 5 votes vote down vote up
def setUp(self):
        # Set up the simple mock for counting the number of times the
        # mkdtemp call has been made from the testing utils module.
        self.mock_tempfile = MockTempfile()
        utils.tempfile, self.old_tempfile = self.mock_tempfile, utils.tempfile 
Example #15
Source File: test_testing.py    From calmjs with GNU General Public License v2.0 5 votes vote down vote up
def test_mkdtemp_clean_ups(self):
        target1 = mkdtemp(self)
        target2 = mkdtemp(self)
        self.assertTrue(exists(target1))
        self.assertTrue(exists(target2))
        self.assertNotEqual(target1, target2)
        self.doCleanups()
        self.assertFalse(exists(target1))
        self.assertFalse(exists(target2))
        self.assertEqual(self.mock_tempfile.count, 2) 
Example #16
Source File: test_testing.py    From calmjs with GNU General Public License v2.0 5 votes vote down vote up
def test_create_fake_bin(self):
        path = mkdtemp(self)
        program = utils.create_fake_bin(path, 'program')
        self.assertTrue(exists(program))
        self.assertIn('program', program)
        # Further, more actual testing will be done in test modules 
Example #17
Source File: test_testing.py    From calmjs with GNU General Public License v2.0 5 votes vote down vote up
def mkdtemp(self):
        self.count += 1
        result = realpath(tempfile.mkdtemp())
        self.dirs.append(result)
        return result 
Example #18
Source File: test_testing.py    From calmjs with GNU General Public License v2.0 5 votes vote down vote up
def test_mkdtemp_missing_addcleanup(self):
        # Quick and dirty subclassing for type signature and cleanup
        # availability sanity checks.
        FakeTestCase = type('FakeTestCase', (unittest.TestCase,), {
            'runTest': None,
            'addCleanup': None,
        })
        with self.assertRaises(TypeError):
            mkdtemp(FakeTestCase())

        self.assertEqual(self.mock_tempfile.count, 0) 
Example #19
Source File: spec_builder_test.py    From DOTA_models with Apache License 2.0 5 votes vote down vote up
def testFillsTaggerTransitions(self):
    lexicon_dir = tempfile.mkdtemp()

    def write_lines(filename, lines):
      with open(os.path.join(lexicon_dir, filename), 'w') as f:
        f.write(''.join('{}\n'.format(line) for line in lines))

    # Label map is required, even though it isn't used
    write_lines('label-map', ['0'])
    write_lines('word-map', ['2', 'miranda 1', 'rights 1'])
    write_lines('tag-map', ['2', 'NN 1', 'NNP 1'])
    write_lines('tag-to-category', ['NN\tNOUN', 'NNP\tNOUN'])

    tagger = spec_builder.ComponentSpecBuilder('tagger')
    tagger.set_network_unit(name='FeedForwardNetwork', hidden_layer_sizes='256')
    tagger.set_transition_system(name='tagger')
    tagger.add_fixed_feature(name='words', fml='input.word', embedding_dim=64)
    tagger.add_rnn_link(embedding_dim=-1)
    tagger.fill_from_resources(lexicon_dir)

    fixed_feature, = tagger.spec.fixed_feature
    linked_feature, = tagger.spec.linked_feature
    self.assertEqual(fixed_feature.vocabulary_size, 5)
    self.assertEqual(fixed_feature.size, 1)
    self.assertEqual(fixed_feature.size, 1)
    self.assertEqual(linked_feature.size, 1)
    self.assertEqual(tagger.spec.num_actions, 2) 
Example #20
Source File: common.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 5 votes vote down vote up
def __init__(self, suffix='', prefix='', dir=''):
            self._dirname = tempfile.mkdtemp(suffix, prefix, dir) 
Example #21
Source File: downloader.py    From Paradrop with Apache License 2.0 5 votes vote down vote up
def __enter__(self):
        self.workDir = tempfile.mkdtemp()
        return self 
Example #22
Source File: test_gluon_utils.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 5 votes vote down vote up
def test_download_successful():
    """ test download with one process """
    tmp = tempfile.mkdtemp()
    tmpfile = os.path.join(tmp, 'README.md')
    _download_successful(tmpfile)
    assert os.path.getsize(tmpfile) > 100, os.path.getsize(tmpfile)
    pattern = os.path.join(tmp, 'README.md*')
    # check only one file we want left
    assert len(glob.glob(pattern)) == 1, glob.glob(pattern)
    # delete temp dir
    shutil.rmtree(tmp) 
Example #23
Source File: test_contrib_svrg_module.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 5 votes vote down vote up
def test_module_save_load():
    import tempfile
    import os

    x = mx.sym.Variable("data")
    y = mx.sym.Variable("softmax_label")
    net = mx.sym.FullyConnected(x, y, num_hidden=1)

    mod = SVRGModule(symbol=net, data_names=['data'], label_names=['softmax_label'], update_freq=2)
    mod.bind(data_shapes=[('data', (1, 1))])
    mod.init_params()
    mod.init_optimizer(optimizer='sgd', optimizer_params={'learning_rate': 0.1})
    mod.update()

    # Create tempfile
    tmp = tempfile.mkdtemp()
    tmp_file = os.path.join(tmp, 'svrg_test_output')
    mod.save_checkpoint(tmp_file, 0, save_optimizer_states=True)

    mod2 = SVRGModule.load(tmp_file, 0, load_optimizer_states=True, data_names=('data', ))
    mod2.bind(data_shapes=[('data', (1, 1))])
    mod2.init_optimizer(optimizer_params={'learning_rate': 0.1})
    assert mod._symbol.tojson() == mod2._symbol.tojson()

    # Multi-device
    mod3 = SVRGModule(symbol=net, data_names=['data'], label_names=['softmax_label'], update_freq=3,
                     context=[mx.cpu(0), mx.cpu(1)])
    mod3.bind(data_shapes=[('data', (10, 10))])
    mod3.init_params()
    mod3.init_optimizer(optimizer_params={'learning_rate': 1.0})
    mod3.update()
    mod3.save_checkpoint(tmp_file, 0, save_optimizer_states=True)

    mod4 = SVRGModule.load(tmp_file, 0, load_optimizer_states=True, data_names=('data', ))
    mod4.bind(data_shapes=[('data', (10, 10))])
    mod4.init_optimizer(optimizer_params={'learning_rate': 1.0})
    assert mod3._symbol.tojson() == mod4._symbol.tojson() 
Example #24
Source File: test_test_utils.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 5 votes vote down vote up
def test_download_successful():
    tmp = tempfile.mkdtemp()
    tmpfile = os.path.join(tmp, 'README.md')
    mx.test_utils.download("https://raw.githubusercontent.com/apache/incubator-mxnet/master/README.md",
                           fname=tmpfile)
    assert os.path.getsize(tmpfile) > 100 
Example #25
Source File: common.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 5 votes vote down vote up
def __init__(self, suffix='', prefix='', dir=''):
            self._dirname = tempfile.mkdtemp(suffix, prefix, dir) 
Example #26
Source File: test_image.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 5 votes vote down vote up
def setupClass(cls):
        cls.IMAGES_DIR = tempfile.mkdtemp()
        cls.IMAGES = _get_data(cls.IMAGES_URL, cls.IMAGES_DIR)
        print("Loaded {} images".format(len(cls.IMAGES))) 
Example #27
Source File: core.py    From neuropythy with GNU Affero General Public License v3.0 5 votes vote down vote up
def cache_root(custom_directory):
        '''
        dataset.cache_root is the root directory in which the given dataset has been cached.
        '''
        if custom_directory is not None: return None
        elif config['data_cache_root'] is None:
            # we create a data-cache in a temporary directory
            path = tempfile.mkdtemp(prefix='npythy_data_cache_')
            if not os.path.isdir(path): raise ValueError('Could not find or create cache directory')
            config['data_cache_root'] = path
            atexit.register(shutil.rmtree, path)
        return config['data_cache_root'] 
Example #28
Source File: test_mtp.py    From mlearn with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def setUpClass(cls):
        cls.this_dir = os.path.dirname(os.path.abspath(__file__))
        cls.test_dir = tempfile.mkdtemp()
        os.chdir(cls.test_dir) 
Example #29
Source File: test_nnp.py    From mlearn with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def setUpClass(cls):
        cls.this_dir = os.path.dirname(os.path.abspath(__file__))
        cls.test_dir = tempfile.mkdtemp()
        os.chdir(cls.test_dir) 
Example #30
Source File: test_snap.py    From mlearn with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def setUpClass(cls):
        cls.this_dir = os.path.dirname(os.path.abspath(__file__))
        cls.test_dir = tempfile.mkdtemp()
        os.chdir(cls.test_dir)