Put in basic framework for handling user- and group names for both Posix and Windows systems.
Added logic for Windows to do glob expansion of the command line file argument list.
1 parent 90f53d1 commit 6095aac5f96b6a635020e6c118498970a400d087
@tundra tundra authored on 18 Mar 2010
Showing 1 changed file
View
98
tren.py
 
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
 
#----------------------------------------------------------#
 
import copy
import getopt
import grp
import pwd
import glob
import os
import random
import re
from stat import *
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 #
#----------------------------------------------------------#
 
# 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
if sortkey in storage:
 
DebugMsg(SEPARATOR + "\n\n")
 
# End of 'DumpObj()'
 
 
#####
# 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
elif r[2] == TOKFILGID:
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])
 
elif r[2] == TOKFILUID:
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])
 
###
# File Time Renaming Tokens
 
sys.exit(1)
 
# End of 'ErrorMsg()'
 
 
#####
# Split -r Argument Into Separate Old And New Strings
#####
# be fully populated before any actual renaming can take place since
# 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