blob: 339f7aebcaf4924d1e371ab6ef4c1ee4e68d9cc8 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
2# SPDX-License-Identifier: GPL-2.0-only
3#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05004
5def prserv_make_conn(d, check = False):
6 import prserv.serv
Brad Bishop6e60e8b2018-02-01 10:27:11 -05007 host_params = list([_f for _f in (d.getVar("PRSERV_HOST") or '').split(':') if _f])
Patrick Williamsc124f4f2015-09-15 14:41:29 -05008 try:
9 conn = None
Andrew Geisslerc926e172021-05-07 16:11:35 -050010 conn = prserv.serv.connect(host_params[0], int(host_params[1]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050011 if check:
12 if not conn.ping():
13 raise Exception('service not available')
Patrick Williamsc0f7c042017-02-23 20:41:17 -060014 except Exception as exc:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050015 bb.fatal("Connecting to PR service %s:%s failed: %s" % (host_params[0], host_params[1], str(exc)))
16
17 return conn
18
19def prserv_dump_db(d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050020 if not d.getVar('PRSERV_HOST'):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050021 bb.error("Not using network based PR service")
22 return None
23
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050024 conn = prserv_make_conn(d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050025 if conn is None:
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050026 bb.error("Making connection failed to remote PR service")
27 return None
Patrick Williamsc124f4f2015-09-15 14:41:29 -050028
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'))
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050034 d = conn.export(opt_version, opt_pkgarch, opt_checksum, opt_col)
35 conn.close()
36 return d
Patrick Williamsc124f4f2015-09-15 14:41:29 -050037
38def prserv_import_db(d, filter_version=None, filter_pkgarch=None, filter_checksum=None):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050039 if not d.getVar('PRSERV_HOST'):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050040 bb.error("Not using network based PR service")
41 return None
42
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050043 conn = prserv_make_conn(d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050044 if conn is None:
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050045 bb.error("Making connection failed to remote PR service")
46 return None
Patrick Williamsc124f4f2015-09-15 14:41:29 -050047 #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))
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050070 conn.close()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050071 return imported
72
73def prserv_export_tofile(d, metainfo, datainfo, lockdown, nomax=False):
74 import bb.utils
75 #initilize the output file
Brad Bishop6e60e8b2018-02-01 10:27:11 -050076 bb.utils.mkdirhier(d.getVar('PRSERV_DUMPDIR'))
77 df = d.getVar('PRSERV_DUMPFILE')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078 #write data
79 lf = bb.utils.lockfile("%s.lock" % df)
Andrew Geissler82c905d2020-04-13 13:39:40 -050080 with open(df, "a") as f:
81 if metainfo:
82 #dump column info
83 f.write("#PR_core_ver = \"%s\"\n\n" % metainfo['core_ver']);
84 f.write("#Table: %s\n" % metainfo['tbl_name'])
85 f.write("#Columns:\n")
86 f.write("#name \t type \t notn \t dflt \t pk\n")
87 f.write("#----------\t --------\t --------\t --------\t ----\n")
88 for i in range(len(metainfo['col_info'])):
89 f.write("#%10s\t %8s\t %8s\t %8s\t %4s\n" %
90 (metainfo['col_info'][i]['name'],
91 metainfo['col_info'][i]['type'],
92 metainfo['col_info'][i]['notnull'],
93 metainfo['col_info'][i]['dflt_value'],
94 metainfo['col_info'][i]['pk']))
95 f.write("\n")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050096
Andrew Geissler82c905d2020-04-13 13:39:40 -050097 if lockdown:
98 f.write("PRSERV_LOCKDOWN = \"1\"\n\n")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050099
Andrew Geissler82c905d2020-04-13 13:39:40 -0500100 if datainfo:
101 idx = {}
102 for i in range(len(datainfo)):
103 pkgarch = datainfo[i]['pkgarch']
104 value = datainfo[i]['value']
105 if pkgarch not in idx:
106 idx[pkgarch] = i
107 elif value > datainfo[idx[pkgarch]]['value']:
108 idx[pkgarch] = i
109 f.write("PRAUTO$%s$%s$%s = \"%s\"\n" %
110 (str(datainfo[i]['version']), pkgarch, str(datainfo[i]['checksum']), str(value)))
111 if not nomax:
112 for i in idx:
113 f.write("PRAUTO_%s_%s = \"%s\"\n" % (str(datainfo[idx[i]]['version']),str(datainfo[idx[i]]['pkgarch']),str(datainfo[idx[i]]['value'])))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500114 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:
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500126 conn = prserv_make_conn(d, True)
127 conn.close()