OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # coding: utf-8 | 2 # coding: utf-8 |
3 # | 3 # |
4 # Copyright 2007 Google Inc. | 4 # Copyright 2007 Google Inc. |
5 # | 5 # |
6 # Licensed under the Apache License, Version 2.0 (the "License"); | 6 # Licensed under the Apache License, Version 2.0 (the "License"); |
7 # you may not use this file except in compliance with the License. | 7 # you may not use this file except in compliance with the License. |
8 # You may obtain a copy of the License at | 8 # You may obtain a copy of the License at |
9 # | 9 # |
10 # http://www.apache.org/licenses/LICENSE-2.0 | 10 # http://www.apache.org/licenses/LICENSE-2.0 |
(...skipping 824 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
835 | 835 |
836 | 836 |
837 def GetContentType(filename): | 837 def GetContentType(filename): |
838 """Helper to guess the content-type from the filename.""" | 838 """Helper to guess the content-type from the filename.""" |
839 return mimetypes.guess_type(filename)[0] or 'application/octet-stream' | 839 return mimetypes.guess_type(filename)[0] or 'application/octet-stream' |
840 | 840 |
841 | 841 |
842 # Use a shell for subcommands on Windows to get a PATH search. | 842 # Use a shell for subcommands on Windows to get a PATH search. |
843 use_shell = sys.platform.startswith("win") | 843 use_shell = sys.platform.startswith("win") |
844 | 844 |
845 def RunShellWithReturnCodeAndStderr(command, print_output=False, | 845 def RunShellWithReturnCodeAndStderr(command, universal_newlines=True, |
846 universal_newlines=True, | 846 env=os.environ): |
847 env=os.environ): | |
848 """Run a command and return output from stdout, stderr and the return code. | 847 """Run a command and return output from stdout, stderr and the return code. |
849 | 848 |
850 Args: | 849 Args: |
851 command: Command to execute. | 850 command: Command to execute. |
852 print_output: If True, the output is printed to stdout. | |
853 If False, both stdout and stderr are ignored. | |
854 universal_newlines: Use universal_newlines flag (default: True). | 851 universal_newlines: Use universal_newlines flag (default: True). |
855 | 852 |
856 Returns: | 853 Returns: |
857 Tuple (stdout, stderr, return code) | 854 Tuple (stdout, stderr, return code) |
858 """ | 855 """ |
859 LOGGER.info("Running %s", command) | 856 LOGGER.info("Running %s", command) |
860 env = env.copy() | 857 env = env.copy() |
861 env['LC_MESSAGES'] = 'C' | 858 env['LC_MESSAGES'] = 'C' |
862 p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, | 859 p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
863 shell=use_shell, universal_newlines=universal_newlines, | 860 shell=use_shell, universal_newlines=universal_newlines, |
864 env=env) | 861 env=env) |
865 if print_output: | 862 output, errout = p.communicate() |
866 # It's very hard to stream both stdout and stderr at the same time | |
867 # without the potential for deadlocks. We will hope for the best | |
868 # since this code path is rarely used. | |
869 output_array = [] | |
870 while True: | |
871 line = p.stdout.readline() | |
872 if not line: | |
873 break | |
874 print line.strip("\n") | |
875 output_array.append(line) | |
876 output = "".join(output_array) | |
877 p.wait() | |
878 errout = p.stderr.read() | |
879 if errout: | |
880 print >> sys.stderr, errout | |
881 else: | |
882 output, errout = p.communicate() | |
883 p.stdout.close() | 863 p.stdout.close() |
884 p.stderr.close() | 864 p.stderr.close() |
885 return output, errout, p.returncode | 865 return output, errout, p.returncode |
886 | 866 |
887 def RunShellWithReturnCode(command, print_output=False, | 867 def RunShellWithReturnCode(command, universal_newlines=True, env=os.environ): |
888 universal_newlines=True, | |
889 env=os.environ): | |
890 """Run a command and return output from stdout and the return code.""" | 868 """Run a command and return output from stdout and the return code.""" |
891 out, err, retcode = RunShellWithReturnCodeAndStderr(command, print_output, | 869 out, err, retcode = RunShellWithReturnCodeAndStderr(command, |
892 universal_newlines, env) | 870 universal_newlines, env) |
893 return out, retcode | 871 return out, retcode |
894 | 872 |
895 def RunShell(command, silent_ok=False, universal_newlines=True, | 873 def RunShell(command, silent_ok=False, universal_newlines=True, |
896 print_output=False, env=os.environ): | 874 env=os.environ): |
897 data, retcode = RunShellWithReturnCode(command, print_output, | 875 data, retcode = RunShellWithReturnCode(command, universal_newlines, env) |
898 universal_newlines, env) | |
899 if retcode: | 876 if retcode: |
900 ErrorExit("Got error status from %s:\n%s" % (command, data)) | 877 ErrorExit("Got error status from %s:\n%s" % (command, data)) |
901 if not silent_ok and not data: | 878 if not silent_ok and not data: |
902 ErrorExit("No output from %s" % command) | 879 ErrorExit("No output from %s" % command) |
903 return data | 880 return data |
904 | 881 |
905 | 882 |
906 class VersionControlSystem(object): | 883 class VersionControlSystem(object): |
907 """Abstract base class providing an interface to the VCS.""" | 884 """Abstract base class providing an interface to the VCS.""" |
908 | 885 |
(...skipping 852 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1761 args.extend(["-G"]) | 1738 args.extend(["-G"]) |
1762 if self.p4_port: | 1739 if self.p4_port: |
1763 args.extend(["-p", self.p4_port]) | 1740 args.extend(["-p", self.p4_port]) |
1764 if self.p4_client: | 1741 if self.p4_client: |
1765 args.extend(["-c", self.p4_client]) | 1742 args.extend(["-c", self.p4_client]) |
1766 if self.p4_user: | 1743 if self.p4_user: |
1767 args.extend(["-u", self.p4_user]) | 1744 args.extend(["-u", self.p4_user]) |
1768 args.extend(extra_args) | 1745 args.extend(extra_args) |
1769 | 1746 |
1770 data, retcode = RunShellWithReturnCode( | 1747 data, retcode = RunShellWithReturnCode( |
1771 args, print_output=False, universal_newlines=universal_newlines) | 1748 args, universal_newlines=universal_newlines) |
1772 if marshal_output and data: | 1749 if marshal_output and data: |
1773 data = marshal.loads(data) | 1750 data = marshal.loads(data) |
1774 return data, retcode | 1751 return data, retcode |
1775 | 1752 |
1776 def RunPerforceCommand(self, extra_args, marshal_output=False, | 1753 def RunPerforceCommand(self, extra_args, marshal_output=False, |
1777 universal_newlines=True): | 1754 universal_newlines=True): |
1778 # This might be a good place to cache call results, since things like | 1755 # This might be a good place to cache call results, since things like |
1779 # describe or fstat might get called repeatedly. | 1756 # describe or fstat might get called repeatedly. |
1780 data, retcode = self.RunPerforceCommandWithReturnCode( | 1757 data, retcode = self.RunPerforceCommandWithReturnCode( |
1781 extra_args, marshal_output, universal_newlines) | 1758 extra_args, marshal_output, universal_newlines) |
(...skipping 773 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2555 print | 2532 print |
2556 StatusUpdate("Interrupted.") | 2533 StatusUpdate("Interrupted.") |
2557 sys.exit(1) | 2534 sys.exit(1) |
2558 except auth.AuthenticationError as e: | 2535 except auth.AuthenticationError as e: |
2559 print >> sys.stderr, e | 2536 print >> sys.stderr, e |
2560 sys.exit(1) | 2537 sys.exit(1) |
2561 | 2538 |
2562 | 2539 |
2563 if __name__ == "__main__": | 2540 if __name__ == "__main__": |
2564 main() | 2541 main() |
OLD | NEW |