Statistics
| Branch: | Revision:

root / env / lib / python2.7 / site-packages / distribute-0.6.19-py2.7.egg / setuptools / command / alias.py @ 1a305335

History | View | Annotate | Download (2.42 KB)

1
import distutils, os
2
from setuptools import Command
3
from distutils.util import convert_path
4
from distutils import log
5
from distutils.errors import *
6
from setuptools.command.setopt import edit_config, option_base, config_file
7

    
8
def shquote(arg):
9
    """Quote an argument for later parsing by shlex.split()"""
10
    for c in '"', "'", "\\", "#":
11
        if c in arg: return repr(arg)
12
    if arg.split()<>[arg]:
13
        return repr(arg)
14
    return arg        
15

    
16

    
17
class alias(option_base):
18
    """Define a shortcut that invokes one or more commands"""
19
    
20
    description = "define a shortcut to invoke one or more commands"
21
    command_consumes_arguments = True
22

    
23
    user_options = [
24
        ('remove',   'r', 'remove (unset) the alias'), 
25
    ] + option_base.user_options
26

    
27
    boolean_options = option_base.boolean_options + ['remove']
28

    
29
    def initialize_options(self):
30
        option_base.initialize_options(self)
31
        self.args = None
32
        self.remove = None
33

    
34
    def finalize_options(self):
35
        option_base.finalize_options(self)
36
        if self.remove and len(self.args)<>1:
37
            raise DistutilsOptionError(
38
                "Must specify exactly one argument (the alias name) when "
39
                "using --remove"
40
            )
41

    
42
    def run(self):
43
        aliases = self.distribution.get_option_dict('aliases')
44

    
45
        if not self.args:
46
            print "Command Aliases"
47
            print "---------------"
48
            for alias in aliases:
49
                print "setup.py alias", format_alias(alias, aliases)
50
            return
51

    
52
        elif len(self.args)==1:
53
            alias, = self.args
54
            if self.remove:
55
                command = None
56
            elif alias in aliases:
57
                print "setup.py alias", format_alias(alias, aliases)
58
                return
59
            else:
60
                print "No alias definition found for %r" % alias
61
                return
62
        else:
63
            alias = self.args[0]
64
            command = ' '.join(map(shquote,self.args[1:]))
65

    
66
        edit_config(self.filename, {'aliases': {alias:command}}, self.dry_run)
67

    
68

    
69
def format_alias(name, aliases):
70
    source, command = aliases[name]
71
    if source == config_file('global'):
72
        source = '--global-config '
73
    elif source == config_file('user'):
74
        source = '--user-config '
75
    elif source == config_file('local'):
76
        source = ''
77
    else:
78
        source = '--filename=%r' % source
79
    return source+name+' '+command
80
            
81

    
82