From 5cb4cd3371258bc321c30699dd74d24ac1746ff8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lorenz=20H=C3=BCdepohl?= <lorenz.huedepohl@rzg.mpg.de>
Date: Wed, 30 Oct 2019 12:04:02 +0100
Subject: [PATCH] Print notification when new commits are available

---
 .gitignore      |  1 +
 mpcdf_common.py | 40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)

diff --git a/.gitignore b/.gitignore
index 0d20b64..9923a65 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
+.remote_head_rev
 *.pyc
diff --git a/mpcdf_common.py b/mpcdf_common.py
index 79d4294..8c52cf0 100644
--- a/mpcdf_common.py
+++ b/mpcdf_common.py
@@ -47,6 +47,46 @@ prjconf_start_marker = "# Autogenerated by osc mpcdf_setup_repos, do not edit ti
 prjconf_end_marker = "# End of autogenerated section\n"
 
 
+def check_for_update():
+    import os
+    import sys
+    import time
+    from subprocess import check_output
+
+    if hasattr(sys.modules["mpcdf_common"], "checked_for_updates"):
+        return
+    else:
+        setattr(sys.modules["mpcdf_common"], "checked_for_updates", 1)
+
+    plugin_dir = os.path.dirname(os.path.realpath(__file__))
+    git_dir = os.path.join(plugin_dir, ".git")
+    local_rev = check_output(["git", "--git-dir", git_dir, "rev-parse", "HEAD"]).strip()
+
+    # Check for update on server just once a day
+    rev_file = os.path.join(plugin_dir, ".remote_head_rev")
+    try:
+        mtime = os.stat(rev_file).st_mtime
+    except FileNotFoundError:
+        mtime = 0
+
+    url = "https://gitlab.mpcdf.mpg.de/mpcdf/osc-plugins.git"
+
+    if time.time() - mtime > 86400:
+        server_rev, _ = check_output(["git", "ls-remote", url, "master"]).split(maxsplit=1)
+        with open(rev_file, "wb") as fd:
+            fd.write(server_rev)
+
+    with open(rev_file, "rb") as fd:
+        server_rev = fd.read().strip()
+        if server_rev != local_rev:
+            print(file=sys.stderr)
+            print("Your plugin directory is out-of-date, new commits available on", url, file=sys.stderr)
+            print(file=sys.stderr)
+
+
+check_for_update()
+
+
 def compiler_module(compiler_repo):
     return compiler_repo.replace("_", "/", 1).replace("_", ".")
 
-- 
GitLab