| |
---|
| | |
---|
| | PROGNAME = "tren.py" |
---|
| | BASENAME = PROGNAME.split(".py")[0] |
---|
| | PROGENV = BASENAME.upper() |
---|
| | RCSID = "$Id: tren.py,v 1.197 2010/03/17 15:16:48 tundra Exp $" |
---|
| | RCSID = "$Id: tren.py,v 1.198 2010/03/17 21:12:05 tundra Exp $" |
---|
| | VERSION = RCSID.split()[2] |
---|
| | |
---|
| | # Copyright Information |
---|
| | |
---|
| |
---|
| | TOKDELIM = "/" # Delimiter for all renaming tokens |
---|
| | |
---|
| | # Shared File Attribute And Sequence Renaming Tokens |
---|
| | |
---|
| | TOKFILADATE = "ADATE" |
---|
| | TOKFILATIME = "ATIME" |
---|
| | TOKFILCMD = "CMDLINE" |
---|
| | TOKFILCDATE = "CDATE" |
---|
| | TOKFILCTIME = "CTIME" |
---|
| | TOKFILDEV = "DEV" |
---|
| | TOKFILFNAME = "FNAME" |
---|
| | TOKFILGID = "GID" |
---|
| | TOKFILGROUP = "GROUP" |
---|
| | TOKFILINODE = "INODE" |
---|
| | TOKFILMODE = "MODE" |
---|
| | TOKFILMDATE = "MDATE" |
---|
| | TOKFILMTIME = "MTIME" |
---|
| | TOKFILNLINK = "NLINK" |
---|
| | TOKFILSIZE = "SIZE" |
---|
| | TOKFILUID = "UID" |
---|
| |
---|
| | |
---|
| | # These literals serve two purposes: |
---|
| | # |
---|
| | # 1) They are used as the type indicator in a Sequence Renaming Token |
---|
| | # 2) They are keys to the SortViews dictionary that stores the prestorted views |
---|
| | |
---|
| | # 2) They are keys to the SortViews and DateViews dictionaries that stores the prestorted views |
---|
| | |
---|
| | ORDERBYADATE = TOKFILADATE |
---|
| | ORDERBYATIME = TOKFILATIME |
---|
| | ORDERBYCMDLINE = TOKFILCMD |
---|
| | ORDERBYCDATE = TOKFILCDATE |
---|
| | ORDERBYCTIME = TOKFILCTIME |
---|
| | ORDERBYDEV = TOKFILDEV |
---|
| | ORDERBYFNAME = TOKFILFNAME |
---|
| | ORDERBYGID = TOKFILGID |
---|
| | ORDERBYGROUP = TOKFILGROUP |
---|
| | ORDERBYINODE = TOKFILINODE |
---|
| | ORDERBYMODE = TOKFILMODE |
---|
| | ORDERBYMDATE = TOKFILMDATE |
---|
| | ORDERBYMTIME = TOKFILMTIME |
---|
| | ORDERBYNLINK = TOKFILNLINK |
---|
| | ORDERBYSIZE = TOKFILSIZE |
---|
| | ORDERBYUID = TOKFILUID |
---|
| |
---|
| | DEBUGFLAG = "-d" |
---|
| | dALPHABETS = "Alphabets" |
---|
| | dCMDLINE = "Command Line" |
---|
| | dCURSTATE = "Current State Of Program Options" |
---|
| | dDATEVIEW = "Date View:" |
---|
| | dDEBUG = "DEBUG" |
---|
| | dDUMPOBJ = "Dumping Object %s" |
---|
| | dINCLFILES = "Included Files:" |
---|
| | dPROGENV = "$" + PROGENV |
---|
| |
---|
| | ... (repeated for each rename target) |
---|
| | } |
---|
| | |
---|
| | self.SortViews = { |
---|
| | |
---|
| | ORDERBYATIME : [fullnames in atimes order], |
---|
| | ORDERBYCMDLINE : [fullnames in command line order], |
---|
| | ORDERBYCTIME : [fullnames in ctimes order], |
---|
| | ORDERBYDEV : [fullnames in devs order], |
---|
| | ORDERBYFNAME : [fullnames in alphabetic order], |
---|
| | ORDERBYGID : [fullnames in gids order], |
---|
| | ORDERBYGROUP ; [fullnames in group name order], |
---|
| | ORDERBYINODE : [fullnames in inode order], |
---|
| | ORDERBYMODE : [fullnames in mode order], |
---|
| | ORDERBYMTIME : [fullnames in mtimes order], |
---|
| | ORDERBYNLINK : [fullnames in nlinks order], |
---|
| | ORDERBYSIZE : [fullnames in size order], |
---|
| | ORDERBYUID : [fullnames in uids order], |
---|
| | ORDERBYUSER : [fullnames in user name order] |
---|
| | ORDERBYATIME : [fullnames in atimes order], |
---|
| | ORDERBYCMDLINE : [fullnames in command line order], |
---|
| | ORDERBYCTIME : [fullnames in ctimes order], |
---|
| | ORDERBYDEV : [fullnames in devs order], |
---|
| | ORDERBYFNAME : [fullnames in alphabetic order], |
---|
| | ORDERBYGID : [fullnames in gids order], |
---|
| | ORDERBYGROUP ; [fullnames in group name order], |
---|
| | ORDERBYINODE : [fullnames in inode order], |
---|
| | ORDERBYMODE : [fullnames in mode order], |
---|
| | ORDERBYMTIME : [fullnames in mtimes order], |
---|
| | ORDERBYNLINK : [fullnames in nlinks order], |
---|
| | ORDERBYSIZE : [fullnames in size order], |
---|
| | ORDERBYUID : [fullnames in uids order], |
---|
| | ORDERBYUSER : [fullnames in user name order] |
---|
| | } |
---|
| | |
---|
| | self.DateViews = { |
---|
| | ORDERBYADATE-date... : [fullnames in order by atime within same 'date'] ... (repeated for each date), |
---|
| | ORDERBYCDATE-date... : [fullnames in order by ctime within same 'date'] ... (repeated for each date), |
---|
| | ORDERBYMDATE-date... : [fullnames in order by mtime within same 'date'] ... (repeated for each date) |
---|
| | } |
---|
| | |
---|
| | self.RenRequests = [ |
---|
| | { |
---|
| | ASK : interactive ask flag |
---|
| | OLD : old rename string, |
---|
| |
---|
| | alpha.sort() |
---|
| | self.SortViews = {ORDERBYCMDLINE : targs, ORDERBYFNAME : alpha} |
---|
| | del alpha |
---|
| | |
---|
| | # Dictionary to hold all possible date views - files sorted |
---|
| | # by time *within* a common date. |
---|
| | |
---|
| | self.DateViews = {} |
---|
| | |
---|
| | # Dictionary of all the renaming requests - will be filled in |
---|
| | # by -r command line parsing. |
---|
| | |
---|
| | self.RenRequests = [] |
---|
| | |
---|
| | |
---|
| | # This data structure is used to build various sort views |
---|
| | # A null first field means the view requires special handling, |
---|
| | # otherwise it's just a stat structure lookup. |
---|
| | |
---|
| | SeqTypes = [ |
---|
| | [ST_ATIME, {}, ORDERBYATIME], |
---|
| | [ST_CTIME, {}, ORDERBYCTIME], |
---|
| |
---|
| | |
---|
| | elif order == ORDERBYUSER: |
---|
| | sortkey = pwd.getpwuid(stats[ST_UID])[0] |
---|
| | |
---|
| | # Save into storage |
---|
| | |
---|
| | if sortkey in storage: |
---|
| | storage[sortkey].append(fullname) |
---|
| | else: |
---|
| | storage[sortkey] = [fullname] |
---|
| |
---|
| | # Release the working data structures |
---|
| | |
---|
| | del SeqTypes |
---|
| | |
---|
| | # Now build the special cases of ordering by time within date |
---|
| | # for each of the timestamp types. |
---|
| | |
---|
| | for dateorder, timeorder, year, mon, day in ((ORDERBYADATE, ORDERBYATIME, |
---|
| | FILETIMETOKS[TOKAYEAR], |
---|
| | FILETIMETOKS[TOKAMON], |
---|
| | FILETIMETOKS[TOKADAY]), |
---|
| | |
---|
| | (ORDERBYCDATE, ORDERBYCTIME, |
---|
| | FILETIMETOKS[TOKCYEAR], |
---|
| | FILETIMETOKS[TOKCMON], |
---|
| | FILETIMETOKS[TOKCDAY]), |
---|
| | |
---|
| | (ORDERBYMDATE, ORDERBYMTIME, |
---|
| | FILETIMETOKS[TOKMYEAR], |
---|
| | FILETIMETOKS[TOKMMON], |
---|
| | FILETIMETOKS[TOKMDAY])): |
---|
| | |
---|
| | |
---|
| | lastdate = "" |
---|
| | for fullname in self.SortViews[timeorder]: |
---|
| | |
---|
| | newdate = year[0] % eval("time.localtime(self.RenNames[fullname][STATS][%s]).%s" % (year[1], year[2])) + \ |
---|
| | mon[0] % eval("time.localtime(self.RenNames[fullname][STATS][%s]).%s" % (mon[1], mon[2])) + \ |
---|
| | day[0] % eval("time.localtime(self.RenNames[fullname][STATS][%s]).%s" % (day[1], day[2])) |
---|
| | |
---|
| | |
---|
| | key = dateorder+newdate |
---|
| | |
---|
| | # New file date encountered |
---|
| | |
---|
| | if newdate != lastdate: |
---|
| | |
---|
| | self.DateViews[key] = [fullname] |
---|
| | lastdate = newdate |
---|
| | |
---|
| | # Add file to existing list of others sharing that date |
---|
| | |
---|
| | else: |
---|
| | self.DateViews[key].append(fullname) |
---|
| | |
---|
| | |
---|
| | |
---|
| | # End of '__init__()' |
---|
| | |
---|
| | |
---|
| | ##### |
---|
| |
---|
| | |
---|
| | |
---|
| | # Dump the RenNames and SortView dictionaries |
---|
| | |
---|
| | for i, msg in ((self.RenNames, dRENTARGET), (self.SortViews, dSORTVIEW)): |
---|
| | for i, msg in ((self.RenNames, dRENTARGET), (self.SortViews, dSORTVIEW), (self.DateViews, dDATEVIEW)): |
---|
| | |
---|
| | for j in i: |
---|
| | DumpList(msg, j, i[j]) |
---|
| | |
---|
| |
---|
| | elif r[2] and (r[2][0] == TOKASCEND or r[2][0] == TOKDESCEND): |
---|
| | |
---|
| | # Parse the Sequence Renaming Token into the token itself |
---|
| | # and its corresponding formatting field. |
---|
| | |
---|
| | |
---|
| | # Note that the a legal Sequence Renaming Token will either |
---|
| | # be one of the keys of the SortViews dictionary or one |
---|
| | # of the "ORDERBYnDATE" orderings. |
---|
| | |
---|
| | token = r[2][1:] |
---|
| | |
---|
| | found = False |
---|
| | for seqtoken in self.SortViews: |
---|
| | for seqtoken in self.SortViews.keys() + [ORDERBYADATE, ORDERBYCDATE, ORDERBYMDATE]: |
---|
| | |
---|
| | if token.startswith(seqtoken): |
---|
| | |
---|
| | token, field = token[:len(seqtoken)], token[len(seqtoken):] |
---|
| | found = True |
---|
| | break |
---|
| | |
---|
| | |
---|
| | if not found: |
---|
| | ErrorMsg(eTOKBADSEQ % fullrentoken) |
---|
| | |
---|
| | # Now derive the name of the alphabet to use |
---|
| |
---|
| | # sequence number for the current renaming target |
---|
| | # (which is just the index of that filename in the |
---|
| | # list). |
---|
| | |
---|
| | orderedlist = self.SortViews[token][:] |
---|
| | # One of the standard sorted views requested |
---|
| | |
---|
| | if token in self.SortViews: |
---|
| | orderedlist = self.SortViews[token][:] |
---|
| | |
---|
| | # One of the views sorted within dates requested |
---|
| | |
---|
| | else: |
---|
| | |
---|
| | |
---|
| | if token == ORDERBYADATE: |
---|
| | year, mon, day = FILETIMETOKS[TOKAYEAR], FILETIMETOKS[TOKAMON], FILETIMETOKS[TOKADAY] |
---|
| | |
---|
| | elif token == ORDERBYCDATE: |
---|
| | year, mon, day = FILETIMETOKS[TOKCYEAR], FILETIMETOKS[TOKCMON], FILETIMETOKS[TOKCDAY] |
---|
| | |
---|
| | elif token == ORDERBYMDATE: |
---|
| | year, mon, day = FILETIMETOKS[TOKMYEAR], FILETIMETOKS[TOKMMON], FILETIMETOKS[TOKMDAY] |
---|
| | |
---|
| | targettime = eval("time.localtime(self.RenNames[target][STATS][%s])" % year[1]) |
---|
| | |
---|
| | key = token + \ |
---|
| | year[0] % eval("targettime.%s" % year[2]) + \ |
---|
| | mon[0] % eval("targettime.%s" % mon[2]) + \ |
---|
| | day[0] % eval("targettime.%s" % day[2]) |
---|
| | |
---|
| | orderedlist = self.DateViews[key][:] |
---|
| | |
---|
| | if r[2][0] == TOKDESCEND: |
---|
| | orderedlist.reverse() |
---|
| | |
---|
| |
---|
| | |