Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
| 2 | # SPDX-License-Identifier: GPL-2.0-only |
| 3 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 4 | |
| 5 | def prserv_make_conn(d, check = False): |
| 6 | import prserv.serv |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 7 | host_params = list([_f for _f in (d.getVar("PRSERV_HOST") or '').split(':') if _f]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 8 | try: |
| 9 | conn = None |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 10 | conn = prserv.serv.connect(host_params[0], int(host_params[1])) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 11 | if check: |
| 12 | if not conn.ping(): |
| 13 | raise Exception('service not available') |
| 14 | d.setVar("__PRSERV_CONN",conn) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 15 | except Exception as exc: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 16 | bb.fatal("Connecting to PR service %s:%s failed: %s" % (host_params[0], host_params[1], str(exc))) |
| 17 | |
| 18 | return conn |
| 19 | |
| 20 | def prserv_dump_db(d): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 21 | if not d.getVar('PRSERV_HOST'): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 22 | bb.error("Not using network based PR service") |
| 23 | return None |
| 24 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 25 | conn = d.getVar("__PRSERV_CONN") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 26 | if conn is None: |
| 27 | conn = prserv_make_conn(d) |
| 28 | if conn is None: |
| 29 | bb.error("Making connection failed to remote PR service") |
| 30 | return None |
| 31 | |
| 32 | #dump db |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 33 | opt_version = d.getVar('PRSERV_DUMPOPT_VERSION') |
| 34 | opt_pkgarch = d.getVar('PRSERV_DUMPOPT_PKGARCH') |
| 35 | opt_checksum = d.getVar('PRSERV_DUMPOPT_CHECKSUM') |
| 36 | opt_col = ("1" == d.getVar('PRSERV_DUMPOPT_COL')) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 37 | return conn.export(opt_version, opt_pkgarch, opt_checksum, opt_col) |
| 38 | |
| 39 | def prserv_import_db(d, filter_version=None, filter_pkgarch=None, filter_checksum=None): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 40 | if not d.getVar('PRSERV_HOST'): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 41 | bb.error("Not using network based PR service") |
| 42 | return None |
| 43 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 44 | conn = d.getVar("__PRSERV_CONN") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 45 | if conn is None: |
| 46 | conn = prserv_make_conn(d) |
| 47 | if conn is None: |
| 48 | bb.error("Making connection failed to remote PR service") |
| 49 | return None |
| 50 | #get the entry values |
| 51 | imported = [] |
| 52 | prefix = "PRAUTO$" |
| 53 | for v in d.keys(): |
| 54 | if v.startswith(prefix): |
| 55 | (remain, sep, checksum) = v.rpartition('$') |
| 56 | (remain, sep, pkgarch) = remain.rpartition('$') |
| 57 | (remain, sep, version) = remain.rpartition('$') |
| 58 | if (remain + '$' != prefix) or \ |
| 59 | (filter_version and filter_version != version) or \ |
| 60 | (filter_pkgarch and filter_pkgarch != pkgarch) or \ |
| 61 | (filter_checksum and filter_checksum != checksum): |
| 62 | continue |
| 63 | try: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 64 | value = int(d.getVar(remain + '$' + version + '$' + pkgarch + '$' + checksum)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 65 | except BaseException as exc: |
| 66 | bb.debug("Not valid value of %s:%s" % (v,str(exc))) |
| 67 | continue |
| 68 | ret = conn.importone(version,pkgarch,checksum,value) |
| 69 | if ret != value: |
| 70 | bb.error("importing(%s,%s,%s,%d) failed. DB may have larger value %d" % (version,pkgarch,checksum,value,ret)) |
| 71 | else: |
| 72 | imported.append((version,pkgarch,checksum,value)) |
| 73 | return imported |
| 74 | |
| 75 | def prserv_export_tofile(d, metainfo, datainfo, lockdown, nomax=False): |
| 76 | import bb.utils |
| 77 | #initilize the output file |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 78 | bb.utils.mkdirhier(d.getVar('PRSERV_DUMPDIR')) |
| 79 | df = d.getVar('PRSERV_DUMPFILE') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 80 | #write data |
| 81 | lf = bb.utils.lockfile("%s.lock" % df) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 82 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 98 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 99 | if lockdown: |
| 100 | f.write("PRSERV_LOCKDOWN = \"1\"\n\n") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 101 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 102 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 116 | bb.utils.unlockfile(lf) |
| 117 | |
| 118 | def prserv_check_avail(d): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 119 | host_params = list([_f for _f in (d.getVar("PRSERV_HOST") or '').split(':') if _f]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 120 | 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: |
| 128 | prserv_make_conn(d, True) |