#!/usr/pkg/bin/python """stat program for cygwin for some reason, I cannot find a stat program under cygwin. this is meant as a crude replacement""" Copyright = """ stat.py - crude implementation of 'stat' for cygwin Copyright (C) 2004 John Comeau This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. """ errormessage = "Not all needed libraries found, upgrade or check path" try: True # not defined in older Python releases except: True, False = 1, 0 try: import sys, os, types, re from com.jcomeau import gpl, jclicense except: try: sys.stderr.write("%s\n" % errormessage) except: print errormessage raise # get name this program was called as self = sys.argv[0].split(os.sep)[-1] # now get name we gave it when we wrote it originalself = Copyright.split()[0] # globals and routines that should be in every program # (yes, you could import them, but there are problems in that approach too) debugging = True # set to False for production release def DebugPrint(*whatever): global debugging if debugging: sys.stderr.write("%s\n" % repr(whatever)) def test(*args): "check if lowercase functions actually work as programs" print "this is a test. it is only a test. passed arguments follow..." DebugPrint(args) # other globals, specific to this program def stat(*args): try: args[0][0] except: sys.stderr.write('Usage: stat FILE[...]') sys.exit(1) for file in args[0]: stat = os.stat(file) print '%s: atime=%d mtime=%d ctime=%d' % (file, stat[7], stat[8], stat[9]) def main(): """main routine, only used for command-line testing let's say this program is called 'template.py' don't, by the way, name the program with the name of an included function if you're using this system... now if the program is invoked as './template.py', it will simply output a usage message. if invoked as './template.py MyFunction 1 2 3' we will attempt to call MyFunction(1, 2, 3) and show whatever is found if invoked as './template.py myfunction 1 2 3' we attempt to call myfunction(1, 2, 3) as a program and let it output whatever it decides likewise if you symlink like so: 'ln -s template.py myfunction' then invoke as './myfunction 1 2 3'""" program = sys.argv[0].split(os.sep)[-1] function = program.split('.')[0] # in case of .py extension try: if eval("type(%s)" % function) == types.FunctionType: if re.compile('^[a-z0-9]+$').match(function) != None: eval("%s(sys.argv[1:])" % function) else: try: print repr(eval("%s(sys.argv[1:])" % function)) except: sys.stderr.write("Error occurred testing routine %s(%s)\n" % ( function, repr(sys.argv[1:]))) raise else: raise NameError, '%s not a function' % function except NameError, instance: # first make sure this wasn't some other type of name error; if it was, barf #DebugPrint(instance.args) try: message = instance.args[0] except: message = 'unknown NameError' if message != '%s not a function' % function: raise NameError, message # called name didn't match anything defined if len(sys.argv) > 1: # hopefully the next arg is the function name we want sys.argv.pop(0) main() else: # called name didn't match any routine, so output usage message sys.stderr.write("""Usage: %s [OPTIONS] INPUT[...] or for more detailed help, read source or: pydoc %s """ % (self, self.split('.')[0])) if __name__ == '__main__': # if this program was imported by another, the above test will fail, # and this following code won't be used... main() else: # if you want something to be done on import, do it here; otherwise pass pass