Skip to content
Snippets Groups Projects
Commit 8aadeaaf authored by Mads M. Pedersen's avatar Mads M. Pedersen
Browse files

add function to install dtu license

parent 2c9563f4
No related branches found
No related tags found
1 merge request!262add function to install dtu license
Pipeline #56926 passed
......@@ -2,12 +2,17 @@ import glob
import io
import json
import os
import stat
import pathlib
import platform
import shutil
import zipfile
import sys
from pathlib import Path
from platform import architecture
from urllib.request import Request, urlopen
def chmod_x(exe_path: str):
"""Utility function to change the file mode of a file to allow execution
......@@ -112,3 +117,89 @@ def install_wind_tool(
print(f"{tool} version {version} succesfully installed in {destination}")
return
def install_hawc2_dtu_license():
"""Function to install the DTU HAWC2 license. In order to install the license, you must be logged in to the DTU network.
"""
if sys.platform.lower() == 'win32':
ping_param = "-n 1"
f = Path(os.getenv('APPDATA')) / 'DTU Wind Energy/hawc2/license.cfg'
else:
ping_param = "-c 1"
f = Path('~/.config/hawc2/license.cfg')
if not f.exists():
f.parent.mkdir(parents=True, exist_ok=True)
if b'LICENSE SERVER RUNNING' in urlopen("http://license-internal.windenergy.dtu.dk:34523").read():
f.write_text("[licensing]\nhost = http://license-internal.windenergy.dtu.dk\nport = 34523")
else:
raise ConnectionError(f"Could not connect to the DTU license server. You must be connected to the DTU network to use this function.")
def install_keygen_license(software: str, cfg_file: str, force : bool=False):
"""Install license file for HAWC2, HAWCStab2 or Ellipsys on your machine
Parameters
----------
software : str
Must be one of HAWC2, HAWCStab2 or Ellipsys. The argument is case insensitive.
cfg_file : str
Path to the license file to install
force : bool, optional
Switch to force the installation, overwriting any existing , by default False
Returns
-------
NoneType
None
Raises
------
ValueError
A ValueError is raised if the name of the software argument is not supported.
"""
SUPPORTED_SOFTWARES = ["hawc2", "hawcstab2", "ellipsys"]
if software.lower() not in SUPPORTED_SOFTWARES:
raise ValueError(f"'software' must be one of {SUPPORTED_SOFTWARES}")
USER_PLATFORM = platform.uname().system
if USER_PLATFORM == "Windows":
APPDATA = f"{os.environ['APPDATA']}"
else:
APPDATA = "None"
def local_license_dir(platform, software):
return {
"Windows": os.path.join(
APPDATA,
"DTU Wind Energy",
f"{software}",
),
"Linux": os.path.join(f"{pathlib.Path.home()}", ".config", f"{software}"),
"Darwin": os.path.join(
f"{pathlib.Path.home()}", "Library", "Application Support"
),
}[platform]
def local_license_file(software):
return {
"hawc2": "license.cfg",
"hawcstab2": "license.cfg",
"pywasp": "",
"ellipsys": "license.cfg",
}[software.lower()]
license_path = local_license_dir(USER_PLATFORM, software)
lic_name = local_license_file(software)
os.makedirs(license_path, exist_ok=True)
if os.path.exists(os.path.join(license_path,lic_name)) and os.path.isfile(os.path.join(license_path,lic_name)) and (not force):
print(f"License already installed for {software}, use 'force=True' to overwrite installation")
else:
shutil.copy(f"{cfg_file}", f"{os.path.join(license_path,lic_name)}")
import platform
import pathlib
import os
import pytest
from wetb.utils.installer import install_wind_tool
from wetb.utils.installer import install_wind_tool, install_hawc2_dtu_license, install_keygen_license
import shutil
DESTINATION="/tmp/hawc2"
USER_PLATFORM = platform.uname().system
if USER_PLATFORM == "Windows":
APPDATA = f"{os.environ['APPDATA']}"
else:
APPDATA = "None"
TEST_LICENSE_FILE = "/tmp/license.cfg"
with open(TEST_LICENSE_FILE, "w") as file:
file.writelines(["\n[licensing]", "\nhost: www.fakehost.com", "\nport=360"])
def local_license_dir(platform, software):
return {
"Windows": os.path.join(
APPDATA,
"DTU Wind Energy",
f"{software}",
),
"Linux": os.path.join(f"{pathlib.Path.home()}", ".config", f"{software}"),
"Darwin": os.path.join(
f"{pathlib.Path.home()}", "Library", "Application Support"
),
}[platform]
def local_license_file(software):
return {
"hawc2": "license.cfg",
"hawcstab2": "license.cfg",
"pywasp": "",
"ellipsys": "license.cfg",
}[software.lower()]
def test_installer_zip():
# Install a program distributed as a zip file
try:
......@@ -40,4 +79,30 @@ def test_version_not_available():
except:
raise
finally:
shutil.rmtree(DESTINATION)
\ No newline at end of file
shutil.rmtree(DESTINATION)
def test_install_hawc2_dtu_license():
license_path = local_license_dir(USER_PLATFORM, "HAWC2")
try:
install_hawc2_dtu_license()
except:
raise
finally:
shutil.rmtree(license_path, ignore_errors=True)
TEST_LICENSE_FILE = "/tmp/license.cfg"
with open(TEST_LICENSE_FILE, "w") as file:
file.writelines(["\n[licensing]", "\nhost: www.fakehost.com", "\nport=360"])
@pytest.mark.parametrize("software,license", [("HAWC2", TEST_LICENSE_FILE),("HAWCStab2", TEST_LICENSE_FILE),("ellipSYS", TEST_LICENSE_FILE)])
def test_install_keygen_license(software, license):
license_path = local_license_dir(USER_PLATFORM, software)
try:
install_keygen_license(software=software, cfg_file=license)
except Exception as exc:
raise exc
finally:
shutil.rmtree(license_path, ignore_errors=True)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment