We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Failing to create and API on Windows
**Describe the bug**
Failing to create an api on windows , 
`bentoml serve {saved_path}`

**To Reproduce**
Steps to reproduce the behavior:
1.  Failing with an error of the  fnctl  was not found 
2. fixed it by using this and creating a fcntl.py 
```
def fcntl(fd, op, arg=0):
    return 0

def ioctl(fd, op, arg=0, mutable_flag=True):
    if mutable_flag:
        return 0
    else:
        return ""

def flock(fd, op):
    return

def lockf(fd, operation, length=0, start=0, whence=0):
    return
```
3. Then the  import pwd started failing , for which I dont have any work around till now .


**Environment:**
 - OS: [windows 10 ]
 - Python/BentoML Version [Python3.6]

**Additional context**
Add any other context about the problem here.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `bentoml/cli/__init__.py`
Content:
```
1 # Copyright 2019 Atalaya Tech, Inc.
2 
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 
7 # http://www.apache.org/licenses/LICENSE-2.0
8 
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 
15 from __future__ import absolute_import
16 from __future__ import division
17 from __future__ import print_function
18 
19 import re
20 import os
21 import json
22 import click
23 import logging
24 import tempfile
25 import subprocess
26 
27 from ruamel.yaml import YAML
28 
29 from bentoml.archive import (
30     load,
31     load_service_api,
32     load_bentoml_config,
33     load_bento_service_metadata,
34 )
35 from bentoml.server import BentoAPIServer, get_docs
36 from bentoml.server.gunicorn_server import GunicornBentoServer
37 from bentoml.cli.click_utils import BentoMLCommandGroup, conditional_argument, _echo
38 from bentoml.cli.deployment import get_deployment_sub_command
39 from bentoml.cli.config import get_configuration_sub_command
40 from bentoml.utils import Path, ProtoMessageToDict
41 from bentoml.utils.log import configure_logging
42 from bentoml.utils.usage_stats import track_cli
43 
44 
45 def escape_shell_params(param):
46     k, v = param.split('=')
47     v = re.sub(r'([^a-zA-Z0-9])', r'\\\1', v)
48     return '{}={}'.format(k, v)
49 
50 
51 def create_bento_service_cli(archive_path=None):
52     # pylint: disable=unused-variable
53 
54     @click.group(cls=BentoMLCommandGroup)
55     @click.option(
56         '-q',
57         '--quiet',
58         is_flag=True,
59         default=False,
60         help="Hide process logs and only print command results",
61     )
62     @click.option(
63         '--verbose',
64         is_flag=True,
65         default=False,
66         help="Print verbose debugging information for BentoML developer",
67     )
68     @click.version_option()
69     @click.pass_context
70     def bentoml_cli(ctx, verbose, quiet):
71         """
72         BentoML CLI tool
73         """
74         ctx.verbose = verbose
75         ctx.quiet = quiet
76 
77         if verbose:
78             configure_logging(logging.DEBUG)
79         elif quiet:
80             configure_logging(logging.ERROR)
81         else:
82             configure_logging()  # use default setting in local bentoml.cfg
83 
84     # Example Usage: bentoml API_NAME /SAVED_ARCHIVE_PATH --input=INPUT
85     @bentoml_cli.command(
86         default_command=True,
87         default_command_usage="API_NAME BENTO_ARCHIVE_PATH --input=INPUT",
88         default_command_display_name="<API_NAME>",
89         short_help="Run API function",
90         help="Run a API defined in saved BentoArchive with cli args as input",
91         context_settings=dict(ignore_unknown_options=True, allow_extra_args=True),
92     )
93     @click.argument("api-name", type=click.STRING)
94     @conditional_argument(archive_path is None, "archive-path", type=click.STRING)
95     @click.option(
96         '--with-conda',
97         is_flag=True,
98         default=False,
99         help="Run API server in a BentoML managed Conda environment",
100     )
101     @click.pass_context
102     def run(ctx, api_name, archive_path=archive_path, with_conda=False):
103         if with_conda:
104             config = load_bentoml_config(archive_path)
105             metadata = config['metadata']
106             env_name = metadata['service_name'] + '_' + metadata['service_version']
107 
108             yaml = YAML()
109             yaml.default_flow_style = False
110             tmpf = tempfile.NamedTemporaryFile(delete=False)
111             env_path = tmpf.name
112             yaml.dump(config['env']['conda_env'], Path(env_path))
113 
114             pip_req = os.path.join(archive_path, 'requirements.txt')
115 
116             subprocess.call(
117                 'command -v conda >/dev/null 2>&1 || {{ echo >&2 "--with-conda '
118                 'parameter requires conda but it\'s not installed."; exit 1; }} && '
119                 'conda env update -n {env_name} -f {env_file} && '
120                 'conda init bash && '
121                 'eval "$(conda shell.bash hook)" && '
122                 'conda activate {env_name} && '
123                 '{{ [ -f {pip_req} ] && pip install -r {pip_req} || echo "no pip '
124                 'dependencies."; }} &&'
125                 'bentoml {api_name} {archive_path} {args}'.format(
126                     env_name=env_name,
127                     env_file=env_path,
128                     archive_path=archive_path,
129                     api_name=api_name,
130                     args=' '.join(map(escape_shell_params, ctx.args)),
131                     pip_req=pip_req,
132                 ),
133                 shell=True,
134             )
135             return
136 
137         track_cli('run')
138 
139         api = load_service_api(archive_path, api_name)
140         api.handle_cli(ctx.args)
141 
142     # Example Usage: bentoml info /SAVED_ARCHIVE_PATH
143     @bentoml_cli.command(
144         help="List all APIs defined in the BentoService loaded from archive.",
145         short_help="List APIs",
146     )
147     @conditional_argument(archive_path is None, "archive-path", type=click.STRING)
148     def info(archive_path=archive_path):
149         """
150         List all APIs defined in the BentoService loaded from archive
151         """
152         track_cli('info')
153         bento_service_metadata_pb = load_bento_service_metadata(archive_path)
154         output = json.dumps(ProtoMessageToDict(bento_service_metadata_pb), indent=2)
155         _echo(output)
156 
157     # Example usage: bentoml open-api-spec /SAVED_ARCHIVE_PATH
158     @bentoml_cli.command(
159         name="open-api-spec",
160         help="Display API specification JSON in Open-API format",
161         short_help="Display OpenAPI/Swagger JSON specs",
162     )
163     @conditional_argument(archive_path is None, "archive-path", type=click.STRING)
164     def open_api_spec(archive_path=archive_path):
165         track_cli('open-api-spec')
166         bento_service = load(archive_path)
167 
168         _echo(json.dumps(get_docs(bento_service), indent=2))
169 
170     # Example Usage: bentoml serve ./SAVED_ARCHIVE_PATH --port=PORT
171     @bentoml_cli.command(
172         help="Start REST API server hosting BentoService loaded from archive",
173         short_help="Start local rest server",
174     )
175     @conditional_argument(archive_path is None, "archive-path", type=click.STRING)
176     @click.option(
177         "--port",
178         type=click.INT,
179         default=BentoAPIServer._DEFAULT_PORT,
180         help="The port to listen on for the REST api server, default is 5000.",
181     )
182     @click.option(
183         '--with-conda',
184         is_flag=True,
185         default=False,
186         help="Run API server in a BentoML managed Conda environment",
187     )
188     def serve(port, archive_path=archive_path, with_conda=False):
189         if with_conda:
190             config = load_bentoml_config(archive_path)
191             metadata = config['metadata']
192             env_name = metadata['service_name'] + '_' + metadata['service_version']
193             pip_req = os.path.join(archive_path, 'requirements.txt')
194 
195             subprocess.call(
196                 'command -v conda >/dev/null 2>&1 || {{ echo >&2 "--with-conda '
197                 'parameter requires conda but it\'s not installed."; exit 1; }} && '
198                 'conda env update -n {env_name} -f {env_file} && '
199                 'conda init bash && '
200                 'eval "$(conda shell.bash hook)" && '
201                 'conda activate {env_name} && '
202                 '{{ [ -f {pip_req} ] && pip install -r {pip_req} || echo "no pip '
203                 'dependencies."; }} &&'
204                 'bentoml serve {archive_path} --port {port}'.format(
205                     env_name=env_name,
206                     env_file=os.path.join(archive_path, 'environment.yml'),
207                     archive_path=archive_path,
208                     port=port,
209                     pip_req=pip_req,
210                 ),
211                 shell=True,
212             )
213             return
214 
215         track_cli('serve')
216 
217         bento_service = load(archive_path)
218         server = BentoAPIServer(bento_service, port=port)
219         server.start()
220 
221     # Example Usage:
222     # bentoml serve-gunicorn ./SAVED_ARCHIVE_PATH --port=PORT --workers=WORKERS
223     @bentoml_cli.command(
224         help="Start REST API gunicorn server hosting BentoService loaded from archive",
225         short_help="Start local gunicorn server",
226     )
227     @conditional_argument(archive_path is None, "archive-path", type=click.STRING)
228     @click.option("-p", "--port", type=click.INT, default=None)
229     @click.option(
230         "-w",
231         "--workers",
232         type=click.INT,
233         default=None,
234         help="Number of workers will start for the gunicorn server",
235     )
236     @click.option("--timeout", type=click.INT, default=None)
237     @click.option(
238         '--with-conda',
239         is_flag=True,
240         default=False,
241         help="Run API server in a BentoML managed Conda environment",
242     )
243     def serve_gunicorn(
244         port, workers, timeout, archive_path=archive_path, with_conda=False
245     ):
246         if with_conda:
247             config = load_bentoml_config(archive_path)
248             metadata = config['metadata']
249             env_name = metadata['service_name'] + '_' + metadata['service_version']
250             pip_req = os.path.join(archive_path, 'requirements.txt')
251 
252             subprocess.call(
253                 'command -v conda >/dev/null 2>&1 || {{ echo >&2 "--with-conda '
254                 'parameter requires conda but it\'s not installed."; exit 1; }} && '
255                 'conda env update -n {env_name} -f {env_file} && '
256                 'conda init bash && '
257                 'eval "$(conda shell.bash hook)" && '
258                 'conda activate {env_name} && '
259                 '{{ [ -f {pip_req} ] && pip install -r {pip_req} || echo "no pip '
260                 'dependencies."; }} &&'
261                 'bentoml serve_gunicorn {archive_path} -p {port} -w {workers} '
262                 '--timeout {timeout}'.format(
263                     env_name=env_name,
264                     env_file=os.path.join(archive_path, 'environment.yml'),
265                     archive_path=archive_path,
266                     port=port,
267                     workers=workers,
268                     timeout=timeout,
269                     pip_req=pip_req,
270                 ),
271                 shell=True,
272             )
273             return
274 
275         track_cli('serve_gunicorn')
276 
277         gunicorn_app = GunicornBentoServer(archive_path, port, workers, timeout)
278         gunicorn_app.run()
279 
280     # pylint: enable=unused-variable
281     return bentoml_cli
282 
283 
284 def create_bentoml_cli():
285     _cli = create_bento_service_cli()
286 
287     # Commands created here aren't mean to be used from generated service archive. They
288     # are used as part of BentoML cli commands only.
289 
290     deployment_sub_command = get_deployment_sub_command()
291     config_sub_command = get_configuration_sub_command()
292     _cli.add_command(config_sub_command)
293     _cli.add_command(deployment_sub_command)
294 
295     return _cli
296 
297 
298 cli = create_bentoml_cli()
299 
300 if __name__ == "__main__":
301     cli()  # pylint: disable=no-value-for-parameter
302
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/bentoml/cli/__init__.py b/bentoml/cli/__init__.py
--- a/bentoml/cli/__init__.py
+++ b/bentoml/cli/__init__.py
@@ -33,7 +33,6 @@
     load_bento_service_metadata,
 )
 from bentoml.server import BentoAPIServer, get_docs
-from bentoml.server.gunicorn_server import GunicornBentoServer
 from bentoml.cli.click_utils import BentoMLCommandGroup, conditional_argument, _echo
 from bentoml.cli.deployment import get_deployment_sub_command
 from bentoml.cli.config import get_configuration_sub_command
@@ -274,6 +273,7 @@
 
         track_cli('serve_gunicorn')
 
+        from bentoml.server.gunicorn_server import GunicornBentoServer
         gunicorn_app = GunicornBentoServer(archive_path, port, workers, timeout)
         gunicorn_app.run()

{"golden_diff": "diff --git a/bentoml/cli/__init__.py b/bentoml/cli/__init__.py\n--- a/bentoml/cli/__init__.py\n+++ b/bentoml/cli/__init__.py\n@@ -33,7 +33,6 @@\n     load_bento_service_metadata,\n )\n from bentoml.server import BentoAPIServer, get_docs\n-from bentoml.server.gunicorn_server import GunicornBentoServer\n from bentoml.cli.click_utils import BentoMLCommandGroup, conditional_argument, _echo\n from bentoml.cli.deployment import get_deployment_sub_command\n from bentoml.cli.config import get_configuration_sub_command\n@@ -274,6 +273,7 @@\n \n         track_cli('serve_gunicorn')\n \n+        from bentoml.server.gunicorn_server import GunicornBentoServer\n         gunicorn_app = GunicornBentoServer(archive_path, port, workers, timeout)\n         gunicorn_app.run()\n", "issue": "Failing to create and API on Windows\n**Describe the bug**\r\nFailing to create an api on windows , \r\n`bentoml serve {saved_path}`\r\n\r\n**To Reproduce**\r\nSteps to reproduce the behavior:\r\n1.  Failing with an error of the  fnctl  was not found \r\n2. fixed it by using this and creating a fcntl.py \r\n```\r\ndef fcntl(fd, op, arg=0):\r\n    return 0\r\n\r\ndef ioctl(fd, op, arg=0, mutable_flag=True):\r\n    if mutable_flag:\r\n        return 0\r\n    else:\r\n        return \"\"\r\n\r\ndef flock(fd, op):\r\n    return\r\n\r\ndef lockf(fd, operation, length=0, start=0, whence=0):\r\n    return\r\n```\r\n3. Then the  import pwd started failing , for which I dont have any work around till now .\r\n\r\n\r\n**Environment:**\r\n - OS: [windows 10 ]\r\n - Python/BentoML Version [Python3.6]\r\n\r\n**Additional context**\r\nAdd any other context about the problem here.\r\n\n", "before_files": [{"content": "# Copyright 2019 Atalaya Tech, Inc.\n\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n\n# http://www.apache.org/licenses/LICENSE-2.0\n\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport re\nimport os\nimport json\nimport click\nimport logging\nimport tempfile\nimport subprocess\n\nfrom ruamel.yaml import YAML\n\nfrom bentoml.archive import (\n    load,\n    load_service_api,\n    load_bentoml_config,\n    load_bento_service_metadata,\n)\nfrom bentoml.server import BentoAPIServer, get_docs\nfrom bentoml.server.gunicorn_server import GunicornBentoServer\nfrom bentoml.cli.click_utils import BentoMLCommandGroup, conditional_argument, _echo\nfrom bentoml.cli.deployment import get_deployment_sub_command\nfrom bentoml.cli.config import get_configuration_sub_command\nfrom bentoml.utils import Path, ProtoMessageToDict\nfrom bentoml.utils.log import configure_logging\nfrom bentoml.utils.usage_stats import track_cli\n\n\ndef escape_shell_params(param):\n    k, v = param.split('=')\n    v = re.sub(r'([^a-zA-Z0-9])', r'\\\\\\1', v)\n    return '{}={}'.format(k, v)\n\n\ndef create_bento_service_cli(archive_path=None):\n    # pylint: disable=unused-variable\n\n    @click.group(cls=BentoMLCommandGroup)\n    @click.option(\n        '-q',\n        '--quiet',\n        is_flag=True,\n        default=False,\n        help=\"Hide process logs and only print command results\",\n    )\n    @click.option(\n        '--verbose',\n        is_flag=True,\n        default=False,\n        help=\"Print verbose debugging information for BentoML developer\",\n    )\n    @click.version_option()\n    @click.pass_context\n    def bentoml_cli(ctx, verbose, quiet):\n        \"\"\"\n        BentoML CLI tool\n        \"\"\"\n        ctx.verbose = verbose\n        ctx.quiet = quiet\n\n        if verbose:\n            configure_logging(logging.DEBUG)\n        elif quiet:\n            configure_logging(logging.ERROR)\n        else:\n            configure_logging()  # use default setting in local bentoml.cfg\n\n    # Example Usage: bentoml API_NAME /SAVED_ARCHIVE_PATH --input=INPUT\n    @bentoml_cli.command(\n        default_command=True,\n        default_command_usage=\"API_NAME BENTO_ARCHIVE_PATH --input=INPUT\",\n        default_command_display_name=\"<API_NAME>\",\n        short_help=\"Run API function\",\n        help=\"Run a API defined in saved BentoArchive with cli args as input\",\n        context_settings=dict(ignore_unknown_options=True, allow_extra_args=True),\n    )\n    @click.argument(\"api-name\", type=click.STRING)\n    @conditional_argument(archive_path is None, \"archive-path\", type=click.STRING)\n    @click.option(\n        '--with-conda',\n        is_flag=True,\n        default=False,\n        help=\"Run API server in a BentoML managed Conda environment\",\n    )\n    @click.pass_context\n    def run(ctx, api_name, archive_path=archive_path, with_conda=False):\n        if with_conda:\n            config = load_bentoml_config(archive_path)\n            metadata = config['metadata']\n            env_name = metadata['service_name'] + '_' + metadata['service_version']\n\n            yaml = YAML()\n            yaml.default_flow_style = False\n            tmpf = tempfile.NamedTemporaryFile(delete=False)\n            env_path = tmpf.name\n            yaml.dump(config['env']['conda_env'], Path(env_path))\n\n            pip_req = os.path.join(archive_path, 'requirements.txt')\n\n            subprocess.call(\n                'command -v conda >/dev/null 2>&1 || {{ echo >&2 \"--with-conda '\n                'parameter requires conda but it\\'s not installed.\"; exit 1; }} && '\n                'conda env update -n {env_name} -f {env_file} && '\n                'conda init bash && '\n                'eval \"$(conda shell.bash hook)\" && '\n                'conda activate {env_name} && '\n                '{{ [ -f {pip_req} ] && pip install -r {pip_req} || echo \"no pip '\n                'dependencies.\"; }} &&'\n                'bentoml {api_name} {archive_path} {args}'.format(\n                    env_name=env_name,\n                    env_file=env_path,\n                    archive_path=archive_path,\n                    api_name=api_name,\n                    args=' '.join(map(escape_shell_params, ctx.args)),\n                    pip_req=pip_req,\n                ),\n                shell=True,\n            )\n            return\n\n        track_cli('run')\n\n        api = load_service_api(archive_path, api_name)\n        api.handle_cli(ctx.args)\n\n    # Example Usage: bentoml info /SAVED_ARCHIVE_PATH\n    @bentoml_cli.command(\n        help=\"List all APIs defined in the BentoService loaded from archive.\",\n        short_help=\"List APIs\",\n    )\n    @conditional_argument(archive_path is None, \"archive-path\", type=click.STRING)\n    def info(archive_path=archive_path):\n        \"\"\"\n        List all APIs defined in the BentoService loaded from archive\n        \"\"\"\n        track_cli('info')\n        bento_service_metadata_pb = load_bento_service_metadata(archive_path)\n        output = json.dumps(ProtoMessageToDict(bento_service_metadata_pb), indent=2)\n        _echo(output)\n\n    # Example usage: bentoml open-api-spec /SAVED_ARCHIVE_PATH\n    @bentoml_cli.command(\n        name=\"open-api-spec\",\n        help=\"Display API specification JSON in Open-API format\",\n        short_help=\"Display OpenAPI/Swagger JSON specs\",\n    )\n    @conditional_argument(archive_path is None, \"archive-path\", type=click.STRING)\n    def open_api_spec(archive_path=archive_path):\n        track_cli('open-api-spec')\n        bento_service = load(archive_path)\n\n        _echo(json.dumps(get_docs(bento_service), indent=2))\n\n    # Example Usage: bentoml serve ./SAVED_ARCHIVE_PATH --port=PORT\n    @bentoml_cli.command(\n        help=\"Start REST API server hosting BentoService loaded from archive\",\n        short_help=\"Start local rest server\",\n    )\n    @conditional_argument(archive_path is None, \"archive-path\", type=click.STRING)\n    @click.option(\n        \"--port\",\n        type=click.INT,\n        default=BentoAPIServer._DEFAULT_PORT,\n        help=\"The port to listen on for the REST api server, default is 5000.\",\n    )\n    @click.option(\n        '--with-conda',\n        is_flag=True,\n        default=False,\n        help=\"Run API server in a BentoML managed Conda environment\",\n    )\n    def serve(port, archive_path=archive_path, with_conda=False):\n        if with_conda:\n            config = load_bentoml_config(archive_path)\n            metadata = config['metadata']\n            env_name = metadata['service_name'] + '_' + metadata['service_version']\n            pip_req = os.path.join(archive_path, 'requirements.txt')\n\n            subprocess.call(\n                'command -v conda >/dev/null 2>&1 || {{ echo >&2 \"--with-conda '\n                'parameter requires conda but it\\'s not installed.\"; exit 1; }} && '\n                'conda env update -n {env_name} -f {env_file} && '\n                'conda init bash && '\n                'eval \"$(conda shell.bash hook)\" && '\n                'conda activate {env_name} && '\n                '{{ [ -f {pip_req} ] && pip install -r {pip_req} || echo \"no pip '\n                'dependencies.\"; }} &&'\n                'bentoml serve {archive_path} --port {port}'.format(\n                    env_name=env_name,\n                    env_file=os.path.join(archive_path, 'environment.yml'),\n                    archive_path=archive_path,\n                    port=port,\n                    pip_req=pip_req,\n                ),\n                shell=True,\n            )\n            return\n\n        track_cli('serve')\n\n        bento_service = load(archive_path)\n        server = BentoAPIServer(bento_service, port=port)\n        server.start()\n\n    # Example Usage:\n    # bentoml serve-gunicorn ./SAVED_ARCHIVE_PATH --port=PORT --workers=WORKERS\n    @bentoml_cli.command(\n        help=\"Start REST API gunicorn server hosting BentoService loaded from archive\",\n        short_help=\"Start local gunicorn server\",\n    )\n    @conditional_argument(archive_path is None, \"archive-path\", type=click.STRING)\n    @click.option(\"-p\", \"--port\", type=click.INT, default=None)\n    @click.option(\n        \"-w\",\n        \"--workers\",\n        type=click.INT,\n        default=None,\n        help=\"Number of workers will start for the gunicorn server\",\n    )\n    @click.option(\"--timeout\", type=click.INT, default=None)\n    @click.option(\n        '--with-conda',\n        is_flag=True,\n        default=False,\n        help=\"Run API server in a BentoML managed Conda environment\",\n    )\n    def serve_gunicorn(\n        port, workers, timeout, archive_path=archive_path, with_conda=False\n    ):\n        if with_conda:\n            config = load_bentoml_config(archive_path)\n            metadata = config['metadata']\n            env_name = metadata['service_name'] + '_' + metadata['service_version']\n            pip_req = os.path.join(archive_path, 'requirements.txt')\n\n            subprocess.call(\n                'command -v conda >/dev/null 2>&1 || {{ echo >&2 \"--with-conda '\n                'parameter requires conda but it\\'s not installed.\"; exit 1; }} && '\n                'conda env update -n {env_name} -f {env_file} && '\n                'conda init bash && '\n                'eval \"$(conda shell.bash hook)\" && '\n                'conda activate {env_name} && '\n                '{{ [ -f {pip_req} ] && pip install -r {pip_req} || echo \"no pip '\n                'dependencies.\"; }} &&'\n                'bentoml serve_gunicorn {archive_path} -p {port} -w {workers} '\n                '--timeout {timeout}'.format(\n                    env_name=env_name,\n                    env_file=os.path.join(archive_path, 'environment.yml'),\n                    archive_path=archive_path,\n                    port=port,\n                    workers=workers,\n                    timeout=timeout,\n                    pip_req=pip_req,\n                ),\n                shell=True,\n            )\n            return\n\n        track_cli('serve_gunicorn')\n\n        gunicorn_app = GunicornBentoServer(archive_path, port, workers, timeout)\n        gunicorn_app.run()\n\n    # pylint: enable=unused-variable\n    return bentoml_cli\n\n\ndef create_bentoml_cli():\n    _cli = create_bento_service_cli()\n\n    # Commands created here aren't mean to be used from generated service archive. They\n    # are used as part of BentoML cli commands only.\n\n    deployment_sub_command = get_deployment_sub_command()\n    config_sub_command = get_configuration_sub_command()\n    _cli.add_command(config_sub_command)\n    _cli.add_command(deployment_sub_command)\n\n    return _cli\n\n\ncli = create_bentoml_cli()\n\nif __name__ == \"__main__\":\n    cli()  # pylint: disable=no-value-for-parameter\n", "path": "bentoml/cli/__init__.py"}], "after_files": [{"content": "# Copyright 2019 Atalaya Tech, Inc.\n\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n\n# http://www.apache.org/licenses/LICENSE-2.0\n\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport re\nimport os\nimport json\nimport click\nimport logging\nimport tempfile\nimport subprocess\n\nfrom ruamel.yaml import YAML\n\nfrom bentoml.archive import (\n    load,\n    load_service_api,\n    load_bentoml_config,\n    load_bento_service_metadata,\n)\nfrom bentoml.server import BentoAPIServer, get_docs\nfrom bentoml.cli.click_utils import BentoMLCommandGroup, conditional_argument, _echo\nfrom bentoml.cli.deployment import get_deployment_sub_command\nfrom bentoml.cli.config import get_configuration_sub_command\nfrom bentoml.utils import Path, ProtoMessageToDict\nfrom bentoml.utils.log import configure_logging\nfrom bentoml.utils.usage_stats import track_cli\n\n\ndef escape_shell_params(param):\n    k, v = param.split('=')\n    v = re.sub(r'([^a-zA-Z0-9])', r'\\\\\\1', v)\n    return '{}={}'.format(k, v)\n\n\ndef create_bento_service_cli(archive_path=None):\n    # pylint: disable=unused-variable\n\n    @click.group(cls=BentoMLCommandGroup)\n    @click.option(\n        '-q',\n        '--quiet',\n        is_flag=True,\n        default=False,\n        help=\"Hide process logs and only print command results\",\n    )\n    @click.option(\n        '--verbose',\n        is_flag=True,\n        default=False,\n        help=\"Print verbose debugging information for BentoML developer\",\n    )\n    @click.version_option()\n    @click.pass_context\n    def bentoml_cli(ctx, verbose, quiet):\n        \"\"\"\n        BentoML CLI tool\n        \"\"\"\n        ctx.verbose = verbose\n        ctx.quiet = quiet\n\n        if verbose:\n            configure_logging(logging.DEBUG)\n        elif quiet:\n            configure_logging(logging.ERROR)\n        else:\n            configure_logging()  # use default setting in local bentoml.cfg\n\n    # Example Usage: bentoml API_NAME /SAVED_ARCHIVE_PATH --input=INPUT\n    @bentoml_cli.command(\n        default_command=True,\n        default_command_usage=\"API_NAME BENTO_ARCHIVE_PATH --input=INPUT\",\n        default_command_display_name=\"<API_NAME>\",\n        short_help=\"Run API function\",\n        help=\"Run a API defined in saved BentoArchive with cli args as input\",\n        context_settings=dict(ignore_unknown_options=True, allow_extra_args=True),\n    )\n    @click.argument(\"api-name\", type=click.STRING)\n    @conditional_argument(archive_path is None, \"archive-path\", type=click.STRING)\n    @click.option(\n        '--with-conda',\n        is_flag=True,\n        default=False,\n        help=\"Run API server in a BentoML managed Conda environment\",\n    )\n    @click.pass_context\n    def run(ctx, api_name, archive_path=archive_path, with_conda=False):\n        if with_conda:\n            config = load_bentoml_config(archive_path)\n            metadata = config['metadata']\n            env_name = metadata['service_name'] + '_' + metadata['service_version']\n\n            yaml = YAML()\n            yaml.default_flow_style = False\n            tmpf = tempfile.NamedTemporaryFile(delete=False)\n            env_path = tmpf.name\n            yaml.dump(config['env']['conda_env'], Path(env_path))\n\n            pip_req = os.path.join(archive_path, 'requirements.txt')\n\n            subprocess.call(\n                'command -v conda >/dev/null 2>&1 || {{ echo >&2 \"--with-conda '\n                'parameter requires conda but it\\'s not installed.\"; exit 1; }} && '\n                'conda env update -n {env_name} -f {env_file} && '\n                'conda init bash && '\n                'eval \"$(conda shell.bash hook)\" && '\n                'conda activate {env_name} && '\n                '{{ [ -f {pip_req} ] && pip install -r {pip_req} || echo \"no pip '\n                'dependencies.\"; }} &&'\n                'bentoml {api_name} {archive_path} {args}'.format(\n                    env_name=env_name,\n                    env_file=env_path,\n                    archive_path=archive_path,\n                    api_name=api_name,\n                    args=' '.join(map(escape_shell_params, ctx.args)),\n                    pip_req=pip_req,\n                ),\n                shell=True,\n            )\n            return\n\n        track_cli('run')\n\n        api = load_service_api(archive_path, api_name)\n        api.handle_cli(ctx.args)\n\n    # Example Usage: bentoml info /SAVED_ARCHIVE_PATH\n    @bentoml_cli.command(\n        help=\"List all APIs defined in the BentoService loaded from archive.\",\n        short_help=\"List APIs\",\n    )\n    @conditional_argument(archive_path is None, \"archive-path\", type=click.STRING)\n    def info(archive_path=archive_path):\n        \"\"\"\n        List all APIs defined in the BentoService loaded from archive\n        \"\"\"\n        track_cli('info')\n        bento_service_metadata_pb = load_bento_service_metadata(archive_path)\n        output = json.dumps(ProtoMessageToDict(bento_service_metadata_pb), indent=2)\n        _echo(output)\n\n    # Example usage: bentoml open-api-spec /SAVED_ARCHIVE_PATH\n    @bentoml_cli.command(\n        name=\"open-api-spec\",\n        help=\"Display API specification JSON in Open-API format\",\n        short_help=\"Display OpenAPI/Swagger JSON specs\",\n    )\n    @conditional_argument(archive_path is None, \"archive-path\", type=click.STRING)\n    def open_api_spec(archive_path=archive_path):\n        track_cli('open-api-spec')\n        bento_service = load(archive_path)\n\n        _echo(json.dumps(get_docs(bento_service), indent=2))\n\n    # Example Usage: bentoml serve ./SAVED_ARCHIVE_PATH --port=PORT\n    @bentoml_cli.command(\n        help=\"Start REST API server hosting BentoService loaded from archive\",\n        short_help=\"Start local rest server\",\n    )\n    @conditional_argument(archive_path is None, \"archive-path\", type=click.STRING)\n    @click.option(\n        \"--port\",\n        type=click.INT,\n        default=BentoAPIServer._DEFAULT_PORT,\n        help=\"The port to listen on for the REST api server, default is 5000.\",\n    )\n    @click.option(\n        '--with-conda',\n        is_flag=True,\n        default=False,\n        help=\"Run API server in a BentoML managed Conda environment\",\n    )\n    def serve(port, archive_path=archive_path, with_conda=False):\n        if with_conda:\n            config = load_bentoml_config(archive_path)\n            metadata = config['metadata']\n            env_name = metadata['service_name'] + '_' + metadata['service_version']\n            pip_req = os.path.join(archive_path, 'requirements.txt')\n\n            subprocess.call(\n                'command -v conda >/dev/null 2>&1 || {{ echo >&2 \"--with-conda '\n                'parameter requires conda but it\\'s not installed.\"; exit 1; }} && '\n                'conda env update -n {env_name} -f {env_file} && '\n                'conda init bash && '\n                'eval \"$(conda shell.bash hook)\" && '\n                'conda activate {env_name} && '\n                '{{ [ -f {pip_req} ] && pip install -r {pip_req} || echo \"no pip '\n                'dependencies.\"; }} &&'\n                'bentoml serve {archive_path} --port {port}'.format(\n                    env_name=env_name,\n                    env_file=os.path.join(archive_path, 'environment.yml'),\n                    archive_path=archive_path,\n                    port=port,\n                    pip_req=pip_req,\n                ),\n                shell=True,\n            )\n            return\n\n        track_cli('serve')\n\n        bento_service = load(archive_path)\n        server = BentoAPIServer(bento_service, port=port)\n        server.start()\n\n    # Example Usage:\n    # bentoml serve-gunicorn ./SAVED_ARCHIVE_PATH --port=PORT --workers=WORKERS\n    @bentoml_cli.command(\n        help=\"Start REST API gunicorn server hosting BentoService loaded from archive\",\n        short_help=\"Start local gunicorn server\",\n    )\n    @conditional_argument(archive_path is None, \"archive-path\", type=click.STRING)\n    @click.option(\"-p\", \"--port\", type=click.INT, default=None)\n    @click.option(\n        \"-w\",\n        \"--workers\",\n        type=click.INT,\n        default=None,\n        help=\"Number of workers will start for the gunicorn server\",\n    )\n    @click.option(\"--timeout\", type=click.INT, default=None)\n    @click.option(\n        '--with-conda',\n        is_flag=True,\n        default=False,\n        help=\"Run API server in a BentoML managed Conda environment\",\n    )\n    def serve_gunicorn(\n        port, workers, timeout, archive_path=archive_path, with_conda=False\n    ):\n        if with_conda:\n            config = load_bentoml_config(archive_path)\n            metadata = config['metadata']\n            env_name = metadata['service_name'] + '_' + metadata['service_version']\n            pip_req = os.path.join(archive_path, 'requirements.txt')\n\n            subprocess.call(\n                'command -v conda >/dev/null 2>&1 || {{ echo >&2 \"--with-conda '\n                'parameter requires conda but it\\'s not installed.\"; exit 1; }} && '\n                'conda env update -n {env_name} -f {env_file} && '\n                'conda init bash && '\n                'eval \"$(conda shell.bash hook)\" && '\n                'conda activate {env_name} && '\n                '{{ [ -f {pip_req} ] && pip install -r {pip_req} || echo \"no pip '\n                'dependencies.\"; }} &&'\n                'bentoml serve_gunicorn {archive_path} -p {port} -w {workers} '\n                '--timeout {timeout}'.format(\n                    env_name=env_name,\n                    env_file=os.path.join(archive_path, 'environment.yml'),\n                    archive_path=archive_path,\n                    port=port,\n                    workers=workers,\n                    timeout=timeout,\n                    pip_req=pip_req,\n                ),\n                shell=True,\n            )\n            return\n\n        track_cli('serve_gunicorn')\n\n        from bentoml.server.gunicorn_server import GunicornBentoServer\n        gunicorn_app = GunicornBentoServer(archive_path, port, workers, timeout)\n        gunicorn_app.run()\n\n    # pylint: enable=unused-variable\n    return bentoml_cli\n\n\ndef create_bentoml_cli():\n    _cli = create_bento_service_cli()\n\n    # Commands created here aren't mean to be used from generated service archive. They\n    # are used as part of BentoML cli commands only.\n\n    deployment_sub_command = get_deployment_sub_command()\n    config_sub_command = get_configuration_sub_command()\n    _cli.add_command(config_sub_command)\n    _cli.add_command(deployment_sub_command)\n\n    return _cli\n\n\ncli = create_bentoml_cli()\n\nif __name__ == \"__main__\":\n    cli()  # pylint: disable=no-value-for-parameter\n", "path": "bentoml/cli/__init__.py"}]}