blob: 2f2a0c128af5daf66b0c7e00dad905bbd25579f9 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
Patrick Williams92b42cb2022-09-03 06:53:57 -05002# Copyright OpenEmbedded Contributors
3#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: GPL-2.0-only
5#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05006
7def prserv_make_conn(d, check = False):
8 import prserv.serv
Brad Bishop6e60e8b2018-02-01 10:27:11 -05009 host_params = list([_f for _f in (d.getVar("PRSERV_HOST") or '').split(':') if _f])
Patrick Williamsc124f4f2015-09-15 14:41:29 -050010 try:
11 conn = None
Andrew Geisslerc926e172021-05-07 16:11:35 -050012 conn = prserv.serv.connect(host_params[0], int(host_params[1]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050013 if check:
14 if not conn.ping():
15 raise Exception('service not available')
Patrick Williamsc0f7c042017-02-23 20:41:17 -060016 except Exception as exc:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050017 bb.fatal("Connecting to PR service %s:%s failed: %s" % (host_params[0], host_params[1], str(exc)))
18
19 return conn
20
21def prserv_dump_db(d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050022 if not d.getVar('PRSERV_HOST'):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050023 bb.error("Not using network based PR service")
24 return None
25
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050026 conn = prserv_make_conn(d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027 if conn is None:
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050028 bb.error("Making connection failed to remote PR service")
29 return None
Patrick Williamsc124f4f2015-09-15 14:41:29 -050030
31 #dump db
Brad Bishop6e60e8b2018-02-01 10:27:11 -050032 opt_version = d.getVar('PRSERV_DUMPOPT_VERSION')
33 opt_pkgarch = d.getVar('PRSERV_DUMPOPT_PKGARCH')
34 opt_checksum = d.getVar('PRSERV_DUMPOPT_CHECKSUM')
35 opt_col = ("1" == d.getVar('PRSERV_DUMPOPT_COL'))
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050036 d = conn.export(opt_version, opt_pkgarch, opt_checksum, opt_col)
37 conn.close()
38 return d
Patrick Williamsc124f4f2015-09-15 14:41:29 -050039
40def prserv_import_db(d, filter_version=None, filter_pkgarch=None, filter_checksum=None):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050041 if not d.getVar('PRSERV_HOST'):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050042 bb.error("Not using network based PR service")
43 return None
44
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050045 conn = prserv_make_conn(d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050046 if conn is None:
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050047 bb.error("Making connection failed to remote PR service")
48 return None
Patrick Williamsc124f4f2015-09-15 14:41:29 -050049 #get the entry values
50 imported = []
51 prefix = "PRAUTO$"
52 for v in d.keys():
53 if v.startswith(prefix):
54 (remain, sep, checksum) = v.rpartition('$')
55 (remain, sep, pkgarch) = remain.rpartition('$')
56 (remain, sep, version) = remain.rpartition('$')
57 if (remain + '$' != prefix) or \
58 (filter_version and filter_version != version) or \
59 (filter_pkgarch and filter_pkgarch != pkgarch) or \
60 (filter_checksum and filter_checksum != checksum):
61 continue
62 try:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050063 value = int(d.getVar(remain + '$' + version + '$' + pkgarch + '$' + checksum))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050064 except BaseException as exc:
65 bb.debug("Not valid value of %s:%s" % (v,str(exc)))
66 continue
67 ret = conn.importone(version,pkgarch,checksum,value)
68 if ret != value:
69 bb.error("importing(%s,%s,%s,%d) failed. DB may have larger value %d" % (version,pkgarch,checksum,value,ret))
70 else:
71 imported.append((version,pkgarch,checksum,value))
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050072 conn.close()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050073 return imported
74
75def prserv_export_tofile(d, metainfo, datainfo, lockdown, nomax=False):
76 import bb.utils
77 #initilize the output file
Brad Bishop6e60e8b2018-02-01 10:27:11 -050078 bb.utils.mkdirhier(d.getVar('PRSERV_DUMPDIR'))
79 df = d.getVar('PRSERV_DUMPFILE')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050080 #write data
81 lf = bb.utils.lockfile("%s.lock" % df)
Andrew Geissler82c905d2020-04-13 13:39:40 -050082 with open(df, "a") as f:
83 if metainfo:
84 #dump column info
85 f.write("#PR_core_ver = \"%s\"\n\n" % metainfo['core_ver']);
86 f.write("#Table: %s\n" % metainfo['tbl_name'])
87 f.write("#Columns:\n")
88 f.write("#name \t type \t notn \t dflt \t pk\n")
89 f.write("#----------\t --------\t --------\t --------\t ----\n")
90 for i in range(len(metainfo['col_info'])):
91 f.write("#%10s\t %8s\t %8s\t %8s\t %4s\n" %
92 (metainfo['col_info'][i]['name'],
93 metainfo['col_info'][i]['type'],
94 metainfo['col_info'][i]['notnull'],
95 metainfo['col_info'][i]['dflt_value'],
96 metainfo['col_info'][i]['pk']))
97 f.write("\n")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050098
Andrew Geissler82c905d2020-04-13 13:39:40 -050099 if lockdown:
100 f.write("PRSERV_LOCKDOWN = \"1\"\n\n")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500101
Andrew Geissler82c905d2020-04-13 13:39:40 -0500102 if datainfo:
103 idx = {}
104 for i in range(len(datainfo)):
105 pkgarch = datainfo[i]['pkgarch']
106 value = datainfo[i]['value']
107 if pkgarch not in idx:
108 idx[pkgarch] = i
109 elif value > datainfo[idx[pkgarch]]['value']:
110 idx[pkgarch] = i
111 f.write("PRAUTO$%s$%s$%s = \"%s\"\n" %
112 (str(datainfo[i]['version']), pkgarch, str(datainfo[i]['checksum']), str(value)))
113 if not nomax:
114 for i in idx:
115 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 -0500116 bb.utils.unlockfile(lf)
117
118def prserv_check_avail(d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500119 host_params = list([_f for _f in (d.getVar("PRSERV_HOST") or '').split(':') if _f])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500120 try:
121 if len(host_params) != 2:
122 raise TypeError
123 else:
124 int(host_params[1])
125 except TypeError:
126 bb.fatal('Undefined/incorrect PRSERV_HOST value. Format: "host:port"')
127 else:
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500128 conn = prserv_make_conn(d, True)
129 conn.close()