| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 8e6a944 commit 94aea52
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -77,6 +77,19 @@ | |||
| 77 | 77 | build.artifacts() | |
| 78 | 78 | # end artifacts | |
| 79 | 79 | ||
| 80 | + # stream artifacts | ||
| 81 | + class Foo(object): | ||
| 82 | + def __init__(self): | ||
| 83 | + self._fd = open('artifacts.zip', 'w') | ||
| 84 | + | ||
| 85 | + def __call__(self, chunk): | ||
| 86 | + self._fd.write(chunk) | ||
| 87 | + | ||
| 88 | + target = Foo() | ||
| 89 | + build.artifacts(streamed=True, streamed=True, action=target) | ||
| 90 | + del(target) # flushes data on disk | ||
| 91 | + # end stream artifacts | ||
| 92 | + | ||
| 80 | 93 | # keep artifacts | |
| 81 | 94 | build.keep_artifacts() | |
| 82 | 95 | # end keep artifacts | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -116,7 +116,16 @@ Get a build artifacts: | |||
| 116 | 116 | ||
| 117 | 117 | .. warning:: | |
| 118 | 118 | ||
| 119 | - Artifacts are entirely stored in memory. | ||
| 119 | + Artifacts are entirely stored in memory in this example. | ||
| 120 | + | ||
| 121 | + .. _streaming_example: | ||
| 122 | + | ||
| 123 | + You can download artifacts as a stream. Provide a callable to handle the | ||
| 124 | + stream: | ||
| 125 | + | ||
| 126 | + .. literalinclude:: builds.py | ||
| 127 | + :start-after: # stream artifacts | ||
| 128 | + :end-before: # end stream artifacts | ||
| 120 | 129 | ||
| 121 | 130 | Mark a build artifact as kept when expiration is set: | |
| 122 | 131 | ||
@@ -132,7 +141,8 @@ Get a build trace: | |||
| 132 | 141 | ||
| 133 | 142 | .. warning:: | |
| 134 | 143 | ||
| 135 | - Traces are entirely stored in memory. | ||
| 144 | + Traces are entirely stored in memory unless you use the streaming feature. | ||
| 145 | + See :ref:`the artifacts example <streaming_example>`. | ||
| 136 | 146 | ||
| 137 | 147 | Cancel/retry a build: | |
| 138 | 148 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -286,7 +286,7 @@ def set_credentials(self, email, password): | |||
| 286 | 286 | self.email = email | |
| 287 | 287 | self.password = password | |
| 288 | 288 | ||
| 289 | - def _raw_get(self, path, content_type=None, **kwargs): | ||
| 289 | + def _raw_get(self, path, content_type=None, streamed=False, **kwargs): | ||
| 290 | 290 | url = '%s%s' % (self._url, path) | |
| 291 | 291 | headers = self._create_headers(content_type) | |
| 292 | 292 | try: | |
@@ -295,6 +295,7 @@ def _raw_get(self, path, content_type=None, **kwargs): | |||
| 295 | 295 | headers=headers, | |
| 296 | 296 | verify=self.ssl_verify, | |
| 297 | 297 | timeout=self.timeout, | |
| 298 | + stream=streamed, | ||
| 298 | 299 | auth=requests.auth.HTTPBasicAuth( | |
| 299 | 300 | self.http_username, | |
| 300 | 301 | self.http_password)) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,6 +29,7 @@ | |||
| 29 | 29 | ||
| 30 | 30 | import gitlab | |
| 31 | 31 | from gitlab.exceptions import * # noqa | |
| 32 | + from gitlab import utils | ||
| 32 | 33 | ||
| 33 | 34 | ||
| 34 | 35 | class jsonEncoder(json.JSONEncoder): | |
@@ -889,22 +890,31 @@ def keep_artifacts(self, **kwargs): | |||
| 889 | 890 | r = self.gitlab._raw_post(url) | |
| 890 | 891 | raise_error_from_response(r, GitlabGetError, 200) | |
| 891 | 892 | ||
| 892 | - def artifacts(self, **kwargs): | ||
| 893 | + def artifacts(self, streamed=False, action=None, chunk_size=1024, | ||
| 894 | + **kwargs): | ||
| 893 | 895 | """Get the build artifacts. | |
| 894 | 896 | ||
| 897 | + Args: | ||
| 898 | + streamed (bool): If True the data will be processed by chunks of | ||
| 899 | + `chunk_size` and each chunk is passed to `action` for | ||
| 900 | + treatment. | ||
| 901 | + action (callable): Callable responsible of dealing with chunk of | ||
| 902 | + data. | ||
| 903 | + chunk_size (int): Size of each chunk. | ||
| 904 | + | ||
| 895 | 905 | Returns: | |
| 896 | - str: The artifacts. | ||
| 906 | + str: The artifacts if `streamed` is False, None otherwise. | ||
| 897 | 907 | ||
| 898 | 908 | Raises: | |
| 899 | 909 | GitlabConnectionError: If the server cannot be reached. | |
| 900 | 910 | GitlabGetError: If the artifacts are not available. | |
| 901 | 911 | """ | |
| 902 | 912 | url = '/projects/%s/builds/%s/artifacts' % (self.project_id, self.id) | |
| 903 | - r = self.gitlab._raw_get(url) | ||
| 913 | + r = self.gitlab._raw_get(url, streamed=streamed, **kwargs) | ||
| 904 | 914 | raise_error_from_response(r, GitlabGetError, 200) | |
| 905 | - return r.content | ||
| 915 | + return utils.response_content(r, streamed, action, chunk_size) | ||
| 906 | 916 | ||
| 907 | - def trace(self, **kwargs): | ||
| 917 | + def trace(self, streamed=False, action=None, chunk_size=1024, **kwargs): | ||
| 908 | 918 | """Get the build trace. | |
| 909 | 919 | ||
| 910 | 920 | Returns: | |
@@ -915,9 +925,9 @@ def trace(self, **kwargs): | |||
| 915 | 925 | GitlabGetError: If the trace is not available. | |
| 916 | 926 | """ | |
| 917 | 927 | url = '/projects/%s/builds/%s/trace' % (self.project_id, self.id) | |
| 918 | - r = self.gitlab._raw_get(url) | ||
| 928 | + r = self.gitlab._raw_get(url, streamed=streamed, **kwargs) | ||
| 919 | 929 | raise_error_from_response(r, GitlabGetError, 200) | |
| 920 | - return r.content | ||
| 930 | + return utils.response_content(r, streamed, action, chunk_size) | ||
| 921 | 931 | ||
| 922 | 932 | ||
| 923 | 933 | class ProjectBuildManager(BaseManager): | |
@@ -972,7 +982,8 @@ def diff(self, **kwargs): | |||
| 972 | 982 | ||
| 973 | 983 | return r.json() | |
| 974 | 984 | ||
| 975 | - def blob(self, filepath, **kwargs): | ||
| 985 | + def blob(self, filepath, streamed=False, action=None, chunk_size=1024, | ||
| 986 | + **kwargs): | ||
| 976 | 987 | """Generate the content of a file for this commit. | |
| 977 | 988 | ||
| 978 | 989 | Args: | |
@@ -988,10 +999,9 @@ def blob(self, filepath, **kwargs): | |||
| 988 | 999 | url = ('/projects/%(project_id)s/repository/blobs/%(commit_id)s' % | |
| 989 | 1000 | {'project_id': self.project_id, 'commit_id': self.id}) | |
| 990 | 1001 | url += '?filepath=%s' % filepath | |
| 991 | - r = self.gitlab._raw_get(url, **kwargs) | ||
| 1002 | + r = self.gitlab._raw_get(url, streamed=streamed, **kwargs) | ||
| 992 | 1003 | raise_error_from_response(r, GitlabGetError) | |
| 993 | - | ||
| 994 | - return r.content | ||
| 1004 | + return utils.response_content(r, streamed, action, chunk_size) | ||
| 995 | 1005 | ||
| 996 | 1006 | def builds(self, **kwargs): | |
| 997 | 1007 | """List the build for this commit. | |
@@ -1734,7 +1744,8 @@ def blob(self, sha, filepath, **kwargs): | |||
| 1734 | 1744 | DeprecationWarning) | |
| 1735 | 1745 | return self.repository_blob(sha, filepath, **kwargs) | |
| 1736 | 1746 | ||
| 1737 | - def repository_blob(self, sha, filepath, **kwargs): | ||
| 1747 | + def repository_blob(self, sha, filepath, streamed=False, action=None, | ||
| 1748 | + chunk_size=1024, **kwargs): | ||
| 1738 | 1749 | """Return the content of a file for a commit. | |
| 1739 | 1750 | ||
| 1740 | 1751 | Args: | |
@@ -1750,11 +1761,12 @@ def repository_blob(self, sha, filepath, **kwargs): | |||
| 1750 | 1761 | """ | |
| 1751 | 1762 | url = "/projects/%s/repository/blobs/%s" % (self.id, sha) | |
| 1752 | 1763 | url += '?filepath=%s' % (filepath) | |
| 1753 | - r = self.gitlab._raw_get(url, **kwargs) | ||
| 1764 | + r = self.gitlab._raw_get(url, streamed=streamed, **kwargs) | ||
| 1754 | 1765 | raise_error_from_response(r, GitlabGetError) | |
| 1755 | - return r.content | ||
| 1766 | + return utils.response_content(r, streamed, action, chunk_size) | ||
| 1756 | 1767 | ||
| 1757 | - def repository_raw_blob(self, sha, **kwargs): | ||
| 1768 | + def repository_raw_blob(self, sha, streamed=False, action=None, | ||
| 1769 | + chunk_size=1024, **kwargs): | ||
| 1758 | 1770 | """Returns the raw file contents for a blob by blob SHA. | |
| 1759 | 1771 | ||
| 1760 | 1772 | Args: | |
@@ -1768,9 +1780,9 @@ def repository_raw_blob(self, sha, **kwargs): | |||
| 1768 | 1780 | GitlabGetError: If the server fails to perform the request. | |
| 1769 | 1781 | """ | |
| 1770 | 1782 | url = "/projects/%s/repository/raw_blobs/%s" % (self.id, sha) | |
| 1771 | - r = self.gitlab._raw_get(url, **kwargs) | ||
| 1783 | + r = self.gitlab._raw_get(url, streamed=streamed, **kwargs) | ||
| 1772 | 1784 | raise_error_from_response(r, GitlabGetError) | |
| 1773 | - return r.content | ||
| 1785 | + return utils.response_content(r, streamed, action, chunk_size) | ||
| 1774 | 1786 | ||
| 1775 | 1787 | def repository_compare(self, from_, to, **kwargs): | |
| 1776 | 1788 | """Returns a diff between two branches/commits. | |
@@ -1813,7 +1825,8 @@ def archive(self, sha=None, **kwargs): | |||
| 1813 | 1825 | DeprecationWarning) | |
| 1814 | 1826 | return self.repository_archive(sha, **kwargs) | |
| 1815 | 1827 | ||
| 1816 | - def repository_archive(self, sha=None, **kwargs): | ||
| 1828 | + def repository_archive(self, sha=None, streamed=False, action=None, | ||
| 1829 | + chunk_size=1024, **kwargs): | ||
| 1817 | 1830 | """Return a tarball of the repository. | |
| 1818 | 1831 | ||
| 1819 | 1832 | Args: | |
@@ -1829,9 +1842,9 @@ def repository_archive(self, sha=None, **kwargs): | |||
| 1829 | 1842 | url = '/projects/%s/repository/archive' % self.id | |
| 1830 | 1843 | if sha: | |
| 1831 | 1844 | url += '?sha=%s' % sha | |
| 1832 | - r = self.gitlab._raw_get(url, **kwargs) | ||
| 1845 | + r = self.gitlab._raw_get(url, streamed=streamed, **kwargs) | ||
| 1833 | 1846 | raise_error_from_response(r, GitlabGetError) | |
| 1834 | - return r.content | ||
| 1847 | + return utils.response_content(r, streamed, action, chunk_size) | ||
| 1835 | 1848 | ||
| 1836 | 1849 | def create_file(self, path, branch, content, message, **kwargs): | |
| 1837 | 1850 | """Creates file in project repository | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,15 @@ | |||
| 1 | + class _StdoutStream(object): | ||
| 2 | + def __call__(self, chunk): | ||
| 3 | + print(chunk) | ||
| 4 | + | ||
| 5 | + | ||
| 6 | + def response_content(response, streamed, action, chunk_size): | ||
| 7 | + if streamed is False: | ||
| 8 | + return response.content | ||
| 9 | + | ||
| 10 | + if action is None: | ||
| 11 | + action = _StdoutStream() | ||
| 12 | + | ||
| 13 | + for chunk in response.iter_content(chunk_size=chunk_size): | ||
| 14 | + if chunk: | ||
| 15 | + action(chunk) | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments