-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpkgsync
More file actions
executable file
·134 lines (112 loc) · 3.22 KB
/
pkgsync
File metadata and controls
executable file
·134 lines (112 loc) · 3.22 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
#!/bin/env python3
import sys
import os
import json
import click
default_pkglist_cmd = 'echo a dummy get pkglist command'
def save_data():
global g_file
global g_data
with open(g_file, 'w+') as f:
f.write(json.dumps(g_data, sort_keys=True, indent=4))
def add_items_to_list(thelist, items):
for item in items:
if item not in thelist:
thelist.append(item)
def del_items_from_list(thelist, items):
for item in items:
if item in thelist:
thelist.remove(item)
@click.group()
@click.argument('file')
def cli(file):
global g_file
global g_data
global g_pending
if os.path.exists(file):
with open(file, 'r+') as f:
g_data = json.loads(f.read())
else:
print(f"warning!!! no such file '{file}'", file=sys.stderr)
g_data = dict()
if 'sync' not in g_data:
g_data['sync'] = list()
if 'ignore' not in g_data:
g_data['ignore'] = list()
if 'pkglist_cmd' not in g_data:
g_data['pkglist_cmd'] = default_pkglist_cmd
g_file = file
# get pending list
os.system(g_data['pkglist_cmd'] + " > pkglist.tmp.txt")
with open("pkglist.tmp.txt") as f:
l = f.read().split()
l = map(lambda x: x.strip(), l)
l = filter(lambda x: x not in g_data['ignore'] and x not in g_data['sync'], l)
g_pending = list(l)
os.system("rm pkglist.tmp.txt")
@click.command(name='list-pending')
def list_pending():
global g_pending
if len(g_pending) != 0:
click.echo('\n'.join(g_pending))
@click.command(name='list-sync')
def list_sync():
global g_data
if len(g_data['sync']) != 0:
click.echo('\n'.join(g_data['sync']))
@click.command(name='list-ignore')
def list_ignore():
global g_data
if len(g_data['ignore']) != 0:
click.echo('\n'.join(g_data['ignore']))
@click.command()
@click.argument('args', nargs=-1)
def add(args):
global g_data
add_items_to_list(g_data['sync'], args)
del_items_from_list(g_data['ignore'], args)
save_data()
@click.command(name='add-all')
def add_all():
global g_data
global g_pending
add_items_to_list(g_data['sync'], g_pending)
del_items_from_list(g_data['ignore'], g_pending)
save_data()
@click.command()
@click.argument('args', nargs=-1)
def ignore(args):
global g_data
add_items_to_list(g_data['ignore'], args)
del_items_from_list(g_data['sync'], args)
save_data()
@click.command(name='ignore-all')
def ignore_all():
global g_data
global g_pending
add_items_to_list(g_data['ignore'], g_pending)
save_data()
@click.command(name='del')
@click.argument('args', nargs=-1)
def delete(args):
global g_data
del_items_from_list(g_data['sync'], args)
del_items_from_list(g_data['ignore'], args)
save_data()
@click.command(name='set-pkglist-cmd')
@click.argument('args', nargs=-1)
def set_pkglist_cmd(args):
global g_data
g_data['pkglist_cmd'] = ' '.join(args)
save_data()
cli.add_command(list_pending)
cli.add_command(list_sync)
cli.add_command(list_ignore)
cli.add_command(add)
cli.add_command(add_all)
cli.add_command(ignore)
cli.add_command(delete)
cli.add_command(ignore_all)
cli.add_command(set_pkglist_cmd)
if __name__ == '__main__':
cli()