psilib / makepsi.py

#! /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 (xanimal@users.sf.net)'

'''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
try:
    # psycho is a performance optimizer for pyton
    import psyco
    # Turns on the psyco Just in Time compiler
    #psyco.jit()
    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
            sys.exit()

    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"
            quit('q')
        # Default
        else:
            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()
        quit(name2)
        # Choose archive or raw XML
        if name2 == 'no':
            name2 = raw_input('Enter destination name: ')
            quit(name2)
            return name2
        else:
            name2 = raw_input('Enter archive name: ')
            quit(name2)
            return name2  
    
    usage = '''psilib outputs a PSI map of SOURCE to DESTINATION 

    Usage: psilib.py [OPTION]... SOURCE DESTINATION

    Examples:
      psilib.py /home home.psi     # Output PSI map of /home to home.psi
      psilib.py -a /home home.psa  # Output PSI map of /home to home.psa
    
    OPTION:
      -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
            else:
                try:
                    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'
        quit(name1)
        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
    else:
        try:
            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()
    else:
        print 'Generating PSI map...'
        Automapper(src, dest, rec, arc, htm).toxml()
    print 'Processing complete'
    
# Run
if __name__ == '__main__': psimake()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.