kkpyutil 1.47.1__tar.gz → 1.48.0__tar.gz

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.1
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
@@ -2079,7 +2079,7 @@ 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, logger=glogger):
2082
+ def sync_dirs(src_root, dst_root, logger=glogger, sudo=False):
2083
2083
  """
2084
2084
  - assume src and dst folders are the same level of folder tree
2085
2085
  - the result will be dst_root mirrors src_root
@@ -2088,19 +2088,44 @@ def sync_dirs(src_root, dst_root, logger=glogger):
2088
2088
  if not os.path.exists(src_root):
2089
2089
  logger.error(f"Error: Source directory {src_root} does not exist.")
2090
2090
  return False
2091
- # Iterate through the items inside the source directory
2092
- for item in os.listdir(src_root):
2093
- src_path = os.path.join(src_root, item)
2094
- dst_path = os.path.join(dst_root, item)
2095
- if os.path.isdir(src_path):
2096
- # copytree with dirs_exist_ok=True will overwrite existing files
2097
- # and merge directories without warning.
2098
- shutil.copytree(src_path, dst_path, dirs_exist_ok=True)
2099
- logger.info(f"Copied directory: {item}")
2100
- else:
2101
- # For individual files at the root of my_src_dir
2102
- shutil.copy2(src_path, dst_path)
2103
- logger.info(f"Copied file: {item}")
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}")
2128
+ return False
2104
2129
  return True
2105
2130
 
2106
2131
 
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "kkpyutil"
3
- version = "1.47.1"
3
+ version = "1.48.0"
4
4
  description = "Building blocks for sysadmin and DevOps"
5
5
  authors = ["Beinan Li <li.beinan@gmail.com>"]
6
6
  maintainers = ["Beinan Li <li.beinan@gmail.com>"]
File without changes
File without changes