-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathport_blobs.py
90 lines (75 loc) · 2.44 KB
/
port_blobs.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
#!/usr/bin/env python3
from pathlib import Path
from urllib.request import urlopen
from configparser import ConfigParser
import argparse
import json
import os
import requests
def get_blobs(demo_id):
'''
get all the blobs
'''
params = {'demo_id': demo_id}
response = requests.get('http://127.0.0.1/api/blobs/get_blobs', params=params).json()
return response
def remove_blob_from_demo(demo_id, blob_set, pos_set):
'''
remove all blobs from demo
'''
params = {'demo_id': demo_id,
'pos_set': pos_set,
'blob_set': blob_set}
response = requests.delete('http://127.0.0.1/api/blobs/remove_blob_from_demo', params=params).json()
print(response)
assert response['status'] == 'OK'
def add_blob_to_demo(path, demo_id, title, credit):
'''
add all blobs to demo
'''
params = {
'demo_id': demo_id,
'title': title,
'credit': credit
}
files = {'blob': urlopen('file:///' + path)}
response = requests.post('http://127.0.0.1/api/blobs/add_blob_to_demo', params=params, files=files).json()
print(response)
assert response['status'] == 'OK'
def gen_input(directory):
"""
Config from input directory
"""
conf = ConfigParser()
conf.read(os.path.join(directory, 'index.cfg'))
for name, blob in [(s, dict(conf.items(s)))for s in conf.sections()]:
path = os.path.join(directory, blob.get('files', ''))
title = blob['title']
credit = blob['credit']
yield path, title, credit
# parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("--demo_id", "-id", required=True,
type=int, help="demo id required")
ap.add_argument("--blobs", "-b", required=True,
help="blobs directory that contains index.cfg file")
args = ap.parse_args()
demo_id = args.demo_id
blobs = args.blobs
# deleting all the blobs
demo_blobs = get_blobs(demo_id)
keys = demo_blobs.keys()
sets = demo_blobs['sets']
print("Deleting Blobs")
for set in sets:
for key in set['blobs'].keys():
blob_set = set['name']
pos_set = key
remove_blob_from_demo(demo_id, blob_set , pos_set)
#adding all the blobs
metadata = gen_input(blobs)
print("\n")
print(f"Adding blobs, metadata={metadata}\n")
for count, (path, title, credit) in enumerate(metadata, 1):
print(count, path, title, credit)
add_blob_to_demo(path, demo_id, title, credit)