ropensci / tidyhydat

An R package to import Water Survey of Canada hydrometric data and make it tidy
https://docs.ropensci.org/tidyhydat
Apache License 2.0
71 stars 20 forks source link

download_hydat() fails to update local hydat copy #180

Closed gdelaplante closed 2 years ago

gdelaplante commented 2 years ago

Working with a dated local version of hydat and version 0.5.6 of tidyhydat downloaded from CRAN OR from GitHub:

library(tidyhydat)

hy_version()
#Yields 2022-04-18 16:49:45 on my system

download_hydat(ask=FALSE)
#(Printed output)
# ✔ Downloading HYDAT to C:\Users\gtdelapl\AppData\Local\tidyhydat\tidyhydat
# • Downloading HYDAT.sqlite3 to C:\Users\gtdelapl\AppData\Local/tidyhydat/tidyhydat
# • Downloading version of HYDAT created on 2022-07-21
# |=============================================================================================================================================| 100%
# • Extracting HYDAT
# Error in file.rename(list.files(dl_hydat_here, pattern = "\\.sqlite3$",  : 
#                                   'from' and 'to' are of different lengths`

hy_version()
#Again yields 2022-04-18 16:49:45, but should return 2022-07-21

#Check again after downloading from ropensci/tidyhydat main branch:
remotes::install_github("ropensci/tidyhydat")
library(tidyhydat)
hy_version()
download_hydat(ask=FALSE)
#(Printed output)
# • Downloading HYDAT.sqlite3 to C:\Users\gtdelapl\AppData\Local/tidyhydat/tidyhydat
# • Downloading version of HYDAT created on 2022-07-21
# |=============================================================================================================================================| 100%
# • Extracting HYDAT
# Error in file.rename(list.files(dl_hydat_here, pattern = "\\.sqlite3$",  : 
#                                   'from' and 'to' are of different lengths

Downloading works if starting with an empty download directory (...Local/tidyhydat/tidyhydat) but fails again once Hydat.sqlite3 is created again:

#after deleting both files present in /tidyhydat folder:
download_hydat(ask=FALSE)
# • Extracting HYDAT
# ★ HYDAT successfully downloaded

hy_version()
#Yields 2022-07-21, correct.
#Looking in tidyhydat\tidyhydat folder shows only one file, Hydat.sqlite3.

#Downloading again yields the problem as before:
download_hydat(ask=FALSE)
# ✔ Downloading HYDAT to C:\Users\gtdelapl\AppData\Local\tidyhydat\tidyhydat
# • Downloading HYDAT.sqlite3 to C:\Users\gtdelapl\AppData\Local/tidyhydat/tidyhydat
# ✖ The existing local version of hydat, published on 2022-07-21, is the most recent version available.
# |=============================================================================================================================================| 100%
# • Extracting HYDAT
# Error in file.rename(list.files(dl_hydat_here, pattern = "\\.sqlite3$",  : 
#                                   'from' and 'to' are of different lengths

After failure, two files appear in the download directory where there should be one:

The issue evidently stems from lines 112:116 of download.R:

 # rename to consistent name
  file.rename(
    list.files(dl_hydat_here, pattern = "\\.sqlite3$", full.names = TRUE),
    hydat_path
  )

'from' is of length 2 (owing to the two files with .sqlite3 extension) while 'to' is of length one. This stems from the line above, utils::unzip(tmp, exdir = dl_hydat_here, overwrite = TRUE)), which creates a second file (the new database).

If I get time I'll fix the issue and submit a pull request in the next day or two!

boshek commented 2 years ago

Yeah I can see the issue. I think a good fix would be to simply ensure that the directory is empty before downloading and then spit out a good error message that you need to move or delete the existing file.

Prior to this we always simply overwrote the previous HYDAT. I think this is still right and that we don't want to preserve old versions of HYDAT on someone's computer but I was to raise that here as well.

To unblock you can of course simply empty the directory in hy_dir() and then run download_hydat() again.