Skip to content

Can't copy a dependency that has all_files=True and includes a directory #87

Closed
@gadenbuie

Description

@gadenbuie

This line throws an exception if src_file is a directory (or a link)

if not os.path.isfile(src_file):

If you make it past that (by checking with os.path.exists(src_file), this line throws:

shutil.copy2(src_file, target_file)

This issue is important because shiny's Bootstrap dependency uses all_files=True and includes a fonts/ subfolder that prevents a shiny app UI from being saved due to this error.

Reprex

from pathlib import Path

import htmltools

# create a dependency with a subdirectory ----
dep_dir = Path(__file__).parent / "the-dep"
dep_dir.mkdir(exist_ok=True)

dep_subdir = dep_dir / "subdir"
dep_subdir.mkdir(exist_ok=True)

with open(dep_dir / "script.js", "w") as f:
    f.write("console.log('hello from script.js')")

with open(dep_subdir / "style.css", "w") as f:
    f.write("body { background-color: lightblue; }")

dep = htmltools.HTMLDependency(
    name="dep",
    version="0.0.1",
    source={"subdir": str(dep_dir)},
    script={"src": "script.js"},
    all_files=True,
)

dep.copy_to(str(Path(__file__).parent / "the-dep-copy"), include_version=False)
# > Traceback (most recent call last):
# >   File "/Users/garrick/work/posit-dev/py-htmltools/_dev/issues/dep-dir/issue-dep-dir.py", line 26, in <module>
# >     dep.copy_to(str(Path(__file__).parent / "the-dep-copy"), include_version=False)
# >   File "/Users/garrick/work/posit-dev/py-htmltools/htmltools/_core.py", line 1613, in copy_to
# >     raise Exception(
# > Exception: Failed to copy HTML dependency dep-0.0.1 because /Users/garrick/work/posit-dev/py-htmltools/_dev/issues/dep-dir/the-dep/subdir doesn't exist.
# Use the dependency in an html fragment...
html = htmltools.tags.p(
    "Hello, world!",
    dep,
)

# ...that cannot be saved
html.save_html("index.html")
#> /work/posit-dev/py-htmltools/_dev/issues/dep-dir/issue-dep-dir.py
#> Traceback (most recent call last):
#>   File "/Users/garrick/work/posit-dev/py-htmltools/_dev/issues/dep-dir/issue-dep-dir.py", line 32, in <module>
#>     html.save_html("index.html")
#>   File "/Users/garrick/work/posit-dev/py-htmltools/htmltools/_core.py", line 823, in save_html
#>     return HTMLDocument(self).save_html(
#>            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#>   File "/Users/garrick/work/posit-dev/py-htmltools/htmltools/_core.py", line 1012, in save_html
#>     dep.copy_to(destdir, include_version=include_version)
#>   File "/Users/garrick/work/posit-dev/py-htmltools/htmltools/_core.py", line 1613, in copy_to
#>     raise Exception(
#> Exception: Failed to copy HTML dependency dep-0.0.1 because /Users/garrick/work/posit-dev/py-htmltools/_dev/issues/dep-dir/the-dep/subdir doesn't exist.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions