kkpyutil 1.47.0__py3-none-any.whl → 1.48.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: kkpyutil
3
- Version: 1.47.0
3
+ Version: 1.48.0
4
4
  Summary: Building blocks for sysadmin and DevOps
5
5
  Home-page: https://github.com/kakyoism/kkpyutil/
6
6
  License: MIT
@@ -0,0 +1,7 @@
1
+ kkpyutil.py,sha256=NmOen-YeJ8oYHswc2Bt596nd5MslbVco1kc8ZhLpFHo,120012
2
+ kkpyutil_helper/windows/kkttssave.ps1,sha256=xa3-WzqNh2rGYlOx_I4ewOuCE94gkTO5cEwYH0M67_0,446
3
+ kkpyutil_helper/windows/kkttsspeak.ps1,sha256=7WUUHMmjTQroUWA2Mvdt4JtSt475nZUHQx-qP-7rS6o,305
4
+ kkpyutil-1.48.0.dist-info/LICENSE,sha256=uISevGnCxB5QOU0ftbofN75_yUtd6E2h_MWE1zqagC8,1065
5
+ kkpyutil-1.48.0.dist-info/METADATA,sha256=EDa23UXz9R6402nMWL2bwFBpixNxG_7fxzS5DivfP2E,1136
6
+ kkpyutil-1.48.0.dist-info/WHEEL,sha256=kLuE8m1WYU0Ig0_YEGrXyTtiJvKPpLpDEiChiNyei5Y,88
7
+ kkpyutil-1.48.0.dist-info/RECORD,,
kkpyutil.py CHANGED
@@ -2079,24 +2079,53 @@ def move_file(src, dst, isdstdir=False):
2079
2079
  return dst if not isdstdir else osp.join(dst, osp.basename(src))
2080
2080
 
2081
2081
 
2082
- def sync_dirs(src_root, dst_root):
2082
+ def sync_dirs(src_root, dst_root, logger=glogger, sudo=False):
2083
+ """
2084
+ - assume src and dst folders are the same level of folder tree
2085
+ - the result will be dst_root mirrors src_root
2086
+ """
2083
2087
  # Ensure the source directory exists
2084
2088
  if not os.path.exists(src_root):
2085
- glogger.error(f"Error: Source directory {src_root} does not exist.")
2089
+ logger.error(f"Error: Source directory {src_root} does not exist.")
2090
+ return False
2091
+ if not sudo:
2092
+ # Iterate through the items inside the source directory
2093
+ for item in os.listdir(src_root):
2094
+ src_path = os.path.join(src_root, item)
2095
+ dst_path = os.path.join(dst_root, item)
2096
+ if os.path.isdir(src_path):
2097
+ # copytree with dirs_exist_ok=True will overwrite existing files
2098
+ # and merge directories without warning.
2099
+ shutil.copytree(src_path, dst_path, dirs_exist_ok=True)
2100
+ logger.info(f"Copied directory: {item}")
2101
+ else:
2102
+ # For individual files at the root of my_src_dir
2103
+ shutil.copy2(src_path, dst_path)
2104
+ logger.info(f"Copied file: {item}")
2105
+ return True
2106
+ # on macOS, merge to system folders
2107
+ # Identify the original user (the one who called sudo)
2108
+ # Default to current effective user if SUDO_USER isn't set
2109
+ user = os.environ.get('SUDO_USER') or os.environ.get('USER')
2110
+ if not user:
2111
+ logger.error("Could not determine the current user.")
2112
+ return False
2113
+ try:
2114
+ # rsync flags:
2115
+ # -a: Archive mode (preserves links, etc.)
2116
+ # --chown: Forces the owner:group of all copied files
2117
+ # Note: 'staff' is the default group for users on macOS
2118
+ cmd = [
2119
+ "sudo", "rsync", "-av", "--inplace",
2120
+ f"--chown={user}:staff",
2121
+ osp.join(src_root, ""), # Trailing slash copies contents
2122
+ dst_root
2123
+ ]
2124
+ logger.info(f"Syncing to {dst_root} and setting owner to '{user}'...")
2125
+ subprocess.check_call(cmd)
2126
+ except subprocess.CalledProcessError as e:
2127
+ logger.error(f"Sync failed with error code: {e.returncode}")
2086
2128
  return False
2087
- # Iterate through the items inside the source directory
2088
- for item in os.listdir(src_root):
2089
- src_path = os.path.join(src_root, item)
2090
- dst_path = os.path.join(dst_root, item)
2091
- if os.path.isdir(src_path):
2092
- # copytree with dirs_exist_ok=True will overwrite existing files
2093
- # and merge directories without warning.
2094
- shutil.copytree(src_path, dst_path, dirs_exist_ok=True)
2095
- glogger.info(f"Copied directory: {item}")
2096
- else:
2097
- # For individual files at the root of my_src_dir
2098
- shutil.copy2(src_path, dst_path)
2099
- glogger.info(f"Copied file: {item}")
2100
2129
  return True
2101
2130
 
2102
2131
 
@@ -1,7 +0,0 @@
1
- kkpyutil.py,sha256=pCKGYZYsZqeEL24S72XlbMMgUxd4CC2oU8D4OHfUIOU,118774
2
- kkpyutil_helper/windows/kkttssave.ps1,sha256=xa3-WzqNh2rGYlOx_I4ewOuCE94gkTO5cEwYH0M67_0,446
3
- kkpyutil_helper/windows/kkttsspeak.ps1,sha256=7WUUHMmjTQroUWA2Mvdt4JtSt475nZUHQx-qP-7rS6o,305
4
- kkpyutil-1.47.0.dist-info/LICENSE,sha256=uISevGnCxB5QOU0ftbofN75_yUtd6E2h_MWE1zqagC8,1065
5
- kkpyutil-1.47.0.dist-info/METADATA,sha256=P0VzS19-22ZtTx20Om97sT9y7OW7H873F3R-Qfm83VQ,1136
6
- kkpyutil-1.47.0.dist-info/WHEEL,sha256=kLuE8m1WYU0Ig0_YEGrXyTtiJvKPpLpDEiChiNyei5Y,88
7
- kkpyutil-1.47.0.dist-info/RECORD,,