-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
169 lines (128 loc) · 4.15 KB
/
main.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
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
import os
import platform
import sys
import shutil
from threading import Semaphore
from time import sleep
from pathlib import Path
if platform.system()=='Windows':
PLATFORM=True
import msvcrt
semaphore = Semaphore()
ROOT = 'C:\\'
else:
PLATFORM=False
ROOT = '/'
import fcntl
semaphore = Semaphore()
def block_access(file=None,command=None,function=None):
##semaphore.acquire()
try:
if PLATFORM:
file = open(file, 'r+')
msvcrt.locking(file.fileno(), msvcrt.LK_RLCK, 1)
function(command)
else:
file = open(file, 'r+')
fcntl.flock(file, fcntl.LOCK_EX | fcntl.LOCK_NB)
function(command)
except Exception as e:
print('Ja exite um processo acessando este arquivo', e)
finally:
if PLATFORM:
msvcrt.locking(file.fileno(), msvcrt.LK_UNLCK, 1)
file.close()
else:
fcntl.flock(file, fcntl.LOCK_UN)
file.close()
##semaphore.release()
sleep(1)
def ver():
#Exibe versão
print(platform.system())
def dir(command):
#Lista o conteúdo do diretório
if PLATFORM:
os.system('dir {0}'.format(command))
else:
os.system('ls {0}'.format(command))
def exit():
#Finalizar o interpretador de comandos
sys.exit(0)
def mkdir(path):
#Criar um diretório
if not Path(path).is_dir():
os.mkdir(path)
else:
print("O diretório ja existe.")
def rmd(path):
#Apaga o diretório
if Path(path).is_dir():
if PLATFORM:
os.system('del {0}'.format(path))
else:
os.system('rm -r {0}'.format(path))
else:
print("Não é um diretório.")
def rma(path):
#Apaga um arquivo
os.remove(path)
def mv(path):
#Modificar o nome no arquivo
shutil.move(path[1],path[2])
def cp(path):
#Copiar um arquivo para outro diretório
shutil.copy(path[1],path[2])
def cat(path):
#Exibe o conteúdo de um arquivo
file = open(path, 'r+')
print(file.read())
file.close()
def edit(path):
#Permite editar um arquivo de texto
if PLATFORM:
os.system('notepad {0}'.format(path))
else:
os.system('nano {0}'.format(path))
def cd(path):
#Muda de diretório
os.chdir(path)
def current_dir():
#Exibe diretório atual
return os.getcwd()
while True:
try:
if current_dir == ROOT:
command = input('> ')
else:
command = input('{0}/> '.format(current_dir().replace('\\','/').replace('C:/','')))
if command.split(' ')[0]=='ver':
ver()
elif command.split(' ')[0]=='dir':
dir(command.split(' ')[1])
elif command.split(' ')[0]=='exit':
exit()
elif command.split(' ')[0]=='mkdir':
mkdir(command.split(' ')[1])
elif command.split(' ')[0]=='rm' and command.split(' ')[1]=='-r':
rmd(command.split(' ')[2])
elif command.split(' ')[0]=='rm' and command.split(' ')[1]=='-a':
block_access(command=command.split(' ')[2], function=rma,file=command.split(' ')[2])
elif command.split(' ')[0]=='mv':
block_access(command=command.split(' '), function=mv,file=command.split(' ')[1])
elif command.split(' ')[0]=='cp':
block_access(command=command.split(' '), function=cp,file=command.split(' ')[1])
elif command.split(' ')[0]=='cat':
block_access(command=command.split(' ')[1], function=cat,file=command.split(' ')[1])
elif command.split(' ')[0]=='edit':
block_access(command=command.split(' ')[1], function=edit,file=command.split(' ')[1])
elif command.split(' ')[0]=='cd':
cd(command.split(' ')[1])
else:
print(command.split(' ')[0],' Comando não foi encontrado.')
except PermissionError as e:
print("Sem permissão")
except Exception as e:
pass
except KeyboardInterrupt as e:
pass