Add full support for pre/post install script
authorKévin THIERRY <kevin.thierry@open.eurogiciel.org>
Thu, 5 Jun 2014 11:52:52 +0000 (13:52 +0200)
committerKévin THIERRY <kevin.thierry@open.eurogiciel.org>
Thu, 5 Jun 2014 12:11:15 +0000 (14:11 +0200)
Spec file sections %pre, %post, %preun, and %postun are now added in
recipes.

All sections of pre/post install/uninstall scripts are now added to the
recipes.

Signed-off-by: Kévin THIERRY <kevin.thierry@open.eurogiciel.org>
tools/spec2yocto.py

index 65f5c79..9e9299b 100755 (executable)
@@ -1173,25 +1173,40 @@ Group: devel
                 return res_license
         return ""
 
-    def get_file_section_key( self ):
+    def get_section_key(self, __flag):
         '''
-        return the list of file section of the spec file.
+        Return the list of "__flag" sections of the spec file.
         '''
-        res_file_list = []
+        res_section = []
         for section_key in self.__spect_dico.keys():
-            # should use re.
-            if section_key.startswith( "%files" ):
-                res_file_list.append( section_key )
-        return res_file_list
+            if section_key.startswith(__flag):
+                res_section.append(section_key)
 
-    def get_script_section_key(self, __flag):
+        return res_section
+
+    def get_script_section_key_pre(self):
         '''
-        return the list of file section of the spec file.
+        Return the list of pre script section of the spec file.
+        get_section_key() can't be used in this case since it will also
+        return the %preun results.
         '''
-        res_script_section = ""
-        if __flag in self.__spect_dico.keys():
-            for line in self.__spect_dico[__flag][1:]:
-                res_script_section += line + "\n"
+        res_script_section = []
+        for section_key in self.__spect_dico.keys():
+            if section_key.startswith("%pre") and not section_key.startswith("%preun"):
+                res_script_section.append(section_key)
+
+        return res_script_section
+
+    def get_script_section_key_post(self):
+        '''
+        Return the list of post script section of the spec file.
+        get_section_key() can't be used in this case since it will also
+        return the %postun results.
+        '''
+        res_script_section = []
+        for section_key in self.__spect_dico.keys():
+            if section_key.startswith("%post") and not section_key.startswith("%postun"):
+                res_script_section.append(section_key)
 
         return res_script_section
 
@@ -1234,7 +1249,16 @@ Group: devel
 
         return res_list
 
+    def get_script_from_section(self, __flag):
+        '''
+        Return the pre/post script section of the spec file.
+        '''
+        res_script_section = ""
+        if __flag in self.__spect_dico.keys():
+            for line in self.__spect_dico[__flag][1:]:
+                res_script_section += line + "\n"
 
+        return res_script_section
 
     def get_files_packages( self ):
         '''
@@ -1242,7 +1266,7 @@ Group: devel
         '''
         res_dico = {}
 
-        for package_section_name in self.get_file_section_key():
+        for package_section_name in self.get_section_key("%file"):
             tmp_package_name = package_section_name
             # Need to get info of the first line.
             if "-f " in tmp_package_name:
@@ -1648,12 +1672,12 @@ class MetaSpec:
         file_d.write( "}\n" )
         file_d.write( "\n" )
 
-    def __create_script_file_section( self, spec_section):
+    def __create_script_file_section_code( self, spec_section):
         '''
         Returns the command executed in one of the %pre, %post, %preun
         or %postun section of a spec file.
         '''
-        code = self.__spec_parser.get_script_section_key(spec_section)
+        code = self.__spec_parser.get_script_from_section(spec_section)
         command_list = code.split("\n")
         formated_code = ""
         for line in command_list:
@@ -1662,36 +1686,55 @@ class MetaSpec:
 
         return formated_code
 
-    def __create_script_file_write( self, file_d, code, recipe_section):
+    def __create_script_file_write( self, file_d, code, recipe_section, package_name):
         '''
         Writes the pre/post (un)install sections of the recipe.
         '''
-        file_d.write("%s() {\n" % recipe_section)
-        file_d.write("    #!/bin/sh -e\n")
+        file_d.write("%s%s() {\n" % (recipe_section, package_name))
+        file_d.write("    #!/bin/sh -e\n\n")
         file_d.write("%s\n" % code)
         file_d.write("}\n")
         file_d.write("\n")
 
-    def __create_script_file( self, file_d):
+    def __create_script_file_session(self, file_d, flag, script_name, script_sections):
+        '''
+        Add script for the given flag.
+        '''
+        for section in script_sections:
+            code = ""
+            # If %section is a one line script such as: "%post -p /sbin/ldconfig"
+            if "-p" in section:
+                # Remove the package name if present to only keep the command
+                section_split = section.split("-p")[1].split("-n")
+                code = "    " + section_split[0].strip()
+            else:
+                code = self.__create_script_file_section_code(section)
+            if code != "":
+                # Set the package name
+                package_name = "${PN}"
+                # If the package name is not the project name
+                if "-n" in section:
+                    # Remove the command if any to only keep the package name
+                    section_split = section.split("-n")[1].split("-p")
+                    package_name = section_split[0].strip()
+                self.__create_script_file_write(file_d, code, script_name, package_name)
+
+    def __create_script_file(self, file_d):
         '''
         Add the pre/post install/uninstall sections of a spec file to
         the recipe.
         '''
-        code = self.__create_script_file_section("%pre")
-        if code != "":
-            self.__create_script_file_write(file_d, code, "pkg_preinst_${PN}")
+        script_sections = self.__spec_parser.get_script_section_key_pre()
+        self.__create_script_file_session(file_d, "%pre", "pkg_preinst_", script_sections)
 
-        code = self.__create_script_file_section("%post")
-        if code != "":
-            self.__create_script_file_write(file_d, code, "pkg_postinst_${PN}")
+        script_sections = self.__spec_parser.get_script_section_key_post()
+        self.__create_script_file_session(file_d, "%post", "pkg_postinst_", script_sections)
 
-        code = self.__create_script_file_section("%preun")
-        if code != "":
-            self.__create_script_file_write(file_d, code, "pkg_prerm_${PN}")
+        script_sections = self.__spec_parser.get_section_key("%preun")
+        self.__create_script_file_session(file_d, "%preun", "pkg_prerm_", script_sections)
 
-        code = self.__create_script_file_section("%postun")
-        if code != "":
-            self.__create_script_file_write(file_d, code, "pkg_postrm_${PN}")
+        script_sections = self.__spec_parser.get_section_key("%postun")
+        self.__create_script_file_session(file_d, "%postun", "pkg_postrm_", script_sections)
 
     def __create_provides_file( self ,file_d):
         '''
@@ -2904,4 +2947,3 @@ def main():
 
 if __name__ == '__main__':
     main()
-