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): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 6 | # 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 10 | import prserv.serv |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 11 | 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] | 12 | 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 19 | except Exception as exc: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 20 | bb.fatal("Connecting to PR service %s:%s failed: %s" % (host_params[0], host_params[1], str(exc))) |
| 21 | |
| 22 | return conn |
| 23 | |
| 24 | def prserv_dump_db(d): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 25 | if not d.getVar('PRSERV_HOST'): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 26 | bb.error("Not using network based PR service") |
| 27 | return None |
| 28 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 29 | conn = d.getVar("__PRSERV_CONN") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 30 | 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 37 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 41 | return conn.export(opt_version, opt_pkgarch, opt_checksum, opt_col) |
| 42 | |
| 43 | 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] | 44 | if not d.getVar('PRSERV_HOST'): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 45 | bb.error("Not using network based PR service") |
| 46 | return None |
| 47 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 48 | conn = d.getVar("__PRSERV_CONN") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 49 | 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 68 | value = int(d.getVar(remain + '$' + version + '$' + pkgarch + '$' + checksum)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 69 | 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 | |
| 79 | def prserv_export_tofile(d, metainfo, datainfo, lockdown, nomax=False): |
| 80 | import bb.utils |
| 81 | #initilize the output file |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 82 | bb.utils.mkdirhier(d.getVar('PRSERV_DUMPDIR')) |
| 83 | df = d.getVar('PRSERV_DUMPFILE') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 84 | #write data |
| 85 | lf = bb.utils.lockfile("%s.lock" % df) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 86 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 102 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 103 | if lockdown: |
| 104 | f.write("PRSERV_LOCKDOWN = \"1\"\n\n") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 105 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 106 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 120 | bb.utils.unlockfile(lf) |
| 121 | |
| 122 | def prserv_check_avail(d): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 123 | 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] | 124 | 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) |