#!/usr/bin/env python # RandPod version 0.2, 2005Jul21 # Copyright 2005 by Mark Damon Hughes # Use and modify freely, but do not redistribute, always refer others # back to import os, shutil, sys from random import randint from os.path import join, getsize from Tkinter import * NODESIZE=4096 class RandPod: def __init__(self, root): # path self.pathvar = StringVar() Label(root, text="Path:").grid(row=0, column=0, sticky=W) Entry(root, width=80, textvariable=self.pathvar).grid(row=0, column=1, sticky=W) self.pathvar.set(os.getcwd()) # size self.sizevar = StringVar() Label(root, text="Max Size (MB):").grid(row=1, column=0, sticky=W) Entry(root, width=6, textvariable=self.sizevar).grid(row=1, column=1, sticky=W) self.sizevar.set("512") # dest self.destvar = StringVar() Label(root, text="Destination:").grid(row=2, column=0, sticky=W) Entry(root, width=80, textvariable=self.destvar).grid(row=2, column=1, sticky=W) self.destvar.set(os.getcwd()) # command buttons buttonFrame = Frame(root) Button(buttonFrame, text="Randomize", command=self.randomizeButton).pack(side=LEFT) Button(buttonFrame, text="Copy", command=self.copyButton).pack(side=LEFT) Button(buttonFrame, text="Exit", command=self.exitButton).pack(side=LEFT) buttonFrame.grid(row=3, column=0, columnspan=2, sticky=W) # output self.chosen = [] outputFrame = Frame(root) self.output = Text(outputFrame, width=120, height=16) scroll = Scrollbar(outputFrame, command=self.output.yview) self.output.configure(yscrollcommand=scroll.set) self.output.pack(side=LEFT) scroll.pack(side=RIGHT, fill=Y) outputFrame.grid(row=4, column=0, columnspan=2, sticky=W) # status self.labelvar = StringVar() Label(root, textvariable=self.labelvar).grid(row=5, column=0, columnspan=2, sticky=W) self.labelvar.set("") root.title("RandPod") def randomizeButton(self, *args): path = self.pathvar.get() maxnodes = int(self.sizevar.get()) * 1024 * 1024 // NODESIZE dest = self.destvar.get() self.chosen = randpod(maxnodes, path) self.output.delete("0.0", END) for pathname in self.chosen: self.output.insert(END, pathname+"\n") self.labelvar.set("Selected %d random files" % (len(self.chosen),) ) def copyButton(self, *args): dest = self.destvar.get() docopy(self.chosen, dest) self.labelvar.set("Copied %d files!" % (len(self.chosen),) ) def exitButton(self, *args): sys.exit(0) #________________________________________ def randomize(list): """Randomly shuffles the contents of list and returns the list. As maximally shuffled as possible.""" size = len(list) for i in range(size): a = randint(0, size-1) tmp = list[i] list[i] = list[a] list[a] = tmp def randpod(maxnodes, path): """Returns a list of randomly-chosen files under path which use less than maxnodes. maxnodes: Maximum number of nodes (4K each). path: Starting path. """ list = [] # := [pathname, size] for root, dirs, files in os.walk(path): for filename in files: if filename.endswith(".mp3"): pathname = join(root, filename) nodes = (getsize(pathname) + NODESIZE-1) // NODESIZE list.append( [pathname, nodes] ) randomize(list) total = 0 chosen = [] # := pathname for pathname, nodes in list: if total + nodes <= maxnodes: chosen.append(pathname) total += nodes if total >= maxnodes: break return chosen def docopy(chosen, dest): """Copies pathnames listed in chosen (a list) to directory dest.""" if not os.path.isdir(dest): os.makedirs(dest) for pathname in chosen: print pathname shutil.copy(pathname, dest) #________________________________________ def main(argv): if len(sys.argv) == 1: # GUI user root = Tk() randpod = RandPod(root) root.mainloop() elif len(sys.argv) == 4: # command line user maxnodes = int(sys.argv[1]) * 1024 * 1024 // NODESIZE path = sys.argv[2] dest = sys.argv[3] chosen = randpod(maxnodes, path) docopy(chosen, dest) else: print >>sys.stderr, "Usage: randpod.py MAXSIZE(MB) PATH DEST" sys.exit(1) #________________________________________ if __name__ == "__main__": main(sys.argv)