Repository navigation
Expand file tree
/
Copy pathcli.py
More file actions
179 lines (143 loc) · 7.44 KB
/
Copy pathcli.py
File metadata and controls
179 lines (143 loc) · 7.44 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
179
import json
from prompt_toolkit import prompt
from prompt_toolkit.history import InMemoryHistory
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.completion import WordCompleter
import requests
import os
commands = ["upload_file", "get_file", "get_info", "exit", "create_directory", "move_file", "move_folder",
"re_replicate", "delete_file", "get_directory", "list_directory", "datanode_status", "delete_folder", "copy_file"]
command_completer = WordCompleter(commands)
# NameNode API endpoint
namenode_url = "http://localhost:8090"
def handle_upload(file_path, chunks, directory_path):
with open(file_path, 'rb') as file:
# Continue with the file upload
files = {'file': (os.path.basename(file.name), file)}
response = requests.post(
f"{namenode_url}/upload_file",
files=files,
data={'number_of_chunks': chunks, 'directory_path': directory_path})
# response = requests.post(f"{namenode_url}/upload_file", files=files, data={'number_of_chunks': chunks})
indented_json = json.dumps(response.json(), indent=2)
print(indented_json)
def handle_get_file(file_name, directory_path):
response = requests.post(
f"{namenode_url}/get_file",
data={'file_name': file_name, 'directory_path': directory_path}
)
print(response.text)
def handle_get_info():
response = requests.get(f"{namenode_url}/get_info")
indented_json = json.dumps(response.json(), indent=2)
print(indented_json)
def handle_get_directory():
response = requests.get(f"{namenode_url}/get_directory")
indented_json = json.dumps(response.json(), indent=2)
print(indented_json)
def handle_datanode_status():
response = requests.get(f"{namenode_url}/datanode_status")
indented_json = json.dumps(response.json(), indent=2)
print(indented_json)
def handle_create_directory(directory_path):
response = requests.post(
f"{namenode_url}/create_directory", data={'directory_path': directory_path})
print(response.text)
def handle_move_file(original_path, destination_path, file_name):
response = requests.post(f"{namenode_url}/move_file", data={"original_path": original_path,
"destination_path": destination_path, "file_name": file_name})
indented_json = json.dumps(response.json(), indent=2)
print(indented_json)
def handle_move_folder(original_path, destination_path, folder_name):
response = requests.post(f"{namenode_url}/move_folder", data={'original_path': original_path,
'destination_path': destination_path, 'folder_name': folder_name})
indented_json = json.dumps(response.json(), indent=2)
print(indented_json)
def handle_re_replicate(file_name, directory_path):
response = requests.post(f"{namenode_url}/re_replicate",
data={'file_name': file_name, "directory_path": directory_path})
indented_json = json.dumps(response.json(), indent=2)
print(indented_json)
def handle_delete_file(file_name, directory_path):
response = requests.post(f"{namenode_url}/delete_file",
data={'file_name': file_name, 'directory_path': directory_path})
indented_json = json.dumps(response.json(), indent=2)
print(indented_json)
def handle_list_directory(directory_path):
response = requests.get(
f"{namenode_url}/list_directory", data={'directory_path': directory_path})
indented_json = json.dumps(response.json(), indent=2)
print(indented_json)
def handle_delete_folder(folder_name, directory_path):
response = requests.post(f"{namenode_url}/delete_folder",
data={'folder_name': folder_name, 'directory_path': directory_path})
indented_json = json.dumps(response.json(), indent=2)
print(indented_json)
def handle_copy_file(original_path, destination_path, file_name):
response = requests.post(f"{namenode_url}/copy_file", data={'original_path': original_path,
'destination_path': destination_path, 'file_name': file_name})
indented_json = json.dumps(response.json(), indent=2)
print(indented_json)
def main():
history = InMemoryHistory()
while True:
user_input = prompt(">>> ",
history=history,
auto_suggest=AutoSuggestFromHistory(),
completer=command_completer)
if user_input.lower() == 'exit':
break
elif user_input.lower() == 'upload_file':
file_path = input("Enter the path to the file to upload: ")
chunks = input("Enter number of chunks: ")
directory_path = input("Enter the directory path: ")
handle_upload(file_path, chunks, directory_path)
elif user_input.lower() == 'get_info':
handle_get_info()
elif user_input.lower() == 'get_file':
file_name = input("Enter the file name to download: ")
directory_path = input(
"Enter the directory path (optional, press Enter to use '/'): ")
handle_get_file(file_name, directory_path)
elif user_input.lower() == "create_directory":
directory_path = input("Enter directory path: ")
handle_create_directory(directory_path)
elif user_input.lower() == "move_file":
original_path = input("Enter the original path: ")
destination_path = input("Enter the destination path: ")
file_name = input("Enter the name of file: ")
handle_move_file(original_path, destination_path, file_name)
elif user_input.lower() == "move_folder":
original_path = input("Enter the original path: ")
destination_path = input("Enter the destination path: ")
folder_name = input("Enter the name of folder: ")
handle_move_folder(original_path, destination_path, folder_name)
elif user_input.lower() == "re_replicate":
file_name = input("Enter name of file: ")
directory_path = input("Enter name of directory_path: ")
handle_re_replicate(file_name, directory_path)
elif user_input.lower() == "delete_file":
file_name = input("Enter name of file: ")
directory_path = input("Enter directory path: ")
handle_delete_file(file_name, directory_path)
elif user_input.lower() == "get_directory":
handle_get_directory()
elif user_input.lower() == "list_directory":
directory_path = input("Enter directory path: ")
handle_list_directory(directory_path)
elif user_input.lower() == "datanode_status":
handle_datanode_status()
elif user_input.lower() == "delete_folder":
folder_name = input("Enter folder_name: ")
directory_path = input("Enter directory path: ")
handle_delete_folder(folder_name, directory_path)
elif user_input.lower() == "copy_file":
original_path = input("Enter original path: ")
destination_path = input("Enter destination path: ")
file_name = input("Enter file name: ")
handle_copy_file(original_path, destination_path, file_name)
else:
print(f"You entered: {user_input}")
# ["upload_file", "get_file", "get_info","exit","create_directory","move_file","move_folder","re_replicate","delete_file","get_directory","list_directory","datanode_status"]
if __name__ == "__main__":
main()