From 4dbf27efee2386b8faf94e1c80b76f477fb5ab3a Mon Sep 17 00:00:00 2001
From: Jeremy Whiting <jeremy.whiting@collabora.com>
Date: Fri, 11 Mar 2022 13:09:22 -0700
Subject: [PATCH] Implement find_hook from utils.c and use to run identify
 hook.

If unable to run devkit-1-identify just get hostname from OS.
---
 src/steamos-devkit-service.py | 63 ++++++++++++++++++++++++++++++++++-
 1 file changed, 62 insertions(+), 1 deletion(-)

diff --git a/src/steamos-devkit-service.py b/src/steamos-devkit-service.py
index 5df0e13..c2bde91 100644
--- a/src/steamos-devkit-service.py
+++ b/src/steamos-devkit-service.py
@@ -24,14 +24,74 @@
 #SOFTWARE.
 
 from http.server import BaseHTTPRequestHandler
+import configparser
 import json
+import os
+import platform
 import socketserver
+import subprocess
 import urllib.parse
 
 SERVICE_PORT = 32000
+PACKAGE = "steamos-devkit-service"
+DEVKIT_HOOKS_DIR = "/usr/share/steamos-devkit/hooks"
+
 # root until config is loaded and told otherwise, etc.
 entry_point_user = "root"
-properties = {"key": "value"}
+properties = {}
+machine_name = ''
+hook_dirs = []
+use_default_hooks = True
+
+global_config = configparser.ConfigParser()
+global_config.read(["/etc/steamos-devkit/steamos-devkit.conf", "/usr/share/steamos-devkit/steamos-devkit.conf"])
+
+def find_hook(hook_dirs, use_default_hooks, name):
+    # First see if it exists in the given paths.
+    for path in hook_dirs:
+        test_path = os.path.join(path, name)
+        if (os.path.exists(test_path) and os.access(test_path, os.X_OK)):
+            return test_path
+    
+    if (not use_default_hooks):
+        print("Error: Unable to find hook for {} in hook directories\n".format(name))
+        return None
+
+    test_path = "/etc/{}/hooks/{}".format(PACKAGE, name)
+    if (os.path.exists(test_path) and os.access(test_path, os.X_OK)):
+        return test_path
+
+    test_path = "{}/{}".format(DEVKIT_HOOKS_DIR, name)
+    if (os.path.exists(test_path) and os.access(test_path, os.X_OK)):
+        return test_path
+
+    test_path = "{}/{}.sample".format(DEVKIT_HOOKS_DIR, name)
+    if (os.path.exists(test_path) and os.access(test_path, os.X_OK)):
+        return test_path
+
+    test_path = "{}/../hooks/{}".format(os.path.dirname(os.path.realpath(__file__)), name)
+    if (os.path.exists(test_path) and os.access(test_path, os.X_OK)):
+        return test_path
+
+    print("Error:: Unable to find hook for {} in /etc/{}/hooks or {}".format(name, PACKAGE, DEVKIT_HOOKS_DIR))
+    return None
+
+# Run devkit-1-identify hook to get hostname, otherwise use default platform.node()
+identify_hook = find_hook(hook_dirs, use_default_hooks, "devkit-1-identify")
+if (identify_hook):
+    # Run hook and parse machine_name out
+    p = subprocess.Popen(identify_hook, shell=False, stdout=subprocess.PIPE)
+    output = ''
+    for line in p.stdout:
+        textline = line.decode(encoding='utf-8', errors="ignore")
+        output += textline
+    p.wait()
+    output_object = json.loads(output)
+    if ('machine_name' in output_object):
+        machine_name = output_object["machine_name"]
+
+if not machine_name:
+    machine_name = platform.node()
 
 class DevkitHandler(BaseHTTPRequestHandler):
     def _send_headers(self, code, type):
@@ -84,6 +144,7 @@ class DevkitService:
 
         self.httpd = socketserver.TCPServer(("", self.port), DevkitHandler, bind_and_activate=False)
         print("serving at port: {}".format(self.port))
+        print("machine name: {}".format(machine_name))
         self.httpd.allow_reuse_address = True
         self.httpd.server_bind()
         self.httpd.server_activate()
-- 
GitLab