Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
Signed-off-by: troychiu <y.troychiu@gmail.com>
  • Loading branch information
troychiu committed Oct 30, 2023
1 parent 0711fe0 commit 19f1463
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
2 changes: 1 addition & 1 deletion plugins/flytekit-vscode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ from flytekitplugins.vscode import vscode
@vscode
def train():
...
```
```
42 changes: 25 additions & 17 deletions plugins/flytekit-vscode/flytekitplugins/vscode/vscode.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import multiprocessing
import os
import shutil
import subprocess
from functools import wraps
import sys
import tarfile
import time
import os
import multiprocessing
from functools import wraps
from typing import Callable, Optional

import fsspec
from typing import Optional, Callable
import tarfile
import shutil

from flytekit.loggers import logger

# Where the code-server tar and plugins are downloaded to
Expand All @@ -16,6 +18,7 @@
HOURS_TO_SECONDS = 60 * 60
DEFAULT_UP_SECONDS = 10 * HOURS_TO_SECONDS # 10 hours


def execute_command(cmd):
"""
Execute a command in the shell.
Expand All @@ -28,7 +31,8 @@ def execute_command(cmd):
logger.info(f"stdout: {stdout}")
logger.info(f"stderr: {stderr}")

def download_file(url, target_dir='.'):

def download_file(url, target_dir="."):
"""
Download a file from a given URL using fsspec.
Expand All @@ -55,6 +59,7 @@ def download_file(url, target_dir='.'):

return local_file_name


def download_vscode(
code_server_remote_path: str,
code_server_dir_name: str,
Expand All @@ -73,10 +78,10 @@ def download_vscode(
executable_path = shutil.which(EXECUTABLE_NAME)
if executable_path is not None:
logger.info(f"Code server binary already exists at {executable_path}")
logger.info(f"Skipping downloading code server...")
logger.info("Skipping downloading code server...")
return
logger.info(f"Code server is not in $PATH, start downloading code server...")

logger.info("Code server is not in $PATH, start downloading code server...")

# Create DOWNLOAD_DIR if not exist
logger.info(f"DOWNLOAD_DIR: {DOWNLOAD_DIR}")
Expand All @@ -96,25 +101,28 @@ def download_vscode(
code_server_bin_dir = os.path.join(code_server_dir_path, "bin")

# Add the directory of code-server binary to $PATH
os.environ['PATH'] = code_server_bin_dir + os.pathsep + os.environ['PATH']
os.environ["PATH"] = code_server_bin_dir + os.pathsep + os.environ["PATH"]


def vscode(
_task_function: Optional[Callable] = None,
server_up_seconds: Optional[int] = DEFAULT_UP_SECONDS,
port: Optional[int] = 8080,
enable: Optional[bool] = True,
code_server_remote_path: Optional[str] = "https://github.com/coder/code-server/releases/download/v4.18.0/code-server-4.18.0-linux-amd64.tar.gz",
code_server_remote_path: Optional[
str
] = "https://github.com/coder/code-server/releases/download/v4.18.0/code-server-4.18.0-linux-amd64.tar.gz",
# The untarred directory name may be different from the tarball name
code_server_dir_name: Optional[str] = "code-server-4.18.0-linux-amd64",
pre_execute: Optional[Callable] = None,
post_execute: Optional[Callable] = None
post_execute: Optional[Callable] = None,
):
"""
vscode decorator modifies a container to run a VSCode server:
1. Overrides the user function with a VSCode setup function.
2. Download vscode server and plugins from remote to local.
3. Launches and monitors the VSCode server.
4. Terminates after
4. Terminates after
Parameters:
- _task_function (function, optional): The user function to be decorated. Defaults to None.
Expand All @@ -125,6 +133,7 @@ def vscode(
- pre_execute (function, optional): The function to be executed before the vscode setup function.
- post_execute (function, optional): The function to be executed before the vscode is self-terminated.
"""

def wrapper(fn):
if not enable:
return fn
Expand All @@ -146,8 +155,7 @@ def inner_wrapper(*args, **kwargs):
# Run the function in the background
logger.info(f"Start the server for {server_up_seconds} seconds...")
child_process = multiprocessing.Process(
target=execute_command,
kwargs={"cmd": f"code-server --bind-addr 0.0.0.0:{port} --auth none"}
target=execute_command, kwargs={"cmd": f"code-server --bind-addr 0.0.0.0:{port} --auth none"}
)

child_process.start()
Expand All @@ -169,4 +177,4 @@ def inner_wrapper(*args, **kwargs):
return wrapper(_task_function)
# for the case when the decorator is used with arguments
else:
return wrapper
return wrapper
2 changes: 1 addition & 1 deletion plugins/flytekit-vscode/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@
"Topic :: Software Development :: Libraries :: Python Modules",
],
entry_points={"flytekit.plugins": [f"{PLUGIN_NAME}=flytekitplugins.{PLUGIN_NAME}"]},
)
)

0 comments on commit 19f1463

Please sign in to comment.