Hugo Deployment

12/10/19

Mood 🚧work work work
Tags Python

I decided I needed to “get er done” with some things about the site that were bugging me so I could stop tinkering with it so much and focus on writing/blogging.

Until now, each time I updated neonaut I re-uploaded the entire site. This bugged me because it takes a few minutes to upload several hundred files and I was uploading a lot of redundant files.

By default Hugo rebuilds the entire site and incremental builds are not supported. Hugo builds a site so quickly the developers do not feel incremental builds are needed at this time. I determined the only options for incremental deployment are to migrate to a different generator, like Nikola, or implement directory comparison on my end.

I was able to use a modified version of danasmera’s script on How do you compare two folders and copy the difference to a third folder? to do just that. The script compares the public directory to public_previous, puts all new and changed files into a folder called public_updates. It then uploads the new stuff.

While testing I noticed the script did not upload new directories, only new files. This is because the iteration is based on dircmp’s common_dirs.

if len(dircomp.common_dirs) > 0:

There is no way to select ONLY left directories. dircomp.left_only provides a list of both file and subdirectory names as strings. But if we add the new directories to public_previous directory filecmp will find them, see they are empty, and register all the missing files.

I have at my disposal dircomp.left_only, which will list those as strings, and dircomp.common_dirs, which tells me which are already shared. Any string with a “.” is a file, if it doesn’t have a period it’s a folder name.

new_subdirs = [item for item in dircomp.left_only if "." not in item]
for sd in new_subdirs:
    os.makedirs(dir2 + "/" + sd)

Unfortunately I ran into some persistent problems with file permissions in Windows (typically WinError 5) when I attempted to rename, create, and copy various directories. Even running the script as administrator did not help. It can be done from the command line but it’s something I’d like to reliably automate. This helps but in the meantime…

C:\Hugo\Sites\neocities> rmdir /Q /S public_previous
C:\Hugo\Sites\neocities> Xcopy /E /I public public_previous