1#
2#  This file is part of Bakefile (http://www.bakefile.org)
3#
4#  Copyright (C) 2003,2004,2008 Vaclav Slavik
5#
6#  Permission is hereby granted, free of charge, to any person obtaining a copy
7#  of this software and associated documentation files (the "Software"), to
8#  deal in the Software without restriction, including without limitation the
9#  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10#  sell copies of the Software, and to permit persons to whom the Software is
11#  furnished to do so, subject to the following conditions:
12#
13#  The above copyright notice and this permission notice shall be included in
14#  all copies or substantial portions of the Software.
15#
16#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21#  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22#  IN THE SOFTWARE.
23#
24#  $Id: portautils.py 1181 2008-01-20 18:23:05Z vaclavslavik $
25#
26#  Portable utilities for misc tasks
27#
28
29import os, tempfile
30
31#
32# Secure temporary file creation:
33#
34
35def mktemp(prefix):
36    """Uses tempfile.mkstemp() to atomically create named file, but works
37       only with Python 2.3+."""
38    handle, filename = tempfile.mkstemp(prefix=prefix)
39    os.close(handle)
40    return filename
41
42def mktempdir(prefix):
43    """Uses tempfile.mkdtemp() to atomically create named directory, but works
44       only with Python 2.3+."""
45    return tempfile.mkdtemp(prefix=prefix)
46
47
48#
49# Cross-platform file locking:
50# (based on portalocker Python Cookbook recipe
51# by John Nielsen <nielsenjf@my-deja.com>:
52# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65203
53#
54
55if os.name == 'nt':
56    import win32con
57    import win32file
58    import pywintypes
59    # is there any reason not to reuse the following structure?
60    __overlapped = pywintypes.OVERLAPPED()
61
62    def lock(file):
63        try:
64            hfile = win32file._get_osfhandle(file.fileno())
65            win32file.LockFileEx(hfile, win32con.LOCKFILE_EXCLUSIVE_LOCK,
66                                 0, 0x7fffffff, __overlapped)
67        except pywintypes.error, e:
68            # err 120 is unimplemented call, happens on win9x:
69            if e.args[0] != 120:
70                raise e
71
72    def unlock(file):
73        try:
74            hfile = win32file._get_osfhandle(file.fileno())
75            win32file.UnlockFileEx(hfile, 0, 0x7fffffff, __overlapped)
76        except pywintypes.error, e:
77            # err 120 is unimplemented call, happens on win9x:
78            if e.args[0] != 120:
79                raise e
80
81elif os.name == 'posix':
82    import fcntl
83
84    def lock(file):
85        fcntl.flock(file.fileno(), fcntl.LOCK_EX)
86
87    def unlock(file):
88        fcntl.flock(file.fileno(), fcntl.LOCK_UN)
89
90else:
91    def lock(file): pass
92    def unlock(file): pass
93