From 871702265f240cd4499cbd45043c55ebff8c2332 Mon Sep 17 00:00:00 2001 From: "biao716.wang" Date: Wed, 16 Sep 2020 21:46:46 +0900 Subject: [PATCH] Refine code: remove duplicated code Change-Id: I697231e7ad5eb02bf8fde1213f615a4f4338043a Signed-off-by: biao716.wang --- mic/rt_util.py | 54 +++++++++++++++++++++++++++++++++++++++++++ plugins/imager/fs_plugin.py | 52 +++-------------------------------------- plugins/imager/loop_plugin.py | 50 ++------------------------------------- plugins/imager/qcow_plugin.py | 51 +++------------------------------------- plugins/imager/raw_plugin.py | 47 ++----------------------------------- 5 files changed, 64 insertions(+), 190 deletions(-) diff --git a/mic/rt_util.py b/mic/rt_util.py index a880135..b79e800 100644 --- a/mic/rt_util.py +++ b/mic/rt_util.py @@ -29,6 +29,8 @@ from mic.utils import errors, proxy from mic.utils.fs_related import find_binary_path, makedirs from mic.chroot import setup_chrootenv, cleanup_chrootenv, ELF_arch +from mic.plugin import pluginmgr + _libc = ctypes.cdll.LoadLibrary(None) _errno = ctypes.c_int.in_dll(_libc, "errno") _libc.personality.argtypes = [ctypes.c_ulong] @@ -260,3 +262,55 @@ def safecopy(src, dst, symlinks=False, ignore_ptns=()): makedirs(os.path.dirname(dst)) shutil.copy2(src, dst) + +def prepare_create(args): + """ + Usage: + ${name} ${cmd_name} [OPTS] + + ${cmd_option_list} + """ + + if args is None: + raise errors.Usage("Invalid arguments.") + + creatoropts = configmgr.create + ksconf = args.ksfile + + if creatoropts['runtime'] == 'bootstrap': + configmgr._ksconf = ksconf + bootstrap_mic() + + recording_pkgs = [] + if len(creatoropts['record_pkgs']) > 0: + recording_pkgs = creatoropts['record_pkgs'] + + if creatoropts['release'] is not None: + if 'name' not in recording_pkgs: + recording_pkgs.append('name') + if 'vcs' not in recording_pkgs: + recording_pkgs.append('vcs') + + configmgr._ksconf = ksconf + + # try to find the pkgmgr + pkgmgr = None + backends = pluginmgr.get_plugins('backend') + if 'auto' == creatoropts['pkgmgr']: + for key in configmgr.prefer_backends: + if key in backends: + pkgmgr = backends[key] + break + else: + for key in backends.keys(): + if key == creatoropts['pkgmgr']: + pkgmgr = backends[key] + break + + if not pkgmgr: + raise errors.CreatorError("Can't find backend: %s, " + "available choices: %s" % + (creatoropts['pkgmgr'], + ','.join(backends.keys()))) + return creatoropts, pkgmgr, recording_pkgs + diff --git a/plugins/imager/fs_plugin.py b/plugins/imager/fs_plugin.py index 702976d..21bb544 100755 --- a/plugins/imager/fs_plugin.py +++ b/plugins/imager/fs_plugin.py @@ -1,4 +1,4 @@ -#!/usr/bin/python -tt +#! /usr/bin/python2 # # Copyright (c) 2011 Intel, Inc. # @@ -19,7 +19,6 @@ import subprocess from mic import chroot, msger, rt_util from mic.utils import misc, errors, fs_related, runner from mic.imager import fs -from mic.conf import configmgr from mic.plugin import pluginmgr from mic.pluginbase import ImagerPlugin @@ -30,53 +29,8 @@ class FsPlugin(ImagerPlugin): def do_create(self, args): """${cmd_name}: create fs image - Usage: - ${name} ${cmd_name} [OPTS] - - ${cmd_option_list} """ - - if args is None: - raise errors.Usage("Invalid arguments.") - - creatoropts = configmgr.create - ksconf = args.ksfile - - if creatoropts['runtime'] == 'bootstrap': - configmgr._ksconf = ksconf - rt_util.bootstrap_mic() - - recording_pkgs = [] - if len(creatoropts['record_pkgs']) > 0: - recording_pkgs = creatoropts['record_pkgs'] - - if creatoropts['release'] is not None: - if 'name' not in recording_pkgs: - recording_pkgs.append('name') - if 'vcs' not in recording_pkgs: - recording_pkgs.append('vcs') - - configmgr._ksconf = ksconf - - # try to find the pkgmgr - pkgmgr = None - backends = pluginmgr.get_plugins('backend') - if 'auto' == creatoropts['pkgmgr']: - for key in configmgr.prefer_backends: - if key in backends: - pkgmgr = backends[key] - break - else: - for key in backends.keys(): - if key == creatoropts['pkgmgr']: - pkgmgr = backends[key] - break - - if not pkgmgr: - raise errors.CreatorError("Can't find backend: %s, " - "available choices: %s" % - (creatoropts['pkgmgr'], - ','.join(backends.keys()))) + creatoropts, pkgmgr, recording_pkgs = rt_util.prepare_create(args) creator = fs.FsImageCreator(creatoropts, pkgmgr) creator._include_src = args.include_src @@ -108,7 +62,7 @@ class FsPlugin(ImagerPlugin): creator.package(creatoropts["destdir"]) creator.create_manifest() if creatoropts['release'] is not None: - creator.release_output(ksconf, creatoropts['destdir'], + creator.release_output(args.ksfile, creatoropts['destdir'], creatoropts['release']) creator.print_outimage_info() except errors.CreatorError: diff --git a/plugins/imager/loop_plugin.py b/plugins/imager/loop_plugin.py index d925ccb..ceaa351 100755 --- a/plugins/imager/loop_plugin.py +++ b/plugins/imager/loop_plugin.py @@ -22,7 +22,6 @@ import tempfile from mic import chroot, msger, rt_util from mic.utils import misc, fs_related, errors, runner -from mic.conf import configmgr from mic.plugin import pluginmgr from mic.imager.loop import LoopImageCreator, load_mountpoints @@ -33,54 +32,9 @@ class LoopPlugin(ImagerPlugin): @classmethod def do_create(self, args): """${cmd_name}: create loop image - - Usage: - ${name} ${cmd_name} [OPTS] - - ${cmd_option_list} """ - if args is None: - raise errors.Usage("Invalid arguments") - - creatoropts = configmgr.create - ksconf = args.ksfile - - if creatoropts['runtime'] == "bootstrap": - configmgr._ksconf = ksconf - rt_util.bootstrap_mic() - - recording_pkgs = [] - if len(creatoropts['record_pkgs']) > 0: - recording_pkgs = creatoropts['record_pkgs'] - - if creatoropts['release'] is not None: - if 'name' not in recording_pkgs: - recording_pkgs.append('name') - if 'vcs' not in recording_pkgs: - recording_pkgs.append('vcs') - - configmgr._ksconf = ksconf - - # try to find the pkgmgr - pkgmgr = None - backends = pluginmgr.get_plugins('backend') - if 'auto' == creatoropts['pkgmgr']: - for key in configmgr.prefer_backends: - if key in backends: - pkgmgr = backends[key] - break - else: - for key in backends.keys(): - if key == creatoropts['pkgmgr']: - pkgmgr = backends[key] - break - - if not pkgmgr: - raise errors.CreatorError("Can't find backend: %s, " - "available choices: %s" % - (creatoropts['pkgmgr'], - ','.join(backends.keys()))) + creatoropts, pkgmgr, recording_pkgs = rt_util.prepare_create(args) creator = LoopImageCreator(creatoropts, pkgmgr, @@ -111,7 +65,7 @@ class LoopPlugin(ImagerPlugin): creator.create_manifest() if creatoropts['release'] is not None: - creator.release_output(ksconf, + creator.release_output(args.ksfile, creatoropts['destdir'], creatoropts['release']) creator.print_outimage_info() diff --git a/plugins/imager/qcow_plugin.py b/plugins/imager/qcow_plugin.py index cec6757..2329958 100755 --- a/plugins/imager/qcow_plugin.py +++ b/plugins/imager/qcow_plugin.py @@ -19,7 +19,6 @@ import subprocess import shutil from mic import msger, rt_util -from mic.conf import configmgr from mic.plugin import pluginmgr from mic.pluginbase import ImagerPlugin from mic.imager.qcow import QcowImageCreator @@ -31,53 +30,9 @@ class QcowPlugin(ImagerPlugin): @classmethod def do_create(cls, args): """${cmd_name}: create qcow image - - Usage: - ${name} ${cmd_name} [OPTS] - - ${cmd_option_list} """ - if args is None: - raise errors.Usage("Invalid arguments") - - creatoropts = configmgr.create - ksconf = args.ksfile - - if creatoropts['runtime'] == "bootstrap": - configmgr._ksconf = ksconf - rt_util.bootstrap_mic() - - recording_pkgs = [] - if len(creatoropts['record_pkgs']) > 0: - recording_pkgs = creatoropts['record_pkgs'] - - if creatoropts['release'] is not None: - if 'name' not in recording_pkgs: - recording_pkgs.append('name') - if 'vcs' not in recording_pkgs: - recording_pkgs.append('vcs') - - configmgr._ksconf = ksconf - - # try to find the pkgmgr - pkgmgr = None - backends = pluginmgr.get_plugins('backend') - if 'auto' == creatoropts['pkgmgr']: - for key in configmgr.prefer_backends: - if key in backends: - pkgmgr = backends[key] - break - else: - for key in backends.keys(): - if key == creatoropts['pkgmgr']: - pkgmgr = backends[key] - break - - if not pkgmgr: - raise errors.CreatorError("Can't find backend: %s, " - "available choices: %s" % - (creatoropts['pkgmgr'], - ','.join(backends.keys()))) + + creatoropts, pkgmgr, recording_pkgs = rt_util.prepare_create(args) creator = QcowImageCreator(creatoropts, pkgmgr) @@ -106,7 +61,7 @@ class QcowPlugin(ImagerPlugin): creator.create_manifest() if creatoropts['release'] is not None: - creator.release_output(ksconf, + creator.release_output(args.ksfile, creatoropts['destdir'], creatoropts['release']) creator.print_outimage_info() diff --git a/plugins/imager/raw_plugin.py b/plugins/imager/raw_plugin.py index 4b4212a..fa24583 100755 --- a/plugins/imager/raw_plugin.py +++ b/plugins/imager/raw_plugin.py @@ -23,7 +23,6 @@ import tempfile from mic import chroot, msger, rt_util from mic.utils import misc, fs_related, errors, runner -from mic.conf import configmgr from mic.plugin import pluginmgr from mic.utils.partitionedfs import PartitionedMount @@ -36,51 +35,9 @@ class RawPlugin(ImagerPlugin): @classmethod def do_create(self, args): """${cmd_name}: create raw image - - Usage: - ${name} ${cmd_name} [OPTS] - - ${cmd_option_list} """ - creatoropts = configmgr.create - ksconf = args.ksfile - - if creatoropts['runtime'] == "bootstrap": - configmgr._ksconf = ksconf - rt_util.bootstrap_mic() - - recording_pkgs = [] - if len(creatoropts['record_pkgs']) > 0: - recording_pkgs = creatoropts['record_pkgs'] - - if creatoropts['release'] is not None: - if 'name' not in recording_pkgs: - recording_pkgs.append('name') - if 'vcs' not in recording_pkgs: - recording_pkgs.append('vcs') - - configmgr._ksconf = ksconf - - # try to find the pkgmgr - pkgmgr = None - backends = pluginmgr.get_plugins('backend') - if 'auto' == creatoropts['pkgmgr']: - for key in configmgr.prefer_backends: - if key in backends: - pkgmgr = backends[key] - break - else: - for key in backends.keys(): - if key == creatoropts['pkgmgr']: - pkgmgr = backends[key] - break - - if not pkgmgr: - raise errors.CreatorError("Can't find backend: %s, " - "available choices: %s" % - (creatoropts['pkgmgr'], - ','.join(backends.keys()))) + creatoropts, pkgmgr, recording_pkgs = rt_util.prepare_create(args) creator = raw.RawImageCreator(creatoropts, pkgmgr, args.compress_image, args.generate_bmap, args.fstab_entry) @@ -107,7 +64,7 @@ class RawPlugin(ImagerPlugin): creator.package(creatoropts["destdir"]) creator.create_manifest() if creatoropts['release'] is not None: - creator.release_output(ksconf, creatoropts['destdir'], creatoropts['release']) + creator.release_output(args.ksfile, creatoropts['destdir'], creatoropts['release']) creator.print_outimage_info() except errors.CreatorError: -- 2.7.4