Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
Merged PR 821: Adding GetHostInfo to UtilityFunctions
Browse files Browse the repository at this point in the history
Python does provide methods for the platform to pass information to the Python environment through the platform module, but it is important that the way this information is parsed is consistent.
To address this issue this function will parse the information into the 3 pieces we care about:
1) Linux or Windows
2) 32 or 64 bit
3)  x86 or ARM
  • Loading branch information
Max Knutsen committed Feb 19, 2019
1 parent c84c1a4 commit a1a1404
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
4 changes: 2 additions & 2 deletions MuPythonLibrary/MuAnsiHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
##
import logging
import re
import os
from MuPythonLibrary.UtilityFunctions import GetHostInfo
try:
# try to import windows types from winDLL
import ctypes
Expand Down Expand Up @@ -248,7 +248,7 @@ class ColoredStreamHandler(logging.StreamHandler):

def __init__(self, stream=None, strip=None, convert=None):
logging.StreamHandler.__init__(self, stream)
self.on_windows = os.name == 'nt'
self.on_windows = GetHostInfo().os == "Windows"
# We test if the WinAPI works, because even if we are on Windows
# we may be using a terminal that doesn't support the WinAPI
# (e.g. Cygwin Terminal). In this case it's up to the terminal
Expand Down
35 changes: 34 additions & 1 deletion MuPythonLibrary/UtilityFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import threading
import subprocess
import sys
import platform
from collections import namedtuple

####
# Helper to allow Enum type to be used which allows better code readability
Expand Down Expand Up @@ -93,7 +95,7 @@ def reader(filepath, outstream, stream):
def GetNugetCmd():
file = "NuGet.exe"
cmd = []
if (os.name == "posix"):
if (GetHostInfo().os == "Linux"):
cmd += ["mono"]
found = False
for env_var in os.getenv("PATH").split(os.pathsep):
Expand All @@ -109,6 +111,37 @@ def GetNugetCmd():
return cmd


####
# Returns a namedtuple containing information about host machine.
#
# @return namedtuple Host(os=OS Type, arch=System Architecture, bit=Highest Order Bit)
####
def GetHostInfo():
Host = namedtuple('Host', 'os arch bit')
host_info = platform.uname()
os = host_info.system
processor_info = host_info.machine
logging.debug("Getting host info for host: {0}".format(str(host_info)))

arch = None
bit = None

if ("x86" in processor_info) or ("AMD" in processor_info) or ("Intel" in processor_info):
arch = "x86"
elif ("ARM" in processor_info) or ("AARCH" in processor_info):
arch = "ARM"

if "32" in processor_info:
bit = "32"
elif "64" in processor_info:
bit = "64"

if (arch is None) or (bit is None):
raise EnvironmentError("Host info could not be parsed: {0}".format(str(host_info)))

return Host(os=os, arch=arch, bit=bit)


####
# Run a shell commmand and print the output to the log file
# This is the public function that should be used to run commands from the shell in python environment
Expand Down
30 changes: 30 additions & 0 deletions MuPythonLibrary/feature_GetHostInfo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# GetHostInfo

This document details the utility function called GetHostInfo. This function was written because NuGet needed a way to determine attributes about the host system to determine what parts of a dependency to use.

## How to Use
```python
from MuPythonLibrary.UtilityFunctions import GetHostInfo

host_info = GetHostInfo()
```

## Usage info

GetHostInfo() will return a namedtuple with 3 attributes describing the host machine. Below for each is the name of the field, description of the field and possible contents therein.

### 1. os - OS Name

Windows, Linux, or Java

### 2. arch - Processor architecture

ARM or x86

### 3. bit - Highest order bit

32 or 64

## Purpose

Since there are multiple different ways one could derive these values, it is necessary provide a common implementation of that logic to ensure it is uniform.
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ Version History

0.4.3-dev
-----

Main Changes:

- Added GetHostInfo to UtilityFunctions. This function will parse the platform module to provide information about the host.
- Added colors for progress and section labels.

0.4.2
Expand Down

0 comments on commit a1a1404

Please sign in to comment.