def test_ambiguous_path_strict(self): win_path = 'tests\\files\\CASE\\a\\B\\file' lin_path_1 = 'tests/files/case/A/b/file' lin_path_2 = 'tests/files/case/a/B/file' self.assertTrue(os.path.exists(lin_path_1)) self.assertTrue(os.path.exists(lin_path_2)) with self.assertRaises(FileNotFoundError): to_posix_path(win_path, strict=True)
def read_trivial_batch(file): """Find DOSBox command in batch file and return its argument list. Returns a tuple: The first element is a directory used by batch file to invoke DOSBox. If batch contains no relevant information, it's 'None'. The second element is an argument list. """ line_num = 1 with open(file, 'r') as bat_file: lines = bat_file.readlines(512) assert lines, 'error processing .bat file (not enough lines)' new_path = None for line in lines: this_line = argsplit_windows(line) if not this_line: continue first_word = this_line[0] if first_word.lower() in ('echo', '@echo'): continue if first_word.lower() in ('cd', '@cd'): # This works only for a single 'CD', but no game uses more than # one (so far). If we'll ever find one, then it's time to # implement more serious batch interpreter instead. new_path = winpathlib.to_posix_path(this_line[1]) assert new_path, 'error processing .bat ' + \ 'file: no directory named ' + \ '{}'.format(this_line[1]) win_path = pathlib.PureWindowsPath(first_word) exe = win_path.parts[-1] if exe.lower() in ('dosbox', 'dosbox.exe'): return new_path, this_line[1:] assert False, 'error processing .bat file (line {})'.format(line_num) return None, []
def __parse_game_entry__(self, launcher, ini_dir, i): num = i + 1 name = launcher['game%dname' % num] prog = launcher['game%dprog' % num] path = launcher['game%dpath' % num] exe = launcher['game%dexe' % num] cmd = launcher['game%dcmd' % num] self.games[i] = {'name': name} game = self.games[i] orig_cwd = os.getcwd() os.chdir(ini_dir) real_path = to_posix_path(path) if real_path: game['path'] = os.path.join(ini_dir, real_path) else: print_err("steam-dos: error: can't find path", path) game['path'] = orig_cwd if prog.lower() == 'dosbox': # used only in Legacy launcher game['args'] = ['-c', 'mount C .', '-c', 'C:', '-c', exe, '-c', 'exit'] # yapf: disable else: # usually 'other', most common option game['args'] = argsplit_windows(cmd) os.chdir(orig_cwd)
def test_tricky_path(self): win_path = 'tests\\files\\CASE\\a\\B\\c' lin_path = 'tests/files/case/A/b/C' self.assertTrue(os.path.exists(lin_path)) self.assertTrue(os.path.exists('tests/files/case/a/B')) self.assertFalse(os.path.exists('tests/files/case/a/B/C')) self.assertEqual(to_posix_path(win_path), lin_path)
def test_ambiguous_path_lenient(self): win_path = 'tests\\files\\CASE\\a\\B\\file' lin_path_1 = 'tests/files/case/A/b/file' lin_path_2 = 'tests/files/case/a/B/file' self.assertTrue(os.path.exists(lin_path_1)) self.assertTrue(os.path.exists(lin_path_2)) self.assertIn(to_posix_path(win_path, strict=False), (lin_path_1, lin_path_2))
def __init__(self, *, commands=[], conf_files=[], exe=None, noautoexec=False, exit_after_exe=False, tweak_conf={}): assert commands or conf_files or exe dict.__init__(self) self['autoexec'] = [] self.raw_autoexec = self['autoexec'] self.encoding = 'utf-8' for win_path in (conf_files or self.__get_default_conf__()): path = to_posix_path(win_path) conf, enc = parse_dosbox_config(path) self.__import_ini_sections__(conf) if enc != 'utf-8': self.encoding = enc if not noautoexec and conf.has_section('autoexec'): self.raw_autoexec.extend(line for line in conf['autoexec']) self.raw_autoexec.extend(cmd for cmd in commands) tweak = configparser.ConfigParser() tweak.read_dict(tweak_conf) self.__import_ini_sections__(tweak) if exe: posix_path = to_posix_path(exe) path, file = os.path.split(posix_path) self.raw_autoexec.append('mount C {0}'.format(path or '.')) self.raw_autoexec.append('C:') if file.lower().endswith('.bat'): self.raw_autoexec.append('call {0}'.format(file)) else: self.raw_autoexec.append(file) if exit_after_exe: self.raw_autoexec.append('exit')
def to_linux_autoexec(autoexec): """Convert case-sensitive parts in autoexec.""" cmd_1 = r'@? *(mount|imgmount) +([a-z]):? +"([^"]+)"( +(.*))?' cmd_2 = r'@? *(mount|imgmount) +([a-z]):? +([^ ]+)( +(.*))?' mount_cmd_1 = re.compile(cmd_1, re.IGNORECASE) mount_cmd_2 = re.compile(cmd_2, re.IGNORECASE) change_drv = re.compile(r'@? *([a-z]:)\\? *$', re.IGNORECASE) for line in autoexec: match = mount_cmd_1.match(line) or mount_cmd_2.match(line) if match: cmd = match.group(1).lower() drive = match.group(2).upper() path = to_posix_path(match.group(3)) rest = match.group(4) or '' yield '{0} {1} "{2}"{3}'.format(cmd, drive, path, rest) continue match = change_drv.match(line) if match: drive = match.group(1).upper() yield drive continue yield line
def __get_default_conf__(self): # pylint: disable=no-self-use path = to_posix_path('dosbox.conf') if path and os.path.isfile(path): return [path] return []
def test_empty(self): self.assertEqual(to_posix_path(''), '')
def test_missing_path_2(self): win_path = 'tests_XXX\\files\\case\\dosbox.confz' self.assertEqual(to_posix_path(win_path), None)
def test_longer_paths_3(self): win_path = 'tests\\files\\..\\files\\.\\.\\case\\dosbox.conf' lin_path = 'tests/files/../files/case/DoSbOx.CoNf' self.assertEqual(to_posix_path(win_path), lin_path)
def test_longer_paths_1(self): win_path = 'TESTS\\FILES\\CONFS\\ABC\\DEF\\FILE' lin_path = 'tests/files/confs/abc/DEF/file' self.assertEqual(to_posix_path(win_path), lin_path)
def test_simple_paths(self): self.assertEqual(to_posix_path('tests'), 'tests') self.assertEqual(to_posix_path('TeStS'), 'tests')
def test_dots_2(self): self.assertEqual(to_posix_path('.\\..'), '..') self.assertEqual(to_posix_path('..\\..'), '../..')
def test_dots(self): self.assertEqual(to_posix_path('..'), '..')
def test_path_none(self): with self.assertRaises(TypeError): to_posix_path(None)
def paths_found(): return all(to_posix_path(p, strict=False) for p in conf_paths)
def test_really_long_path(self): path = 'tests/files/somewhat_long_path/' file = 'With Much Much Longer Path Inside ' + \ 'AbcDefGhiJklMnoPqrStuVwxYz_0123456789.tXt' win_path = (path + file).replace('\\', '/').upper() self.assertEqual(to_posix_path(win_path), path + file)