ostools.py :  » Development » Bazaar » bzr-2.2b3 » tools » win32 » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » Bazaar 
Bazaar » bzr 2.2b3 » tools » win32 » ostools.py
#!/usr/bin/python

"""Cross-platform os tools: files/directories manipulations
Usage:

    ostools.py help
                    prints this help

    ostools.py copytodir FILES... DIR
                    copy files to specified directory

    ostools.py copytree FILES... DIR
                    copy files to specified directory keeping relative paths

    ostools.py remove [FILES...] [DIRS...]
                    remove files or directories (recursive)
"""

import glob
import os
import shutil
import sys

def makedir(dirname):
    if not os.path.exists(dirname):
        os.makedirs(dirname)
    if not os.path.isdir(dirname):
        print "Error: Destination is not a directory", dirname
        return 2
    return 0

def main(argv=None):
    if argv is None:
        argv = sys.argv[1:]

    if not argv:
        argv = ['help']

    cmd = argv.pop(0)

    if cmd == 'help':
        print __doc__
        return 0

    if cmd == 'copytodir':
        if len(argv) < 2:
            print "Usage:  ostools.py copytodir FILES... DIR"
            return 1

        todir = argv.pop()
        retcode = makedir(todir)
        if retcode:
            return retcode

        files = []
        for possible_glob in argv:
            files += glob.glob(possible_glob)

        for src in files:
            dest = os.path.join(todir, os.path.basename(src))
            shutil.copy(src, dest)
            print "Copied:", src, "=>", dest

        return 0

    if cmd == 'copytree':
        if len(argv) < 2:
            print "Usage:  ostools.py copytree FILES... DIR"
            return 1

        todir = argv.pop()
        retcode = makedir(todir)
        if retcode:
            return retcode

        files = []
        for possible_glob in argv:
            files += glob.glob(possible_glob)

        for src in files:
            relative_path = src
            dest = os.path.join(todir, relative_path)
            dest_dir = os.path.dirname(dest)
            retcode = makedir(dest_dir)
            if retcode:
                return retcode
            shutil.copy(src, dest)
            print "Copied:", src, "=>", dest

        return 0

    if cmd == 'remove':
        if len(argv) == 0:
            print "Usage:  ostools.py remove [FILES...] [DIRS...]"
            return 1

        filesdirs = []
        for possible_glob in argv:
            filesdirs += glob.glob(possible_glob)

        for i in filesdirs:
            if os.path.isdir(i):
                shutil.rmtree(i)
                print "Removed:", i
            elif os.path.isfile(i):
                os.remove(i)
                print "Removed:", i
            else:
                print "Not found:", i

        return 0

    if cmd == "basename":
        if len(argv) == 0:
            print "Usage:  ostools.py basename [PATH | URL]"
            return 1

        for path in argv:
            print os.path.basename(path)
        return 0

    if cmd == 'makedir':
        if len(argv) == 0:
            print "Usage:  ostools.py makedir DIR"
            return 1

        retcode = makedir(argv.pop())
        if retcode:
            return retcode
        return 0

    print "Usage error"
    print __doc__
    return 1


if __name__ == "__main__":
    sys.exit(main())
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.