This class emulates the 'flock' command.
| 19 | |
| 20 | |
| 21 | class FlockTool: |
| 22 | """This class emulates the 'flock' command.""" |
| 23 | |
| 24 | def Dispatch(self, args): |
| 25 | """Dispatches a string command to a method.""" |
| 26 | if len(args) < 1: |
| 27 | raise Exception("Not enough arguments") |
| 28 | |
| 29 | method = "Exec%s" % self._CommandifyName(args[0]) |
| 30 | getattr(self, method)(*args[1:]) |
| 31 | |
| 32 | def _CommandifyName(self, name_string): |
| 33 | """Transforms a tool name like copy-info-plist to CopyInfoPlist""" |
| 34 | return name_string.title().replace("-", "") |
| 35 | |
| 36 | def ExecFlock(self, lockfile, *cmd_list): |
| 37 | """Emulates the most basic behavior of Linux's flock(1).""" |
| 38 | # Rely on exception handling to report errors. |
| 39 | # Note that the stock python on SunOS has a bug |
| 40 | # where fcntl.flock(fd, LOCK_EX) always fails |
| 41 | # with EBADF, that's why we use this F_SETLK |
| 42 | # hack instead. |
| 43 | fd = os.open(lockfile, os.O_WRONLY | os.O_NOCTTY | os.O_CREAT, 0o666) |
| 44 | if sys.platform.startswith("aix") or sys.platform == "os400": |
| 45 | # Python on AIX is compiled with LARGEFILE support, which changes the |
| 46 | # struct size. |
| 47 | op = struct.pack("hhIllqq", fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0) |
| 48 | else: |
| 49 | op = struct.pack("hhllhhl", fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0) |
| 50 | fcntl.fcntl(fd, fcntl.F_SETLK, op) |
| 51 | return subprocess.call(cmd_list) |
| 52 | |
| 53 | |
| 54 | if __name__ == "__main__": |