-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
113 lines (79 loc) · 2.58 KB
/
utils.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
# utils.py
import os
import conf
import getpass
def get_directories(directory):
"""
Returns a list of all the directories in a directory.
Args:
directory: The path to the directory.
Returns:
A list of the directories in the directory.
"""
directories = []
for dirr in os.listdir(directory):
if os.path.isdir(os.path.join(directory, dirr)):
directories.append(os.path.join(conf.STORE_DIR, dirr))
return directories
def get_oldest_files(directory, amount):
"""
Returns a list of the oldest files in a directory, not including the newest 10 files and only
if the directory has more than 10 files.
Args:
directory: The path to the directory.
amount: The number of files to return.
Returns:
A list of the oldest files in the directory.
"""
if len(os.listdir(directory)) <= amount:
return []
all_files = []
for file in os.listdir(directory):
if os.path.isfile(os.path.join(directory, file)):
all_files.append(file)
newest_files = all_files[:amount]
files_not_in_newest = [file for file in all_files if file not in newest_files]
return files_not_in_newest
def num_files(directory):
"""
Returns the number of files in a directory.
Args:
directory: The path to the directory.
Returns:
The number of files in the directory.
"""
number_of_files = 0
for file in os.listdir(directory):
if os.path.isfile(os.path.join(directory, file)):
number_of_files += 1
return number_of_files
def del_oldest_configs(amount):
"""
Deletes the oldest configs in the store directory.
Args:
Number of configs to delete.
Returns:
None
"""
for i in get_directories(conf.STORE_DIR):
for file in get_oldest_files(i, amount):
print(f"Removing {os.path.join(i, file)}.")
os.remove(os.path.join(i, file))
def get_credentials():
"""
Get user input for username, password and enable password.
Args:
None
Returns:
A dictionary with the username, password and enable password.
"""
username = input("Enter the username to authenticate with: ")
password = getpass.getpass("Enter the password to authenticate with: ")
enable_password = getpass.getpass(
"Enter the enable password to authenticate with \n(Press Enter if it is the same as previous password): "
)
return {
"username": username,
"password": password,
"enable_password": enable_password,
}