def test_iglob(self): self.assertEqual(list(iglob(jp(self.tmp_dir, "*.py"))), [jp(self.tmp_dir, "x.py")]) self.assertEqual( list(iglob(jp(self.tmp_dir, "**", "*.py"), recursive=True)), [ jp(self.tmp_dir, "x.py"), jp(self.tmp_dir, "a", "y.py"), jp(self.tmp_dir, "a", "b", "z.py") ])
def apply_on_files(start_directory, closure, globs, *additional_closure_arguments, **keyword_closure_arguments): for glob in globs: for absolute_file_name in iglob(normcase( os.path.join(escape(start_directory), glob)), recursive=True): if os.path.isdir(absolute_file_name): continue relative_file_name = os.path.relpath(absolute_file_name, start_directory) closure(absolute_file_name, relative_file_name, *additional_closure_arguments, **keyword_closure_arguments)
def vendorize(project, reactor, logger): target_dir = project.expand_path("$vendorize_target_dir") packages = [ Dependency(p) for p in as_list(project.get_property("vendorize_packages")) ] clean = project.get_property("vendorize_clean_target_dir") logfile = project.expand_path("$dir_logs", "vendorize.log") logger.info("Will vendorize packages %r into %r%s", packages, target_dir, " (cleaning)" if clean else "") if clean: rmtree(target_dir, ignore_errors=False) makedirs(target_dir, exist_ok=True) reactor.pybuilder_venv.install_dependencies(packages, install_log_path=logfile, package_type="vendorized", target_dir=target_dir, ignore_installed=True) # Vendorize _vendorize(target_dir, logger) if project.get_property("vendorize_collect_licenses"): licenses_content = "" for p in _list_metadata_dirs(target_dir): package_name, _ = splitext(basename(p)) lic_file = None for f in ("LICENSE", "LICENSE.txt"): f = jp(p, f) if exists(f): lic_file = f break if not lic_file: logger.warn("No license file found in package %r", package_name) continue with open(lic_file, "rt") as f: licenses_content += "%s\n==========\n%s\n\n" % (package_name, f.read()) logger.debug("Collected license file for package %r", package_name) with open(project.expand_path("$vendorize_licenses"), "wt") as f: f.write(licenses_content) # Cleanup globs for g in project.get_property("vendorize_cleanup_globs"): for p in iglob(jp(target_dir, g), recursive=True): print(p) if isdir(p): rmtree(p) else: unlink(p) # Cleanup metadata for p in _list_metadata_dirs(target_dir): if isdir(p): rmtree(p) else: unlink(p) # Populate names after cleanup cleaned_up_packages = list( chain((basename(dirname(f)) for f in iglob(jp(target_dir, "*", "__init__.py"))), (basename(f)[:-3] for f in iglob(jp(target_dir, "*.py"))))) with open(jp(target_dir, "__init__.py"), "wt") as init_py: init_py.write("__names__ = %r\n" % sorted(cleaned_up_packages))
def _list_metadata_dirs(vendorized_path): return chain(iglob(jp(vendorized_path, "*.egg-info")), iglob(jp(vendorized_path, "*.dist-info")))