Debian Codegraph @

extra_tests/cffi_tests/udir.py

raw symbols SCIP
1 # Generated by pypy/tool/import_cffi.py
2 import py
3 import sys, os, atexit
4
5
6 # This is copied from PyPy's vendored py lib. The latest py lib release
7 # (1.8.1) contains a bug and crashes if it sees another temporary directory
8 # in which we don't have write permission (e.g. because it's owned by someone
9 # else).
10 def make_numbered_dir(prefix='session-', rootdir=None, keep=3,
11 lock_timeout = 172800, # two days
12 min_timeout = 300): # five minutes
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 # compute the maximum number currently in use with the
31 # prefix
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 # make the new directory
41 try:
42 udir = rootdir.mkdir(prefix + str(maxnum+1))
43 except py.error.EEXIST:
44 # race condition: another thread/process created the dir
45 # in the meantime. Try counting again
46 if lastmax == maxnum:
47 raise
48 lastmax = maxnum
49 continue
50 break
51
52 # put a .lock file in the new directory that will be removed at
53 # process exit
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 # in a fork() situation, only the last process should
63 # remove the .lock, otherwise the other processes run the
64 # risk of seeing their temporary dir disappear. For now
65 # we remove the .lock in the parent only (i.e. we assume
66 # that the children finish before the parent).
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 # prune old directories
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 # NB: doing this is needed to prevent (or reduce
82 # a lot the chance of) the following situation:
83 # 'keep+1' processes call make_numbered_dir() at
84 # the same time, they create dirs, but then the
85 # last process notices the first dir doesn't have
86 # (yet) a .lock in it and kills it.
87 try:
88 t1 = path.lstat().mtime
89 t2 = lockfile.lstat().mtime
90 if abs(t2-t1) < min_timeout:
91 continue # skip directories too recent
92 except py.error.Error:
93 continue # failure to get a time, better skip
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 # skip directories still locked
100 except py.error.Error:
101 pass # assume that it means that there is no 'lf'
102 try:
103 path.remove(rec=1)
104 except KeyboardInterrupt:
105 raise
106 except: # this might be py.error.Error, WindowsError ...
107 pass
108
109 # make link...
110 try:
111 username = os.environ['USER'] #linux, et al
112 except KeyError:
113 try:
114 username = os.environ['USERNAME'] #windows
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 # Windows-only workaround for some configurations: see
136 # https://bugs.python.org/issue23246 (Python 2.7.9)
137 if sys.platform == 'win32':
138 try:
139 import setuptools
140 except ImportError:
141 pass