Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
178 lines (152 loc) · 5.8 KB
/
Copy pathmain.py
File metadata and controls
178 lines (152 loc) · 5.8 KB
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
#!/usr/bin/env python3
import os
import sys
import subprocess
from typing import Dict, List, Optional
from dataclasses import dataclass
from abc import ABC, abstractmethod
import readline
from pathlib import Path
class ShellCommand(ABC):
@abstractmethod
def execute(self, args: List[str]) -> None:
pass
class ExitCommand(ShellCommand):
def execute(self, args: List[str]) -> None:
try:
code = int(args[0]) if args else 0
sys.exit(code)
except ValueError:
print("exit: Invalid exit code")
class EchoCommand(ShellCommand):
def execute(self, args: List[str]) -> None:
print(" ".join(args))
class TypeCommand(ShellCommand):
def __init__(self, builtins: Dict[str, ShellCommand]):
self.builtins = builtins
def execute(self, args: List[str]) -> None:
if not args:
print("type: usage: type <command>")
return
cmd = args[0]
print(cmd, end="")
if cmd in self.builtins:
print(" is a shell builtin")
elif path := PathResolver.findinpath(cmd):
print(f" is {path}")
else:
print(": not found")
class PwdCommand(ShellCommand):
def execute(self, args: List[str]) -> None:
print(os.getcwd())
class CdCommand(ShellCommand):
def execute(self, args: List[str]) -> None:
path = args[0] if args else os.path.expanduser("~")
try:
os.chdir(path)
except FileNotFoundError:
print(f"cd: {path}: No such file or directory")
except NotADirectoryError:
print(f"cd: {path}: Not a directory")
except OSError as e:
print(f"cd: {path}: {e}")
class PathResolver:
@staticmethod
def findinpath(cmd: str) -> Optional[str]:
pathdirs = os.environ.get("PATH", "").split(os.pathsep)
for directory in pathdirs:
fullpath = os.path.join(directory, cmd)
if os.path.exists(fullpath) and os.access(fullpath, os.X_OK):
return fullpath
return None
class Completer:
def __init__(self, builtins: Dict[str, ShellCommand]):
self.builtins = builtins
def complete(self, text, state):
if state == 0:
parts = text.split()
if not parts:
self.matches = [cmd for cmd in self.builtins] + [p.name for p in Path('.').iterdir()]
return self.matches[state] if state < len(self.matches) else None
if len(parts) == 1:
self.matches = [cmd for cmd in self.builtins if cmd.startswith(parts[0])] + [
p.name for p in Path('.').iterdir() if p.name.startswith(parts[0])
]
elif len(parts) >= 2:
current_path = Path('.')
if parts[-2] == "cd":
target = parts[-1]
if target.startswith("/"):
current_path = Path("/")
target = target[1:]
for component in target.split("/"):
if component == "..":
current_path = current_path.parent
elif component and component != ".":
current_path = current_path / component
if not current_path.is_dir():
current_path = current_path.parent if current_path.parent.is_dir() else Path(".")
self.matches = [
(str(p) + "/") if p.is_dir() else str(p)
for p in current_path.iterdir()
if str(p.name).startswith(parts[-1])
]
else:
self.matches = [
(str(p) + "/") if p.is_dir() else str(p)
for p in current_path.iterdir()
if str(p.name).startswith(parts[-1])
]
else:
self.matches = []
try:
return self.matches[state]
except IndexError:
return None
class Shell:
def __init__(self):
self.builtins: Dict[str, ShellCommand] = {}
self.initializebuiltins()
self.completer = Completer(self.builtins)
readline.set_completer(self.completer.complete)
readline.parse_and_bind("tab: complete")
def initializebuiltins(self) -> None:
self.builtins.update({
"exit": ExitCommand(),
"echo": EchoCommand(),
"pwd": PwdCommand(),
"cd": CdCommand(),
})
self.builtins["type"] = TypeCommand(self.builtins)
def executeexternalcommand(self, tokens: List[str]) -> None:
try:
result = subprocess.run(tokens, capture_output=True, text=True, check=True)
print(result.stdout, end="")
except FileNotFoundError:
print(f"{tokens[0]}: command not found")
except subprocess.CalledProcessError as e:
print(e.stderr, end="")
except OSError as e:
print(f"Error executing {tokens[0]}: {e}")
def processinput(self, inputstr: str) -> None:
tokens = inputstr.strip().split()
if not tokens:
return
cmdname, cmdargs = tokens[0], tokens[1:]
if cmdname in self.builtins:
self.builtins[cmdname].execute(cmdargs)
elif PathResolver.findinpath(cmdname):
self.executeexternalcommand(tokens)
else:
print(f"{cmdname}: command not found")
def run(self) -> None:
while True:
try:
inputstr = input("$ ")
self.processinput(inputstr)
except KeyboardInterrupt:
print()
def main():
Shell().run()
if __name__ == "__main__":
main()