We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
az webapp new throws an exception when it can't detect a project type.
### Extension name (the extension in question)
webapp

### Description of issue (in as much detail as possible)
This should fail gracefully:

```
bash-4.3# cd /
bash-4.3# ls
appp       bin        etc        lib        media      proc       run        srv        tmp        var
azure-cli  dev        home       linuxrc    mnt        root       sbin       sys        usr
bash-4.3# az webapp new -n foo
list index out of range
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/knack/cli.py", line 194, in invoke
    cmd_result = self.invocation.execute(args)
  File "/usr/local/lib/python3.6/site-packages/azure/cli/core/commands/__init__.py", line 331, in execute
    six.reraise(*sys.exc_info())
  File "/usr/local/lib/python3.6/site-packages/six.py", line 693, in reraise
    raise value
  File "/usr/local/lib/python3.6/site-packages/azure/cli/core/commands/__init__.py", line 304, in execute
    result = cmd(params)
  File "/usr/local/lib/python3.6/site-packages/azure/cli/core/commands/__init__.py", line 168, in __call__
    return super(AzCliCommand, self).__call__(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/knack/commands.py", line 109, in __call__
    return self.handler(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/azure/cli/core/__init__.py", line 348, in default_command_handler
    result = op(**command_args)
  File "/root/.azure/cliextensions/webapp/azext_webapp/custom.py", line 50, in create_deploy_webapp
    lang_details = get_lang_from_content(src_dir)
  File "/root/.azure/cliextensions/webapp/azext_webapp/create_util.py", line 113, in get_lang_from_content
    package_netcore_file = os.path.join(src_path, glob.glob("*.csproj")[0])
IndexError: list index out of range
bash-4.3# 
```

Steps to reproduce: run `az webapp new -n foo` in a *non-empty* directory that does not contain a `package.json` or a `*.csproj` file.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `src/webapp/azext_webapp/create_util.py`
Content:
```
1 # --------------------------------------------------------------------------------------------
2 # Copyright (c) Microsoft Corporation. All rights reserved.
3 # Licensed under the MIT License. See License.txt in the project root for license information.
4 # --------------------------------------------------------------------------------------------
5 
6 import os
7 import zipfile
8 from azure.cli.core.commands.client_factory import get_mgmt_service_client
9 from azure.mgmt.resource.resources.models import ResourceGroup
10 from ._constants import (
11     NETCORE_VERSION_DEFAULT,
12     NETCORE_VERSIONS,
13     NODE_VERSION_DEFAULT,
14     NODE_VERSIONS,
15     NETCORE_RUNTIME_NAME,
16     NODE_RUNTIME_NAME)
17 
18 
19 def _resource_client_factory(cli_ctx, **_):
20     from azure.cli.core.profiles import ResourceType
21     return get_mgmt_service_client(cli_ctx, ResourceType.MGMT_RESOURCE_RESOURCES)
22 
23 
24 def web_client_factory(cli_ctx, **_):
25     from azure.mgmt.web import WebSiteManagementClient
26     return get_mgmt_service_client(cli_ctx, WebSiteManagementClient)
27 
28 
29 def zip_contents_from_dir(dirPath, lang):
30     relroot = os.path.abspath(os.path.join(dirPath, os.pardir))
31     path_and_file = os.path.splitdrive(dirPath)[1]
32     file_val = os.path.split(path_and_file)[1]
33     zip_file_path = relroot + "\\" + file_val + ".zip"
34     abs_src = os.path.abspath(dirPath)
35     with zipfile.ZipFile("{}".format(zip_file_path), "w", zipfile.ZIP_DEFLATED) as zf:
36         for dirname, subdirs, files in os.walk(dirPath):
37             # skip node_modules folder for Node apps,
38             # since zip_deployment will perfom the build operation
39             if lang.lower() == NODE_RUNTIME_NAME and 'node_modules' in subdirs:
40                 subdirs.remove('node_modules')
41             elif lang.lower() == NETCORE_RUNTIME_NAME:
42                 if 'bin' in subdirs:
43                     subdirs.remove('bin')
44                 elif 'obj' in subdirs:
45                     subdirs.remove('obj')
46             for filename in files:
47                 absname = os.path.abspath(os.path.join(dirname, filename))
48                 arcname = absname[len(abs_src) + 1:]
49                 zf.write(absname, arcname)
50     return zip_file_path
51 
52 
53 def get_runtime_version_details(file_path, lang_name):
54     version_detected = None
55     version_to_create = None
56     if lang_name.lower() == NETCORE_RUNTIME_NAME:
57         # method returns list in DESC, pick the first
58         version_detected = parse_netcore_version(file_path)[0]
59         version_to_create = detect_netcore_version_tocreate(version_detected)
60     elif lang_name.lower() == NODE_RUNTIME_NAME:
61         version_detected = parse_node_version(file_path)[0]
62         version_to_create = detect_node_version_tocreate(version_detected)
63     return {'detected': version_detected, 'to_create': version_to_create}
64 
65 
66 def create_resource_group(cmd, rg_name, location):
67     rcf = _resource_client_factory(cmd.cli_ctx)
68     rg_params = ResourceGroup(location=location)
69     return rcf.resource_groups.create_or_update(rg_name, rg_params)
70 
71 
72 def check_resource_group_exists(cmd, rg_name):
73     rcf = _resource_client_factory(cmd.cli_ctx)
74     return rcf.resource_groups.check_existence(rg_name)
75 
76 
77 def check_resource_group_supports_os(cmd, rg_name, location, is_linux):
78     # get all appservice plans from RG
79     client = web_client_factory(cmd.cli_ctx)
80     plans = list(client.app_service_plans.list_by_resource_group(rg_name))
81     for item in plans:
82         # for Linux if an app with reserved==False exists, ASP doesn't support Linux
83         if is_linux and item.location == location and not item.reserved:
84             return False
85         elif not is_linux and item.location == location and item.reserved:
86             return False
87     return True
88 
89 
90 def check_if_asp_exists(cmd, rg_name, asp_name):
91     # get all appservice plans from RG
92     client = web_client_factory(cmd.cli_ctx)
93     for item in list(client.app_service_plans.list_by_resource_group(rg_name)):
94         if item.name == asp_name:
95             return True
96     return False
97 
98 
99 def check_app_exists(cmd, rg_name, app_name):
100     client = web_client_factory(cmd.cli_ctx)
101     for item in list(client.web_apps.list_by_resource_group(rg_name)):
102         if item.name == app_name:
103             return True
104     return False
105 
106 
107 def get_lang_from_content(src_path):
108     import glob
109     # NODE: package.json should exisit in the application root dir
110     # NETCORE: NETCORE.csproj should exist in the root dir
111     runtime_details_dict = dict.fromkeys(['language', 'file_loc', 'default_sku'])
112     package_json_file = os.path.join(src_path, 'package.json')
113     package_netcore_file = os.path.join(src_path, glob.glob("*.csproj")[0])
114     if os.path.isfile(package_json_file):
115         runtime_details_dict['language'] = NODE_RUNTIME_NAME
116         runtime_details_dict['file_loc'] = package_json_file
117         runtime_details_dict['default_sku'] = 'S1'
118     elif os.path.isfile(package_netcore_file):
119         runtime_details_dict['language'] = NETCORE_RUNTIME_NAME
120         runtime_details_dict['file_loc'] = package_netcore_file
121         runtime_details_dict['default_sku'] = 'F1'
122     return runtime_details_dict
123 
124 
125 def parse_netcore_version(file_path):
126     import xml.etree.ElementTree as ET
127     import re
128     version_detected = ['0.0']
129     parsed_file = ET.parse(file_path)
130     root = parsed_file.getroot()
131     for target_ver in root.iter('TargetFramework'):
132         version_detected = re.findall(r"\d+\.\d+", target_ver.text)
133     # incase of multiple versions detected, return list in descending order
134     version_detected = sorted(version_detected, key=float, reverse=True)
135     return version_detected
136 
137 
138 def parse_node_version(file_path):
139     import json
140     import re
141     version_detected = ['0.0']
142     with open(file_path) as data_file:
143         data = []
144         for d in find_key_in_json(json.load(data_file), 'node'):
145             non_decimal = re.compile(r'[^\d.]+')
146             # remove the string ~ or  > that sometimes exists in version value
147             c = non_decimal.sub('', d)
148             # reduce the version to '6.0' from '6.0.0'
149             data.append(c[:3])
150         version_detected = sorted(data, key=float, reverse=True)
151     return version_detected
152 
153 
154 def detect_netcore_version_tocreate(detected_ver):
155     if detected_ver in NETCORE_VERSIONS:
156         return detected_ver
157     return NETCORE_VERSION_DEFAULT
158 
159 
160 def detect_node_version_tocreate(detected_ver):
161     if detected_ver in NODE_VERSIONS:
162         return detected_ver
163     # get major version & get the closest version from supported list
164     major_ver = float(detected_ver.split('.')[0])
165     if major_ver < 4:
166         return NODE_VERSION_DEFAULT
167     elif major_ver >= 4 and major_ver < 6:
168         return '4.5'
169     elif major_ver >= 6 and major_ver < 8:
170         return '6.9'
171     return NODE_VERSION_DEFAULT
172 
173 
174 def find_key_in_json(json_data, key):
175     for k, v in json_data.items():
176         if key in k:
177             yield v
178         elif isinstance(v, dict):
179             for id_val in find_key_in_json(v, key):
180                 yield id_val
181
```
--- 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/src/webapp/azext_webapp/create_util.py b/src/webapp/azext_webapp/create_util.py
--- a/src/webapp/azext_webapp/create_util.py
+++ b/src/webapp/azext_webapp/create_util.py
@@ -106,16 +106,17 @@
 
 def get_lang_from_content(src_path):
     import glob
-    # NODE: package.json should exisit in the application root dir
-    # NETCORE: NETCORE.csproj should exist in the root dir
+    # NODE: package.json should exist in the application root dir
+    # NETCORE: *.csproj should exist in the application root dir
     runtime_details_dict = dict.fromkeys(['language', 'file_loc', 'default_sku'])
     package_json_file = os.path.join(src_path, 'package.json')
-    package_netcore_file = os.path.join(src_path, glob.glob("*.csproj")[0])
+    package_netcore_glob = glob.glob("*.csproj")
     if os.path.isfile(package_json_file):
         runtime_details_dict['language'] = NODE_RUNTIME_NAME
         runtime_details_dict['file_loc'] = package_json_file
         runtime_details_dict['default_sku'] = 'S1'
-    elif os.path.isfile(package_netcore_file):
+    elif package_netcore_glob:
+        package_netcore_file = os.path.join(src_path, package_netcore_glob[0])
         runtime_details_dict['language'] = NETCORE_RUNTIME_NAME
         runtime_details_dict['file_loc'] = package_netcore_file
         runtime_details_dict['default_sku'] = 'F1'
@@ -138,7 +139,6 @@
 def parse_node_version(file_path):
     import json
     import re
-    version_detected = ['0.0']
     with open(file_path) as data_file:
         data = []
         for d in find_key_in_json(json.load(data_file), 'node'):
@@ -148,7 +148,7 @@
             # reduce the version to '6.0' from '6.0.0'
             data.append(c[:3])
         version_detected = sorted(data, key=float, reverse=True)
-    return version_detected
+    return version_detected or ['0.0']
 
 
 def detect_netcore_version_tocreate(detected_ver):

{"golden_diff": "diff --git a/src/webapp/azext_webapp/create_util.py b/src/webapp/azext_webapp/create_util.py\n--- a/src/webapp/azext_webapp/create_util.py\n+++ b/src/webapp/azext_webapp/create_util.py\n@@ -106,16 +106,17 @@\n \n def get_lang_from_content(src_path):\n     import glob\n-    # NODE: package.json should exisit in the application root dir\n-    # NETCORE: NETCORE.csproj should exist in the root dir\n+    # NODE: package.json should exist in the application root dir\n+    # NETCORE: *.csproj should exist in the application root dir\n     runtime_details_dict = dict.fromkeys(['language', 'file_loc', 'default_sku'])\n     package_json_file = os.path.join(src_path, 'package.json')\n-    package_netcore_file = os.path.join(src_path, glob.glob(\"*.csproj\")[0])\n+    package_netcore_glob = glob.glob(\"*.csproj\")\n     if os.path.isfile(package_json_file):\n         runtime_details_dict['language'] = NODE_RUNTIME_NAME\n         runtime_details_dict['file_loc'] = package_json_file\n         runtime_details_dict['default_sku'] = 'S1'\n-    elif os.path.isfile(package_netcore_file):\n+    elif package_netcore_glob:\n+        package_netcore_file = os.path.join(src_path, package_netcore_glob[0])\n         runtime_details_dict['language'] = NETCORE_RUNTIME_NAME\n         runtime_details_dict['file_loc'] = package_netcore_file\n         runtime_details_dict['default_sku'] = 'F1'\n@@ -138,7 +139,6 @@\n def parse_node_version(file_path):\n     import json\n     import re\n-    version_detected = ['0.0']\n     with open(file_path) as data_file:\n         data = []\n         for d in find_key_in_json(json.load(data_file), 'node'):\n@@ -148,7 +148,7 @@\n             # reduce the version to '6.0' from '6.0.0'\n             data.append(c[:3])\n         version_detected = sorted(data, key=float, reverse=True)\n-    return version_detected\n+    return version_detected or ['0.0']\n \n \n def detect_netcore_version_tocreate(detected_ver):\n", "issue": "az webapp new throws an exception when it can't detect a project type.\n### Extension name (the extension in question)\r\nwebapp\r\n\r\n### Description of issue (in as much detail as possible)\r\nThis should fail gracefully:\r\n\r\n```\r\nbash-4.3# cd /\r\nbash-4.3# ls\r\nappp       bin        etc        lib        media      proc       run        srv        tmp        var\r\nazure-cli  dev        home       linuxrc    mnt        root       sbin       sys        usr\r\nbash-4.3# az webapp new -n foo\r\nlist index out of range\r\nTraceback (most recent call last):\r\n  File \"/usr/local/lib/python3.6/site-packages/knack/cli.py\", line 194, in invoke\r\n    cmd_result = self.invocation.execute(args)\r\n  File \"/usr/local/lib/python3.6/site-packages/azure/cli/core/commands/__init__.py\", line 331, in execute\r\n    six.reraise(*sys.exc_info())\r\n  File \"/usr/local/lib/python3.6/site-packages/six.py\", line 693, in reraise\r\n    raise value\r\n  File \"/usr/local/lib/python3.6/site-packages/azure/cli/core/commands/__init__.py\", line 304, in execute\r\n    result = cmd(params)\r\n  File \"/usr/local/lib/python3.6/site-packages/azure/cli/core/commands/__init__.py\", line 168, in __call__\r\n    return super(AzCliCommand, self).__call__(*args, **kwargs)\r\n  File \"/usr/local/lib/python3.6/site-packages/knack/commands.py\", line 109, in __call__\r\n    return self.handler(*args, **kwargs)\r\n  File \"/usr/local/lib/python3.6/site-packages/azure/cli/core/__init__.py\", line 348, in default_command_handler\r\n    result = op(**command_args)\r\n  File \"/root/.azure/cliextensions/webapp/azext_webapp/custom.py\", line 50, in create_deploy_webapp\r\n    lang_details = get_lang_from_content(src_dir)\r\n  File \"/root/.azure/cliextensions/webapp/azext_webapp/create_util.py\", line 113, in get_lang_from_content\r\n    package_netcore_file = os.path.join(src_path, glob.glob(\"*.csproj\")[0])\r\nIndexError: list index out of range\r\nbash-4.3# \r\n```\r\n\r\nSteps to reproduce: run `az webapp new -n foo` in a *non-empty* directory that does not contain a `package.json` or a `*.csproj` file.\r\n\n", "before_files": [{"content": "# --------------------------------------------------------------------------------------------\n# Copyright (c) Microsoft Corporation. All rights reserved.\n# Licensed under the MIT License. See License.txt in the project root for license information.\n# --------------------------------------------------------------------------------------------\n\nimport os\nimport zipfile\nfrom azure.cli.core.commands.client_factory import get_mgmt_service_client\nfrom azure.mgmt.resource.resources.models import ResourceGroup\nfrom ._constants import (\n    NETCORE_VERSION_DEFAULT,\n    NETCORE_VERSIONS,\n    NODE_VERSION_DEFAULT,\n    NODE_VERSIONS,\n    NETCORE_RUNTIME_NAME,\n    NODE_RUNTIME_NAME)\n\n\ndef _resource_client_factory(cli_ctx, **_):\n    from azure.cli.core.profiles import ResourceType\n    return get_mgmt_service_client(cli_ctx, ResourceType.MGMT_RESOURCE_RESOURCES)\n\n\ndef web_client_factory(cli_ctx, **_):\n    from azure.mgmt.web import WebSiteManagementClient\n    return get_mgmt_service_client(cli_ctx, WebSiteManagementClient)\n\n\ndef zip_contents_from_dir(dirPath, lang):\n    relroot = os.path.abspath(os.path.join(dirPath, os.pardir))\n    path_and_file = os.path.splitdrive(dirPath)[1]\n    file_val = os.path.split(path_and_file)[1]\n    zip_file_path = relroot + \"\\\\\" + file_val + \".zip\"\n    abs_src = os.path.abspath(dirPath)\n    with zipfile.ZipFile(\"{}\".format(zip_file_path), \"w\", zipfile.ZIP_DEFLATED) as zf:\n        for dirname, subdirs, files in os.walk(dirPath):\n            # skip node_modules folder for Node apps,\n            # since zip_deployment will perfom the build operation\n            if lang.lower() == NODE_RUNTIME_NAME and 'node_modules' in subdirs:\n                subdirs.remove('node_modules')\n            elif lang.lower() == NETCORE_RUNTIME_NAME:\n                if 'bin' in subdirs:\n                    subdirs.remove('bin')\n                elif 'obj' in subdirs:\n                    subdirs.remove('obj')\n            for filename in files:\n                absname = os.path.abspath(os.path.join(dirname, filename))\n                arcname = absname[len(abs_src) + 1:]\n                zf.write(absname, arcname)\n    return zip_file_path\n\n\ndef get_runtime_version_details(file_path, lang_name):\n    version_detected = None\n    version_to_create = None\n    if lang_name.lower() == NETCORE_RUNTIME_NAME:\n        # method returns list in DESC, pick the first\n        version_detected = parse_netcore_version(file_path)[0]\n        version_to_create = detect_netcore_version_tocreate(version_detected)\n    elif lang_name.lower() == NODE_RUNTIME_NAME:\n        version_detected = parse_node_version(file_path)[0]\n        version_to_create = detect_node_version_tocreate(version_detected)\n    return {'detected': version_detected, 'to_create': version_to_create}\n\n\ndef create_resource_group(cmd, rg_name, location):\n    rcf = _resource_client_factory(cmd.cli_ctx)\n    rg_params = ResourceGroup(location=location)\n    return rcf.resource_groups.create_or_update(rg_name, rg_params)\n\n\ndef check_resource_group_exists(cmd, rg_name):\n    rcf = _resource_client_factory(cmd.cli_ctx)\n    return rcf.resource_groups.check_existence(rg_name)\n\n\ndef check_resource_group_supports_os(cmd, rg_name, location, is_linux):\n    # get all appservice plans from RG\n    client = web_client_factory(cmd.cli_ctx)\n    plans = list(client.app_service_plans.list_by_resource_group(rg_name))\n    for item in plans:\n        # for Linux if an app with reserved==False exists, ASP doesn't support Linux\n        if is_linux and item.location == location and not item.reserved:\n            return False\n        elif not is_linux and item.location == location and item.reserved:\n            return False\n    return True\n\n\ndef check_if_asp_exists(cmd, rg_name, asp_name):\n    # get all appservice plans from RG\n    client = web_client_factory(cmd.cli_ctx)\n    for item in list(client.app_service_plans.list_by_resource_group(rg_name)):\n        if item.name == asp_name:\n            return True\n    return False\n\n\ndef check_app_exists(cmd, rg_name, app_name):\n    client = web_client_factory(cmd.cli_ctx)\n    for item in list(client.web_apps.list_by_resource_group(rg_name)):\n        if item.name == app_name:\n            return True\n    return False\n\n\ndef get_lang_from_content(src_path):\n    import glob\n    # NODE: package.json should exisit in the application root dir\n    # NETCORE: NETCORE.csproj should exist in the root dir\n    runtime_details_dict = dict.fromkeys(['language', 'file_loc', 'default_sku'])\n    package_json_file = os.path.join(src_path, 'package.json')\n    package_netcore_file = os.path.join(src_path, glob.glob(\"*.csproj\")[0])\n    if os.path.isfile(package_json_file):\n        runtime_details_dict['language'] = NODE_RUNTIME_NAME\n        runtime_details_dict['file_loc'] = package_json_file\n        runtime_details_dict['default_sku'] = 'S1'\n    elif os.path.isfile(package_netcore_file):\n        runtime_details_dict['language'] = NETCORE_RUNTIME_NAME\n        runtime_details_dict['file_loc'] = package_netcore_file\n        runtime_details_dict['default_sku'] = 'F1'\n    return runtime_details_dict\n\n\ndef parse_netcore_version(file_path):\n    import xml.etree.ElementTree as ET\n    import re\n    version_detected = ['0.0']\n    parsed_file = ET.parse(file_path)\n    root = parsed_file.getroot()\n    for target_ver in root.iter('TargetFramework'):\n        version_detected = re.findall(r\"\\d+\\.\\d+\", target_ver.text)\n    # incase of multiple versions detected, return list in descending order\n    version_detected = sorted(version_detected, key=float, reverse=True)\n    return version_detected\n\n\ndef parse_node_version(file_path):\n    import json\n    import re\n    version_detected = ['0.0']\n    with open(file_path) as data_file:\n        data = []\n        for d in find_key_in_json(json.load(data_file), 'node'):\n            non_decimal = re.compile(r'[^\\d.]+')\n            # remove the string ~ or  > that sometimes exists in version value\n            c = non_decimal.sub('', d)\n            # reduce the version to '6.0' from '6.0.0'\n            data.append(c[:3])\n        version_detected = sorted(data, key=float, reverse=True)\n    return version_detected\n\n\ndef detect_netcore_version_tocreate(detected_ver):\n    if detected_ver in NETCORE_VERSIONS:\n        return detected_ver\n    return NETCORE_VERSION_DEFAULT\n\n\ndef detect_node_version_tocreate(detected_ver):\n    if detected_ver in NODE_VERSIONS:\n        return detected_ver\n    # get major version & get the closest version from supported list\n    major_ver = float(detected_ver.split('.')[0])\n    if major_ver < 4:\n        return NODE_VERSION_DEFAULT\n    elif major_ver >= 4 and major_ver < 6:\n        return '4.5'\n    elif major_ver >= 6 and major_ver < 8:\n        return '6.9'\n    return NODE_VERSION_DEFAULT\n\n\ndef find_key_in_json(json_data, key):\n    for k, v in json_data.items():\n        if key in k:\n            yield v\n        elif isinstance(v, dict):\n            for id_val in find_key_in_json(v, key):\n                yield id_val\n", "path": "src/webapp/azext_webapp/create_util.py"}], "after_files": [{"content": "# --------------------------------------------------------------------------------------------\n# Copyright (c) Microsoft Corporation. All rights reserved.\n# Licensed under the MIT License. See License.txt in the project root for license information.\n# --------------------------------------------------------------------------------------------\n\nimport os\nimport zipfile\nfrom azure.cli.core.commands.client_factory import get_mgmt_service_client\nfrom azure.mgmt.resource.resources.models import ResourceGroup\nfrom ._constants import (\n    NETCORE_VERSION_DEFAULT,\n    NETCORE_VERSIONS,\n    NODE_VERSION_DEFAULT,\n    NODE_VERSIONS,\n    NETCORE_RUNTIME_NAME,\n    NODE_RUNTIME_NAME)\n\n\ndef _resource_client_factory(cli_ctx, **_):\n    from azure.cli.core.profiles import ResourceType\n    return get_mgmt_service_client(cli_ctx, ResourceType.MGMT_RESOURCE_RESOURCES)\n\n\ndef web_client_factory(cli_ctx, **_):\n    from azure.mgmt.web import WebSiteManagementClient\n    return get_mgmt_service_client(cli_ctx, WebSiteManagementClient)\n\n\ndef zip_contents_from_dir(dirPath, lang):\n    relroot = os.path.abspath(os.path.join(dirPath, os.pardir))\n    path_and_file = os.path.splitdrive(dirPath)[1]\n    file_val = os.path.split(path_and_file)[1]\n    zip_file_path = relroot + \"\\\\\" + file_val + \".zip\"\n    abs_src = os.path.abspath(dirPath)\n    with zipfile.ZipFile(\"{}\".format(zip_file_path), \"w\", zipfile.ZIP_DEFLATED) as zf:\n        for dirname, subdirs, files in os.walk(dirPath):\n            # skip node_modules folder for Node apps,\n            # since zip_deployment will perfom the build operation\n            if lang.lower() == NODE_RUNTIME_NAME and 'node_modules' in subdirs:\n                subdirs.remove('node_modules')\n            elif lang.lower() == NETCORE_RUNTIME_NAME:\n                if 'bin' in subdirs:\n                    subdirs.remove('bin')\n                elif 'obj' in subdirs:\n                    subdirs.remove('obj')\n            for filename in files:\n                absname = os.path.abspath(os.path.join(dirname, filename))\n                arcname = absname[len(abs_src) + 1:]\n                zf.write(absname, arcname)\n    return zip_file_path\n\n\ndef get_runtime_version_details(file_path, lang_name):\n    version_detected = None\n    version_to_create = None\n    if lang_name.lower() == NETCORE_RUNTIME_NAME:\n        # method returns list in DESC, pick the first\n        version_detected = parse_netcore_version(file_path)[0]\n        version_to_create = detect_netcore_version_tocreate(version_detected)\n    elif lang_name.lower() == NODE_RUNTIME_NAME:\n        version_detected = parse_node_version(file_path)[0]\n        version_to_create = detect_node_version_tocreate(version_detected)\n    return {'detected': version_detected, 'to_create': version_to_create}\n\n\ndef create_resource_group(cmd, rg_name, location):\n    rcf = _resource_client_factory(cmd.cli_ctx)\n    rg_params = ResourceGroup(location=location)\n    return rcf.resource_groups.create_or_update(rg_name, rg_params)\n\n\ndef check_resource_group_exists(cmd, rg_name):\n    rcf = _resource_client_factory(cmd.cli_ctx)\n    return rcf.resource_groups.check_existence(rg_name)\n\n\ndef check_resource_group_supports_os(cmd, rg_name, location, is_linux):\n    # get all appservice plans from RG\n    client = web_client_factory(cmd.cli_ctx)\n    plans = list(client.app_service_plans.list_by_resource_group(rg_name))\n    for item in plans:\n        # for Linux if an app with reserved==False exists, ASP doesn't support Linux\n        if is_linux and item.location == location and not item.reserved:\n            return False\n        elif not is_linux and item.location == location and item.reserved:\n            return False\n    return True\n\n\ndef check_if_asp_exists(cmd, rg_name, asp_name):\n    # get all appservice plans from RG\n    client = web_client_factory(cmd.cli_ctx)\n    for item in list(client.app_service_plans.list_by_resource_group(rg_name)):\n        if item.name == asp_name:\n            return True\n    return False\n\n\ndef check_app_exists(cmd, rg_name, app_name):\n    client = web_client_factory(cmd.cli_ctx)\n    for item in list(client.web_apps.list_by_resource_group(rg_name)):\n        if item.name == app_name:\n            return True\n    return False\n\n\ndef get_lang_from_content(src_path):\n    import glob\n    # NODE: package.json should exist in the application root dir\n    # NETCORE: *.csproj should exist in the application root dir\n    runtime_details_dict = dict.fromkeys(['language', 'file_loc', 'default_sku'])\n    package_json_file = os.path.join(src_path, 'package.json')\n    package_netcore_glob = glob.glob(\"*.csproj\")\n    if os.path.isfile(package_json_file):\n        runtime_details_dict['language'] = NODE_RUNTIME_NAME\n        runtime_details_dict['file_loc'] = package_json_file\n        runtime_details_dict['default_sku'] = 'S1'\n    elif package_netcore_glob:\n        package_netcore_file = os.path.join(src_path, package_netcore_glob[0])\n        runtime_details_dict['language'] = NETCORE_RUNTIME_NAME\n        runtime_details_dict['file_loc'] = package_netcore_file\n        runtime_details_dict['default_sku'] = 'F1'\n    return runtime_details_dict\n\n\ndef parse_netcore_version(file_path):\n    import xml.etree.ElementTree as ET\n    import re\n    version_detected = ['0.0']\n    parsed_file = ET.parse(file_path)\n    root = parsed_file.getroot()\n    for target_ver in root.iter('TargetFramework'):\n        version_detected = re.findall(r\"\\d+\\.\\d+\", target_ver.text)\n    # incase of multiple versions detected, return list in descending order\n    version_detected = sorted(version_detected, key=float, reverse=True)\n    return version_detected\n\n\ndef parse_node_version(file_path):\n    import json\n    import re\n    with open(file_path) as data_file:\n        data = []\n        for d in find_key_in_json(json.load(data_file), 'node'):\n            non_decimal = re.compile(r'[^\\d.]+')\n            # remove the string ~ or  > that sometimes exists in version value\n            c = non_decimal.sub('', d)\n            # reduce the version to '6.0' from '6.0.0'\n            data.append(c[:3])\n        version_detected = sorted(data, key=float, reverse=True)\n    return version_detected or ['0.0']\n\n\ndef detect_netcore_version_tocreate(detected_ver):\n    if detected_ver in NETCORE_VERSIONS:\n        return detected_ver\n    return NETCORE_VERSION_DEFAULT\n\n\ndef detect_node_version_tocreate(detected_ver):\n    if detected_ver in NODE_VERSIONS:\n        return detected_ver\n    # get major version & get the closest version from supported list\n    major_ver = float(detected_ver.split('.')[0])\n    if major_ver < 4:\n        return NODE_VERSION_DEFAULT\n    elif major_ver >= 4 and major_ver < 6:\n        return '4.5'\n    elif major_ver >= 6 and major_ver < 8:\n        return '6.9'\n    return NODE_VERSION_DEFAULT\n\n\ndef find_key_in_json(json_data, key):\n    for k, v in json_data.items():\n        if key in k:\n            yield v\n        elif isinstance(v, dict):\n            for id_val in find_key_in_json(v, key):\n                yield id_val\n", "path": "src/webapp/azext_webapp/create_util.py"}]}