-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommitter
More file actions
executable file
·349 lines (261 loc) · 14.1 KB
/
committer
File metadata and controls
executable file
·349 lines (261 loc) · 14.1 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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2020-2022 Jared Daniel Carbonell Recomendable.
from pathlib import Path
from json import dumps, loads
from shutil import copyfile
import argparse
import sys
import os
class Committer:
def __init__(self, data_filepath, args):
self.args = args
self.data_filepath = data_filepath
self.data = None
if not Path(data_filepath).is_file():
with open(data_filepath, 'w') as f:
f.write('{}\n')
with open(data_filepath, 'r') as f:
data_raw = f.read()
self.data = loads(data_raw.replace('~', os.path.expanduser('~')))
action = self.args['action']
if action == 'add':
self.process_add()
elif action == 'import':
self.process_import()
elif action == 'export':
self.process_export()
elif action == 'list':
self.process_list()
elif action == 'info':
self.process_info()
elif action == 'update':
self.process_update()
elif action == 'diff':
self.process_diff()
elif action == 'mv':
self.process_mv()
elif action == 'cp':
self.process_cp()
elif action == 'rm':
self.process_rm()
def save_data(self):
with open(self.data_filepath, 'w') as f:
to_write = dumps(self.data, sort_keys=True, indent=4)
f.write(to_write.replace(os.path.expanduser('~'), '~') + '\n')
def process_add(self):
filename = self.args['filename']
target_location = self.args['target-location']
platforms = self.args['platforms']
if not Path(target_location).is_file():
print('ERROR: File at target location does not exist.')
sys.exit(1)
if not Path(filename).is_file():
copyfile(target_location, filename)
value = {
'target_location': target_location,
'is_in_use': True,
'platforms': platforms
}
self.data[filename] = value
self.save_data()
def process_import(self):
filename = self.args['filename']
custom_filename = self.args['custom_filename']
if not filename in self.data:
print(f'ERROR: File not registered at {self.data_filepath}.')
sys.exit(1)
if not 'target_location' in self.data[filename]:
print(f'ERROR: Possibly malformed {self.data_filepath}.')
sys.exit(1)
if custom_filename:
copyfile(self.data[filename]['target_location'], custom_filename)
self.data[custom_filename] = self.data[filename]
self.save_data()
else:
copyfile(self.data[filename]['target_location'], filename)
def process_export(self):
filename = self.args['filename']
filepath = self.args['filepath']
is_backup = self.args['backup']
is_imported = self.args['imported']
if not filename in self.data:
print(f'ERROR: File not registered at {self.data_filepath}.')
sys.exit(1)
if not 'target_location' in self.data[filename]:
print(f'ERROR: Possibly malformed {self.data_filepath}.')
sys.exit(1)
location_at_system = self.data[filename]['target_location']
if is_backup:
try:
copyfile(location_at_system, f'{location_at_system}.backup')
except FileNotFoundError:
print(f'WARNING: {location_at_system} does not exist. No backup made.')
if filepath is None:
filepath = location_at_system
if is_imported:
filepath += '.imported'
last_index = len(filepath) - len(filepath.split('/')[-1])
Path(filepath[:last_index]).mkdir(parents=True, exist_ok=True)
copyfile(filename, filepath)
def process_list(self):
include_all = self.args['all']
is_verbose = self.args['verbose']
filter_by_filename = self.args['filter_by_filename']
filter_by_target_location = self.args['filter_by_target_location']
filter_by_platform = self.args['filter_by_platform']
if not self.data or len(self.data) < 1:
print('Nothing to display')
sys.exit(0)
print('DOTFILES:')
for filename in self.data:
if not include_all and not self.data[filename]['is_in_use']:
continue
if filter_by_filename is not None and filter_by_filename not in filename:
continue
if filter_by_target_location is not None and filter_by_target_location not in self.data[filename]['target_location']:
continue
if filter_by_platform is not None and not set(filter_by_platform) & set(self.data[filename]['platforms']):
continue
if is_verbose:
print()
print(f'{filename}')
if is_verbose:
self._print_about_dotfile(filename)
def process_info(self):
filename = self.args['filename']
if not filename in self.data:
print(f'ERROR: File not registered at {self.data_filepath}.')
sys.exit(1)
if not 'target_location' in self.data[filename] or not 'is_in_use' in self.data[filename] or not 'platforms' in self.data[filename]:
print(f'ERROR: Possibly malformed {self.data_filepath}.')
sys.exit(1)
print(f'DOTFILE: {filename}')
self._print_about_dotfile(filename)
def process_update(self):
filename = self.args['filename']
target_location = self.args['target_location']
deprecate = self.args['deprecate']
undeprecate = self.args['undeprecate']
platforms = self.args['platforms']
platforms_to_add = self.args['add_platforms']
platforms_to_remove = self.args['remove_platforms']
if not filename in self.data:
print(f'ERROR: File not registered at {self.data_filepath}.')
sys.exit(1)
if target_location is not None:
self.data[filename]['target_location'] = target_location
if deprecate and undeprecate:
print(f'ERROR: Cannot decide whether to deprecate or set active the concerned file.')
sys.exit(1)
if deprecate:
self.data[filename]['is_in_use'] = False
if undeprecate:
self.data[filename]['is_in_use'] = True
if platforms is not None:
self.data[filename]['platforms'] = platforms
if platforms_to_add is not None:
for platform in platforms_to_add:
if platform not in self.data[filename]['platforms']:
self.data[filename]['platforms'].append(platform)
if platforms_to_remove is not None:
for platform in platforms_to_remove:
if platform in self.data[filename]['platforms']:
self.data[filename]['platforms'].remove(platform)
self.save_data()
def process_diff(self):
filename = self.args['filename']
if not filename in self.data:
print(f'ERROR: File not registered at {self.data_filepath}.')
sys.exit(1)
if not 'target_location' in self.data[filename]:
print(f'ERROR: Possibly malformed {self.data_filepath}.')
sys.exit(1)
location_at_system = self.data[filename]['target_location']
command = f'diff {location_at_system} {filename}'
os.system(command)
def process_mv(self):
filename = self.args['filename']
newname = self.args['newname']
if not filename in self.data:
print(f'ERROR: File not registered at {self.data_filepath}.')
sys.exit(1)
os.rename(filename, newname)
self.data[newname] = self.data[filename]
self.data.pop(filename)
self.save_data()
def process_cp(self):
filename = self.args['filename']
newname = self.args['newname']
if not filename in self.data:
print(f'ERROR: File not registered at {self.data_filepath}.')
sys.exit(1)
copyfile(filename, newname)
self.data[newname] = self.data[filename]
self.save_data()
def process_rm(self):
filename = self.args['filename']
is_delete_from_system = self.args['delete_from_system']
if not filename in self.data:
print(f'ERROR: File not registered at {self.data_filepath}.')
sys.exit(1)
if is_delete_from_system:
if not 'target_location' in self.data[filename]:
print(f'ERROR: Possibly malformed {self.data_filepath}.')
sys.exit(1)
os.remove(self.data[filename]['target_location'])
os.remove(filename)
self.data.pop(filename)
self.save_data()
def _print_about_dotfile(self, filename):
print(' Target Location : {}'.format(self.data[filename]['target_location']))
print(' Is in Active Use?: {}'.format(self.data[filename]['is_in_use']))
print(' Platforms:')
for platform in self.data[filename]['platforms']:
print(f' * {platform}')
if __name__ == '__main__':
parser = argparse.ArgumentParser(
prog='committer',
description='Copies dot files to and from the system.'
)
subparsers = parser.add_subparsers(title='actions', dest='action', description='Main action to take on the concerned file', required=True)
parser_add = subparsers.add_parser('add', help='Registers a new file to be tracked into this repository')
parser_import = subparsers.add_parser('import', help='Copies concerned file from the system to this repository')
parser_export = subparsers.add_parser('export', help='Copies concerned file from this repository to the system')
parser_list = subparsers.add_parser('list', help='Shows files being tracked by this repository, by default only active ones')
parser_info = subparsers.add_parser('info', help='Shows information about the concerned file')
parser_update = subparsers.add_parser('update', help='Update information about the concerned file')
parser_diff = subparsers.add_parser('diff', help='Displays the difference between the file in the system and this repository')
parser_mv = subparsers.add_parser('mv', help='Renames the file of concern in this repository')
parser_cp = subparsers.add_parser('cp', help='Copies the file of concern in this repository into another file of concern in this repository')
parser_rm = subparsers.add_parser('rm', help='Removes the file of concern from this repository')
parser_add.add_argument('filename', help='The file to register to this repository')
parser_add.add_argument('target-location', help='The concerned file location in the system')
parser_add.add_argument('platforms', action='extend', nargs='+', type=str, help='The platforms that the file will be supported on')
parser_import.add_argument('filename', help='The file to copy from the system to this repository')
parser_import.add_argument('-c', '--custom-filename', help='Imports the concerned file as a new file to this repository with CUSTOM_FILENAME as the filename')
parser_export.add_argument('filename', help='The file to copy from this repository to the system')
parser_export.add_argument('-p', '--filepath', help='Exports the concerned file to the specified FILEPATH instead of the original target filepath')
parser_export.add_argument('-b', '--backup', action='store_true', help='Duplicates the existing file in the system, so that the duplicate has ".backup" appended')
parser_export.add_argument('-i', '--imported', action='store_true', help='Appends ".imported" to the end of the filename of the file being exported to the system')
parser_list.add_argument('-a', '--all', action='store_true', help='List all files, including deprecated and active ones')
parser_list.add_argument('-v', '--verbose', action='store_true', help='Shows information about files as they are listed')
parser_list.add_argument('-f', '--filter-by-filename', help='Lists files that contain FILTER_BY_NAME')
parser_list.add_argument('-t', '--filter-by-target-location', help='Lists files whose target locations contain FILTER_BY_TARGET_LOCATION')
parser_list.add_argument('-p', '--filter-by-platform', action='extend', nargs='+', type=str, help='Filters by one or more platforms specified, exact keyword and case-sensitive')
parser_info.add_argument('filename', help='The file to show information on')
parser_update.add_argument('filename', help='The file to update information on')
parser_update.add_argument('-t', '--target-location', help='Sets the new concerned file location in the system')
parser_update.add_argument('-d', '--deprecate', action='store_true', help='Sets the concerned file to be no longer active')
parser_update.add_argument('-u', '--undeprecate', action='store_true', help='Sets the concerned file to be active')
parser_update.add_argument('-p', '--platforms', action='extend', nargs='+', type=str, help='Sets the new platforms that the file will be supported on')
parser_update.add_argument('-a', '--add-platforms', action='extend', nargs='+', type=str, help='Adds the one or more new platforms specified that the file will be supported on')
parser_update.add_argument('-r', '--remove-platforms', action='extend', nargs='+', type=str, help='Removes the one or more existing platforms that the file is supported on')
parser_diff.add_argument('filename', help='The file to compare')
parser_mv.add_argument('filename', help='The file to rename')
parser_mv.add_argument('newname', help='The new name of the file')
parser_cp.add_argument('filename', help='The file to copy')
parser_cp.add_argument('newname', help='The new name of the file to create')
parser_rm.add_argument('filename', help='The file to remove')
parser_rm.add_argument('-D', '--delete-from-system', action='store_true', help='Also delete the file in the system, if it exists')
Committer('_about.json', vars(parser.parse_args()))