Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15497575
  • 博文数量: 2005
  • 博客积分: 11986
  • 博客等级: 上将
  • 技术积分: 22535
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-17 13:56
文章分类

全部博文(2005)

文章存档

2014年(2)

2013年(2)

2012年(16)

2011年(66)

2010年(368)

2009年(743)

2008年(491)

2007年(317)

分类: Python/Ruby

2008-12-16 18:43:21

import sys,os,stat,getopt,tempfile,shutil,struct,subprocess

mtypes = [
    'zip',
    'rar',
    'tar',
    'gz'
]


if __name__ == '__main__':
    
    opts,args=getopt.gnu_getopt(sys.argv[1:],"i:t:v:n:h:o:", ["input=", "name=", "type=", "help=", "version=", "outfile="])


    itypes = []

    for o,a in opts:
        if o in ('-n', '--name'):
            print 'name=%s' % (a)
        elif o in ('-i', '--input'):
            filename = os.path.abspath(a)
            fstat = os.stat(filename)
# fd = open(filename, "a")
# fstat = os.fstat(fd.fileno())
            print fstat
# ST_ATIME = 7
# ST_CTIME = 9
# ST_DEV = 2
# ST_GID = 5
# ST_INO = 1
# ST_MODE = 0
# ST_MTIME = 8
# ST_NLINK = 3
# ST_SIZE = 6
# ST_UID = 4
# S_IROTH = 4
# S_IRWXO = 7
# S_IWOTH = 2
# S_IXGRP = 8
# S_IXOTH = 1
            print 'mode=%o' % fstat[stat.ST_MODE]
            print 'inode number=%d' % fstat[stat.ST_INO]
            print 'size=%d' % fstat[stat.ST_SIZE]

            fd = open(filename, "a")
            fd.write(struct.pack('i', fstat[stat.ST_SIZE])) # equal to linux kernel attr : __attribute__ ((packed)) help(struct)[luther.gliethttp])
            fd.close()
        elif o in ('-o', '--outfile'):
            print 'outputfile abs path=%s' % os.path.abspath(a)
        elif o in ('-t', '--type'):
            type = a.lower()
            if type in mtypes:
                itypes.append(type)
            else:
                print 'not support type : %s' % (type)
        elif o in ('-v', '--version'):
            print 'version 1.0'
            sys.stdout.flush()
            sys.exit()
        elif o in ('-h', '--help'):
            print 'luther:gliethttp\njust for test'
            sys.stdout.flush()
            sys.exit()
        else:
            print 'Usage...'
   

    print '########\n',sys.path,'\n########'
   
print sys.path[0]

    print tempfile.gettempdir()
    tempfile.tempdir = os.getcwd()
    tmpdir = tempfile.mkdtemp()
    print tempfile.gettempdir()
    cwd_save = os.getcwd()
    print os.getcwd()
    os.chdir(tmpdir)
    print '=========%s=========' % os.getcwd()
    os.chdir(cwd_save)
    print os.getcwd()
    shutil.rmtree(tmpdir, ignore_errors=True)


    types=['rar','zip','tar','gz']
    files=['luther.rar','gliethttp.zip','a.tar','b.gz']
    print zip(types,files)


    print os.path.abspath(sys.argv[0])
    shutil.copyfile(os.path.abspath(sys.argv[0]), 'bakefile.py')


    subprocess.call('ls')
    subprocess.call(['ls','-l'])
    os.system('ls')
    os.system('ls -l')


    print '''hello hello
    1.luther
    2.gliethttp
    3.china '''

luther@gliethttp:~/python$ python ./test.py  --name luther -o gliethttp -t rar -i /vobs/luther.c
name=luther
outputfile abs path=/home/luther/python/gliethttp
(33188, 8536217L, 2053L, 1, 1000, 1000, 16L, 1229423954, 1229423995, 1229424028)
mode=100644
inode number=8536217
size=16
/tmp
/home/luther/python
/home/luther/python
=========/home/luther/python/tmp56g7nA=========
/home/luther/python
阅读(1628) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~