psilib /

#! /usr/bin/env python

## Copyright (c) 1999 - 2002 L. C. Rees.  All rights reserved.
## See COPYRIGHT file for license terms.

__title__ = 'makepsi'
__version__ = '0.03-test'
__author__ = 'L.C. Rees ('

'''A command line utility for generating PSI maps. Output can be modified by
passing different options at the command line.'''

# Import mappers
from mappers import Automapper
import sys
from os import path
from psiutils import isarchive
# psyco optimizations
    # psycho is a performance optimizer for pyton
    import psyco
    # Turns on the psyco Just in Time compiler
    from psyco.classes import psymetaclass, psyobj
    # psyco is run on key functions in Automapper
    pl = (Automapper._walkDirectory, Automapper._mapNested,
          Automapper._mapFlat, Automapper._walkFile,
          Automapper._walkHttp, Automapper._walkHtml,
          Automapper._walkFtp, xeval)
    for pi in pl: psyco.bind(pi)
except ImportError: pass

def psimake():
    '''Generates PSI map from CLI'''    

    def quit(message):
        '''Quits and prints help message'''
        # make response lowercase
        message = message.lower()
        # If it's time to quit, exit and pring usage message
        if message == 'q':
            print usage

    def issupported(item):
        '''Verifies an input source is supported'''
        # Verifies that HTTP is supported
        if item.find('http') != -1:
            print 'Processing source...'
            return item
        # Verifies that FTP is supported
        elif item.find('ftp') != -1:
            print 'Processing source...'
            return item
        # Verifies that a file is an archive. If not, it's unsupported
        elif path.isfile(item) and not isarchive(item):
            print "Invalid argument: source must be directory or archive"
        # Default
            print 'Processing source...'
            return item
    def truepath():
        # Ensure usage message doesn't display twice
        try: flag = usageviews
        except NameError: print msg
        # Prompt for archive and destination information
        print 'Invalid argument: no destination specified'
        name2 = raw_input('Make PSI archive? (yes/no): ').lower()
        # Choose archive or raw XML
        if name2 == 'no':
            name2 = raw_input('Enter destination name: ')
            return name2
            name2 = raw_input('Enter archive name: ')
            return name2  
    usage = '''psilib outputs a PSI map of SOURCE to DESTINATION 


    Examples: /home home.psi     # Output PSI map of /home to home.psi -a /home home.psa  # Output PSI map of /home to home.psa
      -a            compress PSI map
      -h            show help on command line options for psilib
      -r            recursively scan children of SOURCE
      -x            embed XHTML from SOURCE inside PSI map
      -z            map contents of archives from SOURCE inside PSI map'''
    # Compression, recursive, archive, and embed HTML flags for automapper 
    com, rec, arc, htm = None, None, None, None
    # Quitting prompt
    msg = 'Press "q" to quit and view online help'
    # Iterate over CLI options passed to makepsi
    for item in sys.argv:
        if item != sys.argv[0]:
            # Quit if found
            if item.find('-h') != -1: quit('q')
            # Compress PSI map if fount
            elif item.find('-a') != -1: com = 1
            # Do a recursive scan if found
            elif item.find('-r') != -1: rec = 'dorec'
            # Scan archive contents if found
            elif item.find('-z') != -1: arc = 'doarc'
            # Embed HTML if found
            elif item.find('-x') != -1: htm = 'dohtm'
            # Extrace source and destination paths
                    if dir(name1): name2 = item
                except NameError: name1 = item
    # Assign input if specified at CLI
    try: src = issupported(name1)
    # Prompt for input name if not specified at CLI
    except NameError:
        print msg
        # Only prompt once for quitting
        usageviews = 1
        print 'Invalid argument: no source specified'
        name1 = raw_input('Enter source name: ')
        # Prompt for recursive search
        search = raw_input('Recursively search datasource? (yes/no): ')
        if search == 'yes': rec = 'dorec'
        src = issupported(name1)
    # Check if compressed XML requested at CLI
    if com == 1:
        try: dest = name2
        except NameError:
            print 'Invalid argument: -a: no archive name found'
            dest = truepath()
    # If compressed XML was not requested, default to raw XML
            if dir(name2): dest = name2
        except NameError: dest = truepath()    
    # Make raw or compressed PSI maps as directed
    if com == 1:
        print 'Generating PSI archive...'
        Automapper(src, dest, rec, arc, htm).toarchive()
        print 'Generating PSI map...'
        Automapper(src, dest, rec, arc, htm).toxml()
    print 'Processing complete'
# Run
if __name__ == '__main__': psimake()