def stat_raw(self, repo_id, obj_id): if not self.s3_client.conn or not self.s3_client.bucket: self.s3_client.do_connect() bucket = self.s3_client.bucket s3_path = '%s/%s' % (repo_id, obj_id) key = Key(bucket=bucket, name=s3_path) key.open_read() return key.size
def test_size_object_in_bucket(): # Create bucket name = create_valid_name() bucket = s3conn.create_bucket(name) # Create object using Swift f = open('/dev/urandom', 'r') data = f.read(random.randint(1,30000)) f.close() swiftconn.put_object(name, 'test', data) k = Key(bucket, 'test') k.open_read() # Check size eq(k.size, swift_size(swiftconn, name, 'test'))
def synth(self,fileName,upload=False): self.toFile('temp.mid') try:os.mkdir('tracks') except:pass print "\tGenerating MP3..." try: p=subprocess.Popen('fluidsynth/fluidsynth.exe -nli -r 44100 -g 1 -o synth.cpu-cores=2 -F temp.raw fluidsynth\Scc1t2.sf2 temp.mid'.split(),stdout=subprocess.PIPE,stderr=subprocess.PIPE) out,err=p.communicate() if p.wait()!=0: print err return False except KeyboardInterrupt: print "Interrupted" os.remove('temp.raw') return False os.remove('temp.mid') try: p=subprocess.Popen(['lame/lame.exe','-S','temp.raw','tracks/%s'%fileName], stdout=subprocess.PIPE, stderr=subprocess.PIPE) out,err=p.communicate() if p.wait()!=0: print err return False except KeyboardInterrupt: print "Interrupted" os.remove('tracks/%s'%fileName) return False os.remove('temp.raw') if upload: def cb(cur,total): progress = int((float(cur)/total*40)) print '\r\t|'+'='*progress+'-'*(40-progress)+'| %d%%'%(progress*2.5), print "\tUploading file to S3...", if not self.population.amusic.s3bucket: self.population.amusic.inits3() k = Key(self.population.amusic.s3bucket) k.key = 'tracks/%s' % fileName if k.exists(): k.open_read() if k.etag and k.compute_md5(open('tracks/'+fileName,'rb'))[0] == k.etag[1:-1]: print "up to date" else: print k.set_contents_from_filename('tracks/'+fileName,cb=cb,num_cb=40,replace=True) print '\n' k.set_acl('public-read')
def file_to_s3(self, filekey, filename): """ Uploads a filename to s3 and checks if file on disk is newer or not. """ k = Key(self.CONN.get_bucket(BUCKET_NAME)) k.key = filekey if k.exists(): k.open_read() last_modified_time_on_s3 = datetime.strptime(k.last_modified[5:], "%d %b %Y %H:%M:%S GMT") last_modified_time_on_disk = datetime.fromtimestamp(os.path.getmtime(filename)) if last_modified_time_on_s3 > last_modified_time_on_disk: print "Skipping %s" % filename return print "Uploading %s to bucket %s" % (filename, BUCKET_NAME) f = open(filename, "rb") filedata = f.read() f.close() self.save_key_value(filekey, filedata, True)
def uploadFileToAmazonS3(inputFullFilename, outputBucketName, outputSubfolderName, outputFilename, AccessID, SecretKey): from boto.s3.key import Key c = boto.connect_s3(AccessID, SecretKey) s3bucket = c.create_bucket(outputBucketName) s3bucket.set_acl('public-read') def cb(cur,total): progress = int((float(cur)/total*40)) print '\r\t|'+'='*progress+'-'*(40-progress)+'| %d%%'%(progress*2.5), print "uploading file %s to S3..." % inputFullFilename, k = Key(s3bucket) k.key = outputSubfolderName + "/" + outputFilename if k.exists(): k.open_read() if k.etag and k.compute_md5(open(inputFullFilename, 'rb'))[0] == k.etag[1:-1]: print "up to date" else: print #todo: maybe skip this if the file is known to be up to date k.set_contents_from_filename(inputFullFilename, cb=cb, num_cb=5, replace=True) print '\n' k.set_acl('public-read')
def synth(self, filename=None, upload=False, generateMP3=False, tempFilename="temp"): if filename == None: filename = self.filename() self.toFile(tempFilename + '.mid') print "\tGenerating..." if os.name == 'nt': fileFolder = os.path.join("..", "..", "..", "..", "amusic_files") else: fileFolder = os.path.join("..", "..", "..", "..", "amusic_files") staticFolder = os.path.join("..", "amusicsite", "polls", "static") fullFilename = os.path.join(staticFolder, 'tracks', filename) try:os.mkdir(os.path.join(staticFolder, 'tracks')) except:pass if not(os.path.isdir(fileFolder)): raise Exception("folder does not exist " + fileFolder) #soundFontFile = os.path.join("fluidsynth", "Scc1t2.sf2") soundFontFile = os.path.join(fileFolder, "WeedsGM3.sf2") #soundFontFile = r"I:\downloads\Saber_5ths_and_3rds\Saber_5ths_and_3rds.sf2" #I:\downloads\BassFing if not(os.path.isfile(soundFontFile)): raise Exception("file does not exist " + soundFontFile) try: if os.name == 'nt': print "using fluidsynth command for Windows" #p=subprocess.Popen('fluidsynth/fluidsynth.exe -nli -r 44100 -g 1 -o synth.cpu-cores=2 -F temp.raw fluidsynth\Scc1t2.sf2 temp.mid'.split(),stdout=subprocess.PIPE,stderr=subprocess.PIPE) commandLine = ('fluidsynth/fluidsynth.exe -nli -r 44100 -g 1 -o synth.cpu-cores=2 -F ' + tempFilename + '.wav ' + soundFontFile + ' ' + tempFilename + '.mid') print commandLine p=subprocess.Popen(commandLine.split(),stdout=subprocess.PIPE,stderr=subprocess.PIPE) else: print "using fluidsynth command for Linux" #p=subprocess.Popen('fluidsynth -nli -r 44100 -g 1 -o synth.cpu-cores=2 -F temp.raw fluidsynth/Scc1t2.sf2 temp.mid'.split(),stdout=subprocess.PIPE,stderr=subprocess.PIPE) commandLine = ('fluidsynth -nli -r 44100 -g 1 -o synth.cpu-cores=2 -F ' + tempFilename + '.wav ' + soundFontFile + ' ' + tempFilename + '.mid') print commandLine p=subprocess.Popen(commandLine.split(),stdout=subprocess.PIPE,stderr=subprocess.PIPE) print "running fluidsynth" out,err=p.communicate() if p.wait()!=0: print err return False print "finished fluidsynth" except KeyboardInterrupt: print "Interrupted" #os.remove('temp.raw') return False #os.remove('temp.mid') try: print os.getcwd() if generateMP3: # mp3 file print "generating:", fullFilename if os.name == 'nt': print "using lame (mp3 codec) command for Windows" #p=subprocess.Popen(['lame/lame.exe','-S','temp.raw','tracks/%s'%filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE) p=subprocess.Popen(['lame/lame.exe','-S',tempFilename + '.wav',fullFilename], stdout=subprocess.PIPE, stderr=subprocess.PIPE) else: print "using lame (mp3 codec) command for Linux" #p=subprocess.Popen(['lame','-S','temp.raw','tracks/%s'%filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE) p=subprocess.Popen(['lame','-S',tempFilename + '.wav',fullFilename], stdout=subprocess.PIPE, stderr=subprocess.PIPE) out,err=p.communicate() if p.wait()!=0: print err return False else: # wave file print fullFilename + ".wav" shutil.copyfile(tempFilename + ".wav", fullFilename + ".wav") except KeyboardInterrupt: print "Interrupted" os.remove(fullFilename) return False #os.remove('temp.raw') if upload: def cb(cur,total): progress = int((float(cur)/total*40)) print '\r\t|'+'='*progress+'-'*(40-progress)+'| %d%%'%(progress*2.5), print "\tUploading file %s to S3..." % filename, if not self.population.amusic.s3bucket: self.population.amusic.inits3() k = Key(self.population.amusic.s3bucket) k.key = 'tracks/%s' % filename if k.exists(): k.open_read() if k.etag and k.compute_md5(open('tracks/'+filename,'rb'))[0] == k.etag[1:-1]: print "up to date" else: print k.set_contents_from_filename('tracks/'+filename,cb=cb,num_cb=40,replace=True) print '\n' k.set_acl('public-read')
def synth(self, filename=None, upload=False, generateMP3=False, tempFilename="temp"): if filename == None: filename = self.filename() self.toFile(tempFilename + '.mid') print "\tGenerating..." #if os.name == 'nt': # fileFolder = os.path.join("..", "..", "..", "..", "amusic_files", "sound_fonts") #else: # fileFolder = os.path.join("..", "..", "..", "..", "amusic_files", "sound_fonts") staticFolder = os.path.join("..", "amusicsite", "polls", "static") fullFilename = os.path.join(staticFolder, 'tracks', filename) try:os.mkdir(os.path.join(staticFolder, 'tracks')) except:pass if not(os.path.isdir(fontFolder)): raise Exception("font folder does not exist " + fontFolder) fontFilename = "WeedsGM3.sf2" #fontFilename = random.choice(os.listdir(fontFolder)) #fontFilename = "1115-Filter_Bass_1.SF2" #if " " in fontFilename: # newFilename = string.replace(fontFilename, " ", "_") # os.rename(os.path.join(fontFolder, fontFilename), os.path.join(fontFolder, newFilename)) # fontFilename = newFilename #soundFontFile = os.path.join("fluidsynth", "Scc1t2.sf2") soundFontFile = os.path.join(fontFolder, fontFilename) #soundFontFile = r"I:\downloads\Saber_5ths_and_3rds\Saber_5ths_and_3rds.sf2" #I:\downloads\BassFing if not(os.path.isfile(soundFontFile)): raise Exception("file does not exist " + soundFontFile) try: if os.name == 'nt': print os.getcwd() print "using fluidsynth command for Windows" #p=subprocess.Popen('fluidsynth/fluidsynth.exe -nli -r 44100 -g 1 -o synth.cpu-cores=2 -F temp.raw fluidsynth\Scc1t2.sf2 temp.mid'.split(),stdout=subprocess.PIPE,stderr=subprocess.PIPE) commandLine = (os.path.join("fluidsynth", "fluidsynth.exe") + ' -nli -r 44100 -g 1 -o synth.cpu-cores=2 -F ' + tempFilename + '.wav ' + soundFontFile + ' ' + tempFilename + '.mid') print commandLine #p=subprocess.Popen(commandLine.split(),stdout=subprocess.PIPE,stderr=subprocess.PIPE) else: print "using fluidsynth command for Linux" #p=subprocess.Popen('fluidsynth -nli -r 44100 -g 1 -o synth.cpu-cores=2 -F temp.raw fluidsynth/Scc1t2.sf2 temp.mid'.split(),stdout=subprocess.PIPE,stderr=subprocess.PIPE) commandLine = ('fluidsynth -nli -r 44100 -g 1 -o synth.cpu-cores=2 -F ' + tempFilename + '.wav ' + soundFontFile + ' ' + tempFilename + '.mid') print commandLine #p=subprocess.Popen(commandLine.split(),stdout=subprocess.PIPE,stderr=subprocess.PIPE) print "running fluidsynth" os.system(commandLine) #out,err=p.communicate() #if p.wait()!=0: # print err # return False print "finished fluidsynth" except KeyboardInterrupt: print "Interrupted" #os.remove('temp.raw') return False #os.remove('temp.mid') try: #print os.getcwd() if generateMP3: # mp3 file print "generating:" print os.path.join(os.getcwd(), fullFilename) if os.name == 'nt': print "using lame (mp3 codec) command for Windows" #p=subprocess.Popen(['lame/lame.exe','-S','temp.raw','tracks/%s'%filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE) p=subprocess.Popen(['lame/lame.exe','-S',tempFilename + '.wav',fullFilename], stdout=subprocess.PIPE, stderr=subprocess.PIPE) else: print "using lame (mp3 codec) command for Linux" #p=subprocess.Popen(['lame','-S','temp.raw','tracks/%s'%filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE) p=subprocess.Popen(['lame','-S',tempFilename + '.wav',fullFilename], stdout=subprocess.PIPE, stderr=subprocess.PIPE) out,err=p.communicate() if p.wait()!=0: print err return False else: # wave file print os.path.join(os.getcwd(), fullFilename + ".wav") shutil.copyfile(tempFilename + ".wav", fullFilename + ".wav") except KeyboardInterrupt: print "Interrupted" os.remove(fullFilename) return False #os.remove('temp.raw') if upload: def cb(cur,total): progress = int((float(cur)/total*40)) print '\r\t|'+'='*progress+'-'*(40-progress)+'| %d%%'%(progress*2.5), print "\tUploading file %s to S3..." % filename, if not self.population.amusic.s3bucket: self.population.amusic.inits3() k = Key(self.population.amusic.s3bucket) k.key = 'tracks/%s' % filename if k.exists(): k.open_read() if k.etag and k.compute_md5(open('tracks/'+filename,'rb'))[0] == k.etag[1:-1]: print "up to date" else: print k.set_contents_from_filename('tracks/'+filename,cb=cb,num_cb=40,replace=True) print '\n' k.set_acl('public-read')