build_dlc: Add flag --package

We need to support multiple packages per DLC. This change adds flag
--package to identify the package id for a DLC. A DLC in fact can have
multiple packages.

Also added the key DLC_PACKAGE to the lsb-release of the DLC.

Changed the default dlc filename from dlc_<dlc-id>.img to simply what it
was before: dlc.img

BUG=chromium:908994
TEST=precq, unittest
CQ-DEPEND=CL:1531833

Change-Id: I32b17e334a55df07b6ee361ce5b51fa4a565140a
Reviewed-on: https://chromium-review.googlesource.com/1532770
Commit-Ready: Amin Hassani <ahassani@chromium.org>
Tested-by: Amin Hassani <ahassani@chromium.org>
Reviewed-by: Amin Hassani <ahassani@chromium.org>
diff --git a/scripts/build_dlc.py b/scripts/build_dlc.py
index 3783a1d..82552fc 100644
--- a/scripts/build_dlc.py
+++ b/scripts/build_dlc.py
@@ -33,6 +33,7 @@
 )
 
 DLC_ID_KEY = 'DLC_ID'
+DLC_PACKAGE_KEY = 'DLC_PACKAGE'
 DLC_NAME_KEY = 'DLC_NAME'
 DLC_APPID_KEY = 'DLC_RELEASE_APPID'
 
@@ -72,7 +73,7 @@
   _DLC_ROOT_DIR = 'root'
 
   def __init__(self, src_dir, sysroot, install_root_dir, fs_type,
-               pre_allocated_blocks, version, dlc_id, name):
+               pre_allocated_blocks, version, dlc_id, dlc_package, name):
     """Object initializer.
 
     Args:
@@ -83,6 +84,7 @@
       pre_allocated_blocks: (int) number of blocks pre-allocated on device.
       version: (str) DLC version.
       dlc_id: (str) DLC ID.
+      dlc_package: (str) DLC Package.
       name: (str) DLC name.
     """
     self.src_dir = src_dir
@@ -92,31 +94,21 @@
     self.pre_allocated_blocks = pre_allocated_blocks
     self.version = version
     self.dlc_id = dlc_id
+    self.dlc_package = dlc_package
     self.name = name
 
     self.meta_dir = os.path.join(self.install_root_dir, DLC_META_DIR,
-                                 self.dlc_id)
+                                 self.dlc_id, self.dlc_package)
     self.image_dir = os.path.join(self.install_root_dir, DLC_IMAGE_DIR,
-                                  self.dlc_id)
+                                  self.dlc_id, self.dlc_package)
     osutils.SafeMakedirs(self.meta_dir)
     osutils.SafeMakedirs(self.image_dir)
 
     # Create path for all final artifacts.
-    self.dest_image = os.path.join(self.image_dir, self.GetImageFileName())
+    self.dest_image = os.path.join(self.image_dir, 'dlc.img')
     self.dest_table = os.path.join(self.meta_dir, 'table')
     self.dest_imageloader_json = os.path.join(self.meta_dir, 'imageloader.json')
 
-  def GetImageFileName(self):
-    """Returns the image file name created based on the dlc_id.
-
-    This probably will be replaced by the partition name once we move to a
-    multip-partition DLC.
-
-    Returns:
-      [str]: The image file name for the DLC.
-    """
-    return 'dlc_%s.img' % self.dlc_id
-
   def SquashOwnerships(self, path):
     """Squash the owernships & permissions for files.
 
@@ -205,6 +197,7 @@
 
     fields = {
         DLC_ID_KEY: self.dlc_id,
+        DLC_PACKAGE_KEY: self.dlc_package,
         DLC_NAME_KEY: self.name,
         # The DLC appid is generated by concatenating the platform appid with
         # the DLC ID using an underscore. This pattern should never be changed
@@ -255,6 +248,7 @@
     return {
         'fs-type': self.fs_type,
         'id': self.dlc_id,
+        'package': self.dlc_package,
         'image-sha256-hash': image_hash,
         'image-type': 'dlc',
         'is-removable': True,
@@ -361,6 +355,9 @@
                        'be pre-allocated on device.')
   one_dlc.add_argument('--version', metavar='VERSION', help='DLC Version.')
   one_dlc.add_argument('--id', metavar='ID', help='DLC ID (unique per DLC).')
+  one_dlc.add_argument('--package', metavar='PACKAGE',
+                       help='The package ID that is unique within a DLC, One'
+                       ' DLC cannot have duplicate package IDs.')
   one_dlc.add_argument('--name', metavar='NAME',
                        help='A human-readable name for the DLC.')
   one_dlc.add_argument('--fs-type', metavar='FS_TYPE', default=_SQUASHFS_TYPE,
@@ -378,7 +375,7 @@
   # Make sure if the intention is to build one DLC, all the required arguments
   # are passed.
   per_dlc_req_args = ('src_dir', 'pre_allocated_blocks', 'version', 'id',
-                      'name')
+                      'package', 'name')
   if (opts.id and
       not all(vars(opts)[arg] is not None for arg in per_dlc_req_args)):
     raise Exception('If the intention is to build only one DLC, all the flags'
@@ -403,6 +400,7 @@
                                  pre_allocated_blocks=opts.pre_allocated_blocks,
                                  version=opts.version,
                                  dlc_id=opts.id,
+                                 dlc_package=opts.package,
                                  name=opts.name)
     dlc_generator.GenerateDLC()
   else:
diff --git a/scripts/build_dlc_unittest.py b/scripts/build_dlc_unittest.py
index 926f272..a0a250d 100644
--- a/scripts/build_dlc_unittest.py
+++ b/scripts/build_dlc_unittest.py
@@ -24,6 +24,7 @@
 _PRE_ALLOCATED_BLOCKS = 100
 _VERSION = '1.0'
 _ID = 'id'
+_PACKAGE = 'package'
 _NAME = 'name'
 _META_DIR = 'opt/google/dlc/'
 _IMAGE_DIR = 'build/rootfs/dlc/'
@@ -64,20 +65,16 @@
                                   pre_allocated_blocks=_PRE_ALLOCATED_BLOCKS,
                                   version=_VERSION,
                                   dlc_id=_ID,
+                                  dlc_package=_PACKAGE,
                                   name=_NAME)
 
-  def testGetImageFileName(self):
-    """Tests getting the correct image file name."""
-    generator = self.GetDlcGenerator()
-    self.assertEqual(generator.GetImageFileName(), 'dlc_%s.img' % _ID)
-
   def testSetInstallDir(self):
     """Tests install_root_dir is used correclty."""
     generator = self.GetDlcGenerator()
     self.assertEqual(generator.meta_dir,
-                     os.path.join(self.tempdir, _META_DIR, _ID))
+                     os.path.join(self.tempdir, _META_DIR, _ID, _PACKAGE))
     self.assertEqual(generator.image_dir,
-                     os.path.join(self.tempdir, _IMAGE_DIR, _ID))
+                     os.path.join(self.tempdir, _IMAGE_DIR, _ID, _PACKAGE))
 
   def testSquashOwnerships(self):
     """Test build_dlc.SquashOwnershipsTest"""
@@ -122,6 +119,7 @@
     expected_lsb_release = '\n'.join([
         'DLC_ID=%s' % _ID,
         'DLC_NAME=%s' % _NAME,
+        'DLC_PACKAGE=%s' % _PACKAGE,
         'DLC_RELEASE_APPID=foo_%s' % _ID,
     ]) + '\n'
 
@@ -148,7 +146,8 @@
     self.assertEqual(content, {
         'fs-type': _FS_TYPE_SQUASHFS,
         'pre-allocated-size': _PRE_ALLOCATED_BLOCKS * 4096,
-        'id': 'id',
+        'id': _ID,
+        'package': _PACKAGE,
         'size': blocks * 4096,
         'table-sha256-hash': 'deadbeef',
         'name': _NAME,