aboutsummaryrefslogtreecommitdiffstats
path: root/library/python/testing/swag/pathutil.py
blob: 3f7910eff3e9bf1a08e87559bfaa9efacaceb24d (plain) (blame)
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
import os
import tempfile


def get_valid_filename(filename, dirname):
    current_file, counter = filename, 0
    while os.path.exists(os.path.join(dirname, current_file)):
        current_file = "%s_%d" % (filename, counter)
        counter += 1
    valid_path = os.path.join(dirname, current_file)
    os.mknod(valid_path)
    return valid_path


def get_valid_tmpdir(name, tmp_dir):
    current_dir, counter = name, 0
    while os.path.exists(os.path.join(tmp_dir, current_dir)):
        current_dir = "%s_%d" % (name, counter)
        counter += 1
    os.mkdir(os.path.join(tmp_dir, current_dir))
    return os.path.join(tmp_dir, current_dir)


def get_base_tmpdir(name):
    tmppath = tempfile.gettempdir()
    return get_valid_tmpdir(name, tmppath)