Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | # ex:ts=4:sw=4:sts=4:et |
| 4 | # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- |
| 5 | # |
| 6 | # Copyright (C) 2015 Alexandru Damian for Intel Corp. |
| 7 | # |
| 8 | # This program is free software; you can redistribute it and/or modify |
| 9 | # it under the terms of the GNU General Public License version 2 as |
| 10 | # published by the Free Software Foundation. |
| 11 | # |
| 12 | # This program is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License along |
| 18 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | |
| 21 | # Utilities shared by tests and other common bits of code. |
| 22 | |
| 23 | import sys, os, subprocess, fcntl, errno |
| 24 | import config |
| 25 | from config import logger |
| 26 | |
| 27 | |
| 28 | # License warning; this code is copied from the BitBake project, file bitbake/lib/bb/utils.py |
| 29 | # The code is originally licensed GPL-2.0, and we redistribute it under still GPL-2.0 |
| 30 | |
| 31 | # End of copy is marked with #ENDOFCOPY marker |
| 32 | |
| 33 | def mkdirhier(directory): |
| 34 | """Create a directory like 'mkdir -p', but does not complain if |
| 35 | directory already exists like os.makedirs |
| 36 | """ |
| 37 | |
| 38 | try: |
| 39 | os.makedirs(directory) |
| 40 | except OSError as exc: |
| 41 | if exc.errno != errno.EEXIST: |
| 42 | raise exc |
| 43 | |
| 44 | def lockfile(name, shared=False, retry=True): |
| 45 | """ |
| 46 | Use the file fn as a lock file, return when the lock has been acquired. |
| 47 | Returns a variable to pass to unlockfile(). |
| 48 | """ |
| 49 | config.logger.debug("take lockfile %s", name) |
| 50 | dirname = os.path.dirname(name) |
| 51 | mkdirhier(dirname) |
| 52 | |
| 53 | if not os.access(dirname, os.W_OK): |
| 54 | logger.error("Unable to acquire lock '%s', directory is not writable", |
| 55 | name) |
| 56 | sys.exit(1) |
| 57 | |
| 58 | operation = fcntl.LOCK_EX |
| 59 | if shared: |
| 60 | operation = fcntl.LOCK_SH |
| 61 | if not retry: |
| 62 | operation = operation | fcntl.LOCK_NB |
| 63 | |
| 64 | while True: |
| 65 | # If we leave the lockfiles lying around there is no problem |
| 66 | # but we should clean up after ourselves. This gives potential |
| 67 | # for races though. To work around this, when we acquire the lock |
| 68 | # we check the file we locked was still the lock file on disk. |
| 69 | # by comparing inode numbers. If they don't match or the lockfile |
| 70 | # no longer exists, we start again. |
| 71 | |
| 72 | # This implementation is unfair since the last person to request the |
| 73 | # lock is the most likely to win it. |
| 74 | |
| 75 | # pylint: disable=broad-except |
| 76 | # we disable the broad-except because we want to actually catch all possible exceptions |
| 77 | try: |
| 78 | lock_file = open(name, 'a+') |
| 79 | fileno = lock_file.fileno() |
| 80 | fcntl.flock(fileno, operation) |
| 81 | statinfo = os.fstat(fileno) |
| 82 | if os.path.exists(lock_file.name): |
| 83 | statinfo2 = os.stat(lock_file.name) |
| 84 | if statinfo.st_ino == statinfo2.st_ino: |
| 85 | return lock_file |
| 86 | lock_file.close() |
| 87 | except Exception as exc: |
| 88 | try: |
| 89 | lock_file.close() |
| 90 | except Exception as exc2: |
| 91 | config.logger.error("Failed to close the lockfile: %s", exc2) |
| 92 | config.logger.error("Failed to acquire the lockfile: %s", exc) |
| 93 | if not retry: |
| 94 | return None |
| 95 | |
| 96 | def unlockfile(lock_file): |
| 97 | """ |
| 98 | Unlock a file locked using lockfile() |
| 99 | """ |
| 100 | try: |
| 101 | # If we had a shared lock, we need to promote to exclusive before |
| 102 | # removing the lockfile. Attempt this, ignore failures. |
| 103 | fcntl.flock(lock_file.fileno(), fcntl.LOCK_EX|fcntl.LOCK_NB) |
| 104 | os.unlink(lock_file.name) |
| 105 | except (IOError, OSError): |
| 106 | pass |
| 107 | fcntl.flock(lock_file.fileno(), fcntl.LOCK_UN) |
| 108 | lock_file.close() |
| 109 | |
| 110 | #ENDOFCOPY |
| 111 | |
| 112 | |
| 113 | def mk_lock_filename(): |
| 114 | our_name = os.path.basename(__file__) |
| 115 | our_name = ".%s" % ".".join(reversed(our_name.split("."))) |
| 116 | return config.LOCKFILE + our_name |
| 117 | |
| 118 | |
| 119 | |
| 120 | class ShellCmdException(Exception): |
| 121 | pass |
| 122 | |
| 123 | def run_shell_cmd(command, cwd=None): |
| 124 | if cwd is None: |
| 125 | cwd = os.getcwd() |
| 126 | |
| 127 | config.logger.debug("_shellcmd: (%s) %s", cwd, command) |
| 128 | process = subprocess.Popen(command, cwd=cwd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 129 | (out, err) = process.communicate() |
| 130 | process.wait() |
| 131 | if process.returncode: |
| 132 | if len(err) == 0: |
| 133 | err = "command: %s \n%s" % (command, out) |
| 134 | else: |
| 135 | err = "command: %s \n%s" % (command, err) |
| 136 | config.logger.warn("_shellcmd: error \n%s\n%s", out, err) |
| 137 | raise ShellCmdException(err) |
| 138 | else: |
| 139 | #config.logger.debug("localhostbecontroller: shellcmd success\n%s" % out) |
| 140 | return out |
| 141 | |