Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
99dbcb8
commit 4a289a6
Showing
2 changed files
with
61 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/usr/bin/env python | ||
# coding=utf-8 | ||
# Copyright 2021 The HuggingFace Inc. team. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License | ||
|
||
import subprocess | ||
from typing import List | ||
|
||
from .logging import get_logger | ||
|
||
|
||
logger = get_logger(__name__) | ||
|
||
|
||
def run_subprocess( | ||
command: List[str], folder: str, check=True, **kwargs | ||
) -> subprocess.CompletedProcess: | ||
""" | ||
Method to run subprocesses. Calling this will capture the `stderr` and `stdout`, | ||
please call `subprocess.run` manually in case you would like for them not to | ||
be captured. | ||
Args: | ||
command (`List[str]`): | ||
The command to execute as a list of strings. | ||
folder (`str`): | ||
The folder in which to run the command. | ||
check (`bool`, *optional*, defaults to `True`): | ||
Setting `check` to `True` will raise a `subprocess.CalledProcessError` | ||
when the subprocess has a non-zero exit code. | ||
kwargs (`Dict[str]`): | ||
Keyword arguments to be passed to the `subprocess.run` underlying command. | ||
Returns: | ||
`subprocess.CompletedProcess`: The completed process. | ||
""" | ||
if isinstance(command, str): | ||
raise ValueError("`run_subprocess` should be called with a list of strings.") | ||
|
||
return subprocess.run( | ||
command, | ||
stderr=subprocess.PIPE, | ||
stdout=subprocess.PIPE, | ||
check=check, | ||
encoding="utf-8", | ||
cwd=folder, | ||
**kwargs | ||
) |