inicio mail me! sindicaci;ón

Hypelocate

I wrote a small app called hypelocate to test hype, it’s a clone of unix “locate” tool. You must install hype and my patch (the first, not the second one) to the hype source tree to use it.

You can find the whole app here: http://www.oluyede.org/files/hypelocate-0.5.tar.gz

You can find more informations about hyper in my older post: Hype, the Python indexer

Here are the supported options:

rhymes@voodoo:~/hypelocate $ python hypelocate.py
usage: hypelocate.py [options]

HypeLocate 0.5 - Released 2005-12-10  Copyright (c) 2005, Lawrence Oluyede

options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -u                    Create database starting at path /.
  -U <dir>              Create database starting at path <dir>.
  -n <num>              Limit the amount of results shown to <num>.
  -o <dir>, --output=<dir>
                        Specifies the database to create.
  -d <path>, --database=<path>
                        Specifies the path of databases to search in.
  -v, --verbose         Verbose mode. Display files when creating database.

and this is the main file:

  1. #!/usr/bin/env python
  2.  
  3. from optparse import OptionParser, OptionValueError
  4. import os
  5. import fnmatch
  6. import sys
  7. import string
  8.  
  9. import hype
  10.  
  11. import conf
  12.  
  13. class HypeLocate(object):
  14.     def __init__(self, db_path=None):
  15.         self.db_path = db_path or conf.DB_PATH
  16.         self.max_results = False
  17.    
  18.     def _open_db(self, flags=None):
  19.         try:
  20.             os.mkdir(self.db_path)
  21.         except OSError:
  22.             pass
  23.  
  24.         if flags:
  25.             return hype.Database(self.db_path, flags)
  26.    
  27.         return hype.Database(self.db_path)
  28.  
  29.     def _remove_non_existent(self, db):
  30.         for doc in db.get_documents():
  31.             if not os.path.exists(doc[“@uri”]):
  32.                 db.remove(doc)
  33.  
  34.     def _all_files(self, root, patterns=“*”, single_level=False, yield_folders=True):
  35.         “”” Walks through subdirectories yielding one file (or folder) at time.
  36.    
  37.         root: the root dir to start at
  38.         patterns: which kind of file to catch, es: *.py;*.gif [defaults to anything]
  39.         single_level: True if you don’t want to walk in subdirs [defaults to False]
  40.         yield_folders: True if you want folders in retuns in addition of files [defaults to True]
  41.         ““”
  42.         patterns = patterns.split(“;”)
  43.  
  44.         for path, subdirs, files in os.walk(root):
  45.             if yield_folders:
  46.                 files.extend(subdirs)
  47.                 files.sort()
  48.             for name in files:
  49.                 for pattern in patterns:
  50.                     if fnmatch.fnmatch(name, pattern):
  51.                         yield os.path.abspath(os.path.join(path, name))
  52.                         break
  53.  
  54.             if single_level:
  55.                 break
  56.    
  57.     def updatedb(self, root=“.”, remove=True, verbose=False):
  58.         “”” Generate the index of files starting at root.
  59.    
  60.         root: where to start the indexing [defaults to current dir]
  61.         remove: True if you want to remove non existent paths, False otherwise [defaults to True]
  62.         verbose: True if you want to display files when creating database, False otherwise [defaults to False]
  63.         ““”
  64.         num_docs = 0
  65.        
  66.         try:
  67.             db = self._open_db()
  68.                
  69.             if remove:
  70.                 self._remove_non_existent(db)
  71.        
  72.             for index, name in enumerate(self._all_files(root)):
  73.                 doc = hype.Document()
  74.                 if verbose:
  75.                     print name
  76.                 doc[“@uri”] = name
  77.                 doc.add_text(name)
  78.                 db.put_doc(doc)
  79.    
  80.             num_docs = len(db)
  81.         finally:
  82.             try:
  83.                 db.close()
  84.             except:
  85.                 pass
  86.    
  87.         return num_docs
  88.  
  89.     def locate(self, search_string, remove=True, db_path=None):
  90.         “”” Search through the index the given search string.
  91.    
  92.         search_string: what to search in the index
  93.         remove: remove: True if you want to remove non existent paths, False otherwise [defaults to True]
  94.         db_path: an alternative db to search against. [defaults to None]
  95.         ““”
  96.         db = docs = None
  97.         try:
  98.             if db_path:
  99.                 self.db_path = db_path
  100.  
  101.             db = self._open_db()
  102.    
  103.             if remove:
  104.                 self._remove_non_existent(db)
  105.    
  106.             searcher = db.search(search_string).order(“@uri STRD”)
  107.             list(searcher)
  108.        
  109.             docs = [doc.uri for doc in searcher if doc]
  110.         finally:
  111.             try:
  112.                 db.close()
  113.             except:
  114.                 pass
  115.    
  116.         return docs
  117.    
  118.     def display_results(self, search_strings, db_path=None):
  119.         “”” Concatenate the results of multiple invocations of locate() and
  120.         print them to the standard output.
  121.    
  122.         search_strings: a list of item to search
  123.         db_path: an alternative db to search against. [defaults to None]
  124.         ““”
  125.  
  126.         for search_string in search_strings:
  127.             for index, doc in enumerate(self.locate(search_string, db_path)):
  128.                 if self.max_results:
  129.                     if self.max_results == index:
  130.                         return
  131.                 print doc
  132.  
  133. def main():
  134.     description = “”“$prog_name $version - Released $release_date
  135. Copyright (c) 2005, $author”“”
  136.     desc_template = string.Template(description)
  137.     description = desc_template.substitute({“prog_name”: conf.PROG_NAME,
  138.                                             “version”: conf.VERSION,
  139.                                             “release_date”: conf.RELEASE_DATE,
  140.                                             “author”: conf.AUTHOR})
  141.     version = “%s %s” % (conf.PROG_NAME, conf.VERSION)
  142.     parser = OptionParser(description=description, version=version)
  143.    
  144.     parser.set_default(“start_root”, False)
  145.     parser.add_option(“-u”, action=“store_true”, dest=“start_at_root”,
  146.                       help=“Create database starting at path /.”)
  147.    
  148.     parser.add_option(“-U”, dest=“start_path”, metavar=“<dir>”,
  149.                       help=“Create database starting at path <dir>.”)
  150.    
  151.     parser.add_option(“-n”, dest=“max_results”, type=“int”, metavar=“<num>”,
  152.                       help=“Limit the amount of results shown to <num>.”)
  153.    
  154.     parser.add_option(“-o”, “–output”, dest=“out_db”, metavar=“<dir>”,
  155.                       help=“Specifies the database to create.”)
  156.    
  157.     parser.add_option(“-d”, “–database”, dest=“db_path”, metavar=“<path>”,
  158.                       help=“Specifies the path of databases to search in.”)
  159.    
  160.     parser.add_option(“-v”, “–verbose”, action=“store_true”, dest=“verbose”,
  161.                       help=“Verbose mode. Display files when creating database.”)
  162.    
  163.     if len(sys.argv) < 2:
  164.         parser.print_help()
  165.         return   
  166.    
  167.     options, args = parser.parse_args()
  168.    
  169.     finder = HypeLocate()
  170.  
  171.     if options.out_db:
  172.         if not parser.values.start_at_root and not parser.values.start_path:
  173.             parser.error(
  174.                 “Must specify an ‘Update’ database option first.”)
  175.        
  176.         finder.db_path = options.out_db
  177.    
  178.     if options.max_results:
  179.         finder.max_results = options.max_results
  180.    
  181.     if options.start_at_root and options.start_path:
  182.         parser.error(“Options -u and -U are mutually exclusive”)
  183.        
  184.     if options.start_path:
  185.         # index at start_path
  186.         finder.updatedb(options.start_path, verbose=options.verbose)
  187.     elif options.start_at_root:
  188.         # index the whole fs tree
  189.         finder.updatedb(“/”, verbose=options.verbose)
  190.    
  191.     finder.display_results(args, options.db_path)
  192.  
  193. if __name__ == “__main__”:
  194.     main()

UPDATE: - hypelocate 0.5.1 released in sync with the latest Hype revision number. Fixes a couple of bugs in the handling of the db and uses unicode everywhere. download

Related posts

  • No related posts
  • Leave a Comment