From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.byosoft.com.cn (mail.byosoft.com.cn [58.240.74.242]) by mx.groups.io with SMTP id smtpd.web09.825.1612400376798716346 for ; Wed, 03 Feb 2021 16:59:38 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=none, err=permanent DNS error (domain: byosoft.com.cn, ip: 58.240.74.242, mailfrom: gaoliming@byosoft.com.cn) Received: from DESKTOPS6D0PVI ([58.246.60.130]) (envelope-sender ) by 192.168.6.13 with ESMTP for ; Thu, 04 Feb 2021 08:59:23 +0800 X-WM-Sender: gaoliming@byosoft.com.cn X-WM-AuthFlag: YES X-WM-AuthUser: gaoliming@byosoft.com.cn From: "gaoliming" To: "'Bob Feng'" , Cc: "'Yuwei Chen'" References: <20210201102858.887-1-bob.c.feng@intel.com> In-Reply-To: <20210201102858.887-1-bob.c.feng@intel.com> Subject: =?UTF-8?B?5Zue5aSNOiBbUGF0Y2ggMS8xIFYyXSBCYXNlVG9vbHM6IGZpeCB0aGUgc3BsaXQgb3V0cHV0IGZpbGVzIHJvb3QgZGly?= Date: Thu, 4 Feb 2021 08:59:31 +0800 Message-ID: <003501d6fa90$ff1b6b70$fd524250$@byosoft.com.cn> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: AQISvhebgUuT0kfzdSYJV2JZzeTpwqnPpaXA Content-Type: text/plain; charset="gb2312" Content-Transfer-Encoding: quoted-printable Content-Language: zh-cn Bob: Does this behavior follow original C Split tool? Thanks Liming > -----=D3=CA=BC=FE=D4=AD=BC=FE----- > =B7=A2=BC=FE=C8=CB: Bob Feng > =B7=A2=CB=CD=CA=B1=BC=E4: 2021=C4=EA2=D4=C21=C8=D5 18:29 > =CA=D5=BC=FE=C8=CB: devel@edk2.groups.io > =B3=AD=CB=CD: Liming Gao ; Yuwei Chen > > =D6=F7=CC=E2: [Patch 1/1 V2] BaseTools: fix the split output files = root dir >=20 > 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. >=20 > If the output file path is not specified and output prefix is not > specified, the output file should be under the input file path >=20 > 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(-) >=20 > 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) >=20 > return length >=20 > +def getoutputfileabs(inputfile, prefix, outputfile,index): > + inputfile =3D os.path.abspath(inputfile) > + if outputfile is None: > + if prefix is None: > + outputfileabs =3D = os.path.join(os.path.dirname(inputfile), > "{}{}".format(os.path.basename(inputfile),index)) > + else: > + if os.path.isabs(prefix): > + outputfileabs =3D os.path.join(prefix, > "{}{}".format(os.path.basename(inputfile),index)) > + else: > + outputfileabs =3D os.path.join(os.getcwd(), prefix, > "{}{}".format(os.path.basename(inputfile),index)) > + elif not os.path.isabs(outputfile): > + if prefix is None: > + outputfileabs =3D os.path.join(os.getcwd(), outputfile) > + else: > + if os.path.isabs(prefix): > + outputfileabs =3D os.path.join(prefix, outputfile) > + else: > + outputfileabs =3D os.path.join(os.getcwd(), prefix, > outputfile) > + else: > + outputfileabs =3D outputfile > + return outputfileabs >=20 > def splitFile(inputfile, position, outputdir=3DNone, = outputfile1=3DNone, > outputfile2=3DNone): > ''' > Split the inputfile into outputfile1 and outputfile2 from the position. > ''' > logger =3D logging.getLogger('Split') >=20 > - inputfile =3D os.path.abspath(inputfile) > - workspace =3D os.path.dirname(inputfile) > if not os.path.exists(inputfile): > logger.error("File Not Found: %s" % inputfile) > raise(Exception) >=20 > if outputfile1 and outputfile2 and outputfile1 =3D=3D = outputfile2: > logger.error( > "The firstfile and the secondfile can't be the same: %s" = % > outputfile1) > raise(Exception) >=20 > - if not outputdir: > - outputdir =3D workspace > - elif not os.path.isabs(outputdir): > - outputdir =3D os.path.join(workspace, outputdir) > - > # Create dir for the output files > try: > - if not outputfile1: > - outputfile1 =3D os.path.abspath(os.path.join( > - outputdir, = "{}1".format(os.path.basename(inputfile)))) > - else: > - outputfile1 =3D os.path.abspath(os.path.join(outputdir, > outputfile1)) > - outputdir =3D os.path.dirname(outputfile1) > - if not os.path.exists(outputdir): > - os.makedirs(outputdir) >=20 > - if not outputfile2: > - outputfile2 =3D os.path.abspath(os.path.join( > - outputdir, = "{}2".format(os.path.basename(inputfile)))) > - else: > - outputfile2 =3D os.path.abspath(os.path.join(outputdir, > outputfile2)) > - outputdir =3D os.path.dirname(outputfile2) > - if not os.path.exists(outputdir): > - os.makedirs(outputdir) > + outputfile1 =3D getoutputfileabs(inputfile, outputdir, = outputfile1, 1) > + outputfolder =3D os.path.dirname(outputfile1) > + if not os.path.exists(outputfolder): > + os.makedirs(outputfolder) > + > + outputfile2 =3D getoutputfileabs(inputfile, outputdir, = outputfile2, 2) > + outputfolder =3D 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) >=20 > if position <=3D 0: > - if outputfile2 !=3D inputfile: > - shutil.copy2(inputfile, outputfile2) > + if outputfile2 !=3D os.path.abspath(inputfile): > + shutil.copy2(os.path.abspath(inputfile), outputfile2) > with open(outputfile1, "wb") as fout: > fout.write(b'') > else: > inputfilesize =3D getFileSize(inputfile) > if position >=3D inputfilesize: > - if outputfile1 !=3D inputfile: > - shutil.copy2(inputfile, outputfile1) > + if outputfile1 !=3D os.path.abspath(inputfile): > + shutil.copy2(os.path.abspath(inputfile), outputfile1) > with open(outputfile2, "wb") as fout: > fout.write(b'') > else: > try: > tempdir =3D 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 >=20 >=20 > class TestSplit(unittest.TestCase): > def setUp(self): > - self.WORKSPACE =3D tempfile.mkdtemp() > - self.binary_file =3D os.path.join(self.WORKSPACE, = "Binary.bin") > + self.tmpdir =3D tempfile.mkdtemp() > + self.binary_file =3D os.path.join(self.tmpdir, "Binary.bin") > self.create_inputfile() >=20 > def tearDown(self): > - if os.path.exists(self.WORKSPACE): > - shutil.rmtree(self.WORKSPACE) > + if os.path.exists(self.tmpdir): > + shutil.rmtree(self.tmpdir) >=20 > def test_splitFile_position(self): > position =3D [-1, 0, 256, 512, 700, 1024, 2048] > result =3D [(0, 1024), (0, 1024), (256, 768), > (512, 512), (700, 324), (1024, 0), (1024, 0)] > + outputfolder =3D self.tmpdir > for index, po in enumerate(position): > try: > sp.splitFile(self.binary_file, po) > except Exception as e: > self.assertTrue(False, msg=3D"splitFile function = error") >=20 > - output1 =3D os.path.join(self.WORKSPACE, "Binary.bin1") > - output2 =3D os.path.join(self.WORKSPACE, "Binary.bin2") > + output1 =3D os.path.join(outputfolder, "Binary.bin1") > + output2 =3D os.path.join(outputfolder, "Binary.bin2") > with open(output1, "rb") as f1: > size1 =3D len(f1.read()) > with open(output2, "rb") as f2: > size2 =3D len(f2.read()) >=20 > @@ -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("=20 > def test_splitFile_outputfile(self): > - output =3D [None, "Binary.bin", "Binary1.bin", r"output/Binary1.bin", > - os.path.join(self.WORKSPACE, = r"output/Binary1.bin")] > - for o in output: > + output =3D [ > + None, > + "Binary.bin", > + "Binary1.bin", > + r"output/Binary1.bin", > + os.path.abspath( r"output/Binary1.bin") > + ] > + expected_output =3D [ > + = os.path.join(os.path.dirname(self.binary_file),"Binary.bin1" ), > + os.path.join(os.getcwd(),"Binary.bin"), > + os.path.join(os.getcwd(),"Binary1.bin"), > + os.path.join(os.getcwd(),r"output/Binary1.bin"), > + os.path.join(os.path.abspath( r"output/Binary1.bin")) > + ] > + for index, o in enumerate(output): > try: > sp.splitFile(self.binary_file, 123, outputfile1=3Do) > except Exception as e: > self.assertTrue(False, msg=3D"splitFile function = error") > - if o is None: > - self.assertTrue(os.path.exists( > - os.path.join(self.WORKSPACE, "Binary.bin1"))) > - else: > - if os.path.isabs(o): > - self.assertTrue(os.path.exists(o)) > - else: > - self.assertTrue(os.path.exists( > - os.path.join(self.WORKSPACE, o))) > - self.create_inputfile() >=20 > - try: > - sp.splitFile(self.binary_file, 123, outputfile2=3Do) > - except Exception as e: > - self.assertTrue(False, msg=3D"splitFile function = error") > - if o is None: > - self.assertTrue(os.path.exists( > - os.path.join(self.WORKSPACE, "Binary.bin2"))) > - else: > - if os.path.isabs(o): > - self.assertTrue(os.path.exists(o)) > - else: > - self.assertTrue(os.path.exists( > - os.path.join(self.WORKSPACE, o))) > + self.assertTrue(os.path.exists(expected_output[index])) > self.create_inputfile() >=20 > def test_splitFile_outputfolder(self): > - outputfolder =3D [None, "output", r"output1/output2", > - os.path.join(self.WORKSPACE, "output")] > - for o in outputfolder: > + outputfolder =3D [ > + None, > + "output", > + r"output1/output2", > + os.path.abspath("output"), > + "output" > + ] > + output =3D [ > + None, > + None, > + "Binary1.bin", > + r"output/Binary1.bin", > + os.path.abspath( r"output_1/Binary1.bin") > + ] > + > + expected_output =3D [ > + = os.path.join(os.path.dirname(self.binary_file),"Binary.bin1" ), > + os.path.join(os.getcwd(),"output", "Binary.bin1"), > + os.path.join(os.getcwd(), r"output1/output2" , "Binary1.bin"), > + os.path.join(os.getcwd(),r"output", = "output/Binary1.bin"), > + os.path.join(os.path.abspath( r"output/Binary1.bin")) > + ] > + > + for index, o in enumerate(outputfolder): > try: > - sp.splitFile(self.binary_file, 123, outputdir=3Do) > + sp.splitFile(self.binary_file, 123, > outputdir=3Do,outputfile1=3Doutput[index]) > except Exception as e: > self.assertTrue(False, msg=3D"splitFile function = error") >=20 > - if o is None: > - self.assertTrue(os.path.exists( > - os.path.join(self.WORKSPACE, "Binary.bin1"))) > - else: > - if os.path.isabs(o): > - self.assertTrue(os.path.exists( > - os.path.join(o, "Binary.bin1"))) > - else: > - self.assertTrue(os.path.exists( > - os.path.join(self.WORKSPACE, o, > "Binary.bin1"))) > + self.assertTrue(os.path.exists(expected_output[index])) > + self.create_inputfile() >=20 >=20 > if __name__ =3D=3D '__main__': > unittest.main() > -- > 2.29.1.windows.1