ghost: Fix parsing of mlb_serial_number.

The output format of "factory device-data" is changed after the state
API change. Fix the parsing in ghost.py.

BUG=None
TEST=make test
TEST=manually, delete /var/factory/.device_id on DUT, manually start
     ghost and see the mlb_serial_number can be retrieved.

Change-Id: I9ee789cf8ec213b95b5abc9149397454b6b71a42
Reviewed-on: https://chromium-review.googlesource.com/535494
Commit-Ready: Pi-Hsun Shih <pihsun@chromium.org>
Tested-by: Pi-Hsun Shih <pihsun@chromium.org>
Reviewed-by: Wei-Han Chen <stimim@chromium.org>
diff --git a/py/tools/ghost.py b/py/tools/ghost.py
index a476375..72c4d16 100755
--- a/py/tools/ghost.py
+++ b/py/tools/ghost.py
@@ -1,6 +1,4 @@
 #!/usr/bin/env python
-# -*- coding: utf-8 -*-
-#
 # 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.
@@ -423,7 +421,7 @@
 
   def GetShopfloorIP(self):
     try:
-      import factory_common  # pylint: disable=W0612
+      import factory_common  # pylint: disable=unused-variable
       from cros.factory.test import shopfloor
 
       url = shopfloor.get_server_url()
@@ -463,7 +461,7 @@
 
     # Try factory device id
     try:
-      import factory_common  # pylint: disable=W0612
+      import factory_common  # pylint: disable=unused-variable
       from cros.factory.test import event_log
       with open(event_log.DEVICE_ID_PATH) as f:
         return f.read().strip()
@@ -472,13 +470,11 @@
 
     # Try factory device data
     try:
-      p = subprocess.Popen('factory device-data | grep mlb_serial_number | '
-                           'cut -d " " -f 2', stdout=subprocess.PIPE,
-                           stderr=subprocess.PIPE, shell=True)
-      stdout, unused_stderr = p.communicate()
-      if stdout == '':
-        raise RuntimeError('empty mlb number')
-      return stdout.strip()
+      import factory_common  # pylint: disable=unused-variable
+      from cros.factory.test import state
+      mlb_serial_number = state.GetSerialNumber(state.KEY_MLB_SERIAL_NUMBER)
+      if mlb_serial_number:
+        return mlb_serial_number
     except Exception:
       pass
 
@@ -504,7 +500,7 @@
     except Exception:
       pass
 
-    raise RuntimeError('can\'t generate machine ID')
+    raise RuntimeError("can't generate machine ID")
 
   def GetProcessWorkingDirectory(self, pid):
     if self._platform == 'Linux':