devserver: enhance static subdirectory locking

Shift to using a dedicated lock file in each static subdirectory as an
indicator for exclusive access. This frees us from the implicit
semantics by which a lock on each subdirectory can be obtained at most
once, which is necessary in cases where we revisit a preexisting
directory and stage additional artifacts in it.

- The previous locking behavior is retained by default; a client must
  use an optional argument to AcquireLock() to indicate that the locked
  subdirectory may preexist.

- Uses lockfile.FileLock for the actual locking. This is deemed more
  straightforward and flexible than interprocess locking: we need not
  map resources (directories) to locks, and our implementation is not
  limited to a single multithreaded process.

- ReleaseLock() does not remove the locked subdirectory, unless
  explicitly instructed to. Added code for graceful (non-destructive)
  release in successful branches.

- Test coverage of the new locking semantics.

BUG=None
TEST=devserver prevents concurrent access to the same staging location;
unit tests pass.

Change-Id: Icb8a20d475251b114ba632a040a7815eca395912
Reviewed-on: https://gerrit.chromium.org/gerrit/33139
Reviewed-by: Chris Sosa <sosa@chromium.org>
Commit-Ready: Gilad Arnold <garnold@chromium.org>
Tested-by: Gilad Arnold <garnold@chromium.org>
diff --git a/downloader_unittest.py b/downloader_unittest.py
index 07261d8..56df93c 100755
--- a/downloader_unittest.py
+++ b/downloader_unittest.py
@@ -54,10 +54,13 @@
     self.mox.StubOutWithMock(devserver_util, 'ReleaseLock')
     self.mox.StubOutWithMock(tempfile, 'mkdtemp')
 
+    lock_tag = self._ClassUnderTest().GenerateLockTag(board, self.build)
     devserver_util.AcquireLock(
         static_dir=self._work_dir,
-        tag=self._ClassUnderTest().GenerateLockTag(board, self.build)
-        ).AndReturn(self._work_dir)
+        tag=lock_tag).AndReturn(self._work_dir)
+    devserver_util.ReleaseLock(
+        static_dir=self._work_dir,
+        tag=lock_tag)
 
     tempfile.mkdtemp(suffix=mox.IgnoreArg()).AndReturn(self._work_dir)
     return self._GenerateArtifacts(ignore_background)