-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhosts.py
executable file
·72 lines (63 loc) · 2.37 KB
/
hosts.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
import shutil
from datetime import datetime
import click
# @click.command()
# @click.option('--file', help='process file other than /etc/hosts', required=False)
# def hosts_manager(enable, disable, file):
#
# print(enable)
# print(disable)
@click.command()
@click.option('--file', help="if not /etc/hosts", default='/etc/hosts', type=click.File(mode='r'))
@click.option('--dry-run', help="only pretend to", default=False, is_flag=True)
@click.option('-v', '--verbose', help="output resulting file at the end", default=False, is_flag=True)
@click.option('--backup/--no-backup', help="default: true", default=True, is_flag=True)
@click.argument('command', required=True)
@click.argument('sections', required=True, nargs=-1)
def hosts_manager(command, sections, *args, **kwargs):
"""
enable|disable named sections in your hosts file
filename.bak-YYYY-MM-DD is always created
"""
if not command in ['enable', 'disable']:
click.echo("command must be enable|disable")
return
file = kwargs.get('file')
dry = kwargs.get('dry_run')
verbose = kwargs.get('verbose')
do_backup = kwargs.get('backup')
click.echo('processing section %s in %s' % ('|'.join(sections), file.name, ))
lines = tuple(file)
new_lines = []
started = False
for line in lines:
modified = False
if line.startswith('# end'):
started = False
if started:
if command == 'enable':
if line.startswith('#'):
new_lines.append(line[1:])
modified = True
if command == 'disable':
if not line.startswith('#'):
new_lines.append('#%s' % line)
modified = True
for section in sections:
if line.startswith('# start:%s' % section):
started = True
if not modified:
new_lines.append(line)
if dry or verbose:
for l in new_lines:
click.echo(l.strip())
if not dry:
if do_backup:
backup = '%s.backup-%s' % (file.name, datetime.now().strftime("%Y-%m-%d--%H-%M-%S"))
shutil.copy(file.name, backup)
click.echo('backup file created: %s' % backup)
write_file = open(file.name, 'w')
write_file.writelines(new_lines)
write_file.close()
if __name__ == '__main__':
hosts_manager()