|
5 | 5 | # the BSD License: http://www.opensource.org/licenses/bsd-license.php
|
6 | 6 |
|
7 | 7 | importos
|
| 8 | +importos.path |
8 | 9 | importsys
|
9 | 10 | importselect
|
10 | 11 | importlogging
|
11 | 12 | importthreading
|
12 | 13 | importerrno
|
13 | 14 | importmmap
|
14 | 15 |
|
| 16 | +fromcontextlibimportcontextmanager |
15 | 17 | fromsubprocessimport (
|
16 | 18 | call,
|
17 | 19 | Popen,
|
@@ -223,7 +225,7 @@ class Git(LazyMixin):
|
223 | 225 | Set its value to 'full' to see details about the returned values.
|
224 | 226 | """
|
225 | 227 | __slots__= ("_working_dir","cat_file_all","cat_file_header","_version_info",
|
226 |
| -"_git_options") |
| 228 | +"_git_options","_environment") |
227 | 229 |
|
228 | 230 | # CONFIGURATION
|
229 | 231 | # The size in bytes read from stdout when copying git's output to another stream
|
@@ -413,6 +415,9 @@ def __init__(self, working_dir=None):
|
413 | 415 | self._working_dir=working_dir
|
414 | 416 | self._git_options= ()
|
415 | 417 |
|
| 418 | +# Extra environment variables to pass to git commands |
| 419 | +self._environment= {} |
| 420 | + |
416 | 421 | # cached command slots
|
417 | 422 | self.cat_file_header=None
|
418 | 423 | self.cat_file_all=None
|
@@ -536,6 +541,8 @@ def execute(self, command,
|
536 | 541 | # Start the process
|
537 | 542 | env=os.environ.copy()
|
538 | 543 | env["LC_MESSAGES"]="C"
|
| 544 | +env.update(self._environment) |
| 545 | + |
539 | 546 | proc=Popen(command,
|
540 | 547 | env=env,
|
541 | 548 | cwd=cwd,
|
@@ -608,6 +615,73 @@ def as_text(stdout_value):
|
608 | 615 | else:
|
609 | 616 | returnstdout_value
|
610 | 617 |
|
| 618 | +defset_environment(self,**kwargs): |
| 619 | +""" |
| 620 | + Set environment variables for future git invocations. Return all changed |
| 621 | + values in a format that can be passed back into this function to revert |
| 622 | + the changes: |
| 623 | +
|
| 624 | + ``Examples``:: |
| 625 | +
|
| 626 | + old_env = self.set_environment(PWD='/tmp') |
| 627 | + self.set_environment(**old_env) |
| 628 | +
|
| 629 | + :param kwargs: environment variables to use for git processes |
| 630 | + :return: dict that maps environment variables to their old values |
| 631 | + """ |
| 632 | +old_env= {} |
| 633 | +forkey,valueinkwargs.iteritems(): |
| 634 | +# set value if it is None |
| 635 | +ifvalueisnotNone: |
| 636 | +ifkeyinself._environment: |
| 637 | +old_env[key]=self._environment[key] |
| 638 | +else: |
| 639 | +old_env[key]=None |
| 640 | +self._environment[key]=value |
| 641 | +# remove key from environment if its value is None |
| 642 | +elifkeyinself._environment: |
| 643 | +old_env[key]=self._environment[key] |
| 644 | +delself._environment[key] |
| 645 | +returnold_env |
| 646 | + |
| 647 | +@contextmanager |
| 648 | +defenvironment(self,**kwargs): |
| 649 | +""" |
| 650 | + A context manager around the above set_environment to restore the |
| 651 | + environment back to its previous state after operation. |
| 652 | +
|
| 653 | + ``Examples``:: |
| 654 | +
|
| 655 | + with self.environment(GIT_SSH='/bin/ssh_wrapper'): |
| 656 | + repo.remotes.origin.fetch() |
| 657 | +
|
| 658 | + :param kwargs: see set_environment |
| 659 | + """ |
| 660 | +old_env=self.set_environment(**kwargs) |
| 661 | +try: |
| 662 | +yield |
| 663 | +finally: |
| 664 | +self.set_environment(**old_env) |
| 665 | + |
| 666 | +@contextmanager |
| 667 | +defsshkey(self,sshkey_file): |
| 668 | +""" |
| 669 | + A context manager to temporarily set an SSH key for all operations that |
| 670 | + run inside it. |
| 671 | +
|
| 672 | + ``Examples``:: |
| 673 | +
|
| 674 | + with self.environment(GIT_SSH=project_dir+'deployment_key'): |
| 675 | + repo.remotes.origin.fetch() |
| 676 | +
|
| 677 | + :param sshkey_file: Path to a private SSH key file |
| 678 | + """ |
| 679 | +this_dir=os.path.dirname(__file__) |
| 680 | +ssh_wrapper=os.path.join(this_dir,'..','scripts','ssh_wrapper.py') |
| 681 | + |
| 682 | +withself.environment(GIT_SSH_KEY_FILE=sshkey_file,GIT_SSH=ssh_wrapper): |
| 683 | +yield |
| 684 | + |
611 | 685 | deftransform_kwargs(self,split_single_char_options=False,**kwargs):
|
612 | 686 | """Transforms Python style kwargs into git command line options."""
|
613 | 687 | args=list()
|
|