#!/usr/bin/env python # Spider Python Library # Copyright (C) 2006 Health Research Inc. # # HEALTH RESEARCH INCORPORATED (HRI), # ONE UNIVERSITY PLACE, RENSSELAER, NY 12144-3455 # # Email: spider@wadsworth.org # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as # published by the Free Software Foundation; either version 2 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # make file numbers from a list of filenames. First file is the output file! import sys from Spider.Spiderutils import writedoc, filenumber import os def unique(L): " remove duplicates from a list " D = {} for n in L: D[n] = 1 return D.keys() def backup(filename): if os.path.exists(filename): found_vacancy = 0 tiebreaker = 0 shortdir = os.path.basename(os.path.dirname(filename)) while not found_vacancy: test_filename = filename + '_' + str(tiebreaker) if os.path.exists(test_filename): tiebreaker += 1 else: found_vacancy = 1 short_old = os.path.join(shortdir, os.path.basename(filename)) short_new = os.path.join(shortdir, os.path.basename(test_filename)) print 'Renamed', short_old, 'to', short_new os.rename(filename, test_filename) nargs = len(sys.argv[1:]) if nargs < 2: print 'Usage: mkfilenums.py [-f] outdocfile file**pattern ' print "\nmake file numbers from a list of filenames. First argument is the output file!" sys.exit() if sys.argv[1] == '-f' : outfile = sys.argv[2] filelist = sys.argv[3:] backup(outfile) else: outfile = sys.argv[1] filelist = sys.argv[2:] if os.path.exists(outfile): print print 'WARNING!', outfile, 'exists.' print 'Output filename should be the first argument.' print 'Delete pre-existing file, or use a different filename.' print sys.exit() numlist = [] for file in filelist: numlist.append(filenumber(file)) numlist = unique(numlist) # remove duplicates numlist.sort() writedoc(outfile, columns=[numlist]) length = len(numlist) print length, "keys written to", outfile