factory: fix unit tests

Fix some unit tests in src/platform/factory.
Not fixed:
  py/dkps/dkps_unittest.py
    This test takes 850 seconds, but `make test` only waits for 300s.
  py/umpire/service/overlord_unittest.py
    You need to `sudo emerge chromeos-factory-overlord` first.

BUG=chromium:555350
TEST=make test # py/dkps/dkps_unittest.py won't pass

Change-Id: I9356b24a3eb4339622aaf606011bac31d2c97c04
Reviewed-on: https://chromium-review.googlesource.com/312178
Commit-Ready: Wei-Han Chen <stimim@chromium.org>
Tested-by: Wei-Han Chen <stimim@chromium.org>
Reviewed-by: Hung-Te Lin <hungte@chromium.org>
diff --git a/py/dkps/dkps_unittest.py b/py/dkps/dkps_unittest.py
index 42e47d8..1cfc783 100755
--- a/py/dkps/dkps_unittest.py
+++ b/py/dkps/dkps_unittest.py
@@ -1,3 +1,4 @@
+#!/usr/bin/python
 # Copyright 2015 The Chromium OS Authors. All rights reserved.
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
diff --git a/py/dkps/parsers/widevine_parser_unittest.py b/py/dkps/parsers/widevine_parser_unittest.py
old mode 100644
new mode 100755
index 94ea922..be78fc5
--- a/py/dkps/parsers/widevine_parser_unittest.py
+++ b/py/dkps/parsers/widevine_parser_unittest.py
@@ -1,3 +1,4 @@
+#!/usr/bin/python
 # Copyright 2015 The Chromium OS Authors. All rights reserved.
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
diff --git a/py/test/pytests/spatial_sensor_calibration.py b/py/test/pytests/spatial_sensor_calibration.py
index 6fc8883..3068ee2 100644
--- a/py/test/pytests/spatial_sensor_calibration.py
+++ b/py/test/pytests/spatial_sensor_calibration.py
@@ -10,16 +10,20 @@
 gyroscope.
 
 The step for calibration is as follows:
-1 - Put the device on a flat table, facing up.
-2 - Issue a command to calibrate them:
-  echo 1 > /sys/bus/iio/devices/iio:deviceX/calibrate
-  X being the ids of the accel and gyro.
-3 - Retrieve the calibration offsets
-  cat /sys/bus/iio/devices/iio:deviceX/in_(accel|gyro)_(x|y|z)_calibbias
-4 - Save them in VPD.
+1) Put the device on a flat table, facing up.
+
+2) Issue a command to calibrate them:
+
+  - echo 1 > /sys/bus/iio/devices/iio:deviceX/calibrate
+  - X being the ids of the accel and gyro.
+
+3) Retrieve the calibration offsets
+
+  - cat /sys/bus/iio/devices/iio:deviceX/in_(accel|gyro)_(x|y|z)_calibbias
+
+4) Save them in VPD.
 """
 
-import os
 import threading
 import time
 import unittest
@@ -131,12 +135,12 @@
       if value <= _range[0] or value >= _range[1]:
         factory.console.error(
             'Device not in correct position: %s-axis value: %d. '
-             'Valid range (%d, %d)' % (axis, value, _range[0], _range[1]))
+            'Valid range (%d, %d)', axis, value, _range[0], _range[1])
         raise InvalidPositionError
 
   def EnableAutoCalibration(self, path):
     RETRIES = 5
-    for i in range(RETRIES):
+    for unused_i in range(RETRIES):
       try:
         self.dut.Write(self.dut.path.join(path, 'calibrate'), '1')
       except Exception:
@@ -145,7 +149,7 @@
       else:
         break
     else:
-        raise RuntimeError('calibrate activation failed')
+      raise RuntimeError('calibrate activation failed')
     time.sleep(self.args.stabilize_time)
 
   def RetrieveCalibbiasAndWriteVPD(self):
diff --git a/py/test/run_pytest.py b/py/test/run_pytest.py
index cd64b6e..8b5c459 100755
--- a/py/test/run_pytest.py
+++ b/py/test/run_pytest.py
@@ -197,9 +197,8 @@
                            args=args,
                            dut_options=dut_options,
                            use_goofy=cli_args.use_goofy)
-
   # Exit code and error message.
-  sys.exit(error_msg)  # uses exit code 0 when error_msg == None
+  sys.exit(error_msg or None)
 
 
 if __name__ == '__main__':
diff --git a/py/tools/make_par_unittest.py b/py/tools/make_par_unittest.py
index b632322..2f209be 100755
--- a/py/tools/make_par_unittest.py
+++ b/py/tools/make_par_unittest.py
@@ -39,7 +39,7 @@
                                      (1, 'raise ValueError')):
       self.assertEquals(
           expected_retcode,
-          Spawn(command + ['execpython', '--args', 'dict(script=%r)' % script],
+          Spawn(command + ['execpython', '--args', repr({'script': script})],
                 log=True, call=True, env={}, cwd='/',
                 ignore_stdout=True, ignore_stderr=True).returncode)
 
@@ -86,8 +86,8 @@
                     read_stdout=True, read_stderr=True)
     self.assertEquals(0, process.returncode)
     self.assertTrue(modified_usage in process.stdout_data)
-    self.assertTrue('WARNING: factory.par has been unzipped',
-                    process.stderr_data)
+    self.assertTrue(
+        'WARNING: factory.par has been unzipped' in process.stderr_data)
     # Make sure template config files required by factory_flow tool are packed.
     self.assertTrue(os.path.exists(os.path.join(
         self.tmp, 'cros', 'factory', 'factory_flow', 'templates',
diff --git a/py/utils/file_utils.py b/py/utils/file_utils.py
index 053ede6..bbcc5be 100644
--- a/py/utils/file_utils.py
+++ b/py/utils/file_utils.py
@@ -514,7 +514,7 @@
 
     while True:
       try:
-        self._sys_lock(self._fd, is_exclusive=True, is_blocking=True)
+        self._sys_lock(self._fd, is_exclusive=True, is_blocking=False)
         self._locked = True
         logging.debug('%s locked by %s', self._lockfile, os.getpid())
         break