aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/sqlalchemy_migrate-0.7.2-py2.7.egg/migrate/tests/fixture/pathed.py
blob: b36aa086687112dd4f2cc7ede4e0a04a3f85c7b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import shutil
import tempfile

from migrate.tests.fixture import base


class Pathed(base.Base):
    # Temporary files

    _tmpdir = tempfile.mkdtemp()

    def setUp(self):
        super(Pathed, self).setUp()
        self.temp_usable_dir = tempfile.mkdtemp()
        sys.path.append(self.temp_usable_dir)

    def tearDown(self):
        super(Pathed, self).tearDown()
        try:
            sys.path.remove(self.temp_usable_dir)
        except:
            pass # w00t?
        Pathed.purge(self.temp_usable_dir)

    @classmethod
    def _tmp(cls, prefix='', suffix=''):
        """Generate a temporary file name that doesn't exist
        All filenames are generated inside a temporary directory created by 
        tempfile.mkdtemp(); only the creating user has access to this directory. 
        It should be secure to return a nonexistant temp filename in this 
        directory, unless the user is messing with their own files. 
        """
        file, ret = tempfile.mkstemp(suffix,prefix,cls._tmpdir)
        os.close(file)
        os.remove(ret)
        return ret

    @classmethod
    def tmp(cls, *p, **k):
        return cls._tmp(*p, **k)
        
    @classmethod
    def tmp_py(cls, *p, **k):
        return cls._tmp(suffix='.py', *p, **k)

    @classmethod
    def tmp_sql(cls, *p, **k):
        return cls._tmp(suffix='.sql', *p, **k)

    @classmethod
    def tmp_named(cls, name):
        return os.path.join(cls._tmpdir, name)

    @classmethod
    def tmp_repos(cls, *p, **k):
        return cls._tmp(*p, **k)

    @classmethod
    def purge(cls, path):
        """Removes this path if it exists, in preparation for tests
        Careful - all tests should take place in /tmp. 
        We don't want to accidentally wipe stuff out...
        """
        if os.path.exists(path):
            if os.path.isdir(path):
                shutil.rmtree(path)
            else:
                os.remove(path)
                if path.endswith('.py'):
                    pyc = path + 'c'
                    if os.path.exists(pyc):
                        os.remove(pyc)