Skip to content

Generate environment variable file on install #26

Closed
campb303 opened this issue Apr 28, 2021 · 7 comments
Closed

Generate environment variable file on install #26

campb303 opened this issue Apr 28, 2021 · 7 comments
Assignees

Comments

@campb303
Copy link
Collaborator

When installing the webqueue2 API, it would be helpful to create a JWT key automatically if it does not exist in either the config file or an command line argument.

@campb303 campb303 transferred this issue from ECN/webqueue2-frontend May 5, 2021
@campb303 campb303 added this to the write-access milestone May 17, 2021
@campb303 campb303 added the high-priority Needs immediate extra focus label May 17, 2021
@benne238
Copy link
Collaborator

benne238 commented May 18, 2021

Creating a file on the system when installing package

Setuptools documentation: https://setuptools.readthedocs.io/en/latest/deprecated/distutils/setupscript.html?highlight=data_files#installing-additional-files
Stack-overflow issue: https://stackoverflow.com/questions/21461422/how-to-make-pip-write-a-default-config-file-when-installing-a-python-package

Creating a file on the system after installing the package can be done within setup.py directly:

setup.py:

...
relativeDir = os.getcwd()
setup(
    name="webqueue2_api",
    version="0.9.1",
    data_files=[(relativeDir, ['webqueue2-api.cfg'])]
)
...

In the code above, relativeDir would represent the directory that the pip install command was executed in, and webqueue2-api.cfg would be an example configuration file that is stored in the root of the github repo (or in the same directory in which setup.py is currently in).

The data_files argument in setup specifies pairs of directories and source files to be copied into the corresponding directory, so in the example above, webqueue2-api.cfg would be copied from the github repo into whatever the relative directory is.

The webqueue2-api.cfg file would look somethings like this:

[webqueue2_api]
JWT_SECRET_KEY = shhhhhh
ENVIRONMENT = dev

[ECNQueue]
QUEUES_TO_IGNORE = ["archives", "drafts", "inbox", "coral"]
QUEUE_DIRECTORY = /home/pier/e/queue/Mail

[Logger]
LOGGER_OUT_FILE = /tmp/webqueue2_api.log

@benne238
Copy link
Collaborator

data_files context

It is possible to generate a config file. and copy it to the system when using the data_files argument like this:

setup.py:

...
setup(
    name="webqueue2_api",
    version="0.9.1",
    data_files=[('', ['webqueue2-api.cfg'])]
)
...

The webqueue2-api.cfg file will be copied directly to the venv directory, meaning that relative file paths used in the data_files argument work in relation to the install destination:
image

This is useful for installing configurations that might be used without user modification, however the user should not have to navigate to the installed package to be able to make configuration adjustments.

pip context

When running pip install -v ~/webqueue2-api[all] to install the webueue2-api package, the current working directory is set to the temporary directory where pip copies all of the package files from adn might resmble something similar to this: /tmp/pip-req-tracker-e3a7a343. The __file__ variable is also set to the temporary directory and not the directory that the pip install command was run from.

If setup.py were configured like this:

setup.py:

relativeDir = os.getcwd()   # equivalent to os.path.dirname(__file__)
setup(
    name="webqueue2_api",
    version="0.9.1",
    data_files=[(relativeDir, ['webqueue2-api.cfg'])]
)

relativeDir will be the temporary directory that pip uses, which is not useful as the temporary directory is deleted after pip installs the package (successfully or unsuccessfully)

Problem

It seems that there is no standard way to get the directory that the pip install command was executed in, which makes it difficult to determine a directory to place the configuration file in. It might be possible to get the directory by using the (inspect)[https://docs.python.org/3/library/inspect.html] module which can be used to view tracebacks which might be helpful.

A workaround would be to build a utility script that generates (or copies) the configuration file in the users current working directory. Similar to the functionality of the mkdocs new command, which generates the mkdocs.yaml file (see their code here), we could create a similar command which might resemble something like python3 -m webqueue2_api make-config-file

@benne238
Copy link
Collaborator

Create Config File Function

For now, due to the limitations of being able to get the directory from running the pip install command, the solution will be to implement a function that can create a configuration file by having a path passed to it. The function will be created in a script at the root of the webqueue2_api directory. This function can be implemented in setup.py later if the location of the pip install command can be obtained during install.

@benne238
Copy link
Collaborator

Update on functionality

There is now (as of the latest commit in the feature-generate-environment-file-on-install branch) a script that will copy the webqueue2-api.cfg file (which is stored in the root of the python virtual environment directory) to the path specified.

Example

create_config("~/") # Creates webqueue2-api.cfg file in the users home directory

create_config("/tmp") # Creates webqueue2-api.cfg file in the /tmp directory

Note: This script will need to be properly implemented as part of the webqueue2_api package after the Update package structure pull request is approved

@campb303
Copy link
Collaborator Author

Code is ready in its own branch. Waiting for #20

@campb303 campb303 removed the high-priority Needs immediate extra focus label May 24, 2021
@campb303
Copy link
Collaborator Author

campb303 commented Jun 1, 2021

Need to look at the create_config() function but the following is acceptible.

setup.py:

...
setup(
    name="webqueue2_api",
    version="0.9.1",
    data_files=[('', ['webqueue2-api.cfg'])]
)
...

The webqueue2-api.cfg file will be copied directly to the venv directory, meaning that relative file paths used in the data_files argument work in relation to the install destination:
image

@campb303 campb303 removed this from the write-access milestone Jul 6, 2021
@campb303
Copy link
Collaborator Author

campb303 commented Jul 6, 2021

Defaults have been replaced by dataclass configurations per package. Closing.

@campb303 campb303 closed this as completed Jul 6, 2021
Sign in to join this conversation on GitHub.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants