Repository navigation
Expand file tree
/
Copy pathcommand.py
More file actions
157 lines (124 loc) · 5.26 KB
/
Copy pathcommand.py
File metadata and controls
157 lines (124 loc) · 5.26 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
from fake_filesystem import FakeFileSystem
class CommandProcessor:
def __init__(self):
self.fs = FakeFileSystem()
self.commands = {
'help': self._help,
'dir': self._dir,
'cd': self._cd,
'cls': self._cls,
'echo': self._echo,
'exit': self._exit,
'type': self._type,
'ver': self._ver
}
def get_matching_files(self, partial):
"""Restituisce i file/cartelle che corrispondono al percorso parziale"""
current_dir = self.fs.get_current_directory()
contents = self.fs.get_current_directory_contents()
if not contents:
return []
# Se il percorso è assoluto
if partial.startswith('C:\\'):
base_path = 'C:\\'
rel_path = partial[3:]
else:
base_path = current_dir
rel_path = partial
# Trova la directory base
path_parts = [p for p in rel_path.split('\\') if p]
current = self.fs.file_system['C:\\']
for part in path_parts[:-1]:
if part in current['contents'] and current['contents'][part]['type'] == 'dir':
current = current['contents'][part]
else:
return []
# Trova i match per l'ultima parte
last_part = path_parts[-1] if path_parts else ''
matches = [name for name in current['contents'].keys() if name.startswith(last_part)]
return matches
def process_command(self, command_str):
if not command_str.strip():
return
parts = command_str.split()
command = parts[0].lower()
args = parts[1:] if len(parts) > 1 else []
if command in self.commands:
return self.commands[command](args)
else:
return f"'{command}' is not recognized as an internal or external command,\noperable program or batch file."
def _help(self, args):
help_text = """For more information on a specific command, type HELP command-name
ASSOC Displays or modifies file extension associations.
ATTRIB Displays or changes file attributes.
CD Displays the name of or changes the current directory.
CLS Clears the screen.
COPY Copies one or more files to another location.
DIR Displays a list of files and subdirectories in a directory.
ECHO Displays messages, or turns command echoing on or off.
EXIT Quits the CMD.EXE program (command interpreter).
TYPE Displays the contents of a text file.
VER Displays the version of Windows."""
return help_text
def _dir(self, args):
contents = self.fs.get_current_directory_contents()
if contents is None:
return "Directory not found."
output = []
output.append(f" Directory of {self.fs.get_current_directory()}\n")
dirs = []
files = []
for name, info in contents.items():
if info['type'] == 'dir':
dirs.append(name)
else:
files.append((name, info['size']))
# Sort directories and files
dirs.sort()
files.sort()
for d in dirs:
output.append(f"{'<DIR>':14} {d}")
for f, size in files:
output.append(f"{size:14} {f}")
output.append(f"\n{'{} File(s)'.format(len(files)):20} {'{} bytes'.format(sum(f[1] for f in files))}")
output.append(f"{'{} Dir(s)'.format(len(dirs)):20} {'0 bytes free'.format()}")
return '\n'.join(output)
def _cd(self, args):
if not args:
return self.fs.get_current_directory()
new_dir = args[0]
success = self.fs.change_directory(new_dir)
if not success:
return "The system cannot find the path specified."
def _cls(self, args):
return "\n" * 100 # Simulate clearing the screen
def _echo(self, args):
return ' '.join(args)
def _exit(self, args):
return "EXIT"
def _type(self, args):
if not args:
return "Invalid syntax."
filename = args[0]
if not filename.startswith('C:\\'):
filename = self.fs.get_current_directory() + filename
# Find the file
path_parts = filename.split('\\')
path_parts = [part for part in path_parts if part] # Remove empty parts
current = self.fs.file_system['C:\\']
for part in path_parts[1:]: # Skip 'C:'
if part in current['contents']:
current = current['contents'][part]
else:
return "File not found."
if current['type'] != 'file':
return "Access is denied."
# Return some fake content based on filename
if filename.endswith('.txt'):
return f"This is the content of {filename}"
elif filename.endswith('.bat'):
return f"@echo off\nrem This is a batch file\npause"
else:
return "This is a binary file and cannot be displayed."
def _ver(self, args):
return "Microsoft Windows [Version 10.0.19041.1237]"