# Buggy code
def _link_folders(src, dst, linked_folders):
    for linked_folder in linked_folders:
        link = os.readlink(os.path.join(src, linked_folder))
        dst_link = os.path.join(dst, linked_folder)
        try:
            # Remove the previous symlink
            os.remove(dst_link)
        except OSError:
            pass
        # link is a string relative to linked_folder
        # e.j: os.symlink("test/bar", "./foo/test_link") will create a link to foo/test/bar in ./foo/test_link
        os.symlink(link, dst_link)
    # Remove empty links
    for linked_folder in linked_folders:
        dst_link = os.path.join(dst, linked_folder)
        abs_path = os.path.realpath(dst_link)
        if not os.path.exists(abs_path):
            os.remove(dst_link)


# Traceback (OSError)
...
PROJECT: Generator txt created conanbuildinfo.txt
PROJECT: Generated conaninfo.txt
Traceback (most recent call last):
File "/home/fernando/.local/lib/python2.7/site-packages/conans/client/command.py", line 1182, in run
method(args[0][1:])
File "/home/fernando/.local/lib/python2.7/site-packages/conans/client/command.py", line 325, in install
install_folder=args.install_folder)
File "/home/fernando/.local/lib/python2.7/site-packages/conans/client/conan_api.py", line 77, in wrapper
return f(*args, **kwargs)
File "/home/fernando/.local/lib/python2.7/site-packages/conans/client/conan_api.py", line 465, in install
no_imports=no_imports)
File "/home/fernando/.local/lib/python2.7/site-packages/conans/client/manager.py", line 344, in install
run_imports(conanfile, install_folder, output)
File "/home/fernando/.local/lib/python2.7/site-packages/conans/client/importer.py", line 82, in run_imports
conanfile.imports()
File "/home/fernando/.local/lib/python2.7/site-packages/conans/client/loader_parse.py", line 184, in imports
conan_file.copy(*import_params)
File "/home/fernando/.local/lib/python2.7/site-packages/conans/client/importer.py", line 160, in __call__
excludes=excludes, keep_path=keep_path)
File "/home/fernando/.local/lib/python2.7/site-packages/conans/client/file_copier.py", line 83, in __call__
self._link_folders(src, dst, link_folders)
File "/home/fernando/.local/lib/python2.7/site-packages/conans/client/file_copier.py", line 149, in _link_folders
os.symlink(link, dst_link)
OSError: [Errno 2] No such file or directory

ERROR: [Errno 2] No such file or directory

# Fixed code
def _link_folders(src, dst, linked_folders):
    for linked_folder in linked_folders:
        link = os.readlink(os.path.join(src, linked_folder))
        dst_link = os.path.join(dst, linked_folder)
        try:
            # Remove the previous symlink
            os.remove(dst_link)
        except OSError:
            pass
        # link is a string relative to linked_folder
        # e.j: os.symlink("test/bar", "./foo/test_link") will create a link to foo/test/bar in ./foo/test_link
        mkdir(os.path.dirname(dst_link))
        os.symlink(link, dst_link)
    # Remove empty links
    for linked_folder in linked_folders:
        dst_link = os.path.join(dst, linked_folder)
        abs_path = os.path.realpath(dst_link)
        if not os.path.exists(abs_path):
            os.remove(dst_link)