From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by mx.groups.io with SMTP id smtpd.web12.31912.1612175349219950289 for ; Mon, 01 Feb 2021 02:29:09 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 134.134.136.24, mailfrom: bob.c.feng@intel.com) IronPort-SDR: lLlDhY8rL71NH4VLDiN+fdZCgtTLHUgRzsbsVbroFDkCqdvK6VtGHXUwnbIIA6aGq71ClK++OR yXWcrVzI6tCA== X-IronPort-AV: E=McAfee;i="6000,8403,9881"; a="180809934" X-IronPort-AV: E=Sophos;i="5.79,392,1602572400"; d="scan'208";a="180809934" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2021 02:29:08 -0800 IronPort-SDR: dmrvtYRRJznES6leXdxuEsLNmmDL6/v3ehJXqNrmPIHHQ5FwJ8c40DZXqQ07GbqXCewwu/9OdT k1tMuXbqyshQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.79,392,1602572400"; d="scan'208";a="371500391" Received: from shwdepsi1121.ccr.corp.intel.com ([10.239.158.32]) by orsmga002.jf.intel.com with ESMTP; 01 Feb 2021 02:29:06 -0800 From: "Bob Feng" To: devel@edk2.groups.io Cc: Liming Gao , Yuwei Chen Subject: [Patch 1/1 V2] BaseTools: fix the split output files root dir Date: Mon, 1 Feb 2021 18:28:58 +0800 Message-Id: <20210201102858.887-1-bob.c.feng@intel.com> X-Mailer: git-send-email 2.29.1.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit If the output file path is a relative path, the split tool will create the output file under the input file path. But the expected behavior for this case is the output file should be relative to the current directory. This patch will fix this bug. If the output file path is not specified and output prefix is not specified, the output file should be under the input file path Signed-off-by: Bob Feng Cc: Liming Gao Cc: Yuwei Chen --- V2: If the output file path is not specified and output prefix is not specified, the output file should be under the input file path BaseTools/Source/Python/Split/Split.py | 64 +++++++------ .../Source/Python/tests/Split/test_split.py | 96 ++++++++++--------- 2 files changed, 86 insertions(+), 74 deletions(-) diff --git a/BaseTools/Source/Python/Split/Split.py b/BaseTools/Source/Python/Split/Split.py index 45a5a060474c..e223a72a94e1 100644 --- a/BaseTools/Source/Python/Split/Split.py +++ b/BaseTools/Source/Python/Split/Split.py @@ -90,66 +90,74 @@ def getFileSize(filename): logger.error("Access file failed: %s", filename) raise(e) return length +def getoutputfileabs(inputfile, prefix, outputfile,index): + inputfile = os.path.abspath(inputfile) + if outputfile is None: + if prefix is None: + outputfileabs = os.path.join(os.path.dirname(inputfile), "{}{}".format(os.path.basename(inputfile),index)) + else: + if os.path.isabs(prefix): + outputfileabs = os.path.join(prefix, "{}{}".format(os.path.basename(inputfile),index)) + else: + outputfileabs = os.path.join(os.getcwd(), prefix, "{}{}".format(os.path.basename(inputfile),index)) + elif not os.path.isabs(outputfile): + if prefix is None: + outputfileabs = os.path.join(os.getcwd(), outputfile) + else: + if os.path.isabs(prefix): + outputfileabs = os.path.join(prefix, outputfile) + else: + outputfileabs = os.path.join(os.getcwd(), prefix, outputfile) + else: + outputfileabs = outputfile + return outputfileabs def splitFile(inputfile, position, outputdir=None, outputfile1=None, outputfile2=None): ''' Split the inputfile into outputfile1 and outputfile2 from the position. ''' logger = logging.getLogger('Split') - inputfile = os.path.abspath(inputfile) - workspace = os.path.dirname(inputfile) if not os.path.exists(inputfile): logger.error("File Not Found: %s" % inputfile) raise(Exception) if outputfile1 and outputfile2 and outputfile1 == outputfile2: logger.error( "The firstfile and the secondfile can't be the same: %s" % outputfile1) raise(Exception) - if not outputdir: - outputdir = workspace - elif not os.path.isabs(outputdir): - outputdir = os.path.join(workspace, outputdir) - # Create dir for the output files try: - if not outputfile1: - outputfile1 = os.path.abspath(os.path.join( - outputdir, "{}1".format(os.path.basename(inputfile)))) - else: - outputfile1 = os.path.abspath(os.path.join(outputdir, outputfile1)) - outputdir = os.path.dirname(outputfile1) - if not os.path.exists(outputdir): - os.makedirs(outputdir) - if not outputfile2: - outputfile2 = os.path.abspath(os.path.join( - outputdir, "{}2".format(os.path.basename(inputfile)))) - else: - outputfile2 = os.path.abspath(os.path.join(outputdir, outputfile2)) - outputdir = os.path.dirname(outputfile2) - if not os.path.exists(outputdir): - os.makedirs(outputdir) + outputfile1 = getoutputfileabs(inputfile, outputdir, outputfile1,1) + outputfolder = os.path.dirname(outputfile1) + if not os.path.exists(outputfolder): + os.makedirs(outputfolder) + + outputfile2 = getoutputfileabs(inputfile, outputdir, outputfile2,2) + outputfolder = os.path.dirname(outputfile2) + if not os.path.exists(outputfolder): + os.makedirs(outputfolder) + except Exception as e: - logger.error("Can't make dir: %s" % outputdir) + logger.error("Can't make dir: %s" % outputfolder) raise(e) if position <= 0: - if outputfile2 != inputfile: - shutil.copy2(inputfile, outputfile2) + if outputfile2 != os.path.abspath(inputfile): + shutil.copy2(os.path.abspath(inputfile), outputfile2) with open(outputfile1, "wb") as fout: fout.write(b'') else: inputfilesize = getFileSize(inputfile) if position >= inputfilesize: - if outputfile1 != inputfile: - shutil.copy2(inputfile, outputfile1) + if outputfile1 != os.path.abspath(inputfile): + shutil.copy2(os.path.abspath(inputfile), outputfile1) with open(outputfile2, "wb") as fout: fout.write(b'') else: try: tempdir = tempfile.mkdtemp() diff --git a/BaseTools/Source/Python/tests/Split/test_split.py b/BaseTools/Source/Python/tests/Split/test_split.py index 82f71ecf5372..e4866be390b3 100644 --- a/BaseTools/Source/Python/tests/Split/test_split.py +++ b/BaseTools/Source/Python/tests/Split/test_split.py @@ -16,30 +16,31 @@ import Split.Split as sp import struct as st class TestSplit(unittest.TestCase): def setUp(self): - self.WORKSPACE = tempfile.mkdtemp() - self.binary_file = os.path.join(self.WORKSPACE, "Binary.bin") + self.tmpdir = tempfile.mkdtemp() + self.binary_file = os.path.join(self.tmpdir, "Binary.bin") self.create_inputfile() def tearDown(self): - if os.path.exists(self.WORKSPACE): - shutil.rmtree(self.WORKSPACE) + if os.path.exists(self.tmpdir): + shutil.rmtree(self.tmpdir) def test_splitFile_position(self): position = [-1, 0, 256, 512, 700, 1024, 2048] result = [(0, 1024), (0, 1024), (256, 768), (512, 512), (700, 324), (1024, 0), (1024, 0)] + outputfolder = self.tmpdir for index, po in enumerate(position): try: sp.splitFile(self.binary_file, po) except Exception as e: self.assertTrue(False, msg="splitFile function error") - output1 = os.path.join(self.WORKSPACE, "Binary.bin1") - output2 = os.path.join(self.WORKSPACE, "Binary.bin2") + output1 = os.path.join(outputfolder, "Binary.bin1") + output2 = os.path.join(outputfolder, "Binary.bin2") with open(output1, "rb") as f1: size1 = len(f1.read()) with open(output2, "rb") as f2: size2 = len(f2.read()) @@ -51,61 +52,64 @@ class TestSplit(unittest.TestCase): with open(self.binary_file, "wb") as fout: for i in range(512): fout.write(st.pack("