Пример #1
0
def main():
    """ pystat's main code.
    """
    import getopt, sys

    #
    # Check parameters
    #
    try:
        optlist, args = getopt.getopt(sys.argv[1:],
            'd:q',
            ['depth=', 'full', 'help', 'quiet', 'version'])
    except:
        util.fatal('Invalid parameters!', usage=1)

    if util.haveOptions(optlist, ['--version']): version()
    if not args or util.haveOptions(optlist, ['--help']): usage()

    util.flag_quiet = util.haveOptions(optlist, ['-q', '--quiet'])

    global flag_full, list_depth

    flag_full = util.haveOptions(optlist, ['--full'])
    if flag_full: list_depth = 9999
    else: list_depth = 1

    depth = util.getOption(optlist, ['-d', '--depth'])
    if depth: list_depth = int(depth)

    #
    # Collect file names
    #
    files = []
    for name in args:
        files.extend(util.getFilesForName(name))
    util.log('Found %d file%s.' % (len(files), ('', 's')[len(files) != 1], ))

    if not files: return

    #
    # Process the files
    #
    total = {}
    summarize(total, 'files', len(files))
    for filename in files:
        analyze(filename, total)

    #
    # Print summary
    #
    util.flag_quiet = 0 # always print summary
    title = 'Summary on "%s"' % (' '.join(args))
    util.log('\n%s\n%s' % (title, '=' * len(title), ))
    for key in ['files', 'lines', 'bytes', 'comments',
                'empty lines', 'non-commentary lines']:
        util.log(key.ljust(20) + '%6d' % total[key])
    util.log('')
Пример #2
0
def main():
    """ pystat's main code.
    """
    import getopt, sys

    #
    # Check parameters
    #
    try:
        optlist, args = getopt.getopt(
            sys.argv[1:], 'd:q',
            ['depth=', 'full', 'help', 'quiet', 'version'])
    except:
        util.fatal('Invalid parameters!', usage=1)

    if util.haveOptions(optlist, ['--version']): version()
    if not args or util.haveOptions(optlist, ['--help']): usage()

    util.flag_quiet = util.haveOptions(optlist, ['-q', '--quiet'])

    global flag_full, list_depth

    flag_full = util.haveOptions(optlist, ['--full'])
    if flag_full: list_depth = 9999
    else: list_depth = 1

    depth = util.getOption(optlist, ['-d', '--depth'])
    if depth: list_depth = int(depth)

    #
    # Collect file names
    #
    files = []
    for name in args:
        files.extend(util.getFilesForName(name))
    util.log('Found %d file%s.' % (
        len(files),
        ('', 's')[len(files) != 1],
    ))

    if not files: return

    #
    # Process the files
    #
    total = {}
    summarize(total, 'files', len(files))
    for filename in files:
        analyze(filename, total)

    #
    # Print summary
    #
    util.flag_quiet = 0  # always print summary
    title = 'Summary on "%s"' % (' '.join(args))
    util.log('\n%s\n%s' % (
        title,
        '=' * len(title),
    ))
    for key in [
            'files', 'lines', 'bytes', 'comments', 'empty lines',
            'non-commentary lines'
    ]:
        util.log(key.ljust(20) + '%6d' % total[key])
    util.log('')
Пример #3
0
def main():
    """ pydent's main code.
    """
    import getopt, sys, cStringIO, py_compile

    #
    # Check parameters
    #
    try:
        optlist, args = getopt.getopt(sys.argv[1:], 'cnq', ['compile', 'dry-run', 'help', 'quiet', 'no-backup', 'version'])
    except:
        util.fatal('Invalid parameters!', usage=1)

    if util.haveOptions(optlist, ['--version']): version()
    if not args or util.haveOptions(optlist, ['--help']): usage()

    global flag_backup, flag_compile, flag_dryrun
    flag_backup = not util.haveOptions(optlist, ['--no-backup'])
    flag_compile = util.haveOptions(optlist, ['-c', '--compile'])
    flag_dryrun = util.haveOptions(optlist, ['-n', '--dry-run'])
    util.flag_quiet = util.haveOptions(optlist, ['-q', '--quiet'])

    #
    # Collect file names
    #
    files = []
    for name in args:
        files.extend(util.getFilesForName(name))
    util.log('Found %d file%s.' % (len(files), ('', 's')[len(files) != 1], ))

    #
    # Process the files
    #
    for filename in files:
        # load source
        util.log("Formatting '%s'..." % (filename, ))
        if islink(filename):
            continue
        file = util.FileMorpher(filename, backup=flag_backup, dryrun=flag_dryrun)
        source = file.load()

        # check source
        if not util.checkSource(source):
            util.log("Syntax errors in '%s', skipped." % (filename, ))
            continue

        # indent source
        stream = cStringIO.StringIO()
        indenter = Indenter(stream)
        indenter.parse(source)
        newsource = stream.getvalue()
        del stream
        del indenter

        # save if necessary
        if newsource != source:
            if not util.checkSource(newsource):
                # we produced an error in the formatted source
                open('error.dat', 'wt').write(newsource)
                util.fatal("INTERNAL ERROR: Bad formatting, see file 'error.dat'!")
            elif flag_dryrun:
                util.log('Would write %(new)d bytes (previously %(old)d bytes).' % {
                    'old': len(source), 'new': len(newsource), })
            else:
                # save new source
                util.log("Saving '%s'..." % (filename, ))
                file.save(newsource)

                # optionally compile it
                if flag_compile:
                    util.log("Compiling '%s'..." % (filename, ))
                    py_compile.compile(filename)
        del file
Пример #4
0
def main():
    """ pydent's main code.
    """
    import getopt, sys, cStringIO, py_compile

    #
    # Check parameters
    #
    try:
        optlist, args = getopt.getopt(
            sys.argv[1:], 'cnq',
            ['compile', 'dry-run', 'help', 'quiet', 'no-backup', 'version'])
    except:
        util.fatal('Invalid parameters!', usage=1)

    if util.haveOptions(optlist, ['--version']): version()
    if not args or util.haveOptions(optlist, ['--help']): usage()

    global flag_backup, flag_compile, flag_dryrun
    flag_backup = not util.haveOptions(optlist, ['--no-backup'])
    flag_compile = util.haveOptions(optlist, ['-c', '--compile'])
    flag_dryrun = util.haveOptions(optlist, ['-n', '--dry-run'])
    util.flag_quiet = util.haveOptions(optlist, ['-q', '--quiet'])

    #
    # Collect file names
    #
    files = []
    for name in args:
        files.extend(util.getFilesForName(name))
    util.log('Found %d file%s.' % (
        len(files),
        ('', 's')[len(files) != 1],
    ))

    #
    # Process the files
    #
    for filename in files:
        # load source
        util.log("Formatting '%s'..." % (filename, ))
        if islink(filename):
            continue
        file = util.FileMorpher(filename,
                                backup=flag_backup,
                                dryrun=flag_dryrun)
        source = file.load()

        # check source
        if not util.checkSource(source):
            util.log("Syntax errors in '%s', skipped." % (filename, ))
            continue

        # indent source
        stream = cStringIO.StringIO()
        indenter = Indenter(stream)
        indenter.parse(source)
        newsource = stream.getvalue()
        del stream
        del indenter

        # save if necessary
        if newsource != source:
            if not util.checkSource(newsource):
                # we produced an error in the formatted source
                open('error.dat', 'wt').write(newsource)
                util.fatal(
                    "INTERNAL ERROR: Bad formatting, see file 'error.dat'!")
            elif flag_dryrun:
                util.log(
                    'Would write %(new)d bytes (previously %(old)d bytes).' % {
                        'old': len(source),
                        'new': len(newsource),
                    })
            else:
                # save new source
                util.log("Saving '%s'..." % (filename, ))
                file.save(newsource)

                # optionally compile it
                if flag_compile:
                    util.log("Compiling '%s'..." % (filename, ))
                    py_compile.compile(filename)
        del file