-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall_tech.py
132 lines (104 loc) · 3.91 KB
/
install_tech.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
# ========================================================================
# ------------------------ Copyright 2023 Mabrains -----------------------
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# ========================================================================
"""
Usage:
install_tech.py (--tech_name=<tech_name>) (--tech_path=<tech_path>)
-h, --help Show help text.
-v, --version Show version.
--tech_name=<tech_name> Name of technology to be installed
--tech_path=<tech_path> Path of technology to be installed
"""
from pathlib import Path
from docopt import docopt
import logging
import os
def remove_path_or_dir(dest: Path):
if dest.is_dir():
os.unlink(dest)
else:
os.remove(dest)
def make_link(src, dest, overwrite: bool = True) -> None:
"""
Function to create a symlink of source to destination path.
Parameters
----------
src : str
Path to source we need to create a link from it
dest : str
Path to destiantion we need to add a link to it
overwrite: bool
Option for overwriting the current link.
Returns
-------
None
"""
src_path = Path(src)
dest_path = Path(dest)
if not src_path.exists():
raise ValueError(f"{src} does not exist")
if dest_path.exists() and not overwrite:
logging.info(f"{dest} dir already exists")
return
if dest_path.exists() or dest_path.is_symlink():
logging.info(f"Removing {dest_path} already installed")
remove_path_or_dir(dest_path)
try:
os.symlink(src_path, dest_path, target_is_directory=True)
except OSError as err:
logging.info("Could not create symlink!")
logging.error("Error: ", err)
logging.info("Symlink made:")
logging.info(f"From: {src_path}")
logging.info(f"To: {dest_path}")
def main(tech_name, tech_path):
"""
Main function for PDK installation
"""
# Home directory
home = Path.home()
# Destination of PDK you need to link to it
dest_dir = os.path.join(home, ".klayout", "tech")
dest_path = Path(dest_dir)
dest_path.mkdir(exist_ok=True, parents=True)
# Creating symlink to klayout tech
dest_tech = os.path.join(dest_dir, tech_name)
src_tech = os.path.expanduser(tech_path)
# Check paths
if not os.path.exists(dest_tech) and os.path.isdir(dest_tech):
logging.error(f"Destination path {dest_tech} doesn't exist, please recheck")
exit(1)
if not os.path.exists(src_tech) and os.path.isdir(src_tech):
logging.error(f"Tech path {src_tech} doesn't exist, please recheck")
exit(1)
make_link(src=src_tech, dest=dest_tech)
# ================================================================
# -------------------------- MAIN --------------------------------
# ================================================================
if __name__ == "__main__":
# Args
arguments = docopt(__doc__, version="TECH-INSTALL: 0.1")
tech_name = arguments["--tech_name"]
tech_path = arguments["--tech_path"]
logging.basicConfig(
level=logging.DEBUG,
handlers=[
logging.StreamHandler(),
],
format="%(asctime)s | %(levelname)-7s | %(message)s",
datefmt="%d-%b-%Y %H:%M:%S",
)
main(tech_name, tech_path)