def test_create_and_destory_dir(self): dirpath = TEMP_DIR + '/test_mkdir' filepath = TEMP_DIR + '/test_mkdir/cool' fs.mkdir(dirpath) fs.mkfile(filepath) assert fs.exists(dirpath) assert fs.is_dir(dirpath) fs.rimraf(dirpath) assert not fs.exists(dirpath)
def test_unpack_tarball(self): os.chdir(TEMP_DIR) fs.unpack_tarball( __file__.replace('test_fs.py', 'assets/tar_test.tar.gz')) path = TEMP_DIR + '/tar_test' assert fs.exists(path) assert fs.is_dir(path) assert fs.exists(path + '/1.txt') assert fs.exists(path + '/2.txt') assert fs.exists(path + '/3.txt')
def test_copy(self): path = TEMP_DIR + '/test_copy_file' f = open(path, 'w') f.write('test_copy text') f.close() fs.copy(path, path + '_copy') assert fs.exists(path) assert fs.exists(path + '_copy') one = open(path, 'r') two = open(path + '_copy', 'r') assert one.read() == two.read()
def test_create_mv_and_destory_file(self): path = TEMP_DIR + '/test_makfile' fs.mkfile(path) assert fs.exists(path) assert not fs.is_dir(path) fs.mv(path, path + 'renamed') assert not fs.exists(path) assert fs.exists(path + 'renamed') assert not fs.is_dir(path + 'renamed') fs.rm(path + 'renamed') assert not fs.exists(path)
def _clone(ctx): red = ctx.red cyan = ctx.cyan reset = ctx.reset remote = ctx.get_option(0) if not remote: print(f'''USAGE: mold root --clone (git-uri) [--force] run 'mold root --clone help' for more info''') return ctx.FAIL if fs.exists(ctx.MOLD_ROOT): if not ctx.check_flag_set('--force'): print( f'{red}WARNING:{reset} {ctx.MOLD_ROOT} allready exists do you want to remove it?' ) abort = 'y' != input( f'{cyan}Do you want to contiune the installation? y/n:{reset} ' ).strip() if (abort): print('Ok, mold --clone aborted.') return ctx.OK fs.rimraf(ctx.MOLD_ROOT) git.clone(ctx, remote) result = check(ctx) if result != ctx.NEXT_COMMAND: return ctx.FAIL ctx.link_conf() return ctx.OK
def _create_mold_root(ctx): try: if fs.exists(ctx.MOLD_ROOT): fs.rimraf(ctx.MOLD_ROOT) fs.copy_dir(BUILD_DIR + '/mold_root', ctx.MOLD_ROOT) return True except: return False
def check(ctx): red = ctx.red yellow = ctx.yellow reset = ctx.reset if ctx.check_flag_set('--complete'): return ctx.OK if (not ctx.MOLD_ROOT) or (not fs.exists(ctx.MOLD_ROOT)) or (not fs.is_dir( ctx.MOLD_ROOT)): print( f'''{red}ERROR:{reset} Looks like your MOLD_ROOT directory hasent been set up yet. {yellow}Try runing "mold root --install"{reset}''') return ctx.MOLD_ROOT_ERROR for d in ['conf', 'plug', 'fold', 'leaf', 'exec']: if not fs.exists(ctx.MOLD_ROOT + '/' + d): print( f'''{red}ERROR:{reset} Somthing is wrong with your MOLD_ROOT directory. {yellow}Try runing "mold root --fix"{reset}''') return ctx.MOLD_ROOT_DIRS_ERROR return ctx.NEXT_COMMAND
def _make(ctx): if not ctx.check_has_options(): return _usage(ctx, '(name)') filename = ctx.get_option(0) filepath = ctx.get_command_dir() + '/' + filename if fs.exists(filepath): return print(f'ERROR: {filepath} allready exits try "mold {ctx.command} edit" instead') if ctx.command == 'fold': fs.mkdir(filepath) system.cd(filepath) if not system.shell(ctx.EDITOR + ' ' + filepath).check_ok(): return print(f'ERROR: {ctx.EDITOR} could not open {filepath}') if not fs.exists(filepath): return print(f'MAKE {ctx.command} ABORTED: {filename} not created') if ctx.command == 'exec': fs.chmod(filepath, 0o755) print(f'MADE {ctx.command}:', filename) if ctx.command == 'conf': _link_conf(ctx, filename)
def _fix(ctx): print("Fixing mold root") if not fs.exists(ctx.MOLD_ROOT): print('''ERROR: There is no MOLD_ROOT to fix. Try runing "mold root --install"''') return ctx.MOLD_ROOT_ERROR try: for content_type in ['conf', 'plug', 'exec', 'fold', 'leaf']: content_dir = ctx.MOLD_ROOT + '/' + content_type if not fs.exists(content_dir): fs.mkdir(content_dir) fs.write_file(content_dir + '/.mold', 'KEEP ME') result = _check(ctx) if result != ctx.OK: print('''Sorry, Something went wrong. You may want to open an issue at https://github.com/slugbyte/mold/issues''' ) return ctx.NEXT_COMMAND except: return ctx.FAIL
def _drop(ctx): if not ctx.check_has_options(): return _usage(ctx, '(name)') filename = ctx.get_option(0) filepath = ctx.get_command_dir() + '/' + filename if fs.exists(filepath): if ctx.command == 'fold': fs.rimraf(filepath) else: fs.rm(filepath) print(f'REMOVED {filename} FROM $MOLD_ROOT/{ctx.command}') return print(f'ERROR: no "{filename}" {ctx.command} file found')
def _take(ctx): if not ctx.check_has_options(): return _usage(ctx, '(name) [new name]') if not (ctx.command == 'fold' or ctx.command == 'leaf'): print(f'ERROR: {ctx.command} does not support the file task') return filename = ctx.get_option(0) filepath = ctx.get_command_dir() + '/' + filename output = ctx.get_option(1) or filename if fs.exists(filepath): if ctx.command == 'leaf': fs.copy(filepath, './' + output) return if ctx.command == 'fold': fs.copydir(filepath, './' + output) return print(f'ERROR: no "{filename}" {ctx.command} file found')
def _edit(ctx): if not ctx.check_has_options(): return _usage(ctx, '(name)') if ctx.command == 'conf': if ctx.check_flag_set('--no-linking'): return print('ERROR: conf is allready link --no-linking can not be used with "mold conf edit".') filename = ctx.get_option(0) filepath = ctx.get_command_dir() + '/' + filename if fs.exists(filepath): if ctx.command == 'fold': # IF you dont cd when using TUI editors its edit a dir structure system.cd(filepath) if not system.shell(ctx.EDITOR + ' ' + filepath).check_ok(): return print(f'ERROR: unable to edit {filepath}') if ctx.command == 'conf': _link_conf(ctx, filename) # RELINK CONF ON SUCCES return print(f'ERROR: no "{filename}" {ctx.command} file found')
def _load_file(ctx, filepath): filename = ctx.get_option(1) or fs.basename(filepath) destpath = ctx.get_command_dir() + '/' + filename if fs.exists(filepath): # first load content if ctx.command == 'fold': if not fs.is_dir(filepath): return print(f'USAGE ERROR: {filepath} is not a directory, use mold file instead.') fs.copy_dir(filepath, destpath) else: if fs.is_dir(filepath): return print(f'USAGE ERROR: {filepath}" is a directory, mold {ctx.command} only supports files.') fs.copy(filepath, destpath) # then if its a conf link it if ctx.command == 'exec': fs.chmod(destpath, 0o755) if ctx.command == 'conf': _link_conf(ctx, filename) print(f'LOADED {ctx.command}: {filename}') return print(f'ERROR: could not find "{filepath}"')
def _handle_mold_root_exists(ctx): red = ctx.red cyan = ctx.cyan reset = ctx.reset if ctx.check_flag_set('--force'): return True if fs.exists(ctx.MOLD_ROOT): if ctx.check_flag_set( '--no-prompt') and not ctx.check_flag_set('--force'): print(f'''Sorry, {ctx.MOLD_ROOT} allread exists Use the interactive installer or the flag '--force' to overwrite it.''') return False print(f'{red}Hmm,{reset} {ctx.MOLD_ROOT} {red}allready exits.{reset}') cancel = 'y' != input( f'{cyan}Do you want to remove it and continue? y/n:{reset} ' ).strip() if cancel: print(f'Ok, MOLD_ROOT installation canceled.') return False return True
def test_exists(self): assert fs.exists(TEMP_DIR) assert not fs.exists(TEMP_DIR + '/not-existing-file')