Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | |
| 2 | def prserv_make_conn(d, check = False): |
| 3 | import prserv.serv |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 4 | 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] | 5 | try: |
| 6 | conn = None |
| 7 | conn = prserv.serv.PRServerConnection(host_params[0], int(host_params[1])) |
| 8 | if check: |
| 9 | if not conn.ping(): |
| 10 | raise Exception('service not available') |
| 11 | d.setVar("__PRSERV_CONN",conn) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 12 | except Exception as exc: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 13 | bb.fatal("Connecting to PR service %s:%s failed: %s" % (host_params[0], host_params[1], str(exc))) |
| 14 | |
| 15 | return conn |
| 16 | |
| 17 | def prserv_dump_db(d): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 18 | if not d.getVar('PRSERV_HOST'): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 19 | bb.error("Not using network based PR service") |
| 20 | return None |
| 21 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 22 | conn = d.getVar("__PRSERV_CONN") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 23 | if conn is None: |
| 24 | conn = prserv_make_conn(d) |
| 25 | if conn is None: |
| 26 | bb.error("Making connection failed to remote PR service") |
| 27 | return None |
| 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')) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 34 | return conn.export(opt_version, opt_pkgarch, opt_checksum, opt_col) |
| 35 | |
| 36 | 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] | 37 | if not d.getVar('PRSERV_HOST'): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 38 | bb.error("Not using network based PR service") |
| 39 | return None |
| 40 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 41 | conn = d.getVar("__PRSERV_CONN") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 42 | if conn is None: |
| 43 | conn = prserv_make_conn(d) |
| 44 | if conn is None: |
| 45 | bb.error("Making connection failed to remote PR service") |
| 46 | return None |
| 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)) |
| 70 | return imported |
| 71 | |
| 72 | def prserv_export_tofile(d, metainfo, datainfo, lockdown, nomax=False): |
| 73 | import bb.utils |
| 74 | #initilize the output file |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 75 | bb.utils.mkdirhier(d.getVar('PRSERV_DUMPDIR')) |
| 76 | df = d.getVar('PRSERV_DUMPFILE') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 77 | #write data |
| 78 | lf = bb.utils.lockfile("%s.lock" % df) |
| 79 | f = open(df, "a") |
| 80 | if metainfo: |
| 81 | #dump column info |
| 82 | f.write("#PR_core_ver = \"%s\"\n\n" % metainfo['core_ver']); |
| 83 | f.write("#Table: %s\n" % metainfo['tbl_name']) |
| 84 | f.write("#Columns:\n") |
| 85 | f.write("#name \t type \t notn \t dflt \t pk\n") |
| 86 | f.write("#----------\t --------\t --------\t --------\t ----\n") |
| 87 | for i in range(len(metainfo['col_info'])): |
| 88 | f.write("#%10s\t %8s\t %8s\t %8s\t %4s\n" % |
| 89 | (metainfo['col_info'][i]['name'], |
| 90 | metainfo['col_info'][i]['type'], |
| 91 | metainfo['col_info'][i]['notnull'], |
| 92 | metainfo['col_info'][i]['dflt_value'], |
| 93 | metainfo['col_info'][i]['pk'])) |
| 94 | f.write("\n") |
| 95 | |
| 96 | if lockdown: |
| 97 | f.write("PRSERV_LOCKDOWN = \"1\"\n\n") |
| 98 | |
| 99 | if datainfo: |
| 100 | idx = {} |
| 101 | for i in range(len(datainfo)): |
| 102 | pkgarch = datainfo[i]['pkgarch'] |
| 103 | value = datainfo[i]['value'] |
| 104 | if pkgarch not in idx: |
| 105 | idx[pkgarch] = i |
| 106 | elif value > datainfo[idx[pkgarch]]['value']: |
| 107 | idx[pkgarch] = i |
| 108 | f.write("PRAUTO$%s$%s$%s = \"%s\"\n" % |
| 109 | (str(datainfo[i]['version']), pkgarch, str(datainfo[i]['checksum']), str(value))) |
| 110 | if not nomax: |
| 111 | for i in idx: |
| 112 | f.write("PRAUTO_%s_%s = \"%s\"\n" % (str(datainfo[idx[i]]['version']),str(datainfo[idx[i]]['pkgarch']),str(datainfo[idx[i]]['value']))) |
| 113 | f.close() |
| 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: |
| 126 | prserv_make_conn(d, True) |