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') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 14 | except Exception as exc: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 15 | bb.fatal("Connecting to PR service %s:%s failed: %s" % (host_params[0], host_params[1], str(exc))) |
| 16 | |
| 17 | return conn |
| 18 | |
| 19 | def prserv_dump_db(d): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 20 | if not d.getVar('PRSERV_HOST'): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 21 | bb.error("Not using network based PR service") |
| 22 | return None |
| 23 | |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 24 | conn = prserv_make_conn(d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 25 | if conn is None: |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 26 | bb.error("Making connection failed to remote PR service") |
| 27 | return None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 28 | |
| 29 | #dump db |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 30 | 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 Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 34 | d = conn.export(opt_version, opt_pkgarch, opt_checksum, opt_col) |
| 35 | conn.close() |
| 36 | return d |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 37 | |
| 38 | 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] | 39 | if not d.getVar('PRSERV_HOST'): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 40 | bb.error("Not using network based PR service") |
| 41 | return None |
| 42 | |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 43 | conn = prserv_make_conn(d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 44 | if conn is None: |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 45 | bb.error("Making connection failed to remote PR service") |
| 46 | return None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 61 | value = int(d.getVar(remain + '$' + version + '$' + pkgarch + '$' + checksum)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 62 | 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 Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 70 | conn.close() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 71 | return imported |
| 72 | |
| 73 | def prserv_export_tofile(d, metainfo, datainfo, lockdown, nomax=False): |
| 74 | import bb.utils |
| 75 | #initilize the output file |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 76 | bb.utils.mkdirhier(d.getVar('PRSERV_DUMPDIR')) |
| 77 | df = d.getVar('PRSERV_DUMPFILE') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 78 | #write data |
| 79 | lf = bb.utils.lockfile("%s.lock" % df) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 80 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 96 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 97 | if lockdown: |
| 98 | f.write("PRSERV_LOCKDOWN = \"1\"\n\n") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 99 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 100 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 114 | bb.utils.unlockfile(lf) |
| 115 | |
| 116 | def prserv_check_avail(d): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 117 | 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] | 118 | 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 Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 126 | conn = prserv_make_conn(d, True) |
| 127 | conn.close() |