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

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

Closed
gadenbuie opened this issue May 10, 2024 · 0 comments · Fixed by #88
Closed

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

gadenbuie opened this issue May 10, 2024 · 0 comments · Fixed by #88
Assignees

Comments

@gadenbuie
Copy link
Contributor

gadenbuie commented May 10, 2024

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.
@gadenbuie gadenbuie changed the title Can't copy a dependency that has includes a directory Can't copy a dependency that has all_files=True and includes a directory May 10, 2024
@gadenbuie gadenbuie self-assigned this May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant