diff --git a/tren.py b/tren.py index 2bdd86c..ea04de8 100755 --- a/tren.py +++ b/tren.py @@ -8,7 +8,7 @@ PROGNAME = "tren.py" BASENAME = PROGNAME.split(".py")[0] PROGENV = BASENAME.upper() -RCSID = "$Id: tren.py,v 1.200 2010/03/18 17:40:31 tundra Exp $" +RCSID = "$Id: tren.py,v 1.201 2010/03/18 21:54:30 tundra Exp $" VERSION = RCSID.split()[2] # Copyright Information @@ -39,8 +39,7 @@ import copy import getopt -import grp -import pwd +import glob import os import random import re @@ -48,6 +47,47 @@ import sys import time +##### +# Imports conditional on OS +##### + +# Set OS type - this allows us to trigger OS-specific code +# where needed. + +OSNAME = os.name +POSIX = False +WINDOWS = False + +if OSNAME == 'nt': + WINDOWS = True + +elif OSNAME == 'posix': + POSIX = True + +# If we're on Win32, try to load win32all stuff if it's available + +if WINDOWS: + try: + from win32api import GetFileAttributes + from win32file import GetDriveType + from win32wnet import WNetGetUniversalName + from win32security import * + WIN32ALL = True + + except: + WIN32ALL = False + + +# Get unix password and group features + +elif POSIX: + import grp + import pwd + +else: + sys.stderr.write("Unsupported Operating System! Aborting ...\n") + sys.exit(1) + #----------------------------------------------------------# # Aliases & Redefinitions # @@ -612,12 +652,12 @@ # Handle group name values elif order == ORDERBYGROUP: - sortkey = grp.getgrgid(stats[ST_GID])[0] + sortkey = self.__GetFileGroupname(stats[ST_GID]) # Handle user name values elif order == ORDERBYUSER: - sortkey = pwd.getpwuid(stats[ST_UID])[0] + sortkey = self.__GetFileUsername(stats[ST_UID]) # Save into storage @@ -729,6 +769,36 @@ ##### + # Determine File's Group Name + ##### + + def __GetFileGroupname(self, gid): + + if OSNAME == "posix": + return grp.getgrgid(gid)[0] + + else: + return "win32group" + + # End of 'GetFileGroupname()' + + + ##### + # Determine File's User Name + ##### + + def __GetFileUsername(self, uid): + + if OSNAME == "posix": + return pwd.getpwuid(uid)[0] + + else: + return "win32user" + + # End of 'GetFileUsername()' + + + ##### # Go Do The Requested Renaming ##### @@ -1058,7 +1128,7 @@ r[2] = str(self.RenNames[target][STATS][ST_GID]) elif r[2] == TOKFILGROUP: - r[2] = grp.getgrgid(self.RenNames[target][STATS][ST_GID])[0] + r[2] = self.__GetFileGroupname(self.RenNames[target][STATS][ST_GID]) elif r[2] == TOKFILINODE: r[2] = str(self.RenNames[target][STATS][ST_INO]) @@ -1076,7 +1146,7 @@ r[2] = str(self.RenNames[target][STATS][ST_UID]) elif r[2] == TOKFILUSER: - r[2] = pwd.getpwuid(self.RenNames[target][STATS][ST_UID])[0] + r[2] = self.__GetFileUsername(self.RenNames[target][STATS][ST_UID]) ### @@ -1496,6 +1566,7 @@ # End of 'ErrorMsg()' + ##### # Split -r Argument Into Separate Old And New Strings ##### @@ -1770,6 +1841,18 @@ # many of the renaming tokens derive information about the file being # renamed. + +# Do wildcard expansion for Windows systems because they have a +# brain-damaged shell. + +if WINDOWS: + + expandedlist = [] + for arg in args: + expandedlist += glob.glob(arg) + + args = expandedlist + targs = RenameTargets(args) # Now process the options