diff options
| author | ruki <[email protected]> | 2022-07-30 23:12:39 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-07-30 23:12:39 +0800 |
| commit | 5aec161f35f6905af15764a6da985359d25175ad (patch) | |
| tree | 5cccd627ba6ecec08e81123be66023cd631db825 /xmake/modules | |
| parent | c0f7a2a0af3ded2f704e07225890b9d2e3b3e2ea (diff) | |
add xmake service --pull
Diffstat (limited to 'xmake/modules')
4 files changed, 118 insertions, 0 deletions
diff --git a/xmake/modules/private/service/pull_files.lua b/xmake/modules/private/service/pull_files.lua new file mode 100644 index 000000000..81edc67e1 --- /dev/null +++ b/xmake/modules/private/service/pull_files.lua @@ -0,0 +1,29 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file pull_files.lua +-- + +-- imports +import("core.base.option") +import("private.service.remote_build.client", {alias = "remote_build_client"}) + +function main(filepattern, outputdir) + remote_build_client():pull(filepattern, outputdir) +end + + diff --git a/xmake/modules/private/service/remote_build/client.lua b/xmake/modules/private/service/remote_build/client.lua index 95ad5110e..53fc6b1e3 100644 --- a/xmake/modules/private/service/remote_build/client.lua +++ b/xmake/modules/private/service/remote_build/client.lua @@ -235,6 +235,57 @@ function remote_build_client:sync() end end +-- pull server files +function remote_build_client:pull(filepattern, outputdir) + assert(self:is_connected(), "%s: has been not connected!", self) + local addr = self:addr() + local port = self:port() + local sock = assert(socket.connect(addr, port, {timeout = self:connect_timeout()}), "%s: server unreachable!", self) + local session_id = self:session_id() + local errors + local ok = false + if not filepattern:find("*", 1, true) and os.isdir(filepattern) then + filepattern = path.join(filepattern, "**") + end + print("%s: pull %s in %s:%d ..", self, filepattern, addr, port) + local stream = socket_stream(sock, {send_timeout = self:send_timeout(), recv_timeout = self:recv_timeout()}) + if stream:send_msg(message.new_pull(session_id, filepattern, {token = self:token()})) and stream:flush() then + local fileitems + local msg = stream:recv_msg({timeout = -1}) + if msg then + dprint(msg:body()) + if msg:success() then + fileitems = msg:body().fileitems + else + errors = msg:errors() + end + end + if fileitems then + for _, fileitem in ipairs(fileitems) do + print("recving %s ..", fileitem) + if not stream:recv_file(path.normalize(path.join(outputdir, fileitem))) then + errors = string.format("recv %s failed", fileitem) + break + end + end + msg = stream:recv_msg({timeout = -1}) + if msg then + dprint(msg:body()) + if msg:success() then + ok = true + else + errors = msg:errors() + end + end + end + end + if ok then + print("%s: pull files to %s!", self, outputdir) + else + print("%s: pull files failed in %s:%d, %s", self, addr, port, errors or "unknown") + end +end + -- clean server files function remote_build_client:clean() assert(self:is_connected(), "%s: has been not connected!", self) diff --git a/xmake/modules/private/service/remote_build/server.lua b/xmake/modules/private/service/remote_build/server.lua index 52015c7f2..c890e1158 100644 --- a/xmake/modules/private/service/remote_build/server.lua +++ b/xmake/modules/private/service/remote_build/server.lua @@ -93,6 +93,8 @@ function remote_build_server:_on_handle(stream, msg) session:diff(respmsg) elseif msg:is_sync() then session:sync(respmsg) + elseif msg:is_pull() then + session:pull(respmsg) elseif msg:is_clean() then session:clean() elseif msg:is_runcmd() then diff --git a/xmake/modules/private/service/remote_build/server_session.lua b/xmake/modules/private/service/remote_build/server_session.lua index b56908456..7031c5095 100644 --- a/xmake/modules/private/service/remote_build/server_session.lua +++ b/xmake/modules/private/service/remote_build/server_session.lua @@ -181,6 +181,42 @@ function server_session:sync(respmsg) vprint("%s: sync files ok", self) end +-- pull file +function server_session:pull(respmsg) + local body = respmsg:body() + local stream = self:stream() + local filepattern = body.filename + vprint("pull %s ..", filepattern) + + -- get files + local filepaths = os.files(path.join(self:sourcedir(), filepattern)) + local fileitems = {} + for _, filepath in ipairs(filepaths) do + local fileitem = path.relative(filepath, self:sourcedir()) + if is_host("windows") then + fileitem = fileitem:gsub("\\", "/") + end + if fileitem:startswith("./") then + fileitem = fileitem:sub(3) + end + table.insert(fileitems, fileitem) + end + vprint(fileitems) + + -- send files + body.fileitems = fileitems + respmsg:status_set(true) + if stream:send_msg(respmsg) then + for _, fileitem in ipairs(fileitems) do + local filepath = path.join(self:sourcedir(), fileitem) + vprint("sending %s ..", filepath) + if not stream:send_file(filepath, {compress = os.filesize(filepath) > 4096}) then + raise("send %s failed!", filepath) + end + end + end +end + -- clean files function server_session:clean() vprint("%s: clean files in %s ..", self, self:workdir()) |
