Skip to content

Commit

Permalink
feat: Implement Rosemary CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
drorganvidez committed Mar 24, 2024
1 parent dcfb764 commit bc6abbd
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 16 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ __pycache__/
.idea
uploads/
app.log
.DS_Store
.DS_Store
rosemary.egg-info/
build/
10 changes: 8 additions & 2 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# Use an official Python runtime as a parent image
FROM python:3.11-alpine

# Set this environment variable to suppress the "Running as root" warning from pip
ENV PIP_ROOT_USER_ACTION=ignore

# Install the MySQL client to be able to use it in the standby script.
RUN apk add --no-cache mysql-client

# Set the working directory in the container to /app
WORKDIR /app

# Copy the contents of the local app/ directory to the /app directory in the container
COPY app/ ./app
# Copy the entire project into the container
COPY . .

# Copy requirements.txt at the /app working directory
COPY requirements.txt .
Expand All @@ -19,6 +22,9 @@ COPY --chmod=+x scripts/wait-for-db.sh ./scripts/
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Install rosemary CLI tool
RUN pip install --no-cache-dir .

# Update pip
RUN pip install --no-cache-dir --upgrade pip

Expand Down
50 changes: 38 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,44 @@ To run unit test, please enter inside `web` container:
pytest app/tests/units.py
```

## Using Rosemary CLI

`Rosemary` is a CLI tool developed to facilitate project management and development tasks.

To use the Rosemary CLI, you need to be inside the `web_app_container` Docker container. This ensures that Rosemary operates in the correct environment and has access to all necessary files and settings.

First, make sure your Docker environment is running. Then, access the `web_app_container` using the following command:

```
docker exec -it web_app_container /bin/sh
```

In the terminal, you should see the prefix `/app #`. You are now ready to use Rosemary's commands.

### Update Project Dependencies

To update all project dependencies, run:

```
rosemary update
```

Note: it is the responsibility of the developer to check that the update of the dependencies has not broken any
functionality and each dependency maintains backwards compatibility. Use the script with care!

### Viewing Environment Variables

To view the current `.env` file settings, use:

```
rosemary env
```
### Available Commands

- `rosemary update`: Updates all project dependencies and the `requirements.txt` file.
- `rosemary info`: Displays information about the Rosemary CLI, including version and author.
- `rosemary env`: Displays the current environment variables from the `.env` file.

## Deploy in production (Docker Compose)

```
Expand Down Expand Up @@ -107,15 +145,3 @@ To renew a certificate that is less than 60 days from expiry, execute:
cd scripts
chmod +x ssl_renew.sh && ./ssl_renew.sh
```

## Update dependencies

To update all project dependencies automatically, run:

```
cd scripts
chmod +x update_dependencies.sh && ./update_dependencies.sh
```

Note: it is the responsibility of the developer to check that the update of the dependencies has not broken any functionality and each dependency maintains backwards compatibility. Use the script with care!

3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ PyMySQL==1.1.0
pytest==8.1.1
python-dotenv==1.0.1
requests==2.31.0
SQLAlchemy==2.0.28
rosemary @ file:///app
SQLAlchemy==2.0.29
typing_extensions==4.10.0
Unidecode==1.3.8
urllib3==2.2.1
Expand Down
Empty file added rosemary/__init__.py
Empty file.
19 changes: 19 additions & 0 deletions rosemary/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# rosemary/cli.py

import click
from rosemary.commands.update import update
from rosemary.commands.info import info
from rosemary.commands.env import env


@click.group()
def cli():
pass


cli.add_command(update)
cli.add_command(info)
cli.add_command(env)

if __name__ == '__main__':
cli()
Empty file added rosemary/commands/__init__.py
Empty file.
15 changes: 15 additions & 0 deletions rosemary/commands/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# rosemary/commands/env.py

import click
from dotenv import dotenv_values


@click.command()
def env():
"""Displays the current .env file values."""
# Load the .env file
env_values = dotenv_values(".env")

# Display keys and values
for key, value in env_values.items():
click.echo(f"{key}={value}")
31 changes: 31 additions & 0 deletions rosemary/commands/info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import click
import pkg_resources


def get_metadata_value(metadata_lines, key):
default_value = f"{key}: Unknown"
line = next((line for line in metadata_lines if line.startswith(key)), default_value)
return line.split(':', 1)[1].strip() if line != default_value else default_value.split(':', 1)[1].strip()


@click.command()
def info():
"""Displays information about the Rosemary CLI."""
distribution = pkg_resources.get_distribution("rosemary")

try:
metadata = distribution.get_metadata_lines('METADATA')
author = get_metadata_value(metadata, 'Author')
author_email = get_metadata_value(metadata, 'Author-email')
description = get_metadata_value(metadata, 'Summary')
except FileNotFoundError:
author, author_email, description = "Unknown", "Unknown", "Not available"

name = distribution.project_name
version = distribution.version

click.echo(f"Name: {name}")
click.echo(f"Version: {version}")
click.echo(f"Author: {author}")
click.echo(f"Author-email: {author_email}")
click.echo(f"Description: {description}")
29 changes: 29 additions & 0 deletions rosemary/commands/update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# rosemary/commands/update.py

import click
import subprocess
import os


@click.command()
def update():
"""This command updates pip, all packages, and updates requirements.txt."""
try:
# Update pip
subprocess.check_call(['pip', 'install', '--upgrade', 'pip'])

# Get the list of installed packages and update them
packages = subprocess.check_output(['pip', 'freeze']).decode('utf-8').split('\n')
for package in packages:
package_name = package.split('==')[0]
if package_name: # Check if the package name is not empty
subprocess.check_call(['pip', 'install', '--upgrade', package_name])

# Update requirements.txt
requirements_path = os.path.join(os.getcwd(), 'requirements.txt')
with open(requirements_path, 'w') as f:
subprocess.check_call(['pip', 'freeze'], stdout=f)

click.echo('Update completed!')
except subprocess.CalledProcessError as e:
click.echo(f'Error during the update: {e}')
21 changes: 21 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# setup.py

from setuptools import setup, find_packages

setup(
name='rosemary',
version='0.1.0',
packages=find_packages(),
include_package_data=True,
install_requires=[
'click',
'python-dotenv',
],
author='David Romero',
author_email='[email protected]',
description="Rosemary is a CLI to be able to work on UVLHub development more easily.",
entry_points='''
[console_scripts]
rosemary=rosemary.cli:cli
''',
)

0 comments on commit bc6abbd

Please sign in to comment.