summaryrefslogtreecommitdiffstats
path: root/command.py
diff options
context:
space:
mode:
authorDavid Pursehouse <david.pursehouse@sonymobile.com>2012-11-16 09:33:39 +0900
committerDavid Pursehouse <david.pursehouse@sonymobile.com>2012-11-17 12:40:42 +0900
commitb148ac9d9aefb18baddbf9e28ed33b1b8c6e96e6 (patch)
tree64564abdc213acb66c83f6782566caf51e194678 /command.py
parenta67df63ef18f7bcd1398dc5c3622c01c3fcad15d (diff)
downloadgit-repo-b148ac9d9aefb18baddbf9e28ed33b1b8c6e96e6.tar.gz
Allow command options to be set from environment variables
Extend the Command base class to allow options to be set from values in environment variables, if the user has not given the option on the command line and the environment variable is set. Derived classes of Command can override the implementation of the method _GetEnvironmentOptions to configure which of its options may be set from environment variables. Change-Id: I7c780bcf9644d6567893d9930984c054bce7351e
Diffstat (limited to 'command.py')
-rw-r--r--command.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/command.py b/command.py
index babb8338..dc6052a7 100644
--- a/command.py
+++ b/command.py
@@ -22,6 +22,7 @@ import sys
22from error import NoSuchProjectError 22from error import NoSuchProjectError
23from error import InvalidProjectGroupsError 23from error import InvalidProjectGroupsError
24 24
25
25class Command(object): 26class Command(object):
26 """Base class for any command line action in repo. 27 """Base class for any command line action in repo.
27 """ 28 """
@@ -33,6 +34,27 @@ class Command(object):
33 def WantPager(self, opt): 34 def WantPager(self, opt):
34 return False 35 return False
35 36
37 def ReadEnvironmentOptions(self, opts):
38 """ Set options from environment variables. """
39
40 env_options = self._RegisteredEnvironmentOptions()
41
42 for env_key, opt_key in env_options.items():
43 # Get the user-set option value if any
44 opt_value = getattr(opts, opt_key)
45
46 # If the value is set, it means the user has passed it as a command
47 # line option, and we should use that. Otherwise we can try to set it
48 # with the value from the corresponding environment variable.
49 if opt_value is not None:
50 continue
51
52 env_value = os.environ.get(env_key)
53 if env_value is not None:
54 setattr(opts, opt_key, env_value)
55
56 return opts
57
36 @property 58 @property
37 def OptionParser(self): 59 def OptionParser(self):
38 if self._optparse is None: 60 if self._optparse is None:
@@ -49,6 +71,24 @@ class Command(object):
49 """Initialize the option parser. 71 """Initialize the option parser.
50 """ 72 """
51 73
74 def _RegisteredEnvironmentOptions(self):
75 """Get options that can be set from environment variables.
76
77 Return a dictionary mapping environment variable name
78 to option key name that it can override.
79
80 Example: {'REPO_MY_OPTION': 'my_option'}
81
82 Will allow the option with key value 'my_option' to be set
83 from the value in the environment variable named 'REPO_MY_OPTION'.
84
85 Note: This does not work properly for options that are explicitly
86 set to None by the user, or options that are defined with a
87 default value other than None.
88
89 """
90 return {}
91
52 def Usage(self): 92 def Usage(self):
53 """Display usage and terminate. 93 """Display usage and terminate.
54 """ 94 """