diff options
author | Richard Purdie <richard@openedhand.com> | 2007-11-24 14:44:36 +0000 |
---|---|---|
committer | Richard Purdie <richard@openedhand.com> | 2007-11-24 14:44:36 +0000 |
commit | 701aa74aeb2e06368a927b4b61dd02160066101a (patch) | |
tree | f5e0ffdd16f423883d2ab0e64632afa551b9d323 /bitbake/lib/bb/utils.py | |
parent | 4aaa8343bc1394d58ca6017d46bf11c1f151fae6 (diff) | |
download | poky-701aa74aeb2e06368a927b4b61dd02160066101a.tar.gz |
bitbake: Add bb.utils.lockfile() and bb.utils.unlockfile()
git-svn-id: https://svn.o-hand.com/repos/poky/trunk@3221 311d38ba-8fff-0310-9ca6-ca027cbcb966
Diffstat (limited to 'bitbake/lib/bb/utils.py')
-rw-r--r-- | bitbake/lib/bb/utils.py | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/bitbake/lib/bb/utils.py b/bitbake/lib/bb/utils.py index c2884f2633..c27dafd614 100644 --- a/bitbake/lib/bb/utils.py +++ b/bitbake/lib/bb/utils.py | |||
@@ -22,7 +22,7 @@ BitBake Utility Functions | |||
22 | digits = "0123456789" | 22 | digits = "0123456789" |
23 | ascii_letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | 23 | ascii_letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" |
24 | 24 | ||
25 | import re | 25 | import re, fcntl, os |
26 | 26 | ||
27 | def explode_version(s): | 27 | def explode_version(s): |
28 | r = [] | 28 | r = [] |
@@ -202,3 +202,38 @@ def Enum(*names): | |||
202 | constants = tuple(constants) | 202 | constants = tuple(constants) |
203 | EnumType = EnumClass() | 203 | EnumType = EnumClass() |
204 | return EnumType | 204 | return EnumType |
205 | |||
206 | def lockfile(name): | ||
207 | """ | ||
208 | Use the file fn as a lock file, return when the lock has been aquired. | ||
209 | Returns a variable to pass to unlockfile(). | ||
210 | """ | ||
211 | while True: | ||
212 | # If we leave the lockfiles lying around there is no problem | ||
213 | # but we should clean up after ourselves. This gives potential | ||
214 | # for races though. To work around this, when we aquire the lock | ||
215 | # we check the file we locked was still the lock file on disk. | ||
216 | # by comparing inode numbers. If they don't match or the lockfile | ||
217 | # no longer exists, we start again. | ||
218 | |||
219 | # This implementation is unfair since the last person to request the | ||
220 | # lock is the most likely to win it. | ||
221 | |||
222 | lf = open(name, "a+") | ||
223 | fcntl.flock(lf.fileno(), fcntl.LOCK_EX) | ||
224 | statinfo = os.fstat(lf.fileno()) | ||
225 | if os.path.exists(lf.name): | ||
226 | statinfo2 = os.stat(lf.name) | ||
227 | if statinfo.st_ino == statinfo2.st_ino: | ||
228 | return lf | ||
229 | # File no longer exists or changed, retry | ||
230 | lf.close | ||
231 | |||
232 | def unlockfile(lf): | ||
233 | """ | ||
234 | Unlock a file locked using lockfile() | ||
235 | """ | ||
236 | os.unlink(lf.name) | ||
237 | fcntl.flock(lf.fileno(), fcntl.LOCK_UN) | ||
238 | lf.close | ||
239 | |||