Skip to content

Commit

Permalink
New rm by pattern tool (#11443)
Browse files Browse the repository at this point in the history
  • Loading branch information
lasote committed Jun 13, 2022
1 parent 27a4725 commit cdbebf9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
4 changes: 2 additions & 2 deletions conan/tools/files/__init__.py
@@ -1,5 +1,5 @@
from conan.tools.files.files import load, save, mkdir, rmdir, ftp_download, download, get, rename, \
chdir, unzip, replace_in_file, collect_libs, check_md5, check_sha1, check_sha256
from conan.tools.files.files import load, save, mkdir, rmdir, rm, ftp_download, download, get, \
rename, chdir, unzip, replace_in_file, collect_libs, check_md5, check_sha1, check_sha256

from conan.tools.files.patches import patch, apply_conandata_patches
from conan.tools.files.cpp_package import CppPackage
Expand Down
10 changes: 10 additions & 0 deletions conan/tools/files/files.py
Expand Up @@ -67,6 +67,16 @@ def rmdir(conanfile, path):
_internal_rmdir(path)


def rm(conanfile, pattern, folder, recursive=False):
for root, _, filenames in os.walk(folder):
for filename in filenames:
if fnmatch(filename, pattern):
fullname = os.path.join(root, filename)
os.unlink(fullname)
if not recursive:
break


def get(conanfile, url, md5='', sha1='', sha256='', destination=".", filename="",
keep_permissions=False, pattern=None, verify=True, retry=None, retry_wait=None,
auth=None, headers=None, strip_root=False):
Expand Down
66 changes: 66 additions & 0 deletions conans/test/unittests/tools/files/test_rm.py
@@ -0,0 +1,66 @@
import os

# Check it is importable from tools
from conan.tools.files import rm
from conans.client.tools.files import chdir
from conans.test.utils.test_files import temp_folder
from conans.util.files import save_files


def test_remove_files_by_mask_recursively():
tmpdir = temp_folder()

with chdir(tmpdir):
os.makedirs("subdir")
os.makedirs("dir.pdb")
os.makedirs(os.path.join("subdir", "deepdir"))

save_files(tmpdir, {"1.txt": "",
"1.pdb": "",
"1.pdb1": "",
os.path.join("subdir", "2.txt"): "",
os.path.join("subdir", "2.pdb"): "",
os.path.join("subdir", "2.pdb1"): "",
os.path.join("subdir", "deepdir", "3.txt"): "",
os.path.join("subdir", "deepdir", "3.pdb"): "",
os.path.join("subdir", "deepdir", "3.pdb1"): ""})

rm(None, "*.sh", tmpdir, recursive=True)

removed_files = rm(None, "*.pdb", tmpdir, recursive=True)

assert os.path.isdir(os.path.join(tmpdir, "dir.pdb"))

assert os.path.isfile(os.path.join(tmpdir, "1.txt"))
assert not os.path.isfile(os.path.join(tmpdir, "1.pdb"))
assert os.path.isfile(os.path.join(tmpdir, "1.pdb1"))

assert os.path.isfile(os.path.join(tmpdir, "subdir", "2.txt"))
assert not os.path.isfile(os.path.join(tmpdir, "subdir", "2.pdb"))
assert os.path.isfile(os.path.join(tmpdir, "subdir", "2.pdb1"))

assert os.path.isfile(os.path.join(tmpdir, "subdir", "deepdir", "3.txt"))
assert not os.path.isfile(os.path.join(tmpdir, "subdir", "deepdir", "3.pdb"))
assert os.path.isfile(os.path.join(tmpdir, "subdir", "deepdir", "3.pdb1"))

rm(None, "*.pdb", tmpdir, recursive=True)


def test_remove_files_by_mask_non_recursively():
tmpdir = temp_folder()
with chdir(tmpdir):
os.makedirs("subdir")

save_files(tmpdir, {"1.txt": "",
"1.pdb": "",
"1.pdb1": "",
os.path.join("subdir", "2.txt"): "",
os.path.join("subdir", "2.pdb"): "",
os.path.join("subdir", "2.pdb1"): ""})

rm(None, "*.pdb", tmpdir)
assert not os.path.exists(os.path.join(tmpdir, "1.pdb"))
assert os.path.exists(os.path.join(tmpdir, "subdir", "2.pdb"))

assert os.path.exists(os.path.join(tmpdir, "1.txt"))
assert os.path.exists(os.path.join(tmpdir, "subdir", "2.txt"))

0 comments on commit cdbebf9

Please sign in to comment.