Skip to content

Commit

Permalink
Merge pull request #4 from paulo-coutinho/symlink-support
Browse files Browse the repository at this point in the history
support for symbolic link in file module
  • Loading branch information
Paulo Coutinho authored Jan 19, 2022
2 parents 874d774 + 810230d commit b352f75
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pygemstones/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.8"
__version__ = "0.0.9"
49 changes: 49 additions & 0 deletions pygemstones/io/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -943,3 +943,52 @@ def get_file_line_numbers_with_enclosing_tags(
f.close()

return result


# -----------------------------------------------------------------------------
def symlink(source_path, target_path, recreate=False, target_is_directory=False):
"""
Create symbolic link from source path to target path.
Arguments:
source_path : str
target_path : str
recreate : bool
target_is_directory : bool
Returns:
None
"""

try:
if os.path.islink(target_path):
if recreate:
unlink(target_path)
os.symlink(
source_path, target_path, target_is_directory=target_is_directory
)
else:
os.symlink(
source_path, target_path, target_is_directory=target_is_directory
)
except Exception:
pass


# -----------------------------------------------------------------------------
def unlink(path):
"""
Remove symbolic link from path.
Arguments:
path : str
Returns:
None
"""

if os.path.islink(path):
os.unlink(path)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
description = "Python package that group a lot of classes and functions that help software development."
name = "pygemstones"
version = "0.0.8"
version = "0.0.9"

authors = ["Paulo Coutinho <[email protected]>"]
license = "MIT"
Expand Down
71 changes: 71 additions & 0 deletions tests/io/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -1086,3 +1086,74 @@ def test_get_file_line_numbers_with_enclosing_tags_with_more_end_than_start_tags

assert line_numbers[0] == 1
assert line_numbers[1] == 4


# -----------------------------------------------------------------------------
def test_create_symbolic_link(tmp_path):
source_path = os.path.join(tmp_path, "source-dir")

f.set_file_content(os.path.join(source_path, "file.txt"), "test")
f.symlink(
os.path.join(source_path, "file.txt"),
os.path.join(source_path, "file_symbolic.txt"),
)

is_link = os.path.islink(os.path.join(source_path, "file_symbolic.txt"))

assert is_link


# -----------------------------------------------------------------------------
def test_create_symbolic_link_with_error(tmp_path):
source_path = os.path.join(tmp_path, "source-dir")

f.set_file_content(os.path.join(source_path, "file.txt"), "test")
f.set_file_content(os.path.join(source_path, "file_symbolic.txt"), "test")

f.symlink(
os.path.join(source_path, "file.txt"),
os.path.join(source_path, "file_symbolic.txt"),
)

is_link = os.path.islink(os.path.join(source_path, "file_symbolic.txt"))

assert is_link == False


# -----------------------------------------------------------------------------
def test_recreate_symbolic_link(tmp_path):
source_path = os.path.join(tmp_path, "source-dir")

f.set_file_content(os.path.join(source_path, "file.txt"), "test")
f.symlink(
os.path.join(source_path, "file.txt"),
os.path.join(source_path, "file_symbolic.txt"),
)
f.symlink(
os.path.join(source_path, "file.txt"),
os.path.join(source_path, "file_symbolic.txt"),
recreate=True,
)

is_link = os.path.islink(os.path.join(source_path, "file_symbolic.txt"))

assert is_link


# -----------------------------------------------------------------------------
def test_remove_symbolic_link(tmp_path):
source_path = os.path.join(tmp_path, "source-dir")

f.set_file_content(os.path.join(source_path, "file.txt"), "test")
f.symlink(
os.path.join(source_path, "file.txt"),
os.path.join(source_path, "file_symbolic.txt"),
)

is_link = os.path.islink(os.path.join(source_path, "file_symbolic.txt"))
assert is_link

f.unlink(os.path.join(source_path, "file_symbolic.txt"))

is_link = os.path.islink(os.path.join(source_path, "file_symbolic.txt"))
assert is_link == False
2 changes: 1 addition & 1 deletion tests/test_pygems.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

# -----------------------------------------------------------------------------
def test_version():
assert __version__ == "0.0.8"
assert __version__ == "0.0.9"

0 comments on commit b352f75

Please sign in to comment.