Skip to content
Extraits de code Groupes Projets
Valider 62c13d8c rédigé par LaplongeJunior's avatar LaplongeJunior
Parcourir les fichiers

Branch to add the file attached in issue comments

parent 85e6c4ff
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!12Resolve "Create a "tar and send" tool, and an untar tool to archive and send scripts to another machine and then unarchive them"
// Used by passing the path of the (txt) archive, then the path of all the files to add
// More information at https://usine.solution-libre.fr/qtg/grey-hack/-/issues/6
imports = {}
// The methods here are "good enough" to work in those cases
// For example, the ParamLength normally discards flag-style args
// But I don't want to give you all my tricks... yet, at least ;)
imports.utils = {}
imports.utils.ParamLength = function(params)
return params.len
end function
imports.utils.HasFlag = function(params, short, long)
return params[0] == "-h" or params[0] == "--help"
end function
imports.utils.GetParam = function(params, index)
return params[index]
end function
// Dynamically getting the name of the command is left as an exercise to the reader :)
imports.utils.GetProgName() = function()
return program_path.split("/")[-1]
end function
imports.utils.Print = function(reason)
print(imports.utils.GetProgName()+": "+reason)
end function
imports.file = {}
imports.file.NEW_LINE = char(10)
// Disclaimer : brand new method created for this script, may not handle special cases
imports.file.RelativePath = function(path)
if path[0] != "/" then path = get_shell.host_computer.current_path + "/" + path
return path
end function
// Should return [] for unavailable files (binary or no read perms)
imports.file.ReadAllLines = function(file)
if typeof(file) == "string" then
file = get_shell.host_computer.File(file)
end if
if not imports.file.FileAccess(file,"r") then return []
if file.is_binary then return []
return file.content.split(imports.file.NEW_LINE)
end function
imports.file.FileAccess = function(file, perm)
if not file then return false
if file.has_permission(perm) then return true
imports.utils.Print("Error: can't use ('"+perm+"') contents of '"+file.path+"'")
return false
end function
// To avoid bruning our eyes with escaping
QUOTE = """"
// Metadatas are encoded as 'unused' strings
// Extracted straight from my preprocessor as that's too specific for an include IMHO
GetLastChar = function(str)
if not str then return null
if str.len < 1 then return null
return str[str.len-1]
end function
// Returns the text in the 'unused' string or NULL if not a standalone string
StringInstruction = function(str)
if GetLastChar(str) != QUOTE then return null
if str[0] != QUOTE then return null
str = str[1:str.len-1]
if str.indexOf(QUOTE) != null then return null
str = str.trim()
if str[0] != "#" then return ""
return str[1:str.len]
end function
// To identify archived data
TAG = "#archive "
ArchiveName = function(str)
str = StringInstruction(str)
if str == null then return null
if str.len < TAG.len then return
if str[0:TAG.len-1] != TAG[1:] then return null
return str[TAG.len-1:]
end function
// command: archive
length = imports.utils.ParamLength(params)
if length == 0 or imports.utils.HasFlag(params, "h", "help") then exit("<b>Usage: "+imports.utils.GetProgName()+" [file to compress/decompress] [...file paths]</b>")
comp = get_shell.host_computer
mainPath = imports.file.RelativePath(imports.utils.GetParam(params, 0) + ".tar")
mainFile = comp.File(mainPath)
temp = mainPath.lastIndexOf("/")
path = mainPath[:temp]
name = mainPath[temp+1:]
if mainFile == null then
imports.utils.Print("Combining files")
// Creating the file
comp.touch(path, name)
mainFile = comp.File(mainPath)
// Flag to avoid a new line on the first line
nextLine = false
content = ""
// Read all files in order and add them
for i in range(1,length-1)
path = imports.file.RelativePath(imports.utils.GetParam(params, i))
// Get the name of the file
name = path[path.lastIndexOf("/")+1:]
lines = imports.file.ReadAllLines(path)
// Sanity checking the file is left as an exercise to the reader :)
if lines.len == 0 then
imports.utils.Print("Can't read file "+i+" ("+name+")")
continue
end if
if nextLine then
content = content + imports.file.NEW_LINE
else
nextLine = true
end if
// add "#archive filename.ext" then the file
content = content + QUOTE+TAG+name+QUOTE
for line in lines
content = content + imports.file.NEW_LINE + line
end for
end for
mainFile.set_content(content)
print("Archive is now available")
else
print("Unarchiving")
// Using the archive name as a new folder
if temp > 1 then name = name[:name.lastIndexOf(".")]
comp.create_folder(path, name)
path = path + "/" + name
lines = imports.file.ReadAllLines(mainPath)
if ArchiveName(lines[0]) == null then exit("Not a valid archive file")
currentName = null
content = ""
nextLine = false
for line in lines
temp = ArchiveName(line)
// New file
if temp != null then
comp.touch(path, currentName)
comp.File(path+"/"+currentName).set_content(content)
currentName = temp
content = ""
nextLine = false
continue
end if
if nextLine then
content = content + imports.file.NEW_LINE
else
nextLine = true
end if
content = content + line
end for
comp.touch(path, currentName)
comp.File(path+"/"+currentName).set_content(content)
print("Unarchiving completed")
end if
\ No newline at end of file
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter