blob: b0cbcb1fbc41234e3cafa3c531c258dee0a13b7c [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001
2def prserv_make_conn(d, check = False):
3 import prserv.serv
4 host_params = filter(None, (d.getVar("PRSERV_HOST", True) or '').split(':'))
5 try:
6 conn = None
7 conn = prserv.serv.PRServerConnection(host_params[0], int(host_params[1]))
8 if check:
9 if not conn.ping():
10 raise Exception('service not available')
11 d.setVar("__PRSERV_CONN",conn)
12 except Exception, exc:
13 bb.fatal("Connecting to PR service %s:%s failed: %s" % (host_params[0], host_params[1], str(exc)))
14
15 return conn
16
17def prserv_dump_db(d):
18 if not d.getVar('PRSERV_HOST', True):
19 bb.error("Not using network based PR service")
20 return None
21
22 conn = d.getVar("__PRSERV_CONN", True)
23 if conn is None:
24 conn = prserv_make_conn(d)
25 if conn is None:
26 bb.error("Making connection failed to remote PR service")
27 return None
28
29 #dump db
30 opt_version = d.getVar('PRSERV_DUMPOPT_VERSION', True)
31 opt_pkgarch = d.getVar('PRSERV_DUMPOPT_PKGARCH', True)
32 opt_checksum = d.getVar('PRSERV_DUMPOPT_CHECKSUM', True)
33 opt_col = ("1" == d.getVar('PRSERV_DUMPOPT_COL', True))
34 return conn.export(opt_version, opt_pkgarch, opt_checksum, opt_col)
35
36def prserv_import_db(d, filter_version=None, filter_pkgarch=None, filter_checksum=None):
37 if not d.getVar('PRSERV_HOST', True):
38 bb.error("Not using network based PR service")
39 return None
40
41 conn = d.getVar("__PRSERV_CONN", True)
42 if conn is None:
43 conn = prserv_make_conn(d)
44 if conn is None:
45 bb.error("Making connection failed to remote PR service")
46 return None
47 #get the entry values
48 imported = []
49 prefix = "PRAUTO$"
50 for v in d.keys():
51 if v.startswith(prefix):
52 (remain, sep, checksum) = v.rpartition('$')
53 (remain, sep, pkgarch) = remain.rpartition('$')
54 (remain, sep, version) = remain.rpartition('$')
55 if (remain + '$' != prefix) or \
56 (filter_version and filter_version != version) or \
57 (filter_pkgarch and filter_pkgarch != pkgarch) or \
58 (filter_checksum and filter_checksum != checksum):
59 continue
60 try:
61 value = int(d.getVar(remain + '$' + version + '$' + pkgarch + '$' + checksum, True))
62 except BaseException as exc:
63 bb.debug("Not valid value of %s:%s" % (v,str(exc)))
64 continue
65 ret = conn.importone(version,pkgarch,checksum,value)
66 if ret != value:
67 bb.error("importing(%s,%s,%s,%d) failed. DB may have larger value %d" % (version,pkgarch,checksum,value,ret))
68 else:
69 imported.append((version,pkgarch,checksum,value))
70 return imported
71
72def prserv_export_tofile(d, metainfo, datainfo, lockdown, nomax=False):
73 import bb.utils
74 #initilize the output file
75 bb.utils.mkdirhier(d.getVar('PRSERV_DUMPDIR', True))
76 df = d.getVar('PRSERV_DUMPFILE', True)
77 #write data
78 lf = bb.utils.lockfile("%s.lock" % df)
79 f = open(df, "a")
80 if metainfo:
81 #dump column info
82 f.write("#PR_core_ver = \"%s\"\n\n" % metainfo['core_ver']);
83 f.write("#Table: %s\n" % metainfo['tbl_name'])
84 f.write("#Columns:\n")
85 f.write("#name \t type \t notn \t dflt \t pk\n")
86 f.write("#----------\t --------\t --------\t --------\t ----\n")
87 for i in range(len(metainfo['col_info'])):
88 f.write("#%10s\t %8s\t %8s\t %8s\t %4s\n" %
89 (metainfo['col_info'][i]['name'],
90 metainfo['col_info'][i]['type'],
91 metainfo['col_info'][i]['notnull'],
92 metainfo['col_info'][i]['dflt_value'],
93 metainfo['col_info'][i]['pk']))
94 f.write("\n")
95
96 if lockdown:
97 f.write("PRSERV_LOCKDOWN = \"1\"\n\n")
98
99 if datainfo:
100 idx = {}
101 for i in range(len(datainfo)):
102 pkgarch = datainfo[i]['pkgarch']
103 value = datainfo[i]['value']
104 if pkgarch not in idx:
105 idx[pkgarch] = i
106 elif value > datainfo[idx[pkgarch]]['value']:
107 idx[pkgarch] = i
108 f.write("PRAUTO$%s$%s$%s = \"%s\"\n" %
109 (str(datainfo[i]['version']), pkgarch, str(datainfo[i]['checksum']), str(value)))
110 if not nomax:
111 for i in idx:
112 f.write("PRAUTO_%s_%s = \"%s\"\n" % (str(datainfo[idx[i]]['version']),str(datainfo[idx[i]]['pkgarch']),str(datainfo[idx[i]]['value'])))
113 f.close()
114 bb.utils.unlockfile(lf)
115
116def prserv_check_avail(d):
117 host_params = filter(None, (d.getVar("PRSERV_HOST", True) or '').split(':'))
118 try:
119 if len(host_params) != 2:
120 raise TypeError
121 else:
122 int(host_params[1])
123 except TypeError:
124 bb.fatal('Undefined/incorrect PRSERV_HOST value. Format: "host:port"')
125 else:
126 prserv_make_conn(d, True)