def makedirs(target): try: if not os.access(target, os.F_OK): os.makedirs(target) except OSError as err: raise exceptions.BuiltinError("[makedirs] an error occured: %s" % target)
def echo(content, target): mode = "a" if not os.path.isfile(target): mode = "w" try: with open(target, mode) as _file: _file.write('%s\n' % content) except IOError as err: raise exceptions.BuiltinError( "[echo] given content was not written to %s" % target)
def move(source, target): src = glob.glob(source) if len(src) == 0: raise exceptions.BuiltinError("[move] %s is empty" % source) if len(target.split("/")) > 1 and not os.path.isdir( os.path.dirname(target)): makedirs(os.path.dirname(target)) for path in src: if is_file(path) or is_link(path) or is_dir(path): try: shutil.move(path, target) except OSError as err: raise exceptions.BuiltinError( "[move] an error occured while moving: %s -> %s" % (source, target)) else: raise exceptions.BuiltinError("[move] file %s doesn\'t exists." % path)
def copytree(source, target, sym=True): if is_dir(source): if os.path.exists(target): if is_dir(target): copytree( source, os.path.join(target, os.path.basename(source.strip('/')))) return else: copytree(source, os.path.join(target, os.path.basename(source))) return try: shutil.copytree(source, target, sym) except OSError as err: raise exceptions.BuiltinError( "[copytree] an error occured while copying: %s -> %s" % (source, target)) else: raise exceptions.BuiltinError("[copytree] %s does not exists" % source, stage=1)
def remove_file(pattern): src = glob.glob(pattern) if len(src) == 0: out.error("[remove_file] no file matched pattern: %s." % pattern) return False for path in src: if is_link(path): try: os.unlink(path) except OSError as err: raise exceptions.BuiltinError( "[remove_file] an error occured: %s" % path) elif is_file(path): try: os.remove(path) except OSError as err: raise exceptions.BuiltinError( "[remove_file] an error occured: %s" % path) elif not is_dir(path): out.error("[remove_file] file %s doesn\'t exists." % path) return False
def copy(source, target, sym=True): src = glob.glob(source) if len(src) == 0: raise exceptions.BuiltinError("[copy] no file matched pattern %s." % source) if len(target.split("/")) > 1 and not os.path.exists( os.path.dirname(target)): makedirs(os.path.dirname(target)) for path in src: if is_file(path) and not is_link(path): try: shutil.copy2(path, target) except IOError as err: raise exceptions.BuiltinError( "[copy] an error occured while copying: %s -> %s" % (source, target)) elif is_link(path) and sym: if is_dir(target): os.symlink(os.readlink(path), os.path.join(target, os.path.basename(path))) else: if is_file(target): os.remove(target) os.symlink(os.readlink(path), target) elif is_link(path) and not sym: if is_dir(path): copytree(path, target) else: shutil.copy2(path, target) elif is_dir(path): copytree(path, target, sym) else: raise exceptions.BuiltinError('[copy] file %s does not exist.' % filePath)
def install_executable(sources, target): if not os.path.isdir(os.path.dirname(target)): makedirs(os.path.dirname(target)) for source in sources: srcs = glob.glob(source) if len(srcs) == 0: raise exceptions.BuiltinError( "[install_executable] file not found: %s" % source) for src in srcs: if not system('install -m0755 -o root -g root %s %s' % (src, target)): out.error("[install_executable] %s could not installed to %s" % (src, target)) return False
def insinto(source, target, install_dir=None, target_file='', sym=True): if install_dir is not None: target = os.path.join(install_dir, target) makedirs(target) if not target_file: src = glob.glob(source) if len(src) == 0: raise exceptions.BuiltinError( "[instinto] no file matched pattern %s." % source) for path in src: if os.access(path, os.F_OK): copy(path, os.path.join(target, os.path.basename(path)), sym) else: copy(source, os.path.join(target, target_file), sym)
def remove_dir(source_dir): if is_link(source_dir): os.unlink(source_dir) return if is_dir(source_dir): try: # rmtree gets string shutil.rmtree(str(source_dir)) except OSError as err: raise exceptions.BuiltinError( "[remove_dir] an error occured while removing: %s" % source_dir) elif is_file(source_dir): pass else: out.error("[remove_dir] directory %s doesn\'t exists." % source_dir) return False
def rename(source, target): try: os.rename(source, target) except OSError as err: raise exceptions.BuiltinError( "an error occured while renaming: %s -> %s" % (source, target))
def make_symlink(source, target): try: os.symlink(source, target) except OSError as err: raise exceptions.BuiltinError( "[make_symlink] symlink not created: %s -> %s" % (target, source))
def change(trgt): try: os.chdir(trgt) except OSError as err: raise exceptions.BuiltinError( "[cd] directory was not changed: %s" % trgt)