text stringlengths 0 828 |
|---|
if isinstance(command, str): |
command = shlex.split(command) |
if os.name == 'nt': |
return _run_as_admin_windows(command, cwd, environ) |
elif os.name == 'posix': |
command = ['sudo', '-E'] + list(command) |
sys.exit(subprocess.call(command)) |
else: |
raise RuntimeError('Unsupported os: {!r}'.format(os.name))" |
884,"def add_body_part(self, key, data, mime_type, size=None): |
""""""Adds data to the HTTP request body. |
If more than one part is added, this is assumed to be a mime-multipart |
request. This method is designed to create MIME 1.0 requests as specified |
in RFC 1341. |
Args: |
data: str or a file-like object containing a part of the request body. |
mime_type: str The MIME type describing the data |
size: int Required if the data is a file like object. If the data is a |
string, the size is calculated so this parameter is ignored. |
"""""" |
if isinstance(data, str): |
size = len(data) |
if hasattr(data, ""fileno""): |
size = os.fstat(data.fileno())[stat.ST_SIZE] |
if size is None: |
# TODO: support chunked transfer if some of the body is of unknown size. |
raise UnknownSize('Each part of the body must have a known size.') |
if 'Content-Length' in self.headers: |
content_length = int(self.headers['Content-Length']) |
else: |
content_length = 0 |
# If this is the first part added to the body, then this is not a multipart |
# request. |
boundary_string = '\r\n--%s\r\n' % (MIME_BOUNDARY,) |
self._body_parts.append(boundary_string) |
content_length += len(boundary_string) + size |
# Include the mime type of this part. |
cd = 'Content-Disposition: form-data; name=""%s""' % key |
mt = mime_type |
if hasattr(data, ""fileno""): |
cd += '; filename=""%s""' % data.name.split('/')[-1] |
mt = mimetypes.guess_type(data.name)[0] or 'application/octet-stream' |
cd += '\r\n' |
type_string = 'Content-Type: %s\r\n\r\n' % (mt) |
self._body_parts.append(cd) |
self._body_parts.append(type_string) |
content_length += len(type_string) + len(cd) |
self._body_parts.append(data) |
self.headers['Content-Length'] = str(content_length)" |
885,"def _copy(self): |
""""""Creates a deep copy of this request."""""" |
copied_uri = Uri(self.uri.scheme, self.uri.host, self.uri.port, |
self.uri.path, self.uri.query.copy()) |
new_request = HttpRequest(uri=copied_uri, method=self.method, |
headers=self.headers.copy()) |
new_request._body_parts = self._body_parts[:] |
return new_request" |
886,"def _get_relative_path(self): |
""""""Returns the path with the query parameters escaped and appended."""""" |
param_string = self._get_query_string() |
if self.path is None: |
path = '/' |
else: |
path = self.path |
if param_string: |
return '?'.join([path, param_string]) |
else: |
return path" |
887,"def modify_request(self, http_request=None): |
""""""Sets HTTP request components based on the URI."""""" |
if http_request is None: |
http_request = HttpRequest() |
if http_request.uri is None: |
http_request.uri = Uri() |
# Determine the correct scheme. |
if self.scheme: |
http_request.uri.scheme = self.scheme |
if self.port: |
http_request.uri.port = self.port |
if self.host: |
http_request.uri.host = self.host |
# Set the relative uri path |
if self.path: |
http_request.uri.path = self.path |
if self.query: |
http_request.uri.query = self.query.copy() |
return http_request" |
888,"def parse_uri(uri_string): |
""""""Creates a Uri object which corresponds to the URI string. |
This method can accept partial URIs, but it will leave missing |
members of the Uri unset. |
"""""" |
parts = urlparse.urlparse(uri_string) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.