-
Notifications
You must be signed in to change notification settings - Fork 14
/
__init__.py
153 lines (125 loc) · 3.79 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#! /usr/bin/env python3
# Standard library
from argparse import (
ArgumentDefaultsHelpFormatter,
ArgumentParser,
RawTextHelpFormatter,
REMAINDER,
SUPPRESS,
)
import os
import sys
# Packages
from termcolor import cprint
# Local
from canonicalwebteam.dotrun.models import Project
DOTRUN_DOCKER_COMPOSE_ACTIONS = os.environ.get(
"DOTRUN_DOCKER_COMPOSE_ACTIONS", "start:serve"
).split(":")
# Define available commands
# ===
class RawWithDefaultsFormatter(
RawTextHelpFormatter, ArgumentDefaultsHelpFormatter
):
pass
cli_parser = ArgumentParser(
description=(
"Containerized project-level dependency management and "
"package.json commands"
),
formatter_class=RawWithDefaultsFormatter,
)
# Options
cli_parser.add_argument(
"-C",
"--directory",
help="The directory in which to run commands (default: current directory)",
default=SUPPRESS,
)
cli_parser.add_argument(
"-s",
"--skip-install",
action="store_true",
default=False,
help="Don't check for python or node dependencies before running",
)
cli_parser.add_argument(
"-e",
"--env",
action="append",
default=SUPPRESS,
help=(
"Environment variables to use when running commands.\n"
"These will override what's in .env or .env.local"
),
)
# Main command
cli_parser.add_argument(
"command",
help=(
"A package.json command to run with `yarn run XXX` inside dotrun.\n"
"Simply typing `dotrun` will run `yarn run start`.\n"
"\n"
"Or a special command - one of:\n"
"- version: Print the version\n"
"- exec [command]: Execute a command or open a bash shell"
" within the dotrun context\n"
"- install: Reinstall node and python dependencies\n"
"- clean: Run `yarn run clean` and remove all dotrun files\n"
"- clean-cache: Delete the snap's cache, including yarn & pip caches\n"
),
nargs="?",
default="start",
)
cli_parser.add_argument("remainder", nargs=REMAINDER, help=SUPPRESS)
def cli(args=None):
"""
Parse the command-line arguments
"""
if args is None:
args = sys.argv[1:]
arguments = cli_parser.parse_args(args)
command = arguments.command
# Commands that don't need dependencies
if command == "version":
print(os.environ.get("SNAP_VERSION", "SNAP_VERSION not found"))
sys.exit()
# Compile list of environment variables
env_extra = {}
if "env" in arguments:
for env_string in arguments.env:
key, value = env_string.split("=")
env_extra[key] = value
# Prepare dotrun object
path = os.getcwd()
if "directory" in arguments:
path = os.path.abspath(arguments.directory)
dotrun = Project(path=path, env_extra=env_extra)
# Clean runs before install
if command == "clean":
return dotrun.clean()
if command == "clean-cache":
return dotrun.exec(["rm", "-rf", f"{os.environ['HOME']}/.cache"])
if command == "exec":
return dotrun.exec(arguments.remainder or ["bash"])
if command == "install":
return dotrun.install(force=True)
if not arguments.skip_install:
dotrun.install(force=False)
# By default, run a yarn script
if dotrun.has_script(command):
# Some commands can run docker-compose in the background
if command in DOTRUN_DOCKER_COMPOSE_ACTIONS:
if dotrun._check_docker_compose():
dotrun._docker_compose_start()
try:
return dotrun.yarn_run(command, arguments.remainder)
finally:
dotrun.terminate_background_processes()
else:
cprint(
f"\n[ `{command}` script not found in `package.json` ]\n",
"red",
)
cli_parser.print_usage()
sys.exit(1)