Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # SPDX-License-Identifier: GPL-2.0-only |
| 5 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 6 | |
| 7 | def prserv_make_conn(d, check = False): |
| 8 | import prserv.serv |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 9 | 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] | 10 | try: |
| 11 | conn = None |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 12 | conn = prserv.serv.connect(host_params[0], int(host_params[1])) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 13 | if check: |
| 14 | if not conn.ping(): |
| 15 | raise Exception('service not available') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 16 | except Exception as exc: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 17 | bb.fatal("Connecting to PR service %s:%s failed: %s" % (host_params[0], host_params[1], str(exc))) |
| 18 | |
| 19 | return conn |
| 20 | |
| 21 | def prserv_dump_db(d): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 22 | if not d.getVar('PRSERV_HOST'): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 23 | bb.error("Not using network based PR service") |
| 24 | return None |
| 25 | |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 26 | conn = prserv_make_conn(d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 27 | if conn is None: |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 28 | bb.error("Making connection failed to remote PR service") |
| 29 | return None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 30 | |
| 31 | #dump db |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 32 | 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 Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 36 | d = conn.export(opt_version, opt_pkgarch, opt_checksum, opt_col) |
| 37 | conn.close() |
| 38 | return d |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 39 | |
| 40 | 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] | 41 | if not d.getVar('PRSERV_HOST'): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 42 | bb.error("Not using network based PR service") |
| 43 | return None |
| 44 | |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 45 | conn = prserv_make_conn(d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 46 | if conn is None: |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 47 | bb.error("Making connection failed to remote PR service") |
| 48 | return None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 49 | #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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 63 | value = int(d.getVar(remain + '$' + version + '$' + pkgarch + '$' + checksum)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 64 | 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 Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 72 | conn.close() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 |
Patrick Williams | 169d7bc | 2024-01-05 11:33:25 -0600 | [diff] [blame] | 81 | with open(df, "a") as f, bb.utils.fileslocked(["%s.lock" % df]) as locks: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 82 | if metainfo: |
| 83 | #dump column info |
| 84 | f.write("#PR_core_ver = \"%s\"\n\n" % metainfo['core_ver']); |
| 85 | f.write("#Table: %s\n" % metainfo['tbl_name']) |
| 86 | f.write("#Columns:\n") |
| 87 | f.write("#name \t type \t notn \t dflt \t pk\n") |
| 88 | f.write("#----------\t --------\t --------\t --------\t ----\n") |
| 89 | for i in range(len(metainfo['col_info'])): |
| 90 | f.write("#%10s\t %8s\t %8s\t %8s\t %4s\n" % |
| 91 | (metainfo['col_info'][i]['name'], |
| 92 | metainfo['col_info'][i]['type'], |
| 93 | metainfo['col_info'][i]['notnull'], |
| 94 | metainfo['col_info'][i]['dflt_value'], |
| 95 | metainfo['col_info'][i]['pk'])) |
| 96 | f.write("\n") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 97 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 98 | if lockdown: |
| 99 | f.write("PRSERV_LOCKDOWN = \"1\"\n\n") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 100 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 101 | if datainfo: |
| 102 | idx = {} |
| 103 | for i in range(len(datainfo)): |
| 104 | pkgarch = datainfo[i]['pkgarch'] |
| 105 | value = datainfo[i]['value'] |
| 106 | if pkgarch not in idx: |
| 107 | idx[pkgarch] = i |
| 108 | elif value > datainfo[idx[pkgarch]]['value']: |
| 109 | idx[pkgarch] = i |
| 110 | f.write("PRAUTO$%s$%s$%s = \"%s\"\n" % |
| 111 | (str(datainfo[i]['version']), pkgarch, str(datainfo[i]['checksum']), str(value))) |
| 112 | if not nomax: |
| 113 | for i in idx: |
| 114 | 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] | 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() |