PyXMake Developer Guide: src/PyXMake/Build/__install__.py Source File

| PyXMake Developer Guide 1.0 PyXMake

Loading...
Searching...
No Matches
__install__.py
1# -*- coding: utf-8 -*-
2# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3# % Installation %
4# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5"""
6Fetch 3rd party dependencies for PyXMake from DLR's resource server.
7
8@note: PyXMake 3rd party dependency installer
9Created on 04.08.2020
10
11@version: 1.0
12----------------------------------------------------------------------------------------------
13@requires:
14 -
15
16@change:
17 -
18
19@author: garb_ma [DLR-FA,STM Braunschweig]
20----------------------------------------------------------------------------------------------
21"""
22
23## @package PyXMake.Build.__install__
24# Fetch 3rd party dependencies for PyXMake from DLR's resource server.
25## @author
26# Marc Garbade
27## @date
28# 04.08.2020
29## @par Notes/Changes
30# - Added documentation // mg 04.08.2020
31
32import os, sys
33import urllib.request
34import subprocess
35import zipfile
36import atexit
37
38try:
39 import PyXMake as _ #@UnusedImport
40except ImportError:
41 # Script is executed as a plug-in
42 sys.path.insert(0,os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
43finally:
44 from PyXMake import Build #@UnresolvedImport
45 from PyXMake.Tools import Utility #@UnresolvedImport
46
47## Some immutable variables
48__cmd_delimn = " "
49__pyx_dependency = ["perl","latex","pandoc"]
50# Add custom user dependencies
51__pyx_dependency.extend([os.getenv("pyx_user_dependency",None)])
52# Only install a single entity. Overwrite everything else.
53if os.getenv("pyx_user_install",""): __pyx_dependency = [os.getenv("pyx_user_install")]
54
55 def main(*args, **kwargs): # pragma: no cover
56 """
57 Main function to execute the script.
58 """
59 # Loop over all dependencies. Remove user dependency if not set.
60 for __package in list(filter(None, __pyx_dependency)):
61
62 # Resolve the correct name.
63 if __package.lower() == "perl":
64 package_name = "Perl"
65 elif __package.lower() == "latex":
66 package_name = "MikTeX"
67 elif __package.lower() == "pandoc":
68 package_name = "Pandoc"
69 elif __package.lower() == "chocolatey":
70 # For now, this is the last installation procedure - the process is terminated afterwards.
71 if not Utility.GetExecutable("choco") and not os.path.exists(os.path.join(Build.__path__[0],"bin",__package)):
72
73 # Print an informational message
74 print('==================================')
75 print('%s is required for the current process, but is' % __package.upper())
76 print('not found on your machine.')
77 print('Installing dependency. This might take a while.. ')
78 print('==================================')
79
80 # Install a full portable version of MSYS2 with MINGW64
81 __pyx_ps_delimn = " "; __pyx_package = os.path.join(Build.__path__[0],"bin",__package.lower());
82 Utility.Popen(["powershell.exe", __pyx_ps_delimn.join([os.path.join(Build.__path__[0],"config","stm_choco.ps1"),__pyx_package])], verbosity=2, shell=False);
83 continue
84 else:
85 raise NotImplementedError
86
87 # Set some temporary variables for path manipulations.
88 __pyx_url_delimn = "/"; __pyx_point = "."; __pyx_space = " ";
89 __pyx_package_url = __pyx_url_delimn.join(["https:","","jenkins.fa-services.intra.dlr.de","job","STM_Archive","lastSuccessfulBuild","artifact","Archive", __pyx_point.join([package_name, "zip"])])
90
91 # Download a ZIP archive and store its content temporarily in the user's download folder.
92 __pyx_zip_file = __pyx_package_url.rsplit(__pyx_url_delimn, 1)[-1].lower()
93 __pyx_package = os.path.join(Build.__path__[0],"bin",__pyx_zip_file.split(".")[0])
94 __pyx_source = __pyx_zip_file.split(".")[0]
95
96 # Create a file handler
97 devnull = open(os.devnull, 'w')
98 atexit.register(devnull.close)
99
100 try:
101 # Check if dependency can be executed on the current machine.
102 subprocess.check_call([__package.lower(), "--help"], stdout=devnull, stderr=subprocess.STDOUT)
103 except OSError:
104 # Executable not found. Attempt installation from archive
105 if __debug__ and not os.path.exists(__pyx_package):
106
107 # Attempt remote installation from STM Archive.
108 print('==================================')
109 print('%s is required for the current process, but is' % __package.upper())
110 print('not found on your machine.')
111 print('Fetching portable installation from STM archive. ')
112 print('==================================')
113
114 # Download a ZIP archive and store its content temporarily in the user's download folder.
115 download_path = os.path.join(os.path.expanduser('~/Downloads'), __pyx_zip_file)
116 urllib.request.urlretrieve(__pyx_package_url, download_path)
117
118 # Extract archive into binary folder
119 with zipfile.ZipFile(download_path, 'r') as zip_folder:
120 zip_folder.extractall(__pyx_package)
121
122 # Add Latex templates
123 if __package.lower() == "latex":
124 import git
125 __url_delimn = "/"; __git_server_access = "gitlab.dlr.de"
126
127 # Install DLR specific Latex templates into MikTex distribution
128 __templates_package = os.path.join(__pyx_package,"user","RM_LaTeX")
129 __templates_repo = __url_delimn.join(["https:","",__git_server_access,"innersource","latex-templates.git"])
130
131 # Only if the path does not already exists
132 if not os.path.exists(__templates_package):
133 print("==================================")
134 print("Installing %s from GIT repository" % "DLR LaTeX")
135 print("==================================")
136 # Local variable
137 master="master";
138 if not os.path.exists(os.path.dirname(__templates_package)):
139 os.mkdir(os.path.dirname(__templates_package))
140 # Clone LaTeX templates repository
141 g = git.Repo.clone_from(__templates_repo, __templates_package) #@UndefinedVariable
142 # Switch to local master branch
143 g.git.switch(master)
144
145 # Print success message.
146 print('==================================')
147 print('Successfully installed %s' % __package.upper())
148 print('==================================')
149
150 # Remove temporary download path
151 os.remove(download_path)
152 finally:
153
154 # Package exists on the current machine or was installed.
155 if os.path.exists(__pyx_package):
156 pathlist = list([])
157 # Add portable PERL distribution to overall path
158 if os.path.exists(os.path.join(__pyx_package,__pyx_source,"site","bin")):
159 pathlist.extend([os.path.join(__pyx_package,__pyx_source,"site","bin"),os.path.join(__pyx_package,__pyx_source,"bin"),os.path.join(__pyx_package,"c","bin")])
160 # Add portable LATEX distribution (MikTeX) to overall path
161 elif os.path.exists(os.path.join(__pyx_package,"texmfs","install",__pyx_source,"bin","x64")):
162 pathlist.append(os.path.join(__pyx_package,"texmfs","install",__pyx_source,"bin","x64"))
163 # Add portable PANDOC distribution to overall path
164 else:
165 pathlist.append(__pyx_package)
166 # Modify the current path variable
167 os.environ["PATH"] += os.pathsep + os.pathsep.join(pathlist)
168
169 # Run command to check validity of the installation
170 assert Utility.Popen(__pyx_space.join([__package,"--help"]),0).returncode == 0
171
172# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
173# % Execute install script %
174# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175if __name__ == '__main__':
176 main();
PyXMake.Build.__install__.main
main(*args, **kwargs)
Definition __install__.py:55
PyXMake.Tools
Module containing basic functionalities defined for convenience.
Definition __init__.py:1