path_ops.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. """Path operations."""
  2. from __future__ import annotations
  3. import json
  4. import os
  5. import re
  6. import shutil
  7. from pathlib import Path
  8. from reflex import constants
  9. # Shorthand for join.
  10. join = os.linesep.join
  11. def rm(path: str | Path):
  12. """Remove a file or directory.
  13. Args:
  14. path: The path to the file or directory.
  15. """
  16. path = Path(path)
  17. if path.is_dir():
  18. shutil.rmtree(path)
  19. elif path.is_file():
  20. path.unlink()
  21. def cp(src: str | Path, dest: str | Path, overwrite: bool = True) -> bool:
  22. """Copy a file or directory.
  23. Args:
  24. src: The path to the file or directory.
  25. dest: The path to the destination.
  26. overwrite: Whether to overwrite the destination.
  27. Returns:
  28. Whether the copy was successful.
  29. """
  30. src, dest = Path(src), Path(dest)
  31. if src == dest:
  32. return False
  33. if not overwrite and dest.exists():
  34. return False
  35. if src.is_dir():
  36. rm(dest)
  37. shutil.copytree(src, dest)
  38. else:
  39. shutil.copyfile(src, dest)
  40. return True
  41. def mv(src: str | Path, dest: str | Path, overwrite: bool = True) -> bool:
  42. """Move a file or directory.
  43. Args:
  44. src: The path to the file or directory.
  45. dest: The path to the destination.
  46. overwrite: Whether to overwrite the destination.
  47. Returns:
  48. Whether the move was successful.
  49. """
  50. src, dest = Path(src), Path(dest)
  51. if src == dest:
  52. return False
  53. if not overwrite and dest.exists():
  54. return False
  55. rm(dest)
  56. shutil.move(src, dest)
  57. return True
  58. def mkdir(path: str | Path):
  59. """Create a directory.
  60. Args:
  61. path: The path to the directory.
  62. """
  63. Path(path).mkdir(parents=True, exist_ok=True)
  64. def ln(src: str | Path, dest: str | Path, overwrite: bool = False) -> bool:
  65. """Create a symbolic link.
  66. Args:
  67. src: The path to the file or directory.
  68. dest: The path to the destination.
  69. overwrite: Whether to overwrite the destination.
  70. Returns:
  71. Whether the link was successful.
  72. """
  73. src, dest = Path(src), Path(dest)
  74. if src == dest:
  75. return False
  76. if not overwrite and (dest.exists() or dest.is_symlink()):
  77. return False
  78. if src.is_dir():
  79. rm(dest)
  80. src.symlink_to(dest, target_is_directory=True)
  81. else:
  82. src.symlink_to(dest)
  83. return True
  84. def which(program: str | Path) -> str | Path | None:
  85. """Find the path to an executable.
  86. Args:
  87. program: The name of the executable.
  88. Returns:
  89. The path to the executable.
  90. """
  91. return shutil.which(str(program))
  92. def get_node_bin_path() -> str | None:
  93. """Get the node binary dir path.
  94. Returns:
  95. The path to the node bin folder.
  96. """
  97. bin_path = Path(constants.Node.BIN_PATH)
  98. if not bin_path.exists():
  99. str_path = which("node")
  100. return str(Path(str_path).parent.resolve()) if str_path else str_path
  101. return str(bin_path.resolve())
  102. def get_node_path() -> str | None:
  103. """Get the node binary path.
  104. Returns:
  105. The path to the node binary file.
  106. """
  107. node_path = Path(constants.Node.PATH)
  108. if not node_path.exists():
  109. return str(which("node"))
  110. return str(node_path)
  111. def get_npm_path() -> str | None:
  112. """Get npm binary path.
  113. Returns:
  114. The path to the npm binary file.
  115. """
  116. npm_path = Path(constants.Node.NPM_PATH)
  117. if not npm_path.exists():
  118. return str(which("npm"))
  119. return str(npm_path)
  120. def update_json_file(file_path: str | Path, update_dict: dict[str, int | str]):
  121. """Update the contents of a json file.
  122. Args:
  123. file_path: the path to the JSON file.
  124. update_dict: object to update json.
  125. """
  126. fp = Path(file_path)
  127. # Create the file if it doesn't exist.
  128. fp.touch(exist_ok=True)
  129. # Create an empty json object if file is empty
  130. fp.write_text("{}") if fp.stat().st_size == 0 else None
  131. # Read the existing json object from the file.
  132. json_object = {}
  133. if fp.stat().st_size == 0:
  134. with open(fp) as f:
  135. json_object = json.load(f)
  136. # Update the json object with the new data.
  137. json_object.update(update_dict)
  138. # Write the updated json object to the file
  139. with open(fp, "w") as f:
  140. json.dump(json_object, f, ensure_ascii=False)
  141. def find_replace(directory: str | Path, find: str, replace: str):
  142. """Recursively find and replace text in files in a directory.
  143. Args:
  144. directory: The directory to search.
  145. find: The text to find.
  146. replace: The text to replace.
  147. """
  148. directory = Path(directory)
  149. for root, _dirs, files in os.walk(directory):
  150. for file in files:
  151. filepath = Path(root, file)
  152. text = filepath.read_text(encoding="utf-8")
  153. text = re.sub(find, replace, text)
  154. filepath.write_text(text)