commit 165f75f257d9ba30007f410e71763a8b9869d8df
parent 7aa98e51c71c648987fd0a4d94b98d71c216f283
Author: oscarbenedito <oscar@obenedito.org>
Date: Sun, 8 Mar 2020 17:24:45 +0100
Added Hugo post-script
Diffstat:
8 files changed, 94 insertions(+), 43 deletions(-)
diff --git a/git-backup/git-backup.py b/git-backup/git-backup.py
@@ -91,10 +91,10 @@ if 'github.com' in tokens:
repositories = get_repositories_data_github(url, tokens['github.com'], page)
# custom
-if os.path.exists("custom_directories.json"):
- custom_file = open('custom_directories.json', 'r')
- repositories = json.load(custom_file)
- custom_file.close()
+if os.path.exists('custom_directories.json'):
+ with open('custom_directories.json', 'r') as custom_file:
+ repositories = json.load(custom_file)
+
for repository in repositories:
clone_dir = 'repositories/' + repository['host'] + '/' + repository['path']
print(repository['host'] + '/' + repository['path'])
diff --git a/miscellanea/.gitignore b/miscellanea/.gitignore
@@ -0,0 +1 @@
+login_api_token.txt
diff --git a/scripts/login-notify.sh b/miscellanea/login-notify.sh
diff --git a/scripts/.gitignore b/scripts/.gitignore
@@ -1,2 +0,0 @@
-website_api_token.txt
-login_api_token.txt
diff --git a/scripts/deploy_website.sh b/scripts/deploy_website.sh
@@ -1,37 +0,0 @@
-#!/usr/bin/env sh
-# Copyright (C) 2020 Oscar Benedito
-#
-# This file is part of Utilities.
-#
-# Utilities is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# Utilities is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with Utilities. If not, see <https://www.gnu.org/licenses/>.
-
-# Script to deploy a website built with Hugo.
-
-HUGO_PATH="/srv/oscarbenedito.com"
-GOTIFY_DOMAIN="gotify.oscarbenedito.com"
-
-git -C $HUGO_PATH pull
-rm -rf $HUGO_PATH/public
-rm -rf $HUGO_PATH/resources
-hugo -s $HUGO_PATH --gc
-
-API_TOKEN="$(cat "$(dirname "$(realpath "$0")")/website_api_token.txt")"
-TITLE="Web update triggered"
-MESSAGE="Git hooks triggered an update of the website."
-
-curl -X POST "https://$GOTIFY_DOMAIN/message?token=$API_TOKEN" \
- -F "title=$TITLE" \
- -F "message=$MESSAGE" \
- -F "priority=5" \
- >/dev/null 2>&1
diff --git a/website-deployment/.gitignore b/website-deployment/.gitignore
@@ -0,0 +1,2 @@
+website_api_token.txt
+post_hugo_script.json
diff --git a/website-deployment/deploy-website.sh b/website-deployment/deploy-website.sh
@@ -0,0 +1,39 @@
+#!/usr/bin/env sh
+# Copyright (C) 2020 Oscar Benedito
+#
+# This file is part of Utilities.
+#
+# Utilities is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Utilities is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Utilities. If not, see <https://www.gnu.org/licenses/>.
+
+# Script to deploy a website built with Hugo.
+
+HUGO_PATH="/srv/oscarbenedito.com"
+GOTIFY_DOMAIN="gotify.oscarbenedito.com"
+FILE_DIR="$(dirname "$(realpath "$0")")"
+
+git -C $HUGO_PATH pull
+rm -rf $HUGO_PATH/public
+rm -rf $HUGO_PATH/resources
+hugo -s $HUGO_PATH --gc
+$FILE_DIR/post-hugo-script.py $FILE_DIR/post_hugo_script.json
+
+API_TOKEN="$(cat "$FILE_DIR/website_api_token.txt")"
+TITLE="Web update triggered"
+MESSAGE="Git hooks triggered an update of the website."
+
+curl -X POST "https://$GOTIFY_DOMAIN/message?token=$API_TOKEN" \
+ -F "title=$TITLE" \
+ -F "message=$MESSAGE" \
+ -F "priority=5" \
+ >/dev/null 2>&1
diff --git a/website-deployment/post-hugo-script.py b/website-deployment/post-hugo-script.py
@@ -0,0 +1,48 @@
+#!/usr/bin/env python3
+# Copyright (C) 2020 Oscar Benedito
+#
+# This file is part of Utilities.
+#
+# Utilities is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Utilities is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Utilities. If not, see <https://www.gnu.org/licenses/>.
+
+import os
+import sys
+import re
+import json
+import shutil
+
+HUGO_OUT_DIR = '/srv/oscarbenedito.com/public'
+
+if len(sys.argv) != 2:
+ print("Usage:\n", sys.argv[0], "file.json")
+ exit(1)
+
+with open(sys.argv[1], 'r') as f:
+ data = json.load(f)
+
+for dir in data['directories']:
+ shutil.rmtree(os.path.join(HUGO_OUT_DIR, dir))
+
+for file in data['files']:
+ os.remove(os.path.join(HUGO_OUT_DIR, file))
+
+with open(os.path.join(HUGO_OUT_DIR, 'sitemap.xml'), 'r') as f:
+ sitemap = f.read()
+
+for line in data['sitemap']:
+ block = '\n[ \t]*<url>\n[ \t]*<loc>https://oscarbenedito\.com' + line + '</loc>\n(?:[ \t]*<lastmod>.*</lastmod>\n)?[ \t]*</url>\n[ \t]*'
+ sitemap = re.sub(block, '', sitemap)
+
+with open(os.path.join(HUGO_OUT_DIR, 'sitemap.xml'), 'w') as f:
+ f.write(sitemap)