-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.py
360 lines (264 loc) · 8.85 KB
/
file.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
"""tarstall: A package manager for managing archives
Copyright (C) 2022 hammy275
tarstall is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
tarstall is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with tarstall. If not, see <https://www.gnu.org/licenses/>."""
import json
import os
import re
import shutil
def file_vprint(to_print, end=None):
"""vprint used here to prevent circular import.
Args:
to_print (str): String to print if verbose
end (str): String to place at end of message (Python's end= argument in print())
"""
from config import vprint
vprint(to_print, end)
def check_bin(bin):
"""Check for Binary on System.
Args:
bin (str): Binary to check for
Returns:
bool: Whether or not the binary exists.
"""
return shutil.which(bin) is not None
def get_shell_file():
"""Get Shell File.
Attempts to automatically obtain the file used by the user's shell for PATH,
variable exporting, etc.
Returns:
str: File name in home directory to store PATHs, variables, etc.
"""
shell = os.environ["SHELL"]
if "bash" in shell:
file_vprint("Auto-detected bash")
return ".bashrc"
elif "zsh" in shell:
file_vprint("Auto-detected zsh")
return ".zshrc"
elif "fish" in shell:
file_vprint("Auto-detected fish")
return ".config/fish/config.fish"
else:
file_vprint("Couldn't auto-detect shell environment!")
return None
def get_shell_path():
"""Get Shell Path.
Attempts to automatically obtain the file used by the user's shell for PATH,
variable exporting, etc.
Returns:
str: Full path to the shell file.
"""
shell_file = get_shell_file()
if shell_file:
if shell_file == ".bashrc":
return full("~/.bashrc")
elif shell_file == ".zshrc":
return full("~/.zshrc")
elif "fish" in shell_file:
return full("~/.config/fish/config.fish")
else:
return None
def lock():
"""Lock tarstall.
Lock tarstall to prevent multiple instances of tarstall being used alongside each other
"""
create("/tmp/tarstall-lock")
file_vprint("Lock created!")
def unlock():
"""Remove tarstall lock."""
try:
os.remove(full("/tmp/tarstall-lock"))
except FileNotFoundError:
pass
file_vprint("Lock removed!")
def name(program):
"""Get Program Name.
Get the name of a program given the path to its archive/folder.
Args:
program (str): Path to program archive
Returns:
str: Name of program to use internally
"""
program_internal_name = re.sub(r'.*/', '/', program)
extension_length = len(extension(program))
program_internal_name = program_internal_name[program_internal_name.find('/')+1:(len(program_internal_name) - extension_length)]
return program_internal_name
def dirname(path):
"""Get Program Name for Directory
Args:
path (str): Path to program folder
Returns:
str: Name of program to user internall
"""
prog_int_name_temp = path[0:len(path)-1]
if prog_int_name_temp.startswith('/'):
program_internal_name = name(prog_int_name_temp + '.tar.gz')
else:
program_internal_name = name('/' + prog_int_name_temp + '.tar.gz')
return program_internal_name
def extension(program):
"""Get Extension of Program.
Args:
program (str): File name of program or URL/path to program.
Returns:
str: Extension of program
"""
if program[-3:].lower() == '.7z':
return program[-3:].lower()
elif program[-4:].lower() in ['.zip', '.rar', '.git']:
return program[-4:]
elif program[-7:].lower() in ['.tar.gz', '.tar.xz']:
return program[-7:]
else:
# Default to returning everything after the last .
return program[program.rfind("."):]
def exists(file_name):
"""Check if File Exists.
Args:
file_name (str): Path to file
Returns:
bool: Whether the file exists or not
"""
try:
return os.path.isfile(full(file_name)) or os.path.isdir(full(file_name))
except FileNotFoundError:
return False
def locked():
"""Get Lock State.
Returns:
bool: True if tarstall is locked. False otherwise.
"""
return exists("/tmp/tarstall-lock")
def full(file_name):
"""Full Path.
Converts ~'s, .'s, and ..'s to their full paths (~ to /home/username)
Args:
file_name (str): Path to convert
Returns:
str: Converted path
"""
return os.path.abspath(os.path.expanduser(file_name))
def spaceify(file_name):
"""Add Backslashes.
Adds backslashes before each space in a path.
Args:
file_name (str): Path to add backslashes to
Returns:
str: The path with backslashes
"""
return file_name.replace(" ", "\\ ")
def replace_in_file(old, new, file_path):
"""Replace Strings in File.
Replaces all instances of "old" with "new" in "file".
Args:
old (str): String to replace
new (str): String to replace with
file (str): Path to file to replace strings in
"""
rewrite = """"""
file_path = full(file_path)
f = open(file_path, 'r')
open_file = f.readlines()
f.close()
for l in open_file:
rewrite += l.replace(old, new)
written = open(file_path, 'w')
written.write(str(rewrite))
written.close() # Write then close our new copy of the file
return
def check_line(line, file_path, mode):
"""Check for Line.
Checks to see if a line is inside of a file. Modes are:
word: Split all lines into a list of WORDS, and check to see if the word supplied is in that list
fuzzy: Check if the supplied line is in the file, even if it doesn't make up the whole line.
Args:
line (str): Line/word to look for
file_path (str): Path to file to look for the line/word in
mode (str): Mode to search with
Returns:
bool: Whether or not the line/word is in the file
"""
f = open(full(file_path), 'r')
open_file = f.readlines()
f.close()
for l in open_file:
if mode == 'word':
new_l = l.rstrip()
new_l = new_l.split()
elif mode == 'fuzzy':
new_l = l.rstrip()
if line in new_l:
return True
return False
def create(file_path):
"""Create Empty File.
Args:
file_path (str): Path to file to create
"""
f = open(full(file_path), "w+")
f.close()
def remove_line(line, file_path, mode):
"""Remove Line from File.
Removes a line from a file. Uses the following modes:
word: Removes line if supplied word is found in it (words are sets of chars seperated by spaces)
poundword: Same as word, but line must also contain a #
fuzzy: Removes line, matching with supplied line, even if the line being removed has more than the supplied line
Args:
line (str)): Line/word to remove
file_path (str): Path to file to remove lines from
mode (str): Mode to use to find lines to remove
"""
rewrite = """"""
file_path = full(file_path)
f = open(file_path, 'r')
open_file = f.readlines()
f.close()
for l in open_file:
if mode == 'word' or mode == 'poundword':
new_l = l.rstrip()
new_l = new_l.split()
elif mode == 'fuzzy':
new_l = l.rstrip()
if line in new_l:
if not ('#' in new_l) and mode == 'poundword':
rewrite += l
else:
pass
else:
rewrite += l
written = open(file_path, 'w')
written.write(str(rewrite))
written.close() # Write then close our new copy of the file
return
def add_line(line, file_path):
"""Adds Line to a File."""
file_path = full(file_path)
f = open(file_path, 'a')
f.write(line)
f.close()
def char_check(name):
"""Check Chars.
Checks if a string contains a # or a space.
Returns:
bool: True if line contains space or #; False otherwise
"""
return ' ' in name or '#' in name
def get_db(db_check=""):
"""Get Database.
Returns:
dict: Database. {} if database fails to be read or found on disk.
"""
try:
with open(full("~/.tarstall/database")) as f:
return json.load(f)
except (FileNotFoundError, json.decoder.JSONDecodeError):
return {}