Skip to content

Commit

Permalink
More fixes for dotenv compatibility
Browse files Browse the repository at this point in the history
- Handle env files with empty and malformed lines. Just skip those and
  ignore them.
  • Loading branch information
wynnw committed Aug 23, 2023
1 parent 40aeb02 commit f10b179
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions supervisor/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -1992,30 +1992,38 @@ def load_external_environment_definition_for_config(cls, config):

for line in envdata.splitlines():
line = line.strip()
if line.startswith('#'): # ignore comments
if not line or line.startswith('#'): # ignore comments and empty lines
continue

key, val = [s.strip() for s in line.split("=", 1)]
if key:
# for compatibility with .env files and the npm dotenv library, if the value is quoted let's
# unquote it. Also, double quoted strings get embedded '\\n' chars expanded to real newlines.
# Don't strip whitespace inside quoted values.
if val.startswith("\""):
val = val.strip("\"")
line_parts = line.split("=", 1) # ignore malformed lines
if len(line_parts) != 2:
continue

key, val = [s.strip() for s in line_parts]
if not key or not val:
continue

# expand embedded '\n' chars into actual newlines
val.replace("\\n", "\n")
# for compatibility with .env files and the npm dotenv library, if the value is quoted let's
# unquote it. Also, double quoted strings get embedded '\\n' chars expanded to real newlines.
# Don't strip whitespace inside quoted values.
if val.startswith("\""):
val = val.strip("\"")

elif val.startswith("'"):
val = val.strip("'")
elif val.startswith("`"):
val = val.strip("`")
# expand embedded '\n' chars into actual newlines
val.replace("\\n", "\n")

extra_env[key.upper()] = val
elif val.startswith("'"):
val = val.strip("'")
elif val.startswith("`"):
val = val.strip("`")

extra_env[key.upper()] = val

if extra_env:
print(f"updating process env with: {extra_env}")
env.update(extra_env)


return env

class EventListenerConfig(ProcessConfig):
Expand Down

0 comments on commit f10b179

Please sign in to comment.