forked from jessepeterson/margarita
-
Notifications
You must be signed in to change notification settings - Fork 0
/
margarita.py
148 lines (115 loc) · 4.38 KB
/
margarita.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
from flask import Flask
from flask import jsonify, render_template, redirect
from flask import request
app = Flask(__name__)
import os, sys
try:
import json
except ImportError:
# couldn't find json, try simplejson library
import simplejson as json
import getopt
from operator import itemgetter
from reposadolib import reposadocommon
@app.route('/')
def index():
return render_template('margarita.html')
@app.route('/list_branches', methods=['POST'])
def list_branches():
'''Returns catalog branch names'''
catalog_branches = reposadocommon.getCatalogBranches()
return jsonify(result=catalog_branches)
@app.route('/products', methods=['POST'])
def products():
products = reposadocommon.getProductInfo()
prodlist = []
for prodid in products.keys():
if 'title' in products[prodid] and 'version' in products[prodid] and 'PostDate' in products[prodid]:
prodlist.append({
'title': products[prodid]['title'],
'version': products[prodid]['version'],
'PostDate': products[prodid]['PostDate'].strftime('%Y-%m-%d'),
'id': prodid,
'depr': len(products[prodid].get('AppleCatalogs', [])) < 1,
})
else:
print 'Invalid update!'
sprodlist = sorted(prodlist, key=itemgetter('PostDate'), reverse=True)
return jsonify(result=sprodlist)
@app.route('/new_branch/<branchname>', methods=['POST'])
def new_branch(branchname):
catalog_branches = reposadocommon.getCatalogBranches()
if branchname in catalog_branches:
reposadocommon.print_stderr('Branch %s already exists!', branchname)
abort(401)
catalog_branches[branchname] = []
reposadocommon.writeCatalogBranches(catalog_branches)
return jsonify(result='success')
@app.route('/delete_branch/<branchname>', methods=['POST'])
def delete_branch(branchname):
catalog_branches = reposadocommon.getCatalogBranches()
if not branchname in catalog_branches:
reposadocommon.print_stderr('Branch %s does not exist!', branchname)
return
del catalog_branches[branchname]
# this is not in the common library, so we have to duplicate code
# from repoutil
for catalog_URL in reposadocommon.pref('AppleCatalogURLs'):
localcatalogpath = reposadocommon.getLocalPathNameFromURL(catalog_URL)
# now strip the '.sucatalog' bit from the name
if localcatalogpath.endswith('.sucatalog'):
localcatalogpath = localcatalogpath[0:-10]
branchcatalogpath = localcatalogpath + '_' + branchname + '.sucatalog'
if os.path.exists(branchcatalogpath):
reposadocommon.print_stdout(
'Removing %s', os.path.basename(branchcatalogpath))
os.remove(branchcatalogpath)
reposadocommon.writeCatalogBranches(catalog_branches)
return jsonify(result=True);
@app.route('/add_all/<branchname>', methods=['POST'])
def add_all(branchname):
products = reposadocommon.getProductInfo()
catalog_branches = reposadocommon.getCatalogBranches()
catalog_branches[branchname] = products.keys()
reposadocommon.writeCatalogBranches(catalog_branches)
reposadocommon.writeAllBranchCatalogs()
return jsonify(result=True)
@app.route('/process_queue', methods=['POST'])
def process_queue():
queue = json.loads(request.form['queue'])
catalog_branches = reposadocommon.getCatalogBranches()
for cat in queue['listing']:
if cat not in catalog_branches.keys():
print 'No such catalog'
continue
for prodid in queue['listing'][cat]:
if prodid not in catalog_branches[cat]:
# TODO: check for actual prodid?
print 'Adding product',prodid,'to cat',cat
catalog_branches[cat].append(prodid)
for cat in queue['delisting']:
if cat not in catalog_branches.keys():
print 'No such catalog'
continue
for prodid in queue['delisting'][cat]:
if prodid in catalog_branches[cat]:
print 'Removing product',prodid,'from cat',cat
catalog_branches[cat].remove(prodid)
reposadocommon.writeCatalogBranches(catalog_branches)
reposadocommon.writeAllBranchCatalogs()
return jsonify(result=True);
def main():
optlist, args = getopt.getopt(sys.argv[1:], 'db:p:')
flaskargs = {}
flaskargs['host'] = '0.0.0.0'
flaskargs['port'] = 8089
for o, a in optlist:
if o == '-d':
flaskargs['debug'] = True
elif o == '-b':
flaskargs['host'] = a
elif o == '-p':
flaskargs['port'] = int(a)
app.run(**flaskargs)
if __name__ == '__main__':
main()