path_ops.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 ls(path: str | Path) -> list[Path]:
  65. """List the contents of a directory.
  66. Args:
  67. path: The path to the directory.
  68. Returns:
  69. A list of paths to the contents of the directory.
  70. """
  71. return list(Path(path).iterdir())
  72. def ln(src: str | Path, dest: str | Path, overwrite: bool = False) -> bool:
  73. """Create a symbolic link.
  74. Args:
  75. src: The path to the file or directory.
  76. dest: The path to the destination.
  77. overwrite: Whether to overwrite the destination.
  78. Returns:
  79. Whether the link was successful.
  80. """
  81. src, dest = Path(src), Path(dest)
  82. if src == dest:
  83. return False
  84. if not overwrite and (dest.exists() or dest.is_symlink()):
  85. return False
  86. if src.is_dir():
  87. rm(dest)
  88. src.symlink_to(dest, target_is_directory=True)
  89. else:
  90. src.symlink_to(dest)
  91. return True
  92. def which(program: str | Path) -> str | Path | None:
  93. """Find the path to an executable.
  94. Args:
  95. program: The name of the executable.
  96. Returns:
  97. The path to the executable.
  98. """
  99. return shutil.which(str(program))
  100. def get_node_bin_path() -> str | None:
  101. """Get the node binary dir path.
  102. Returns:
  103. The path to the node bin folder.
  104. """
  105. bin_path = Path(constants.Node.BIN_PATH)
  106. if not bin_path.exists():
  107. str_path = which("node")
  108. return str(Path(str_path).parent.resolve()) if str_path else str_path
  109. return str(bin_path.resolve())
  110. def get_node_path() -> str | None:
  111. """Get the node binary path.
  112. Returns:
  113. The path to the node binary file.
  114. """
  115. node_path = Path(constants.Node.PATH)
  116. if not node_path.exists():
  117. return str(which("node"))
  118. return str(node_path)
  119. def get_npm_path() -> str | None:
  120. """Get npm binary path.
  121. Returns:
  122. The path to the npm binary file.
  123. """
  124. npm_path = Path(constants.Node.NPM_PATH)
  125. if not npm_path.exists():
  126. return str(which("npm"))
  127. return str(npm_path)
  128. def update_json_file(file_path: str | Path, update_dict: dict[str, int | str]):
  129. """Update the contents of a json file.
  130. Args:
  131. file_path: the path to the JSON file.
  132. update_dict: object to update json.
  133. """
  134. fp = Path(file_path)
  135. # Create the file if it doesn't exist.
  136. fp.touch(exist_ok=True)
  137. # Create an empty json object if file is empty
  138. fp.write_text("{}") if fp.stat().st_size == 0 else None
  139. # Read the existing json object from the file.
  140. json_object = {}
  141. if fp.stat().st_size == 0:
  142. with open(fp) as f:
  143. json_object = json.load(f)
  144. # Update the json object with the new data.
  145. json_object.update(update_dict)
  146. # Write the updated json object to the file
  147. with open(fp, "w") as f:
  148. json.dump(json_object, f, ensure_ascii=False)
  149. def find_replace(directory: str | Path, find: str, replace: str):
  150. """Recursively find and replace text in files in a directory.
  151. Args:
  152. directory: The directory to search.
  153. find: The text to find.
  154. replace: The text to replace.
  155. """
  156. directory = Path(directory)
  157. for root, _dirs, files in os.walk(directory):
  158. for file in files:
  159. filepath = Path(root, file)
  160. text = filepath.read_text(encoding="utf-8")
  161. text = re.sub(find, replace, text)
  162. filepath.write_text(text, encoding="utf-8")