pyEDAA.Launcher

pyEDAA/Launcher/__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# ==================================================================================================================== #
#              _____ ____    _        _      _                           _                                             #
#  _ __  _   _| ____|  _ \  / \      / \    | |    __ _ _   _ _ __   ___| |__   ___ _ __                               #
# | '_ \| | | |  _| | | | |/ _ \    / _ \   | |   / _` | | | | '_ \ / __| '_ \ / _ \ '__|                              #
# | |_) | |_| | |___| |_| / ___ \  / ___ \ _| |__| (_| | |_| | | | | (__| | | |  __/ |                                 #
# | .__/ \__, |_____|____/_/   \_\/_/   \_(_)_____\__,_|\__,_|_| |_|\___|_| |_|\___|_|                                 #
# |_|    |___/                                                                                                         #
# ==================================================================================================================== #
# Authors:                                                                                                             #
#   Stefan Unrein                                                                                                      #
#   Patrick Lehmann                                                                                                    #
#                                                                                                                      #
# License:                                                                                                             #
# ==================================================================================================================== #
# Copyright 2021-2024 Stefan Unrein - Endingen, Germany                                                                #
#                                                                                                                      #
# Licensed under the Apache License, Version 2.0 (the "License");                                                      #
# you may not use this file except in compliance with the License.                                                     #
# You may obtain a copy of the License at                                                                              #
#                                                                                                                      #
#   http://www.apache.org/licenses/LICENSE-2.0                                                                         #
#                                                                                                                      #
# Unless required by applicable law or agreed to in writing, software                                                  #
# distributed under the License is distributed on an "AS IS" BASIS,                                                    #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.                                             #
# See the License for the specific language governing permissions and                                                  #
# limitations under the License.                                                                                       #
#                                                                                                                      #
# SPDX-License-Identifier: Apache-2.0                                                                                  #
# ==================================================================================================================== #
#
"""Start the correct Vivado Version based on version in `*.xpr`file."""
__author__ =    "Stefan Unrein"
__email__ =     "stefan.unrein@gmx.net"
__copyright__ = "2021-2024, Stefan Unrein"
__license__ =   "Apache License, Version 2.0"
__version__ =   "0.1.0"
__keywords__ =  ["launcher", "version selector", "xilinx", "vivado"]

from re import compile as re_compile

from sys import exit, argv
import subprocess
from pathlib import Path
from textwrap import dedent
from time import sleep
from typing import NoReturn, Generator

from pyTooling.Decorators import export


@export
class Program:
	"""Program instance of pyEDAA.Launcher."""

	vivadoBatchfile = Path("bin/vivado.bat")
	vvglWrapperFile = Path("bin/unwrapped/win64.o/vvgl.exe")

	versionLineRegExp = re_compile(r"^<!--\s*Product\sVersion:\s+Vivado\s+v(?P<major>\d+).(?P<minor>\d+)(?:.(?P<patch>\d+))?\s+\(64-bit\)\s+-->")

	_projectFilePath: Path


	def __init__(self, projectFilePath: Path) -> None:
		"""Initializer.

		:param projectFilePath: Path to the ``*.xpr`` file.
		:raises Exception: When the given ``*.xpr`` file doesn't exist.
		"""
		if not projectFilePath.exists():
			raise Exception(f"Vivado project file '{projectFilePath}' not found.") \
				from FileNotFoundError(f"File '{projectFilePath}' not found.")

		self._projectFilePath = projectFilePath

	def GetVersion(self) -> str:
		"""Opens an ``*.xpr`` file and returns the Vivado version used to save this file.

		:returns: Used Vivado version to save the given ``*.xpr`` file.
		:raises Exception: When the version information isn't found in the file.
		"""
		with self._projectFilePath.open("r", encoding="utf-8") as file:
			for line in file:
				match = self.versionLineRegExp.match(line)
				if match is not None:
					return f"{match['major']}.{match['minor']}"
			else:
				raise Exception(f"Pattern not found in '{self._projectFilePath}'.")

	@classmethod
	def GetVivadoVersions(self, installPath: Path) -> Generator[str, None, None]:
		"""Scan a given directory for installed Vivado versions.

		:param installPath: Xilinx installation directory.
		:returns: A generator for a sequence of installed Vivado versions.
		"""
		for item in installPath.iterdir():
			if item.is_dir():
				yield item.name

	def StartVivado(self, xilinxInstallationPath: Path, version: str) -> NoReturn:
		"""Start the given Vivado version with an ``*.xpr`` file as parameter.

		:param xilinxInstallationPath: Path to the Xilinx toolchain installations.
		:param version: The Vivado version to start.
		"""
		vivadoInstallationPath = xilinxInstallationPath / version

		vvglWrapperPath = vivadoInstallationPath / self.vvglWrapperFile
		vivadoBatchfilePath = vivadoInstallationPath / self.vivadoBatchfile

		cmd = [str(vvglWrapperPath), str(vivadoBatchfilePath), str(self._projectFilePath)]
		subprocess.Popen(cmd, cwd=self._projectFilePath.parent)  # , creationflags=subprocess.DETACHED_PROCESS)

		print("")
		print(f"Opening project with Vivado {version}.")

		sleep(2)
		exit(0)

	@classmethod
	def PrintHelp(cls, scriptPath: Path) -> None:
		"""Print a help page.

		:param scriptPath: Path to this script.
		"""
		print(dedent(f"""\
			Run-Path '{scriptPath}'

			For using this Launcher, please bind the *.xpr file extension to this executable with:
			* Put this executable into the Vivado installation folder. E.g: C:\\Xilinx\\Vivado\\
			* Change *.xpr association: right-click -> open with -> VivadoManager.exe
		"""))


@export
def main() -> NoReturn:
	"""Entry point function.

	It creates an instance of :class:`Program` and hands over the execution to the OOP world.
	"""
	xilinxInstallationPath = Path.cwd()
	scriptPath = Path(argv[0])

	if (argc := len(argv)) == 0:
		Program.PrintHelp(scriptPath)

		print(f"Current path '{xilinxInstallationPath}' contains the following folders:")
		for version in Program.GetVivadoVersions(xilinxInstallationPath):
			print(f"* {version}")

		print("")
		print("Press any key to exit.")

		# wait on user interaction
		input()
		exit(0)

	elif argc == 1:
		projectFileArgument = argv[1]
		projectFilePath = Path(projectFileArgument)

		program = Program(projectFilePath)

		try:
			versionFromXPRFile = program.GetVersion()
		except Exception as ex:
			print(f"[ERROR] {ex}")
			exit(1)

		for version in program.GetVivadoVersions(xilinxInstallationPath):
			if version == versionFromXPRFile:
				program.StartVivado(xilinxInstallationPath, versionFromXPRFile)
		else:
			vivadoPath = xilinxInstallationPath / versionFromXPRFile
			print(dedent(f"""\
				ERROR: Vivado version {versionFromXPRFile} not available at path '{vivadoPath}'. Please start manually!

				Press any key to exit.
			"""))

			# wait on user interaction
			input()
			exit(1)


# Entry point
if __name__ == "__main__":
	main()