<commit_before>
--[[
	USAGE:
	local cxjob = require('cxjob')

	local jobmgr = cxjob.new(
	      'username',
	      'to_sshkey_path',
	      'server_address')

	  or

	local jobmgr = cxjob.new(
	      {username = 'username',
	       sshkey   = 'to_sshkey_path',
	       server   = 'server_address'})
--]]

local cxjob = {}

local function getServerInfo(server)
    local focusSetting = {
        submitCmd = 'fjsub',
        submitIDRow = 4,
        delCmd = 'fjdel',
        statCmd = 'fjstat',
        statStateColumn = 5,
        statStateRow = 4,
        jobEndFunc = function (t)
        if (t[1][1] == 'Invalid' and t[1][2] == 'job' and t[1][3] == 'ID') then return true
        else return false end
        end,
    }
    local kSetting = {
        submitCmd = 'pjsub',
        submitIDRow = 6,
        delCmd = 'pjdel',
        statCmd = 'pjstat',
        statStateColumn = 6,
        statStateRow = 4,
        jobEndFunc = function(t)
            -- TODO: 'END'
            return false
        end,
    }
    local info = {
        ["k.aics.riken.jp"] = kSetting,
        ["ff01.j-focus.jp"] = focusSetting,
        ["ff02.j-focus.jp"] = focusSetting,
        ["ff01"] = focusSetting,
        ["ff02"] = focusSetting,
    }
    return info[server]
end


function cxjob.new(username_or_table, sshkey, server)
    local username
    if type(username_or_table) == 'table' then
        username = username_or_table.user
        sshkey   = username_or_table.sshkey
        server   = username_or_table.server
    else
        username = username_or_table -- this is username.
    end

    local inst = {
        user   = username,
        sshkey = sshkey,
        server = server
    }
    inst.jobinfo = getServerInfo(server)
    setmetatable(inst, {__index = cxjob})
    return inst;
end

function cxjob:uploadFile(localfile, remotefile)
    if (not remotefile) then remotefile = "./" end
    local scpcmd = 'scp -i '.. self.sshkey .. ' ' .. localfile .. ' ' .. self.user ..'@'.. self.server .. ':' .. remotefile;
    print('CMD>' .. scpcmd)
    local handle = io.popen(scpcmd)
    local result = handle:read("*a")
    print('OUT>' .. result)
    handle:close()
    return result
end

function cxjob:downloadFile(remotefile, localfile)
    if (not localfile) then localfile = "./" end
    local scpcmd = 'scp -i '.. self.sshkey .. ' ' .. self.user ..'@'.. self.server .. ':' .. remotefile ..  ' ' .. localfile;
    print('CMD>' .. scpcmd)
    local handle = io.popen(scpcmd)
    local result = handle:read("*a")
    print('OUT>' .. result)
    handle:close()
    return result
end


--- Remote(ssh) commands
local function scpCmd(user, server, key, fromfile, tofile)
    local scpcmd = 'scp -B -v -i '.. key .. ' ' .. fromfile .. ' ' .. tofile
    --local scpcmd = 'scp -B -v ' .. fromfile .. ' ' .. tofile
    
    print(scpcmd)
    local handle = io.popen(scpcmd)
    local result = handle:read("*a")
    print(result)
    handle:close()
    return result
end

local function sshCmd(user, server, key, cmd, disableErr)
	local nullDev = '/dev/null'
	if (getPlatform() == 'Windows') then
		disableErr = false
	end
    local sshcmd = 'ssh -i '.. key .. ' ' .. user ..'@'.. server .. ' "' .. cmd ..'"' .. (disableErr and (' 2>'..nullDev) or '')
	--print('CMD>' .. sshcmd)
    local handle = io.popen(sshcmd)
    local result = handle:read("*a")
    --print('OUT>' .. result)
    handle:close()
    return result
end

function cxjob:remoteExtractFile(filepath, varbose)
    local option = (verbose == true) and 'xvf' or 'xf'
    local cmd = 'tar ' .. option .. ' ' .. filepath
    print(cmd)
    return sshCmd(self.user, self.server, self.sshkey, cmd)
end

function cxjob:remoteCompressFile(srcfile, tarfile, verbose)
    local option = (verbose == true) and 'czvf' or 'czf'
    local cmd = 'tar ' .. option .. ' ' .. tarfile .. ' ' .. srcfile
    print(cmd)<commit_msg>update for windows<commit_after>
--[[
	USAGE:
	local cxjob = require('cxjob')

	local jobmgr = cxjob.new(
	      'username',
	      'to_sshkey_path',
	      'server_address')

	  or

	local jobmgr = cxjob.new(
	      {username = 'username',
	       sshkey   = 'to_sshkey_path',
	       server   = 'server_address'})
--]]

local cxjob = {}

local function getServerInfo(server)
    local focusSetting = {
        submitCmd = 'fjsub',
        submitIDRow = 4,
        delCmd = 'fjdel',
        statCmd = 'fjstat',
        statStateColumn = 5,
        statStateRow = 4,
        jobEndFunc = function (t)
        if (t[1][1] == 'Invalid' and t[1][2] == 'job' and t[1][3] == 'ID') then return true
        else return false end
        end,
    }
    local kSetting = {
        submitCmd = 'pjsub',
        submitIDRow = 6,
        delCmd = 'pjdel',
        statCmd = 'pjstat',
        statStateColumn = 6,
        statStateRow = 4,
        jobEndFunc = function(t)
            -- TODO: 'END'
            return false
        end,
    }
    local info = {
        ["k.aics.riken.jp"] = kSetting,
        ["ff01.j-focus.jp"] = focusSetting,
        ["ff02.j-focus.jp"] = focusSetting,
        ["ff01"] = focusSetting,
        ["ff02"] = focusSetting,
    }
    return info[server]
end


function cxjob.new(username_or_table, sshkey, server)
    local username
    if type(username_or_table) == 'table' then
        username = username_or_table.user
        sshkey   = username_or_table.sshkey
        server   = username_or_table.server
    else
        username = username_or_table -- this is username.
    end

    local inst = {
        user   = username,
        sshkey = sshkey,
        server = server
    }
    inst.jobinfo = getServerInfo(server)
    setmetatable(inst, {__index = cxjob})
    return inst;
end

function cxjob:uploadFile(localfile, remotefile)
    if (not remotefile) then remotefile = "./" end
    local scpcmd = 'scp -i '.. self.sshkey .. ' ' .. localfile .. ' ' .. self.user ..'@'.. self.server .. ':' .. remotefile;
    print('CMD>' .. scpcmd)
    local handle = io.popen(scpcmd)
    local result = handle:read("*a")
    print('OUT>' .. result)
    handle:close()
    return result
end

function cxjob:downloadFile(remotefile, localfile)
    if (not localfile) then localfile = "./" end
    local scpcmd = 'scp -i '.. self.sshkey .. ' ' .. self.user ..'@'.. self.server .. ':' .. remotefile ..  ' ' .. localfile;
    print('CMD>' .. scpcmd)
    local handle = io.popen(scpcmd)
    local result = handle:read("*a")
    print('OUT>' .. result)
    handle:close()
    return result
end


--- Remote(ssh) commands
local function scpCmd(user, server, key, fromfile, tofile)
    local scpcmd = 'scp  -i '.. key .. ' ' .. fromfile .. ' ' .. tofile
    print(scpcmd)
    local handle = io.popen(scpcmd)
    local result = handle:read("*a")
    print(result)
    handle:close()
    return result
end

local function sshCmd(user, server, key, cmd, disableErr)
	local nullDev = '/dev/null'
	if (getPlatform() == 'Windows') then
		disableErr = false
	end
    local sshcmd = 'ssh -i '.. key .. ' ' .. user ..'@'.. server .. ' "' .. cmd ..'"' .. (disableErr and (' 2>'..nullDev) or '')
	--print('CMD>' .. sshcmd)
    local handle = io.popen(sshcmd)
    local result = handle:read("*a")
    --print('OUT>' .. result)
    handle:close()
    return result
end

function cxjob:remoteExtractFile(filepath, verbose)
    local option = (verbose == true) and 'xvf' or 'xf'
    local cmd = 'tar ' .. option .. ' ' .. filepath
    print(cmd)
    return sshCmd(self.user, self.server, self.sshkey, cmd)
end

function cxjob:remoteCompressFile(srcfile, tarfile, verbose)
    local option = (verbose == true) and 'czvf' or 'czf'
    local cmd = 'tar ' .. option .. ' ' .. tarfile .. ' ' .. srcfile
    print(cmd)
{'fraction_non_alphanumeric': 0.15221354166666667, 'fraction_numerical': 0.00546875, 'mean_word_length': 2.231384097602019, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '709454', 'n_tokens_mistral': 2670, 'n_tokens_neox': 2581, 'n_words': 634}