<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: find code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 01:14:50 GMT</pubDate>
    <description>DZone Snippets: find code</description>
    <item>
      <title>Linux - Change File/Directory Permissions Separately</title>
      <link>http://snippets.dzone.com/posts/show/5569</link>
      <description>Change permission of all files or all directories separately from a set starting point.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;// Directories:&lt;br /&gt;find . -type d -exec chmod XXX {} \;&lt;br /&gt;&lt;br /&gt;// Files:&lt;br /&gt;find . -type f -exec chmod XXX {} \;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 02 Jun 2008 10:21:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5569</guid>
      <author>k1mmeh (Kim Chirnside)</author>
    </item>
    <item>
      <title>Perform a Rails find() and iterate over the resulting records in groups</title>
      <link>http://snippets.dzone.com/posts/show/5461</link>
      <description>&lt;code&gt;&lt;br /&gt;module ActiveRecord&lt;br /&gt;  class Base&lt;br /&gt;    # This method lets you iterate over the results of a .find, in groups.&lt;br /&gt;    # (Basically an interface to LIMIT.)&lt;br /&gt;    # Anything you can pass as options to .find, you can pass here. &lt;br /&gt;    # Example 1:&lt;br /&gt;    #   Order.each_by(100, :conditions =&gt; { :cc_processed_at =&gt; nil }) do |order|&lt;br /&gt;    #     # do stuff with order&lt;br /&gt;    #   end&lt;br /&gt;    # Example 2:&lt;br /&gt;    #   Person.each_by(50, :order =&gt; 'name') do |person, index|&lt;br /&gt;    #     # do stuff with person and index&lt;br /&gt;    #   end&lt;br /&gt;    # Pass :update =&gt; true in the options to print a message before each group is&lt;br /&gt;    # fetched from the db.&lt;br /&gt;    #&lt;br /&gt;    # Author: Elliot Winkler &lt;elliot.winkler@gmail.com&gt;&lt;br /&gt;    # Source: http://snippets.dzone.com/posts/show/5461&lt;br /&gt;    def self.each_by(group_size, options={}, &amp;blk)&lt;br /&gt;      update = options.delete(:update) || false&lt;br /&gt;      num_records = count(options.except(:from))&lt;br /&gt;      return 0 if num_records == 0&lt;br /&gt;      #raise "Number of records: #{num_records}"&lt;br /&gt;      also_pass_offset = (blk.arity == 2)&lt;br /&gt;      0.step(num_records, group_size) do |offset|&lt;br /&gt;        find_options = { :offset =&gt; offset, :limit =&gt; group_size }.merge(options)&lt;br /&gt;        if update&lt;br /&gt;          if num_records == 1&lt;br /&gt;            puts "&gt;&gt; Reading the only record."&lt;br /&gt;          else&lt;br /&gt;            start_offset = offset + 1&lt;br /&gt;            end_offset   = offset + group_size&lt;br /&gt;            end_offset   = num_records if num_records &lt; end_offset&lt;br /&gt;            puts "&gt;&gt; Reading records #{start_offset}-#{end_offset}."&lt;br /&gt;          end&lt;br /&gt;        end &lt;br /&gt;        find(:all, find_options).each do |record|&lt;br /&gt;          also_pass_offset ? blk.call(record, offset) : blk.call(record)&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;      num_records&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Also see:&lt;br /&gt;&lt;br /&gt;* http://weblog.jamisbuck.org/2007/4/6/faking-cursors-in-activerecord&lt;br /&gt;* http://weblog.jamisbuck.org/2007/2/28/poor-man-s-pagination</description>
      <pubDate>Mon, 05 May 2008 15:35:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5461</guid>
      <author>mcmire ()</author>
    </item>
    <item>
      <title>Simple File.find</title>
      <link>http://snippets.dzone.com/posts/show/5457</link>
      <description>&lt;code&gt;&lt;br /&gt;# Simple File.find by c00lryguy&lt;br /&gt;# Thanks to justinwr for adding what I forgot to do&lt;br /&gt;# ------------------------------&lt;br /&gt;# Usage: &lt;br /&gt;#     * = wildcard in filename&lt;br /&gt;#   File.find("E:\\") =&gt; All files in E:\&lt;br /&gt;#   File.find("E:\\Ruby", "*.rb") =&gt; All .rb files in E:\Ruby&lt;br /&gt;#   File.find("E:\\", "*.rb", false) =&gt; All .rb files in E:\, but not in its subdirs&lt;br /&gt;class File&lt;br /&gt;  def self.find(dir, filename="*.*", subdirs=true)&lt;br /&gt;    Dir[ subdirs ? File.join(dir.split(/\\/), "**", filename) : File.join(dir.split(/\\/), filename) ]&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 03 May 2008 18:56:28 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5457</guid>
      <author>c00lryguy (c00lryguy@gmail.com)</author>
    </item>
    <item>
      <title>Creating a bucket in Amazon S3 through an irb session</title>
      <link>http://snippets.dzone.com/posts/show/5441</link>
      <description>1) Log into an irb session, and enter your S3 login details.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'rubygems'&lt;br /&gt;require 'aws/s3'&lt;br /&gt;&lt;br /&gt;  AWS::S3::Base.establish_connection!(&lt;br /&gt;    :access_key_id     =&gt; 'REPLACE_ME',&lt;br /&gt;    :secret_access_key =&gt; 'REPLACE_ME'&lt;br /&gt;  )&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;output:&lt;br /&gt;=&gt; #&lt;AWS::S3::Connection:0xb75e0594 @http=#&lt;Net::HTTP s3.amazonaws.com:80 open=false&gt;, @secret_access_key="", @options={:server=&gt;"s3.amazonaws.com", :access_key_id=&gt;"", :port=&gt;80, :secret_access_key=&gt;"", :persistent=&gt;true}, @access_key_id="19S45GYAGWK8DC2B8VG2"&gt;&lt;br /&gt;&lt;br /&gt;2) Browse the existing buckets.&lt;br /&gt;&lt;code&gt;AWS::S3::Service.buckets&lt;/code&gt;&lt;br /&gt;output:&lt;br /&gt;=&gt; [#&lt;AWS::S3::Bucket:0xb75cc850 @object_cache=[], @attributes={"name"=&gt;"ogg.twitteraudio.com", "creation_date"=&gt;Sat Apr 26 10:40:16 UTC 2008}&gt;, #&lt;AWS::S3::Bucket:0xb75cc83c @object_cache=[], @attributes={"name"=&gt;"t1000", "creation_date"=&gt;Fri Apr 25 21:35:21 UTC 2008}&gt;, #&lt;AWS::S3::Bucket:0xb75cc814 @object_cache=[], @attributes={"name"=&gt;"t2000", "creation_date"=&gt;Fri Apr 25 21:53:15 UTC 2008}&gt;]&lt;br /&gt;&lt;br /&gt;3) Browse the buckets in a programmatical way.&lt;br /&gt;&lt;code&gt;AWS::S3::Service.buckets.each {|b| puts b.name}&lt;/code&gt;&lt;br /&gt;output:&lt;br /&gt;ogg.twitteraudio.com&lt;br /&gt;t1000&lt;br /&gt;t2000&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;4) Add a new bucket called t3000.&lt;br /&gt;&lt;code&gt;AWS::S3::Bucket.create('t3000')&lt;/code&gt;&lt;br /&gt;output:&lt;br /&gt;=&gt; true&lt;br /&gt;&lt;br /&gt;5) Observe adding the bucket again doesn't cause an error.&lt;br /&gt;&lt;code&gt;AWS::S3::Bucket.create('t3000')&lt;/code&gt;&lt;br /&gt;output:&lt;br /&gt;=&gt; true&lt;br /&gt;&lt;br /&gt;6) View the buckets again. &lt;br /&gt;&lt;code&gt;AWS::S3::Service.buckets&lt;/code&gt;&lt;br /&gt;output:&lt;br /&gt;=&gt; [#&lt;AWS::S3::Bucket:0xb75cc850 @object_cache=[], @attributes={"name"=&gt;"ogg.twitteraudio.com", "creation_date"=&gt;Sat Apr 26 10:40:16 UTC 2008}&gt;, #&lt;AWS::S3::Bucket:0xb75cc83c @object_cache=[], @attributes={"name"=&gt;"t1000", "creation_date"=&gt;Fri Apr 25 21:35:21 UTC 2008}&gt;, #&lt;AWS::S3::Bucket:0xb75cc814 @object_cache=[], @attributes={"name"=&gt;"t2000", "creation_date"=&gt;Fri Apr 25 21:53:15 UTC 2008}&gt;]&lt;br /&gt;&lt;br /&gt;Note: You would expect t3000 to be in there however it didn't appear possibly because of the bucket permissions.&lt;br /&gt;&lt;br /&gt;7) Let's then look for bucket t3000.&lt;br /&gt;&lt;code&gt;t3000 = AWS::S3::Bucket.find('t3000')&lt;/code&gt;&lt;br /&gt;output:&lt;br /&gt;=&gt; #&lt;AWS::S3::Bucket:0xb76df724 @object_cache=[], @attributes={"prefix"=&gt;nil, "name"=&gt;"t3000", "marker"=&gt;nil, "max_keys"=&gt;1000, "is_truncated"=&gt;false, "xmlns"=&gt;"http://s3.amazonaws.com/doc/2006-03-01/"}&gt;&lt;br /&gt;&lt;br /&gt;8) Now that we've found the bucket let's upload a text file called works.txt.&lt;br /&gt;&lt;code&gt;file = "works.txt"&lt;/code&gt;&lt;br /&gt;output:&lt;br /&gt;=&gt; "works.txt"&lt;br /&gt;&lt;code&gt;AWS::S3::S3Object.store(file, open(file), 't3000', :access =&gt; :public_read)&lt;/code&gt;&lt;br /&gt;output:&lt;br /&gt;=&gt; #&lt;AWS::S3::S3Object::Response:0x-608926458 200 OK&gt;&lt;br /&gt;&lt;br /&gt;9) Setting the file access to :public_read allows us to view the file from the http location http://t3000.s3.amazonaws.com/works.txt&lt;br /&gt;&lt;br /&gt;References: &lt;br /&gt;&lt;a href="http://amazon.rubyforge.org/"&gt;http://amazon.rubyforge.org/&lt;/a&gt;&lt;br /&gt;&lt;a href="http://snippets.dzone.com/posts/show/4088"&gt;upload_to_s3 - Ruby S3 upload client&lt;/a&gt; [dzone.com]&lt;br /&gt;&lt;br /&gt;*update: 14:30 30 April 2008 *&lt;br /&gt;I didn't use Bucket.objects(:reload) which is the reason why the bucket t3000 didn't show up with the statement Service.buckets&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://spattendesign.com/2007/12/5/amazon-s3-ruby-and-rails-slides"&gt;spatten design - Amazon S3, Ruby and Rails slides&lt;/a&gt; [spattendesign.com]</description>
      <pubDate>Tue, 29 Apr 2008 17:20:25 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5441</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>process email files like unix find</title>
      <link>http://snippets.dzone.com/posts/show/5248</link>
      <description>I call this program whitelist. It lets you run a command on a bunch of files depending on whether the file is an email and has a from address in a whitelist.&lt;br /&gt;&lt;br /&gt;It's useful for maintaining whitelisted mailboxes and analysing mailboxes. With a few more tests it might be a generically useful tool.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/python&lt;br /&gt;# Copyright (C) 2008 by Tapsell-Ferrier Limited&lt;br /&gt;&lt;br /&gt;# This program is free software; you can redistribute it and/or modify&lt;br /&gt;# it under the terms of the GNU General Public License as published by&lt;br /&gt;# the Free Software Foundation; either version 2, or (at your option)&lt;br /&gt;# any later version.&lt;br /&gt;&lt;br /&gt;# This program is distributed in the hope that it will be useful,&lt;br /&gt;# but WITHOUT ANY WARRANTY; without even the implied warranty of&lt;br /&gt;# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the&lt;br /&gt;# GNU General Public License for more details.&lt;br /&gt;&lt;br /&gt;# You should have received a copy of the GNU General Public License&lt;br /&gt;# along with this program; see the file COPYING.  If not, write to the&lt;br /&gt;# Free Software Foundation, Inc.,   51 Franklin Street, Fifth Floor,&lt;br /&gt;# Boston, MA  02110-1301  USA&lt;br /&gt;&lt;br /&gt;import commands&lt;br /&gt;import email.Parser&lt;br /&gt;import sys&lt;br /&gt;import re&lt;br /&gt;import getopt&lt;br /&gt;import os&lt;br /&gt;import os.path&lt;br /&gt;&lt;br /&gt;try:&lt;br /&gt;    from email.utils import parseaddr&lt;br /&gt;except:&lt;br /&gt;    from rfc822 import parseaddr&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;def help():&lt;br /&gt;    print """whitelist.py -h&lt;br /&gt;whitelist.py [-v] [-f whitelist filename] command ; filelist [-]&lt;br /&gt;&lt;br /&gt;Execute the specified command (which must be shell escaped if calling&lt;br /&gt;from shell) on all the files in the filelist or, if - is present in&lt;br /&gt;the filelist, read from stdin (like xargs) whenever the file is an&lt;br /&gt;email that contains a from address specified in the whitelist.&lt;br /&gt;&lt;br /&gt;Like xargs, or find, the command can include {} as a replacement token&lt;br /&gt;for the matched filename.&lt;br /&gt;&lt;br /&gt;The command can also be a header reference, for example:&lt;br /&gt;&lt;br /&gt;  $FROM&lt;br /&gt;&lt;br /&gt;will print the specified mails From address.&lt;br /&gt;&lt;br /&gt;Options:&lt;br /&gt;&lt;br /&gt; -v   specifies that the test is to be negated, executing the action if&lt;br /&gt;      the file does NOT contain a from address in the whiltelist.&lt;br /&gt;&lt;br /&gt; -f   specifies a whitelist, the default is $HOME/.addresses&lt;br /&gt;&lt;br /&gt;For example:&lt;br /&gt;&lt;br /&gt; whitelist.py -f .wlist wc \{} \: maildir/cur/*&lt;br /&gt;&lt;br /&gt;runs wc on each file in maildir/cur with a FROM address matching&lt;br /&gt;something in the whitelist; or:&lt;br /&gt;&lt;br /&gt; find maildir/INBOX/cur -type f | whitelist.py -v mv \{} mailbox/TRASH/cur \; -&lt;br /&gt;&lt;br /&gt;mv's all files in the INBOX with FROMs not matching the whitelist into&lt;br /&gt;a TRASH folder.&lt;br /&gt;&lt;br /&gt;  find maildir/Greylist/new -type f | whitelist.py -v $TO \; -&lt;br /&gt;&lt;br /&gt;displays the TO address of all messages where the from didn't match&lt;br /&gt;the whitelist.&lt;br /&gt;"""&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;def read_whitelisted(filename):&lt;br /&gt;    fd = open(filename)&lt;br /&gt;    data = fd.read()&lt;br /&gt;    fd.close()&lt;br /&gt;    return data.split()&lt;br /&gt;&lt;br /&gt;def get_msg(filename):&lt;br /&gt;    fd = open(filename)&lt;br /&gt;    try:&lt;br /&gt;        msg = email.Parser.HeaderParser().parse(fd, True)&lt;br /&gt;        return msg&lt;br /&gt;    finally:&lt;br /&gt;        fd.close()&lt;br /&gt;&lt;br /&gt;action_re = re.compile("\{}")&lt;br /&gt;&lt;br /&gt;def handle(filenames_fn, action, whitelist, negate=False):&lt;br /&gt;    for filename in filenames_fn():&lt;br /&gt;        msg = get_msg(filename)&lt;br /&gt;        realname, addr = parseaddr(msg["from"])&lt;br /&gt;        result = addr in whitelist&lt;br /&gt;&lt;br /&gt;        if negate:&lt;br /&gt;            result = not result&lt;br /&gt;&lt;br /&gt;        if result:&lt;br /&gt;            try:&lt;br /&gt;                m = re.match("\$(.+)", action)&lt;br /&gt;                result = msg[m.group(1)]&lt;br /&gt;            except Exception:&lt;br /&gt;                cmd_str = action_re.sub(filename, action)&lt;br /&gt;                os.system(cmd_str)&lt;br /&gt;            else:&lt;br /&gt;                print result&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;def main(args):&lt;br /&gt;    negate = False&lt;br /&gt;    whitelist_filename = os.path.join(os.environ["HOME"], ".addresses")&lt;br /&gt;    opts, args = getopt.getopt(args, "hv")&lt;br /&gt;    for o,a in opts:&lt;br /&gt;        if o == "-h":&lt;br /&gt;            help()&lt;br /&gt;            sys.exit(0)&lt;br /&gt;&lt;br /&gt;        elif o == "-v":&lt;br /&gt;            negate = True&lt;br /&gt;&lt;br /&gt;        elif o == "-f":&lt;br /&gt;            whitelist_filename = a&lt;br /&gt;&lt;br /&gt;    if not os.access(whitelist_filename, os.F_OK):&lt;br /&gt;        print &gt;&gt;sys.stderr, "whitelist.py   -  no whitelist filename\n"&lt;br /&gt;        help()&lt;br /&gt;        sys.exit(1)&lt;br /&gt;&lt;br /&gt;    cmdstr = " ".join(args)&lt;br /&gt;    m = re.match("(.*) ;([ ]*.*)", cmdstr)&lt;br /&gt;    if not m:&lt;br /&gt;        sys.exit(1)&lt;br /&gt;&lt;br /&gt;    cmd = m.group(1)&lt;br /&gt;    files = m.group(2).strip().split(" ")&lt;br /&gt;&lt;br /&gt;    def ffn():&lt;br /&gt;        for f in files:&lt;br /&gt;            if f == "-":&lt;br /&gt;                for innerf in sys.stdin:&lt;br /&gt;                    yield innerf.strip()&lt;br /&gt;            else:&lt;br /&gt;                yield f&lt;br /&gt;        return&lt;br /&gt;&lt;br /&gt;    whitelist = read_whitelisted(whitelist_filename)&lt;br /&gt;    handle(ffn, cmd, whitelist, negate)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;if __name__ == "__main__":&lt;br /&gt;    main(sys.argv[1:])&lt;br /&gt;&lt;br /&gt;# End&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 18 Mar 2008 16:54:19 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5248</guid>
      <author>nicferrier (Nic Ferrier)</author>
    </item>
    <item>
      <title>Parsing Errors on Command Line</title>
      <link>http://snippets.dzone.com/posts/show/4998</link>
      <description>Get PHP parsing errors on command line. Useful for those extreme cases where you can't get them to print to the browser.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;find . -name \*.php \! -exec php -l {} \;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 17 Jan 2008 22:35:17 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4998</guid>
      <author>seanmurphy (Sean Murphy)</author>
    </item>
    <item>
      <title>Shell script to recursively find files with the same name and replace text within each of them</title>
      <link>http://snippets.dzone.com/posts/show/4808</link>
      <description>This shell script recursively finds files with the same name and replaces text within each of them&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;FILE="filename.txt"&lt;br /&gt;FIND="old text"&lt;br /&gt;REPLACE="new text"&lt;br /&gt;find . -name $FILE -print0 | xargs -0p perl -pi -w -e "s/$FIND/$REPLACE/g;"&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Remove the p flag if confirmation is not necessary.</description>
      <pubDate>Wed, 21 Nov 2007 18:43:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4808</guid>
      <author>davebryand (Dave Bryand)</author>
    </item>
    <item>
      <title>find all files in a directory based on exclusion rather than inclusion..</title>
      <link>http://snippets.dzone.com/posts/show/4669</link>
      <description>&lt;code&gt;&lt;br /&gt;find . -type f ! -name "*xyz*"&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 18 Oct 2007 19:31:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4669</guid>
      <author>sikelianos (Zeke Sikelianos)</author>
    </item>
    <item>
      <title>find and replace text from the shell</title>
      <link>http://snippets.dzone.com/posts/show/4645</link>
      <description>Snagged from http://snippets.dzone.com/posts/show/116&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;find . -name '*.txt' -print0 |xargs -0 perl -pi -e 's/find/replace/g'&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 12 Oct 2007 19:02:38 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4645</guid>
      <author>sikelianos (Zeke Sikelianos)</author>
    </item>
    <item>
      <title>find line in text file, add to another text file</title>
      <link>http://snippets.dzone.com/posts/show/4262</link>
      <description>// Batch - search .txt / .csv / etc using argument, return matches to .txt / .csv / etc file&lt;br /&gt;// skips the ---------- foo.csv returned by find alone&lt;br /&gt;&lt;code&gt;&lt;br /&gt;find /i "%1" foo.csv | find /i "%1" &gt;&gt; bar.txt&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 05 Jul 2007 12:03:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4262</guid>
      <author>thefamousnomo (Robin)</author>
    </item>
  </channel>
</rss>
