162 lines
5.5 KiB
Python
162 lines
5.5 KiB
Python
import os
|
|
from typing import Any
|
|
import toml
|
|
import datetime
|
|
|
|
|
|
def findProjectRoot() -> "tuple[bool, str]":
|
|
[exists, projectFile] = findProjectFile()
|
|
if not exists:
|
|
return (False, "")
|
|
return (True, "/".join(projectFile.split("/")[0:-1]))
|
|
|
|
def getRelativePathToRoot(path: str) -> str:
|
|
(exists, projectRoot) = findProjectRoot()
|
|
if not exists:
|
|
return ""
|
|
return os.path.relpath(path, projectRoot)
|
|
|
|
def findProjectFile() -> "tuple[bool, str]":
|
|
cwd = os.getcwd().split("/")
|
|
for i in range(len(cwd) - 2):
|
|
searchPath = "/".join(cwd[0:len(cwd)-i])
|
|
[exists, pathToProjectFile] = projectFileExists(searchPath)
|
|
if exists:
|
|
return (True, pathToProjectFile)
|
|
return (False, "")
|
|
|
|
def projectFileExists(path: str) -> "tuple[bool, str]":
|
|
files = os.listdir(path)
|
|
for file in files:
|
|
if "gantry.toml" in file:
|
|
return (True, os.path.join(path, file))
|
|
return (False,"")
|
|
|
|
def initProjectFile(projectName: str) -> "tuple[bool, str]":
|
|
cwd = os.getcwd()
|
|
projectPath = os.path.join(cwd, "gantry.toml")
|
|
[exists, existingProjectPath] = projectFileExists(cwd)
|
|
if exists:
|
|
existingProjectName = existingProjectPath.split("/")[-1]
|
|
return (False, f"Project {existingProjectName} already exists, amend it to fit your intention or delete it to create a new project")
|
|
try:
|
|
with open(projectPath, "w") as f:
|
|
parsedTOML = toml.loads(createProjectFileTemplate(projectName))
|
|
toml.dump(parsedTOML, f)
|
|
except:
|
|
return (False, "Creation of file failed, permissions may be set wrong")
|
|
|
|
return (True, projectPath)
|
|
|
|
def loadProjectFile() -> "tuple[bool, str | dict[str, Any]]" :
|
|
[exists, path] = findProjectFile()
|
|
if not exists:
|
|
return (False, "")
|
|
try:
|
|
with open(path, "r") as f:
|
|
toml.load
|
|
parsedTOML = toml.load(f)
|
|
return (True, parsedTOML)
|
|
except:
|
|
return (False, "Reading Project file failed, permissions may be set wrong")
|
|
|
|
def writeProjectFile(projectDict: "dict[str, Any]") -> "tuple[bool, str]":
|
|
[exists, path] = findProjectFile()
|
|
if not exists:
|
|
return (False, "")
|
|
try:
|
|
with open(path, "w") as f:
|
|
toml.dump(projectDict, f)
|
|
return (True, "")
|
|
except:
|
|
return (False, "Reading Project file failed, permissions may be set wrong")
|
|
|
|
def getProjectDict() -> "tuple[bool, dict[str, Any]]":
|
|
[exists, output] = loadProjectFile()
|
|
if not exists:
|
|
print(output)
|
|
return (False, {})
|
|
if isinstance(output, dict):
|
|
return (True, output)
|
|
else:
|
|
print(output)
|
|
return (False, {})
|
|
|
|
def removeLibraryInProject(lib: str) -> "tuple[bool, str]":
|
|
[exists, projectDict] = getProjectDict()
|
|
if not exists:
|
|
return (False, "Found no project dictionary")
|
|
if "libraries" not in projectDict.keys():
|
|
return (False, "No libraries are declared in this project.")
|
|
if lib in projectDict["libraries"].keys():
|
|
projectDict["libraries"].pop(lib)
|
|
[wentWell, _] = writeProjectFile(projectDict)
|
|
return (wentWell, "")
|
|
return (False, "Library with this name is not declared")
|
|
|
|
|
|
def addLibraryInProject(lib: str, relPath: str, std: str) -> "tuple[bool, str]":
|
|
(exists, projectDict) = getProjectDict()
|
|
if not exists:
|
|
return (False, "Project doesn't exist.")
|
|
if "libraries" not in projectDict.keys():
|
|
projectDict["libraries"] = {}
|
|
if lib not in projectDict["libraries"].keys():
|
|
libDir = getRelativePathToRoot(os.path.join(os.getcwd(), relPath))
|
|
projectDict["libraries"][lib] = {}
|
|
projectDict["libraries"][lib]["vhdl-version"] = std
|
|
projectDict["libraries"][lib]["path"] = libDir
|
|
os.makedirs(name=relPath,exist_ok=True)
|
|
print(libDir)
|
|
[wentWell, _] = writeProjectFile(projectDict)
|
|
return (wentWell, "")
|
|
return (False, "Library with this name is already declared")
|
|
|
|
def getLibraryInProject(lib: str) -> "tuple[bool, dict[str, Any]]":
|
|
(exists, projectDict) = getProjectDict()
|
|
if not exists:
|
|
return (False, {})
|
|
libs = {}
|
|
if "libraries" not in projectDict.keys():
|
|
## Successful read, no libs found -> empty return
|
|
return (False, {})
|
|
(_, projectRoot) = findProjectRoot()
|
|
if lib in projectDict["libraries"].keys():
|
|
return (True, projectDict["libraries"][lib])
|
|
return (False, {})
|
|
def getLibrariesInProject() -> "tuple[bool, dict[str, Any]]":
|
|
(exists, projectDict) = getProjectDict()
|
|
if not exists:
|
|
return (False, {})
|
|
libs = {}
|
|
if "libraries" not in projectDict.keys():
|
|
## Successful read, no libs found -> empty return
|
|
return (True, {})
|
|
(_, projectRoot) = findProjectRoot()
|
|
for lib in projectDict["libraries"].keys():
|
|
libs[lib] = projectDict["libraries"][lib]
|
|
absPath = os.path.join(projectRoot, libs[lib]["path"])
|
|
libs[lib]["path"] = os.path.relpath(absPath, os.getcwd())
|
|
return (True, libs)
|
|
|
|
def getLibrariesPresent() -> "tuple[bool, dict[str, Any]]":
|
|
cwd = os.getcwd()
|
|
(exists, libs) = getLibrariesInProject()
|
|
if not exists:
|
|
return (False, {})
|
|
libsInCwd = {}
|
|
for lib in libs.keys():
|
|
if os.path.samefile(libs[lib]["path"], cwd):
|
|
libsInCwd[lib] = libs[lib]
|
|
return (True, libsInCwd)
|
|
|
|
def createProjectFileTemplate(projectName: str) -> str:
|
|
return f"""
|
|
title = "{projectName}"
|
|
createdAt = "{datetime.date.today()}"
|
|
maintainer = ""
|
|
email = ""
|
|
version = "0.0.1"
|
|
"""
|
|
|