mirror of
				https://github.com/python/cpython.git
				synced 2025-11-02 22:51:25 +00:00 
			
		
		
		
	add filtering of individual files to PyZipFile
changed output of debug messages to say "path" or "file" extended test for filtering certain files in a package added test for filtering files in a python dir (no package)
This commit is contained in:
		
							parent
							
								
									8ed30c15e8
								
							
						
					
					
						commit
						410d931a17
					
				
					 2 changed files with 40 additions and 2 deletions
				
			
		| 
						 | 
					@ -604,12 +604,21 @@ def test_write_filtered_python_package(self):
 | 
				
			||||||
            reportStr = reportSIO.getvalue()
 | 
					            reportStr = reportSIO.getvalue()
 | 
				
			||||||
            self.assertTrue('SyntaxError' in reportStr)
 | 
					            self.assertTrue('SyntaxError' in reportStr)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # then check that the filter works
 | 
					            # then check that the filter works on the whole package
 | 
				
			||||||
            with captured_stdout() as reportSIO:
 | 
					            with captured_stdout() as reportSIO:
 | 
				
			||||||
                zipfp.writepy(packagedir, filterfunc=lambda whatever: False)
 | 
					                zipfp.writepy(packagedir, filterfunc=lambda whatever: False)
 | 
				
			||||||
            reportStr = reportSIO.getvalue()
 | 
					            reportStr = reportSIO.getvalue()
 | 
				
			||||||
            self.assertTrue('SyntaxError' not in reportStr)
 | 
					            self.assertTrue('SyntaxError' not in reportStr)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            # then check that the filter works on individual files
 | 
				
			||||||
 | 
					            with captured_stdout() as reportSIO:
 | 
				
			||||||
 | 
					                zipfp.writepy(packagedir, filterfunc=lambda fn:
 | 
				
			||||||
 | 
					                                                     'bad' not in fn)
 | 
				
			||||||
 | 
					            reportStr = reportSIO.getvalue()
 | 
				
			||||||
 | 
					            if reportStr:
 | 
				
			||||||
 | 
					                print(reportStr)
 | 
				
			||||||
 | 
					            self.assertTrue('SyntaxError' not in reportStr)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_write_with_optimization(self):
 | 
					    def test_write_with_optimization(self):
 | 
				
			||||||
        import email
 | 
					        import email
 | 
				
			||||||
        packagedir = os.path.dirname(email.__file__)
 | 
					        packagedir = os.path.dirname(email.__file__)
 | 
				
			||||||
| 
						 | 
					@ -649,6 +658,26 @@ def test_write_python_directory(self):
 | 
				
			||||||
        finally:
 | 
					        finally:
 | 
				
			||||||
            shutil.rmtree(TESTFN2)
 | 
					            shutil.rmtree(TESTFN2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_write_python_directory_filtered(self):
 | 
				
			||||||
 | 
					        os.mkdir(TESTFN2)
 | 
				
			||||||
 | 
					        try:
 | 
				
			||||||
 | 
					            with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp:
 | 
				
			||||||
 | 
					                fp.write("print(42)\n")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp:
 | 
				
			||||||
 | 
					                fp.write("print(42 * 42)\n")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
 | 
				
			||||||
 | 
					                zipfp.writepy(TESTFN2, filterfunc=lambda fn:
 | 
				
			||||||
 | 
					                                                  not fn.endswith('mod2.py'))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                names = zipfp.namelist()
 | 
				
			||||||
 | 
					                self.assertCompiledIn('mod1.py', names)
 | 
				
			||||||
 | 
					                self.assertNotIn('mod2.py', names)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        finally:
 | 
				
			||||||
 | 
					            shutil.rmtree(TESTFN2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_write_non_pyfile(self):
 | 
					    def test_write_non_pyfile(self):
 | 
				
			||||||
        with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
 | 
					        with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
 | 
				
			||||||
            with open(TESTFN, 'w') as f:
 | 
					            with open(TESTFN, 'w') as f:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1582,7 +1582,8 @@ def writepy(self, pathname, basename="", filterfunc=None):
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        if filterfunc and not filterfunc(pathname):
 | 
					        if filterfunc and not filterfunc(pathname):
 | 
				
			||||||
            if self.debug:
 | 
					            if self.debug:
 | 
				
			||||||
                print('pathname "%s" skipped by filterfunc' % pathname)
 | 
					                label = 'path' if os.path.isdir(pathname) else 'file'
 | 
				
			||||||
 | 
					                print('%s "%s" skipped by filterfunc' % (label, pathname))
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
        dir, name = os.path.split(pathname)
 | 
					        dir, name = os.path.split(pathname)
 | 
				
			||||||
        if os.path.isdir(pathname):
 | 
					        if os.path.isdir(pathname):
 | 
				
			||||||
| 
						 | 
					@ -1611,6 +1612,10 @@ def writepy(self, pathname, basename="", filterfunc=None):
 | 
				
			||||||
                            self.writepy(path, basename,
 | 
					                            self.writepy(path, basename,
 | 
				
			||||||
                                         filterfunc=filterfunc)  # Recursive call
 | 
					                                         filterfunc=filterfunc)  # Recursive call
 | 
				
			||||||
                    elif ext == ".py":
 | 
					                    elif ext == ".py":
 | 
				
			||||||
 | 
					                        if filterfunc and not filterfunc(path):
 | 
				
			||||||
 | 
					                            if self.debug:
 | 
				
			||||||
 | 
					                                print('file "%s" skipped by filterfunc' % path)
 | 
				
			||||||
 | 
					                            continue
 | 
				
			||||||
                        fname, arcname = self._get_codename(path[0:-3],
 | 
					                        fname, arcname = self._get_codename(path[0:-3],
 | 
				
			||||||
                                                            basename)
 | 
					                                                            basename)
 | 
				
			||||||
                        if self.debug:
 | 
					                        if self.debug:
 | 
				
			||||||
| 
						 | 
					@ -1624,6 +1629,10 @@ def writepy(self, pathname, basename="", filterfunc=None):
 | 
				
			||||||
                    path = os.path.join(pathname, filename)
 | 
					                    path = os.path.join(pathname, filename)
 | 
				
			||||||
                    root, ext = os.path.splitext(filename)
 | 
					                    root, ext = os.path.splitext(filename)
 | 
				
			||||||
                    if ext == ".py":
 | 
					                    if ext == ".py":
 | 
				
			||||||
 | 
					                        if filterfunc and not filterfunc(path):
 | 
				
			||||||
 | 
					                            if self.debug:
 | 
				
			||||||
 | 
					                                print('file "%s" skipped by filterfunc' % path)
 | 
				
			||||||
 | 
					                            continue
 | 
				
			||||||
                        fname, arcname = self._get_codename(path[0:-3],
 | 
					                        fname, arcname = self._get_codename(path[0:-3],
 | 
				
			||||||
                                                            basename)
 | 
					                                                            basename)
 | 
				
			||||||
                        if self.debug:
 | 
					                        if self.debug:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue