Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for .conanignore in conan config install #13269

Merged
merged 4 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions conans/client/conanignore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os
import fnmatch


class ConanIgnoreMatcher:
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
def __init__(self, conanignore_path):
self.conanignore_path = os.path.abspath(conanignore_path)
self._ignored_entries = {".conanignore"}
self._parse_conanignore()

def _parse_conanignore(self):
with open(self.conanignore_path, 'r') as conanignore:
for line in conanignore:
line_content = line.strip()
if line_content != "":
self._ignored_entries.add(line_content)

def matches(self, path):
for ignore_entry in self._ignored_entries:
if fnmatch.fnmatch(path, ignore_entry):
return True
return False
10 changes: 9 additions & 1 deletion conans/client/conf/config_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from conan.api.output import ConanOutput
from conans.client.downloaders.file_downloader import FileDownloader
from conans.client.conanignore import ConanIgnoreMatcher
from conans.errors import ConanException
from conans.util.files import mkdir, rmdir, remove, unzip, chdir
from conans.util.runners import detect_runner
Expand Down Expand Up @@ -99,10 +100,17 @@ def _process_folder(config, folder, cache):
raise ConanException("No such directory: '%s'" % str(folder))
if config.source_folder:
folder = os.path.join(folder, config.source_folder)
conanignore_path = os.path.join(folder, '.conanignore')
conanignore = None
if os.path.exists(conanignore_path):
conanignore = ConanIgnoreMatcher(conanignore_path)
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
for root, dirs, files in os.walk(folder):
# .git is always ignored by default, even if not present in .conanignore
dirs[:] = [d for d in dirs if d != ".git"]
for f in files:
_process_file(root, f, config, cache, folder)
rel_path = os.path.relpath(os.path.join(root, f), folder)
if conanignore is None or not conanignore.matches(rel_path):
_process_file(root, f, config, cache, folder)


def _process_download(config, cache, requester):
Expand Down
49 changes: 49 additions & 0 deletions conans/test/integration/command/config_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
import textwrap

from conan.api.conan_api import ConanAPI
from conans.model.conf import BUILT_IN_CONFS
Expand Down Expand Up @@ -45,3 +46,51 @@ def test_config_list():
assert f"{k}: {v}" in client.out
client.run("config list --format=json")
assert f"{json.dumps(BUILT_IN_CONFS, indent=4)}\n" == client.stdout


def test_config_install_conanignore():
tc = TestClient()
conanignore = textwrap.dedent("""
a/*
b/c/*
d/*
""")
tc.save({
'config_folder/.conanignore': conanignore,
"config_folder/a/test": '',
'config_folder/abracadabra': '',
'config_folder/b/bison': '',
'config_folder/b/a/test2': '',
'config_folder/b/c/helmet': '',
'config_folder/d/prix': '',
'config_folder/d/foo/bar': '',
'config_folder/foo': ''
})
def _assert_config_exists(path):
assert os.path.exists(os.path.join(tc.cache_folder, path))

def _assert_config_not_exists(path):
assert not os.path.exists(os.path.join(tc.cache_folder, path))

tc.run('config install config_folder')

_assert_config_not_exists(".conanignore")

_assert_config_not_exists("a")
_assert_config_not_exists("a/test")

_assert_config_exists("abracadabra")

_assert_config_exists("b")
_assert_config_exists("b/bison")
_assert_config_exists("b/a/test2")
_assert_config_not_exists("b/c/helmet")
_assert_config_not_exists("b/c")

_assert_config_not_exists("d/prix")
_assert_config_not_exists("d/foo/bar")
_assert_config_not_exists("d")

_assert_config_exists("foo")

os.listdir(tc.current_folder)