Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .dazelrc.local support #48

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ Even bazel is not required!

## Configuration

You can configure dazel in two ways (or combine):
* A .dazelrc file in the current directory.
You can configure dazel in three ways (listed in order of ascending precedence):
* A `.dazelrc` file in the current directory.
* A `~/.dazelrc` file in the user's `$HOME` directory.
* A `.dazelrc.local` file in the current directory (this is for local user overrides - _should be .gitignored if used_).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would make more sense to have the "local" file in the homedir (so that it'll be loaded from ~/.dazelrc.
It's the more standard way of doing that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had considered that but at least for our use case it seems that these local overrides may/will change between projects which use dazel. I can change this if you feel that the local file in the $HOME dir makes the most sense, but perhaps it makes sense to have a few different levels of options.
Thoughts?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm working on implementing this when I have a few minutes. Standby for updates

* Environment variables with the configuration parameters mentioned below.

Note that specific environment variables supercede the values in the .dazelrc file.
You may combine these methods, but note that parameter values will supercede each other in the order listed above (i.e: specific parameters specified by environment variables will supercede values in both the `.dazelrc` and the `.dazelrc.local`). Parameters whose values are lists such as `DAZEL_VOLUMES` will override each other as opposed to being merged.

The possible parameters to set are (with their defaults):
```python
Expand Down
42 changes: 28 additions & 14 deletions dazel.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
import collections


DAZEL_RC_FILE = ".dazelrc"
# Dazel RC files and their respective environment variable path overrides (in order of ascending precedence)
DAZEL_RC_FILE_ENVS = [
("DAZEL_RC_FILE", ".dazelrc"),
("DAZEL_RC_USER_FILE", "~/.dazelrc"),
("DAZEL_RC_LOCAL_FILE", ".dazelrc.local")
]

DAZEL_RUN_FILE = ".dazel_run"
BAZEL_WORKSPACE_FILE = "WORKSPACE"

Expand Down Expand Up @@ -99,7 +105,9 @@ def __init__(self, instance_name, image_name, run_command, docker_command, docke

@classmethod
def from_config(cls):
config = cls._config_from_file()
config = {}
for (env_var, default_path) in DAZEL_RC_FILE_ENVS:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for parenthesis

config.update(cls._config_from_file(filename=default_path, env_var=env_var))
config.update(cls._config_from_environment())
return DockerInstance(
instance_name=config.get("DAZEL_INSTANCE_NAME", DEFAULT_INSTANCE_NAME),
Expand Down Expand Up @@ -487,19 +495,25 @@ def _with_docker_machine(self, cmd):
return "eval $(docker-machine env %s) && (%s)" % (self.docker_machine, cmd)

@classmethod
def _config_from_file(cls):
def _config_from_file(cls, filename, env_var):
"""Creates a configuration from a .dazelrc file."""
directory = cls._find_workspace_directory()
local_dazelrc_path = os.path.join(directory, DAZEL_RC_FILE)
dazelrc_path = os.environ.get("DAZEL_RC_FILE", local_dazelrc_path)

if not os.path.exists(dazelrc_path):
return { "DAZEL_DIRECTORY": os.environ.get("DAZEL_DIRECTORY", directory) }

config = {}
with open(dazelrc_path, "r") as dazelrc:
exec(dazelrc.read(), config)
config["DAZEL_DIRECTORY"] = os.environ.get("DAZEL_DIRECTORY", directory)
dazel_dir = cls._find_workspace_directory()
# Build the expanded path for the config file and join it to the dazel_dir if
# necessary
expanded_path = os.path.expanduser(filename)
if expanded_path.startswith('/'):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use consistent quotes (use double quotes here)

dazelrc_path = expanded_path
else:
dazelrc_path = os.path.join(dazel_dir, filename)
# If a custom config path was defined by the env_var, use that path
config_path = os.environ.get(env_var, dazelrc_path)

# Use the workspace directory by default. Lower precendence than DAZEL_DIRECTORY env var.
config = { "DAZEL_DIRECTORY": dazel_dir }
if os.path.exists(config_path):
with open(config_path, "r") as dazelrc:
# This will merge/overwrite any existing values
exec(dazelrc.read(), config)
return config

@classmethod
Expand Down
Binary file added dist/dazel-0.0.37-py2.py3-none-any.whl
Binary file not shown.
Binary file added dist/dazel-0.0.37.tar.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# Versions should comply with PEP440. For a discussion on single-sourcing
# the version across setup.py and the project code, see
# https://packaging.python.org/en/latest/single_source_version.html
version='0.0.36',
version='0.0.37',

description='Run bazel in Docker, in a reproducible and portable container.',
long_description=long_description,
Expand Down