kkpyutil 1.46.3__tar.gz → 1.47.1__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.46.3
3
+ Version: 1.47.1
4
4
  Summary: Building blocks for sysadmin and DevOps
5
5
  Home-page: https://github.com/kakyoism/kkpyutil/
6
6
  License: MIT
@@ -2079,6 +2079,31 @@ 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):
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
+ """
2087
+ # Ensure the source directory exists
2088
+ if not os.path.exists(src_root):
2089
+ logger.error(f"Error: Source directory {src_root} does not exist.")
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}")
2104
+ return True
2105
+
2106
+
2082
2107
  def compare_dirs(dir1, dir2, ignoreddirpatterns=(), ignoredfilepatterns=(), showdiff=True):
2083
2108
  """
2084
2109
  - filecmp.dircmp() supports explicit name ignores only
@@ -2974,8 +2999,10 @@ def lazy_download(local_file, url, file_open_mode='wb', logger=None):
2974
2999
  return local_file
2975
3000
  os.makedirs(osp.dirname(local_file), exist_ok=True)
2976
3001
  logger = logger or glogger
3002
+ # Add User-Agent header to avoid 403 errors from websites that block bots
3003
+ req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
2977
3004
  with open(local_file, file_open_mode) as fp:
2978
- with urllib.request.urlopen(url) as response:
3005
+ with urllib.request.urlopen(req) as response:
2979
3006
  logger.info(f'Downloading: {url} => {local_file}')
2980
3007
  fp.write(response.read())
2981
3008
  return local_file
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "kkpyutil"
3
- version = "1.46.3"
3
+ version = "1.47.1"
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