from string import * def command_generic( head, args ): def arg_process(arg): if (arg.get("name", None) is None): if (arg.get("value", None) is None): template = None else: template = "{{value}}" else: if (arg.get("value", None) is None): template = "--{{name}}" else: template = "--{{name}}={{value}}" return string_coin( template, { "name": (arg.get("name", None) or ""), "value": string_wrap( (arg.get("value", None) or ""), "'", "'", { "mode": arg.get("quote", False), } ), } ) return " ".join( [head] + list( map( arg_process, args ) ) ) def command_directory_create( path ): return command_generic( "mkdir", [ {"value": path}, {"name": "parents"}, ] ) def command_remove( path ): return command_generic( "rm", [ {"value": path}, {"name": "recursive"}, {"name": "force"}, ] ) def command_copy( path_from, path_to, options = None ): options = ( { "update": False, "recursive": True, } | (options or {}) ) return command_generic( "cp", [ {"value": path_from}, {"value": path_to}, ] + ( [{"name": "update"}] if options["update"] else [] ) + ( [{"name": "recursive"}] if options["recursive"] else [] ) ) def command_rsync( path_from, path_to, options = None ): options = ( { "verbose": False, "excludes": [], } | (options or {}) ) return command_generic( "rsync", ( [ {"name": "update"}, {"name": "recursive"}, ] + ( [{"name": "verbose"}] if options["verbose"] else [] ) + list( map( lambda exclude: {"name": "exclude", "value": exclude.replace(".", "\\."), "quote": True}, options["excludes"] ) ) + [ {"value": string_coin("{{path}}/.", {"path": path_from})}, {"value": string_coin("{{path}}", {"path": path_to})}, ] ) )