Skip to content

Commit

Permalink
Support symlinking unix username to steamuser
Browse files Browse the repository at this point in the history
- Closes #15
  • Loading branch information
R1kaB3rN committed Feb 9, 2024
1 parent a8f3e26 commit 125fce8
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 2 deletions.
27 changes: 25 additions & 2 deletions gamelauncher.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,36 @@ def parse_args() -> Namespace: # noqa: D103


def _setup_pfx(path: str) -> None:
"""Create a symlink to the WINE prefix and tracked_files file."""
"""Create a symlink to the WINE prefix and tracked_files file.
Also, create a symlink of steamuser to the Unix username
"""
username: str = Path().home().name

if not (Path(path + "/pfx")).expanduser().is_symlink():
# When creating the symlink, we want it to be in expanded form when passed unexpanded paths
# Example: pfx -> /home/.wine
# Good: pfx -> /home/foo/.wine
# Bad: pfx -> ~/.wine
Path(path + "/pfx").expanduser().symlink_to(Path(path).expanduser())
Path(path + "/tracked_files").expanduser().touch()

# Create a symlink of the current user to the steamuser dir for Steam functionality
if Path(path + "/drive_c/users/" + username).expanduser().is_dir():
# If the prefix exists, rename the dir to steamuser then create a symlink
Path(path + "/drive_c/users/" + username).expanduser().rename(
Path(path + "/drive_c/users/steamuser").expanduser()
)
Path(path + "/drive_c/users/" + username).expanduser().symlink_to(
Path(path + "/drive_c/users/steamuser").expanduser()
)
else:
Path(path + "/drive_c/users/steamuser").expanduser().mkdir(
parents=True, exist_ok=True
)
Path(path + "/drive_c/users/" + username).expanduser().symlink_to(
Path(path + "/drive_c/users/steamuser").expanduser()
)


def check_env(env: Dict[str, str]) -> Dict[str, str]:
"""Before executing a game, check for environment variables and set them.
Expand Down
100 changes: 100 additions & 0 deletions gamelauncher_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,106 @@ def test_set_env(self):
result = gamelauncher.set_env(self.env, result_args)
self.assertTrue(result is self.env, "Expected the same reference")

def test_setup_pfx_symlinks_steam_mv(self):
"""Test _setup_pfx for symbolic link to steamuser.
Expects that symlinks of the user to steamuser are properly created for existing prefixes.
The user dir is renamed to steamuser then a symlink to that is created in the current dir
"""
result = None
pattern = r"^/home/[a-zA-Z]+"
unexpanded_path = re.sub(
pattern,
"~",
Path(
Path(self.test_file).cwd().as_posix() + "/" + self.test_file
).as_posix(),
)
# Create a user dir in the prefix
Path(
unexpanded_path + "/drive_c/users/" + Path().home().name
).expanduser().mkdir(parents=True, exist_ok=True)

result = gamelauncher._setup_pfx(unexpanded_path)
# Replaces the expanded path to unexpanded
# Example: ~/some/path/to/this/file
self.assertIsNone(
result,
"Expected None when creating symbolic link to WINE prefix and tracked_files file",
)
self.assertTrue(
Path(self.test_file + "/pfx").is_symlink(), "Expected pfx to be a symlink"
)
self.assertTrue(
Path(self.test_file + "/tracked_files").is_file(),
"Expected tracked_files to be a file",
)
self.assertTrue(
Path(self.test_file + "/pfx").is_symlink(), "Expected pfx to be a symlink"
)
# Check if the symlink is in its unexpanded form
self.assertEqual(
Path(self.test_file + "/pfx").readlink().as_posix(),
Path(unexpanded_path).expanduser().as_posix(),
)
self.assertTrue(
Path(self.test_file + "/drive_c/users/steamuser").is_dir(),
"Expected steamuser to be created",
)
self.assertTrue(
Path(unexpanded_path + "/drive_c/users/" + Path().home().name)
.expanduser()
.is_symlink(),
"Expected symlink of username -> steamuser",
)

def test_setup_pfx_symlinks_steam(self):
"""Test _setup_pfx for symbolic link to steamuser.
Expects that symlinks of the user to steamuser are properly created for new prefixes
"""
result = None
pattern = r"^/home/[a-zA-Z]+"
unexpanded_path = re.sub(
pattern,
"~",
Path(
Path(self.test_file).cwd().as_posix() + "/" + self.test_file
).as_posix(),
)
result = gamelauncher._setup_pfx(unexpanded_path)
# Replaces the expanded path to unexpanded
# Example: ~/some/path/to/this/file
self.assertIsNone(
result,
"Expected None when creating symbolic link to WINE prefix and tracked_files file",
)
self.assertTrue(
Path(self.test_file + "/pfx").is_symlink(), "Expected pfx to be a symlink"
)
self.assertTrue(
Path(self.test_file + "/tracked_files").is_file(),
"Expected tracked_files to be a file",
)
self.assertTrue(
Path(self.test_file + "/pfx").is_symlink(), "Expected pfx to be a symlink"
)
# Check if the symlink is in its unexpanded form
self.assertEqual(
Path(self.test_file + "/pfx").readlink().as_posix(),
Path(unexpanded_path).expanduser().as_posix(),
)
self.assertTrue(
Path(self.test_file + "/drive_c/users/steamuser").is_dir(),
"Expected steamuser to be created",
)
self.assertTrue(
Path(unexpanded_path + "/drive_c/users/" + Path().home().name)
.expanduser()
.is_symlink(),
"Expected symlink of username -> steamuser",
)

def test_setup_pfx_symlinks(self):
"""Test _setup_pfx for valid symlinks.
Expand Down

0 comments on commit 125fce8

Please sign in to comment.