Skip to content

Commit

Permalink
get_mac_address
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen1993 committed Feb 8, 2024
1 parent 2f6f724 commit af572ad
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 52 deletions.
3 changes: 0 additions & 3 deletions src/promptflow/promptflow/_sdk/_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,6 @@ def get_or_set_installation_id(self):
return installation_id

installation_id = gen_uuid_by_compute_info()
if not installation_id:
installation_id = str(uuid.uuid4())

self.set_config(key=self.INSTALLATION_ID, value=installation_id)
return installation_id

Expand Down
40 changes: 13 additions & 27 deletions src/promptflow/promptflow/_sdk/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1120,37 +1120,23 @@ def pd_read_json(file) -> "DataFrame":
return pd.read_json(f, orient="records", lines=True)


def get_mac_address() -> Union[str, None]:
"""Get the MAC ID of the first network card."""
def get_mac_address() -> str:
"""Obtain all MAC addresses, then sort and concatenate them."""
try:
import psutil

mac_address = None
net_address = psutil.net_if_addrs()
eth = []
# Query the first network card in order and obtain the MAC address of the first network card.
# "Ethernet" is the name of the Windows network card.
# "eth", "ens", "eno" are the name of the Linux & Mac network card.
net_interface_names = ["Ethernet", "eth0", "eth1", "ens0", "ens1", "eno0", "eno1"]
for net_interface_name in net_interface_names:
if net_interface_name in net_address:
eth = net_address[net_interface_name]
break
for net_interface in eth:
if net_interface.family == psutil.AF_LINK: # mac address
mac_address = str(net_interface.address)
break

# If obtaining the network card MAC ID fails, obtain other MAC ID
if mac_address is None:
node = uuid.getnode()
if node != 0:
mac_address = str(uuid.UUID(int=node).hex[-12:])

return mac_address
mac_address = []
net_addresses = psutil.net_if_addrs()
# Obtain all MAC addresses, then sort and concatenate them
for net_address in net_addresses.values():
for net_interface in net_address:
if net_interface.family == psutil.AF_LINK and net_interface.address != "00-00-00-00-00-00":
mac_address.append(net_interface.address)

return ':'.join(mac_address)
except Exception as e:
logger.debug(f"get mac id error: {str(e)}")
return None
return ""


def get_system_info() -> Tuple[str, str, str]:
Expand All @@ -1173,7 +1159,7 @@ def gen_uuid_by_compute_info() -> Union[str, None]:
system_info_hash = hashlib.sha256((host_name + system + machine).encode()).hexdigest()
compute_info_hash = hashlib.sha256((mac_address + system_info_hash).encode()).hexdigest()
return str(uuid.uuid5(uuid.NAMESPACE_OID, compute_info_hash))
return None
return str(uuid.uuid4())


def convert_time_unix_nano_to_timestamp(time_unix_nano: str) -> str:
Expand Down
24 changes: 2 additions & 22 deletions src/promptflow/tests/sdk_cli_test/unittests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,34 +474,14 @@ def mock_http_request():
http_retry_wrapper(mock_http_request, tries=2, delay=1, backoff=1)()
assert counter == 2

def test_get_mac_address(self):
import psutil

mac_address = None
net_address = psutil.net_if_addrs()
eth = []
# Query the first network card in order and obtain the MAC address of the first network card.
# "Ethernet" is the name of the Windows network card.
# "eth", "ens", "eno" are the name of the Linux & Mac network card.
net_interface_names = ["Ethernet", "eth0", "eth1", "ens0", "ens1", "eno0", "eno1"]
for net_interface_name in net_interface_names:
if net_interface_name in net_address:
eth = net_address[net_interface_name]
break
for net_interface in eth:
if net_interface.family == psutil.AF_LINK: # mac address
mac_address = str(net_interface.address)
break

assert mac_address != ""
assert mac_address == get_mac_address()

def test_gen_uuid_by_compute_info(self):
uuid1 = gen_uuid_by_compute_info()
uuid2 = gen_uuid_by_compute_info()
assert uuid1 == uuid2

mac_address = get_mac_address()
assert mac_address

host_name, system, machine = get_system_info()
system_info_hash = hashlib.sha256((host_name + system + machine).encode()).hexdigest()
compute_info_hash = hashlib.sha256((mac_address + system_info_hash).encode()).hexdigest()
Expand Down

0 comments on commit af572ad

Please sign in to comment.