Skip to content

Commit

Permalink
CP-50091: Moved inventory.py from scripts/examples/python/ to python3…
Browse files Browse the repository at this point in the history
…/packages/inventory.py

- Fixed bare-except, unspecified-encoding  and indentation issue

Signed-off-by: Ashwinh <ashwin.h@cloud.com>
  • Loading branch information
ashwin9390 committed Jun 27, 2024
1 parent f80b126 commit 39a5384
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 34 deletions.
1 change: 1 addition & 0 deletions python3/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 37 additions & 0 deletions python3/packages/inventory.py
Original file line number Diff line number Diff line change
@@ -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]
2 changes: 0 additions & 2 deletions scripts/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 0 additions & 32 deletions scripts/examples/python/inventory.py

This file was deleted.

0 comments on commit 39a5384

Please sign in to comment.