blob: 32dfc15e884021e0f21828089312830d757ffe52 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001
2def prserv_make_conn(d, check = False):
3 import prserv.serv
Brad Bishop6e60e8b2018-02-01 10:27:11 -05004 host_params = list([_f for _f in (d.getVar("PRSERV_HOST") or '').split(':') if _f])
Patrick Williamsc124f4f2015-09-15 14:41:29 -05005 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)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060012 except Exception as exc:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050013 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):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050018 if not d.getVar('PRSERV_HOST'):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050019 bb.error("Not using network based PR service")
20 return None
21
Brad Bishop6e60e8b2018-02-01 10:27:11 -050022 conn = d.getVar("__PRSERV_CONN")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050023 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
Brad Bishop6e60e8b2018-02-01 10:27:11 -050030 opt_version = d.getVar('PRSERV_DUMPOPT_VERSION')
31 opt_pkgarch = d.getVar('PRSERV_DUMPOPT_PKGARCH')
32 opt_checksum = d.getVar('PRSERV_DUMPOPT_CHECKSUM')
33 opt_col = ("1" == d.getVar('PRSERV_DUMPOPT_COL'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050034 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):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050037 if not d.getVar('PRSERV_HOST'):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050038 bb.error("Not using network based PR service")
39 return None
40
Brad Bishop6e60e8b2018-02-01 10:27:11 -050041 conn = d.getVar("__PRSERV_CONN")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050042 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:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050061 value = int(d.getVar(remain + '$' + version + '$' + pkgarch + '$' + checksum))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050062 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
Brad Bishop6e60e8b2018-02-01 10:27:11 -050075 bb.utils.mkdirhier(d.getVar('PRSERV_DUMPDIR'))
76 df = d.getVar('PRSERV_DUMPFILE')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050077 #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):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500117 host_params = list([_f for _f in (d.getVar("PRSERV_HOST") or '').split(':') if _f])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500118 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)