| 1 |
|
| 2 |
import py |
| 3 |
import sys, os, atexit |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
def make_numbered_dir(prefix='session-', rootdir=None, keep=3, |
| 11 |
lock_timeout = 172800, |
| 12 |
min_timeout = 300): |
| 13 |
""" return unique directory with a number greater than the current |
| 14 |
maximum one. The number is assumed to start directly after prefix. |
| 15 |
if keep is true directories with a number less than (maxnum-keep) |
| 16 |
will be removed. |
| 17 |
""" |
| 18 |
if rootdir is None: |
| 19 |
rootdir = py.path.local.get_temproot() |
| 20 |
|
| 21 |
def parse_num(path): |
| 22 |
""" parse the number out of a path (if it matches the prefix) """ |
| 23 |
bn = path.basename |
| 24 |
if bn.startswith(prefix): |
| 25 |
try: |
| 26 |
return int(bn[len(prefix):]) |
| 27 |
except ValueError: |
| 28 |
pass |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
lastmax = None |
| 33 |
while True: |
| 34 |
maxnum = -1 |
| 35 |
for path in rootdir.listdir(): |
| 36 |
num = parse_num(path) |
| 37 |
if num is not None: |
| 38 |
maxnum = max(maxnum, num) |
| 39 |
|
| 40 |
|
| 41 |
try: |
| 42 |
udir = rootdir.mkdir(prefix + str(maxnum+1)) |
| 43 |
except py.error.EEXIST: |
| 44 |
|
| 45 |
|
| 46 |
if lastmax == maxnum: |
| 47 |
raise |
| 48 |
lastmax = maxnum |
| 49 |
continue |
| 50 |
break |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
if lock_timeout: |
| 55 |
lockfile = udir.join('.lock') |
| 56 |
mypid = os.getpid() |
| 57 |
if hasattr(lockfile, 'mksymlinkto'): |
| 58 |
lockfile.mksymlinkto(str(mypid)) |
| 59 |
else: |
| 60 |
lockfile.write(str(mypid)) |
| 61 |
def try_remove_lockfile(): |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
if os.getpid() != mypid: |
| 68 |
return |
| 69 |
try: |
| 70 |
lockfile.remove() |
| 71 |
except py.error.Error: |
| 72 |
pass |
| 73 |
atexit.register(try_remove_lockfile) |
| 74 |
|
| 75 |
|
| 76 |
if keep: |
| 77 |
for path in rootdir.listdir(): |
| 78 |
num = parse_num(path) |
| 79 |
if num is not None and num <= (maxnum - keep): |
| 80 |
if min_timeout: |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
try: |
| 88 |
t1 = path.lstat().mtime |
| 89 |
t2 = lockfile.lstat().mtime |
| 90 |
if abs(t2-t1) < min_timeout: |
| 91 |
continue |
| 92 |
except py.error.Error: |
| 93 |
continue |
| 94 |
lf = path.join('.lock') |
| 95 |
try: |
| 96 |
t1 = lf.lstat().mtime |
| 97 |
t2 = lockfile.lstat().mtime |
| 98 |
if not lock_timeout or abs(t2-t1) < lock_timeout: |
| 99 |
continue |
| 100 |
except py.error.Error: |
| 101 |
pass |
| 102 |
try: |
| 103 |
path.remove(rec=1) |
| 104 |
except KeyboardInterrupt: |
| 105 |
raise |
| 106 |
except: |
| 107 |
pass |
| 108 |
|
| 109 |
|
| 110 |
try: |
| 111 |
username = os.environ['USER'] |
| 112 |
except KeyError: |
| 113 |
try: |
| 114 |
username = os.environ['USERNAME'] |
| 115 |
except KeyError: |
| 116 |
username = 'current' |
| 117 |
|
| 118 |
src = str(udir) |
| 119 |
dest = src[:src.rfind('-')] + '-' + username |
| 120 |
try: |
| 121 |
os.unlink(dest) |
| 122 |
except OSError: |
| 123 |
pass |
| 124 |
try: |
| 125 |
os.symlink(src, dest) |
| 126 |
except (OSError, AttributeError, NotImplementedError): |
| 127 |
pass |
| 128 |
|
| 129 |
return udir |
| 130 |
|
| 131 |
|
| 132 |
udir = make_numbered_dir(prefix = 'ffi-') |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
if sys.platform == 'win32': |
| 138 |
try: |
| 139 |
import setuptools |
| 140 |
except ImportError: |
| 141 |
pass |