File: fsrm.py

package info (click to toggle)
python-fs 0.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,452 kB
  • ctags: 2,991
  • sloc: python: 20,659; makefile: 5
file content (54 lines) | stat: -rw-r--r-- 1,889 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python

from fs.errors import ResourceNotFoundError
from fs.opener import opener
from fs.commands.runner import Command
import sys

class FSrm(Command):
    
    usage = """fsrm [OPTION]... [PATH]
Remove a file or directory at PATH"""
    
    def get_optparse(self):
        optparse = super(FSrm, self).get_optparse()
        optparse.add_option('-f', '--force', dest='force', action='store_true', default=False,
                            help='ignore non-existent files, never prompt')
        optparse.add_option('-i', '--interactive', dest='interactive', action='store_true', default=False,
                            help='prompt before removing')
        optparse.add_option('-r', '--recursive', dest='recursive', action='store_true', default=False,
                            help='remove directories and their contents recursively')
        return optparse
    
    def do_run(self, options, args):
                        
        interactive = options.interactive
        verbose = options.verbose
        
        for fs, path, is_dir in self.get_resources(args):                              
            if interactive:
                if is_dir:
                    msg = "remove directory '%s'?" % path
                else:
                    msg = "remove file '%s'?" % path
                if not self.ask(msg) in 'yY':
                    continue
            try:
                if is_dir:
                    fs.removedir(path, force=options.recursive)
                else:
                    fs.remove(path)
            except ResourceNotFoundError:
                if not options.force:
                    raise
            else:
                if verbose:
                    self.output("removed '%s'\n" % path)
            
        
def run():         
    return FSrm().run()
    
if __name__ == "__main__":
    sys.exit(run())