Skip to content

Commit 1ba9531

Browse files
committed
machine: Retrieve an unique identifier if one is known.
This commit adds a proper implementation for "machine.unique_id" for selected systems, as opposed to returning a fixed value in every case. On a Unix system there are several incompatible ways to retrieve something that can be called an unique machine identifier. However, the only semblance of a specification comes from freedesktop.org, which says that a file called "/etc/machine-id" has to exist and must contain a 32-digits long hexadecimal identifier. Given that the current specification is the DBus unique identifier retrieval mechanism made official, if the identifier file is not found the code will attempt to read the file provided by DBus. These changes only apply to Linux and recent BSD systems. On other systems the old, fixed value for the machine identifier will be returned instead - to avoid breaking compatibility with existing code. The specification used in this commit is available at https://www.freedesktop.org/software/systemd/man/latest/machine-id.html. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
1 parent d0511a4 commit 1ba9531

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

unix-ffi/machine/machine/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,16 @@
44

55

66
def unique_id():
7+
for base in ("/etc", "/var/lib/dbus"):
8+
try:
9+
with open(base + "/machine-id", "rb") as source:
10+
data = source.read(32)
11+
if len(data) == 32:
12+
for byte in data:
13+
if byte not in b"0123456789abcdef":
14+
break
15+
return data
16+
except OSError as e:
17+
if "ENOENT" not in str(e):
18+
raise
719
return b"upy-non-unique"

unix-ffi/machine/manifest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
metadata(version="0.2.2")
1+
metadata(version="0.2.3")
22

33
# Originally written by Paul Sokolovsky.
44

0 commit comments

Comments
 (0)