Skip to content

Instantly share code, notes, and snippets.

@KoheiKanagu
Last active November 4, 2017 09:01
Show Gist options
  • Save KoheiKanagu/b3ba2bb68d36509b6cee054e077bd415 to your computer and use it in GitHub Desktop.
Save KoheiKanagu/b3ba2bb68d36509b6cee054e077bd415 to your computer and use it in GitHub Desktop.
venvを使ってPythonの環境をローカルに構築して、VSCodeで開くツール。http://qiita.com/KoheiKanagu/items/752c64aeeb154970d22e
import argparse
import os.path
import subprocess
parser = argparse.ArgumentParser(
description="This script deitals : http://qiita.com/KoheiKanagu/items/752c64aeeb154970d22e"
)
parser.add_argument(
"--init",
action="store_true",
help="Initialize Python virtual environment"
)
parser.add_argument(
"-i", "--install",
action="store_true",
help="Install packages from \"requirements.txt\""
)
parser.add_argument(
"-s", "--sync",
action="store_true",
help="Sync packages using of pip-compile and pip-sync"
)
parser.add_argument(
"-c", "--code",
action="store_true",
help="Open VSCode at virtual environment"
)
args = parser.parse_args()
def exists(path) -> bool:
if not os.path.exists(path):
print(path, "Not found.")
return False
return True
def exists_env_dir()-> bool:
return exists(".env")
def exists_requirements()-> bool:
return exists("requirements.in")
def arg_init():
if exists_env_dir():
print("\".env\" is exist.")
return
print("Initializing venv...")
subprocess.run(["bash", "-c",
"python3 -m venv .env"])
print("Initialized \".env\"")
print("Install pip-tools")
subprocess.run(["bash", "-c",
"source .env/bin/activate && pip install pip-tools"])
if not exists_requirements():
subprocess.run(["bash", "-c",
"echo pip-tools >> requirements.in"])
print("Initialized \"requirements.in\"")
print("Done")
if args.init:
arg_init()
def arg_install():
if not exists_env_dir() or not exists_requirements():
return
print("Install packages.")
subprocess.run(["bash", "-c",
"source .env/bin/activate && pip install -r requirements.txt"])
if args.install:
arg_install()
def arg_sync():
if not exists_env_dir():
return
if not exists_requirements():
open("requirements.in", "w").close
print("Checking the packages version...")
subprocess.run(["bash", "-c",
"source .env/bin/activate && pip-compile requirements.in"])
print("Install packages.")
subprocess.run(["bash", "-c",
"source .env/bin/activate && pip-sync requirements.txt"])
if args.sync:
arg_sync()
def arg_code():
if not exists_env_dir():
return
subprocess.run(["bash", "-c",
"source .env/bin/activate && code ./"])
if args.code:
arg_code()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment