Source code for GLXShell.libs.core

try:
    import uos as os
except ImportError:
    import os
try:
    import ure as re
except ImportError:
    import re
import sys

from GLXShell.libs.utils import file_exist


[docs]def basename(string=None, suffix=None): step = 1 # 1. If string is a null string, it is unspecified whether the resulting string is '.' or a null string. In # either case, skip steps 2 through 6. if step == 1: if string is None or string == "": string = "." step = 6 else: step = 2 # 2. If string is "//", it is implementation-defined whether steps 3 to 6 are skipped or processed. # here we processed if step == 2: if string == "//": step = "End" else: step = 3 # 3. If string consists entirely of <slash> characters, string shall be set to a single <slash> character. In # this case, skip steps 4 to 6. if step == 3: if string == len(string) * string[0]: string = "/" step = "End" else: step = 4 # 4. If there are any trailing <slash> characters in string, they shall be removed. if step == 4: string = re.sub(r"/+", "/", string) step = 5 # 5. If there are any <slash> characters remaining in string, the prefix of string up to and including the last # <slash> character in string shall be removed. if step == 5: if string.endswith("//"): string = string[:-2] elif string.endswith("/"): string = string[:-1] if "/" in string: string = string.split("/")[-1] step = 6 # 6. If the suffix operand is present, is not identical to the characters remaining in string, and is identical # to a suffix of the characters remaining in string, the suffix suffix shall be removed from string. Otherwise, # string is not modified by this step. It shall not be considered an error if suffix is not found in string. if step == 6: if suffix and string != suffix and not suffix.endswith('/'): string = string.replace(suffix, "") return string
[docs]def cat(f): head(f, 1 << 30)
[docs]def cd(directory, shell): if directory is None or directory == "": try: if shell and hasattr(shell, "environ") and "HOME" in shell.environ: directory = shell.environ["HOME"] else: directory = "." except Exception as error: sys.stderr.write("cd: %s\n" % error) try: os.chdir(directory) if shell and hasattr(shell, "environ"): shell.environ["PWD"] = os.getcwd() else: os.environ["PWD"] = os.getcwd() except Exception as error: sys.stderr.write("cd: %s\n" % error)
[docs]def clear(): sys.stdout.write("\x1b[2J\x1b[H")
[docs]def cp(source_file, target_file, interactive=False): if interactive: if file_exist(target_file): if ( input( "do you want to overwrite %s file ? (Y/n)" % target_file ) .upper() .startswith("N") ): return try: with open(source_file, "r") as source_file: with open(target_file, "w") as target_file: target_file.write(source_file.read()) except Exception as error: sys.stdout.write("cp: %s\n" % error)
[docs]def dirname(text): step = 1 if text is None: text = "" if step == 1: if text == "//": step = 5 else: step = 2 if step == 2: if text != "" and text == len(text) * text[0]: text = "/" else: step = 3 if step == 3: text = re.sub(r"/+", "/", text) # try: # text = re.sub(r"/+", "/", text) # except Exception: # pass step = 4 if step == 4: if "/" not in text: text = "." else: step = 5 if step == 5: text = re.sub(r"\\+", "", text) # try: # text = re.sub(r"\\+", "", text) # except Exception: # pass step = 6 if step == 6: if text == "//": step = 8 else: step = 7 if step == 7: text = re.sub(r"/+", "/", text) # try: # text = re.sub(r"/+", "/", text) # except Exception: # pass step = 8 if step == 8: if text == "": text = "/" r = text.rsplit("/", 1) if len(r) == 1: head = "", text head = r[0] # .rstrip("/") if not head: head = "/" return head
[docs]def env(shell, name, utility, argument): if shell and hasattr(shell, "environ"): if name: shell.stdout.write("%s\n" % name) else: for name, value in shell.environ.items(): shell.stdout.write("%s=%s\n" % (name, value))
[docs]def uname( all=False, sysname=False, nodename=False, release=False, version=False, machine=False, ): info = os.uname() def gen_lines(): if all or nodename: yield info.nodename if all or release: yield info.release if all or version: yield info.version if all or machine: yield info.machine lines = list(gen_lines()) if all or sysname or (not lines): lines.insert(0, info.sysname) return " ".join(lines)
[docs]def mkdir(directories=None, parents=False, mode="755"): mode = int(mode, 8) if hasattr(os, "sep"): sep = os.sep else: sep = "/" for directory in directories: if parents: if str(directory).startswith(sep): directory_to_create = sep else: directory_to_create = "" for sub_directory in directory.split(sep): if directory_to_create == "/": directory_to_create = "%s%s" % (directory_to_create, sub_directory) elif directory_to_create != "" and sub_directory != "": directory_to_create = "%s%s%s" % (directory_to_create, os.sep, sub_directory) else: if sub_directory == "": continue elif sub_directory == '.': directory_to_create = "." continue elif sub_directory == '..': directory_to_create = ".." continue else: directory_to_create = sub_directory try: os.mkdir(path=directory_to_create, mode=mode) except TypeError: os.mkdir(directory_to_create) except OSError as error: sys.stderr.write("mkdir: %s\n" % error) else: try: os.stat(directory) sys.stderr.write("mkdir: cannot create directory '%s': File exists\n" % directory) except OSError: try: try: os.mkdir(path=directory, mode=mode) except OSError as error: sys.stderr.write("mkdir: %s\n" % error) except TypeError: try: os.mkdir(directory) except OSError as error: sys.stderr.write("mkdir: %s\n" % error)
[docs]def mv(source_file=None, target_file=None, target_dir=None, force=None, interactive=None): if source_file is None: source_file = "None" if target_file is None: target_file = "None" if target_dir is None: target_dir = "None" if force is None: force = "None" if interactive is None: interactive = "None" sys.stdout.write("source_file = %s\n" % source_file) sys.stdout.write("target_file = %s\n" % target_file) sys.stdout.write("target_dir = %s\n" % target_dir) sys.stdout.write("force = %s\n" % force) sys.stdout.write("interactive = %s\n" % interactive) try: if os.access(source_file, os.F_OK): os.rename(source_file, target_file) except Exception as error: sys.stderr.write("mv: %s\n" % error) # real_dst = target_file # if os.path.isdir(target_file): # if _samefile(source_file, target_file): # # We might be on a case insensitive filesystem, # # perform the rename anyway. # os.rename(source_file, target_file) # return # # real_dst = os.path.join(target_file, _basename(source_file)) # if os.path.exists(real_dst): # raise FileExistsError("Destination path '%s' already exists" % real_dst) # try: # os.rename(source_file, real_dst) # except OSError: # if os.path.islink(source_file): # linkto = os.readlink(source_file) # os.symlink(linkto, real_dst) # os.unlink(source_file) # elif os.path.isdir(source_file): # if _destinsrc(source_file, target_file): # raise RecursionError("Cannot move a directory '%s' into itself" # " '%s'." % (source_file, target_file)) # copytree(source_file, real_dst, copy_function=copy_function, # symlinks=True) # rmtree(source_file) # else: # copy_function(source_file, real_dst) # os.unlink(source_file) # return pass
[docs]def rmdir(directories=None, parents=False): if hasattr(os, "sep"): sep = os.sep else: sep = "/" for directory in directories: if parents: if str(directory).startswith(sep): directory_to_remove = sep else: directory_to_remove = "" for sub_directory in directory.split(sep): if directory_to_remove == "/": directory_to_remove = "%s%s" % (directory_to_remove, sub_directory) elif directory_to_remove != "" and sub_directory != "": directory_to_remove = "%s%s%s" % (directory_to_remove, os.sep, sub_directory) else: if sub_directory == "": continue elif sub_directory == '.': directory_to_remove = "." continue elif sub_directory == '..': directory_to_remove = ".." continue else: directory_to_remove = sub_directory try: os.rmdir(path=directory_to_remove) except OSError as error: sys.stderr.write("rmdir: %s\n" % error) else: try: os.rmdir(directory) except OSError as error: sys.stderr.write("rmdir: %s\n" % error)