blob: 2d3c9c7e50663e9324a839caa1941b09af2643c4 [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):
Andrew Geissler82c905d2020-04-13 13:39:40 -05006 # Otherwise this fails when called from recipes which e.g. inherit python3native (which sets _PYTHON_SYSCONFIGDATA_NAME) with:
7 # No module named '_sysconfigdata'
8 if '_PYTHON_SYSCONFIGDATA_NAME' in os.environ:
9 del os.environ['_PYTHON_SYSCONFIGDATA_NAME']
Patrick Williamsc124f4f2015-09-15 14:41:29 -050010 import prserv.serv
Brad Bishop6e60e8b2018-02-01 10:27:11 -050011 host_params = list([_f for _f in (d.getVar("PRSERV_HOST") or '').split(':') if _f])
Patrick Williamsc124f4f2015-09-15 14:41:29 -050012 try:
13 conn = None
14 conn = prserv.serv.PRServerConnection(host_params[0], int(host_params[1]))
15 if check:
16 if not conn.ping():
17 raise Exception('service not available')
18 d.setVar("__PRSERV_CONN",conn)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060019 except Exception as exc:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050020 bb.fatal("Connecting to PR service %s:%s failed: %s" % (host_params[0], host_params[1], str(exc)))
21
22 return conn
23
24def prserv_dump_db(d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050025 if not d.getVar('PRSERV_HOST'):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050026 bb.error("Not using network based PR service")
27 return None
28
Brad Bishop6e60e8b2018-02-01 10:27:11 -050029 conn = d.getVar("__PRSERV_CONN")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050030 if conn is None:
31 conn = prserv_make_conn(d)
32 if conn is None:
33 bb.error("Making connection failed to remote PR service")
34 return None
35
36 #dump db
Brad Bishop6e60e8b2018-02-01 10:27:11 -050037 opt_version = d.getVar('PRSERV_DUMPOPT_VERSION')
38 opt_pkgarch = d.getVar('PRSERV_DUMPOPT_PKGARCH')
39 opt_checksum = d.getVar('PRSERV_DUMPOPT_CHECKSUM')
40 opt_col = ("1" == d.getVar('PRSERV_DUMPOPT_COL'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050041 return conn.export(opt_version, opt_pkgarch, opt_checksum, opt_col)
42
43def prserv_import_db(d, filter_version=None, filter_pkgarch=None, filter_checksum=None):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050044 if not d.getVar('PRSERV_HOST'):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050045 bb.error("Not using network based PR service")
46 return None
47
Brad Bishop6e60e8b2018-02-01 10:27:11 -050048 conn = d.getVar("__PRSERV_CONN")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050049 if conn is None:
50 conn = prserv_make_conn(d)
51 if conn is None:
52 bb.error("Making connection failed to remote PR service")
53 return None
54 #get the entry values
55 imported = []
56 prefix = "PRAUTO$"
57 for v in d.keys():
58 if v.startswith(prefix):
59 (remain, sep, checksum) = v.rpartition('$')
60 (remain, sep, pkgarch) = remain.rpartition('$')
61 (remain, sep, version) = remain.rpartition('$')
62 if (remain + '$' != prefix) or \
63 (filter_version and filter_version != version) or \
64 (filter_pkgarch and filter_pkgarch != pkgarch) or \
65 (filter_checksum and filter_checksum != checksum):
66 continue
67 try:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050068 value = int(d.getVar(remain + '$' + version + '$' + pkgarch + '$' + checksum))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050069 except BaseException as exc:
70 bb.debug("Not valid value of %s:%s" % (v,str(exc)))
71 continue
72 ret = conn.importone(version,pkgarch,checksum,value)
73 if ret != value:
74 bb.error("importing(%s,%s,%s,%d) failed. DB may have larger value %d" % (version,pkgarch,checksum,value,ret))
75 else:
76 imported.append((version,pkgarch,checksum,value))
77 return imported
78
79def prserv_export_tofile(d, metainfo, datainfo, lockdown, nomax=False):
80 import bb.utils
81 #initilize the output file
Brad Bishop6e60e8b2018-02-01 10:27:11 -050082 bb.utils.mkdirhier(d.getVar('PRSERV_DUMPDIR'))
83 df = d.getVar('PRSERV_DUMPFILE')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050084 #write data
85 lf = bb.utils.lockfile("%s.lock" % df)
Andrew Geissler82c905d2020-04-13 13:39:40 -050086 with open(df, "a") as f:
87 if metainfo:
88 #dump column info
89 f.write("#PR_core_ver = \"%s\"\n\n" % metainfo['core_ver']);
90 f.write("#Table: %s\n" % metainfo['tbl_name'])
91 f.write("#Columns:\n")
92 f.write("#name \t type \t notn \t dflt \t pk\n")
93 f.write("#----------\t --------\t --------\t --------\t ----\n")
94 for i in range(len(metainfo['col_info'])):
95 f.write("#%10s\t %8s\t %8s\t %8s\t %4s\n" %
96 (metainfo['col_info'][i]['name'],
97 metainfo['col_info'][i]['type'],
98 metainfo['col_info'][i]['notnull'],
99 metainfo['col_info'][i]['dflt_value'],
100 metainfo['col_info'][i]['pk']))
101 f.write("\n")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500102
Andrew Geissler82c905d2020-04-13 13:39:40 -0500103 if lockdown:
104 f.write("PRSERV_LOCKDOWN = \"1\"\n\n")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500105
Andrew Geissler82c905d2020-04-13 13:39:40 -0500106 if datainfo:
107 idx = {}
108 for i in range(len(datainfo)):
109 pkgarch = datainfo[i]['pkgarch']
110 value = datainfo[i]['value']
111 if pkgarch not in idx:
112 idx[pkgarch] = i
113 elif value > datainfo[idx[pkgarch]]['value']:
114 idx[pkgarch] = i
115 f.write("PRAUTO$%s$%s$%s = \"%s\"\n" %
116 (str(datainfo[i]['version']), pkgarch, str(datainfo[i]['checksum']), str(value)))
117 if not nomax:
118 for i in idx:
119 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 -0500120 bb.utils.unlockfile(lf)
121
122def prserv_check_avail(d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500123 host_params = list([_f for _f in (d.getVar("PRSERV_HOST") or '').split(':') if _f])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500124 try:
125 if len(host_params) != 2:
126 raise TypeError
127 else:
128 int(host_params[1])
129 except TypeError:
130 bb.fatal('Undefined/incorrect PRSERV_HOST value. Format: "host:port"')
131 else:
132 prserv_make_conn(d, True)