diff --git a/python3/Makefile b/python3/Makefile index 8f34cb8e107..91479a31d8d 100644 --- a/python3/Makefile +++ b/python3/Makefile @@ -13,6 +13,7 @@ install: $(IPROG) -d $(DESTDIR)$(PLUGINDIR) + $(IDATA) packages/inventory.py $(DESTDIR)$(SITE3_DIR)/ $(IDATA) packages/observer.py $(DESTDIR)$(SITE3_DIR)/ $(IPROG) libexec/host-display $(DESTDIR)$(LIBEXECDIR) diff --git a/python3/packages/inventory.py b/python3/packages/inventory.py new file mode 100644 index 00000000000..87847cf5cde --- /dev/null +++ b/python3/packages/inventory.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 + +""" +inventory.py + +This module defines functions to read and parse constants from the xensource-inventory file. +""" +import sys + +INVENTORY = "@INVENTORY@" +INSTALLATION_UUID = "INSTALLATION_UUID" + + +def read_kvpairs(filename): + """Read in a file of key-value pairs in the format used by the inventory file""" + all_entries = {} + with open(filename, 'r', encoding='utf-8') as f: + for line in f: + equals = line.index("=") + key = line[:equals] + value = line[equals+1:].strip().strip("'") + all_entries[key] = value + return all_entries + + +def parse(): + """Return the contents of the xensource inventory file as a dictionary""" + try: + return read_kvpairs(INVENTORY) + except FileNotFoundError as e: + print("Error: File '{}' not found. {}".format(INVENTORY, e), file=sys.stderr) + return {} + + +def get_localhost_uuid(): + """Return the UUID of the local host""" + return parse()[INSTALLATION_UUID] diff --git a/scripts/Makefile b/scripts/Makefile index 705b161158a..8204a2a0e66 100644 --- a/scripts/Makefile +++ b/scripts/Makefile @@ -155,12 +155,10 @@ install: ifneq ($(BUILD_PY2), NO) $(IDATA) examples/python/XenAPIPlugin.py $(DESTDIR)$(SITE_DIR)/ $(IDATA) examples/python/XenAPI/XenAPI.py $(DESTDIR)$(SITE_DIR)/ - $(IDATA) examples/python/inventory.py $(DESTDIR)$(SITE_DIR)/ endif $(IDATA) examples/python/XenAPIPlugin.py $(DESTDIR)$(SITE3_DIR)/ sed -i 's/#!\/usr\/bin\/python/#!\/usr\/bin\/python3/' $(DESTDIR)$(SITE3_DIR)/XenAPIPlugin.py $(IDATA) examples/python/XenAPI/XenAPI.py $(DESTDIR)$(SITE3_DIR)/ - $(IDATA) examples/python/inventory.py $(DESTDIR)$(SITE3_DIR)/ $(IPROG) examples/python/shell.py $(DESTDIR)$(LIBEXECDIR)/shell.py # YUM plugins $(IPROG) yum-plugins/accesstoken.py $(DESTDIR)$(YUMPLUGINDIR) diff --git a/scripts/examples/python/inventory.py b/scripts/examples/python/inventory.py deleted file mode 100644 index 9fd645b5d32..00000000000 --- a/scripts/examples/python/inventory.py +++ /dev/null @@ -1,32 +0,0 @@ -# Simple functions to read the constants from the xensource-inventory file - -INVENTORY="@INVENTORY@" -INSTALLATION_UUID="INSTALLATION_UUID" - - -def read_kvpairs(filename): - """Read in a file of key-value pairs in the format used by the inventory file""" - f = open(filename) - all_entries = {} - try: - for line in f.readlines(): - equals = line.index("=") - key = line[0:equals] - value = line[equals+1:].strip().strip("'") - all_entries[key] = value - finally: - f.close() - return all_entries - - -def parse(): - """Return the contents of the xensource inventory file as a dictionary""" - try: - return read_kvpairs(INVENTORY) - except: - return {} - - -def get_localhost_uuid(): - """Return the UUID of the local host""" - return parse()[INSTALLATION_UUID]