Create rename utility function

os.rename() fails on Windows if file already exists.

Create getuid utility function (#21736)

On Windows, replace os.getuid with ctypes.windll.shell32.IsUserAnAdmin().

Tests: Use getuid util function

Co-authored-by: lou.lawrence@kitware.com <lou.lawrence@kitware.com>
Co-authored-by: Betsy McPhail <betsy.mcphail@kitware.com>
This commit is contained in:
Betsy McPhail
2021-01-26 14:30:32 -05:00
committed by Peter Scheibel
parent b60a0eea01
commit a7de2fa380
11 changed files with 45 additions and 23 deletions

View File

@@ -6,6 +6,7 @@
import errno
import glob
import grp
import ctypes
import hashlib
import itertools
import numbers
@@ -44,6 +45,7 @@
'fix_darwin_install_name',
'force_remove',
'force_symlink',
'getuid',
'chgrp',
'chmod_x',
'copy',
@@ -60,6 +62,7 @@
'remove_directory_contents',
'remove_if_dead_link',
'remove_linked_tree',
'rename',
'set_executable',
'set_install_permissions',
'touch',
@@ -71,6 +74,23 @@
]
def getuid():
if _platform == "win32":
if ctypes.windll.shell32.IsUserAnAdmin() == 0:
return 1
return 0
else:
return os.getuid()
def rename(src, dst):
# On Windows, os.rename will fail if the destination file already exists
if is_windows:
if os.path.exists(dst):
os.remove(dst)
os.rename(src, dst)
def path_contains_subdirectory(path, root):
norm_root = os.path.abspath(root).rstrip(os.path.sep) + os.path.sep
norm_path = os.path.abspath(path).rstrip(os.path.sep) + os.path.sep
@@ -293,7 +313,7 @@ def group_ids(uid=None):
(list of int): gids of groups the user is a member of
"""
if uid is None:
uid = os.getuid()
uid = getuid()
user = pwd.getpwuid(uid).pw_name
return [g.gr_gid for g in grp.getgrall() if user in g.gr_mem]