2022-11-11 20:21:52 +01:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-10-27 13:40:31 +01:00
|
|
|
# Very inspired by https://github.com/bgawalt/uuidBabyName
|
|
|
|
import re, uuid, random, os
|
|
|
|
|
|
|
|
consonants = "bcdfgjklmnpqrstvwxy"
|
|
|
|
vowels = ["a", "e", "i", "o", "u", "ai", "eo", "ou", "io", "au", "ao", "ui", "oa"]
|
|
|
|
|
|
|
|
combos = []
|
|
|
|
for con in consonants:
|
|
|
|
for vow in vowels:
|
|
|
|
combos.append(con + vow)
|
|
|
|
combos += ["puo", "loi", "boi", "roi", "toi", "foi", "poi", "tuo", "ruo"]
|
|
|
|
|
|
|
|
|
|
|
|
def popHexPair(s):
|
|
|
|
if len(s) < 3:
|
|
|
|
num = int(s, 16)
|
|
|
|
rest = ""
|
|
|
|
else:
|
|
|
|
num = int(s[0:2], 16)
|
|
|
|
rest = s[2:]
|
|
|
|
return (num, rest)
|
|
|
|
|
|
|
|
|
|
|
|
def uuidMachineName(machine="", type=1):
|
|
|
|
"""Generates a universally unique name, including the machine name
|
2020-12-02 19:25:59 +01:00
|
|
|
|
2020-10-27 13:40:31 +01:00
|
|
|
When using a type 4 id, the machine name is not taken into account as the uuid is completely random
|
|
|
|
|
|
|
|
Args:
|
|
|
|
machine (str, optional): hex string that defines a machine serial number. Defaults to "".
|
|
|
|
type (int, optional): uuid type to use (1 is node dependant, 4 is completely random). Defaults to 1.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: universally unique name
|
|
|
|
"""
|
|
|
|
if type == 4:
|
|
|
|
id = str(uuid.uuid4())
|
2021-11-19 13:31:24 +01:00
|
|
|
elif machine == "":
|
|
|
|
id = str(uuid.uuid1())
|
2020-10-27 13:40:31 +01:00
|
|
|
else:
|
2021-11-19 13:31:24 +01:00
|
|
|
id = str(uuid.uuid1(node=int(str(machine)[-12:], 16)))
|
2020-10-27 13:40:31 +01:00
|
|
|
name = ""
|
|
|
|
x = id.rsplit("-", 1)
|
|
|
|
machine = x[1]
|
|
|
|
x = x[0]
|
|
|
|
x = x.replace("-", "")
|
|
|
|
count = 0
|
|
|
|
while len(x) > 0:
|
|
|
|
tup = popHexPair(x)
|
|
|
|
n = tup[0]
|
2020-11-12 19:04:25 +01:00
|
|
|
if count >= random.randint(2, 4): # nosec
|
2020-10-27 13:40:31 +01:00
|
|
|
name += " "
|
|
|
|
count = 0
|
|
|
|
name += combos[n]
|
|
|
|
count += 1
|
|
|
|
x = tup[1]
|
|
|
|
|
|
|
|
name += " "
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
while len(machine) > 0:
|
|
|
|
tup = popHexPair(machine)
|
|
|
|
n = tup[0]
|
|
|
|
if count == 3:
|
|
|
|
name += " "
|
|
|
|
count = 0
|
|
|
|
name += combos[n]
|
|
|
|
count += 1
|
|
|
|
machine = tup[1]
|
|
|
|
return name.title()
|
|
|
|
|
|
|
|
|
2021-02-26 16:49:28 +01:00
|
|
|
def uuidMachine(machine="", type=1):
|
|
|
|
"""Generates a universally unique id, including the machine id
|
|
|
|
|
|
|
|
When using a type 4 id, the machine name is not taken into account as the uuid is completely random
|
|
|
|
|
|
|
|
Args:
|
|
|
|
machine (str, optional): hex string that defines a machine serial number. Defaults to "".
|
|
|
|
type (int, optional): uuid type to use (1 is node dependant, 4 is completely random). Defaults to 1.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: universally unique id
|
|
|
|
"""
|
|
|
|
if type == 4:
|
2021-11-19 13:31:24 +01:00
|
|
|
return str(uuid.uuid4())
|
2021-02-26 16:49:28 +01:00
|
|
|
else:
|
2021-11-19 13:31:24 +01:00
|
|
|
return (
|
|
|
|
str(uuid.uuid1())
|
|
|
|
if machine == ""
|
|
|
|
else str(uuid.uuid1(node=int(str(machine)[-12:], 16)))
|
|
|
|
)
|
2021-02-26 16:49:28 +01:00
|
|
|
|
|
|
|
|
2020-10-27 13:40:31 +01:00
|
|
|
def uuidName():
|
|
|
|
"""Generates a universally unique name, without the machine unique part
|
|
|
|
When used alone, this function can have collisions with other uuname generated on other machines.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: universally unique name, machine dependant
|
|
|
|
"""
|
|
|
|
id = str(uuid.uuid1())
|
|
|
|
name = ""
|
|
|
|
x = id.rsplit("-", 1)
|
|
|
|
x = x[0]
|
|
|
|
x = x.replace("-", "")
|
|
|
|
count = 0
|
|
|
|
while len(x) > 0:
|
|
|
|
tup = popHexPair(x)
|
|
|
|
n = tup[0]
|
2020-11-12 19:04:25 +01:00
|
|
|
if count >= random.randint(2, 4): # nosec
|
2020-10-27 13:40:31 +01:00
|
|
|
name += " "
|
|
|
|
count = 0
|
|
|
|
name += combos[n]
|
|
|
|
count += 1
|
|
|
|
x = tup[1]
|
|
|
|
|
|
|
|
return name.title()
|
|
|
|
|
|
|
|
|
|
|
|
def machineName(machine=""):
|
|
|
|
"""Generates a machine name based on the same conversion mechanism as other functions in this module
|
|
|
|
|
|
|
|
Args:
|
|
|
|
machine (str, optional): hex string that defines a machine serial number. Defaults to "".
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: machine name
|
|
|
|
"""
|
|
|
|
if machine == "":
|
|
|
|
machine = str(uuid.getnode())
|
|
|
|
name = ""
|
|
|
|
count = 0
|
|
|
|
machine = machine[-12:]
|
|
|
|
while len(machine) > 0:
|
|
|
|
tup = popHexPair(machine)
|
|
|
|
n = tup[0]
|
|
|
|
if count == 3:
|
|
|
|
name += " "
|
|
|
|
count = 0
|
|
|
|
name += combos[n]
|
|
|
|
count += 1
|
|
|
|
machine = tup[1]
|
|
|
|
return name.title()
|
|
|
|
|
|
|
|
|
|
|
|
def getSerial():
|
|
|
|
"""Returns a serial number for the machine if run on a Raspberry Pi, otherwise a MAC address
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: serial number or MAC address
|
|
|
|
"""
|
2021-11-19 13:31:24 +01:00
|
|
|
if not os.path.exists("/sys/firmware/devicetree/base/serial-number"):
|
2020-10-27 13:40:31 +01:00
|
|
|
return str(uuid.getnode())
|
|
|
|
|
2021-11-19 13:31:24 +01:00
|
|
|
with open("/sys/firmware/devicetree/base/serial-number", "r") as serial_file:
|
|
|
|
return serial_file.readline().strip("\x00")
|
|
|
|
|
2020-10-27 13:40:31 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
print("Type 4:")
|
|
|
|
print(uuidMachineName(type=4))
|
|
|
|
print("Type 1:")
|
|
|
|
print(uuidName())
|
|
|
|
print(uuidMachineName())
|
|
|
|
print(machineName())
|
|
|
|
print(uuidMachineName(machine=getSerial()))
|
|
|
|
print(machineName(machine=getSerial()))
|
|
|
|
print(uuidMachineName(machine="10000000e108bd59"))
|
|
|
|
print(machineName(machine="10000000e108bd59"))
|