Skip to content

Commit

Permalink
initial stage if adding commit command
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashif4354 committed Jul 27, 2024
1 parent a214b16 commit 8c2c7a0
Show file tree
Hide file tree
Showing 13 changed files with 178 additions and 7 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
# misc
plan.txt
notes.txt
.dgupdaterignore
.env
*.exe

# Virtual Environments
venv/

# Tests
tests/
main.py
dgupdater.py
dgupdaterconf.json

# Builds
# Builds & release
dgupdater.egg-info/
dist/
build/
release/

# Pycache
*.pyc
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@
You need to have a mongodb database preferrably on cloud. You can use mongodb atlas for the same, MongoDB have a serverless db cluster which is very cheap (0.1$ / 1M reads). they also have a good free tier. After configuring, get the connection url and save it.

You need to get two connection string from mongo db, one with write access and one with read access.
the one with write access will be used by the developer to write data to the database and the one with read access will be used by the client to read data from the database. both the connection string should have access to the same cluser and databases.
the one with write access will be used by the developer to write data to the database and the one with read access will be used by the client to read data from the database. both the connection string should have access to the same cluser and databases.


# Initialization
You have open a terminal and navigate into th directory which you want to initialize for auto updation. Then run the following command:

```bash
dgupdater init
```

This will ask you for the application name, application version, mongodb connection string with read access, mongodb connection string with write access. After providing the required information, it will create a file named 'dgupdaterconf.json' in the current directory. This file will be used to store the configuration of the application.

The mongodbconnection string with write access will be stored in a different place

It will be stored in the following location:
```bash
Windows: C:\Users\<username>\AppData\local\DarkGlance\dgupdater\dgupdaterconf.json
Linux: /home/<username>/.config/DarkGlance/dgupdater/dgupdaterconf.json
Mac: Users/<username>/Library/Application Support/DarkGlance/dgupdater/dgupdaterconf.json
```

Then it will check if the application is already registered in the database or not. If it is not registered, it will register the application in the database. or it will ask to overwrite the application details.

It will also create a file named .dgupdaterignore in the current directory.
this file works just like the .gitignore file. you can add the files and directories which you want to ignore while updating the application.

If you want tp change the mongodb connection string, you can do it by running 'dgupdater init' again and providing the new connection string.

3 changes: 2 additions & 1 deletion dgupdater/cli.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from click import group

from .cli_commands.init.init import init
from .cli_commands.commit.commit import commit

@group()
def cli():
pass

cli.add_command(init)
# cli.add_command()
cli.add_command(commit)
# cli.add_command()
# cli.add_command()
# cli.add_command()
Expand Down
23 changes: 23 additions & 0 deletions dgupdater/cli_commands/commit/commit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from click import command, option, prompt, echo

from .func.check_configuration_files_exists import check_configuration_files_exists
from .func.update_version import update_version
from .func.commit_changes import commit_changes

@command()
@option('--version', '-v', required = True, prompt = 'New version number', help='New version number of the application')
def commit(version: str) -> None:

check_configuration_files_exists()

if prompt("Are you sure you want to commit the changes? (y/n): ").lower() in ["y", "yes"]:
echo("Committing...")

update_version(version)
commit_changes()

echo("Committed successfully.")
return

echo("Commit aborted.")

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from os.path import exists
from click import echo
from json import load
from ...init.init import dgupdaterconf_json


def check_configuration_files_exists() -> bool:
if not exists('dgupdaterconf.json'):
echo("\ndgupdaterconf.json not found. Run 'dgupdater init' first. \nAlso check if you are in the correct directory.")
return False

with open('dgupdaterconf.json') as f:
dgupdaterconf_json_ = load(f)

if (dgupdaterconf_json_.keys() != dgupdaterconf_json.keys()):
echo("dgupdaterconf.json is not valid. \nTry 'dgupdater init' again.")
return False

return True


53 changes: 53 additions & 0 deletions dgupdater/cli_commands/commit/func/commit_changes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from os import makedirs, getcwd
from json import load, dump, dumps
from .find_files import find_files

from .create_chunks import create_chunks

def commit_changes() -> None:

makedirs('release/chunks', exist_ok = True)

release_json = {}

release_files = find_files()
# print(release_files)

for file in release_files:

file_path = f'{getcwd()}{file}'
try:
with open(file_path, 'r') as f:
# print(file_path)
release_json[file] = f.read()
except UnicodeDecodeError as _:
with open(file_path, 'rb') as f:
content = str(f.read())
release_json[file] = content
# print(content[:10])


# with open('release/release.json', 'w') as f:
# dump(release_json, f, indent = 4)

release_json_str = dumps(release_json)

# with open('release/testrelease.json', 'w') as f:
# dump({'release': release_json_str}, f, indent = 4)

# print(len(release_json_str))

with open('dgupdaterconf.json') as f:
dgupdaterconf_json = load(f)

no_of_chunks = create_chunks(release_json_str, dgupdaterconf_json['app_name']) #creating chunks as well as getting the number of chunks

dgupdaterconf_json['files_in_latest_version'] = release_files
dgupdaterconf_json['no_of_chunks'] = no_of_chunks

with open(f'{getcwd()}/release/dgupdaterconf.json', 'w') as f:
dump(dgupdaterconf_json, f, indent = 4)


if __name__ == "__main__":
commit_changes()
31 changes: 31 additions & 0 deletions dgupdater/cli_commands/commit/func/create_chunks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from json import load, dump
from math import ceil


def create_chunks(release_json_str: str, app_name: str) -> int:

max_chunk_size = 1000000 # 10 Lakh (or) 1 Million

release_json_size = len(release_json_str)

no_of_chunks = ceil(release_json_size / max_chunk_size)

start = 0
for i in range(no_of_chunks):
chunk_part = i + 1
chunk_name = f'{app_name}_part{chunk_part}'

chunk = release_json_str[start:start + max_chunk_size]
chunk = {
'_id': chunk_name,
'chunk_data': chunk
}

with open(f'release/chunks/{chunk_name}.json', 'w') as f:
dump(chunk, f, indent = 4)

start += max_chunk_size

return no_of_chunks


10 changes: 10 additions & 0 deletions dgupdater/cli_commands/commit/func/update_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from json import load, dump

def update_version(version: str) -> None:
with open('dgupdaterconf.json') as f:
dgupdaterconf_json = load(f)

dgupdaterconf_json['version'] = version

with open('dgupdaterconf.json', 'w') as f:
dump(dgupdaterconf_json, f, indent = 4)
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def create_configuration_files(data: dict, app_name: str, mongodbstrd: str) -> N
with open(file, "w") as f:
dump(dgupdaterconf_json, f, indent = 4)

with open('.dgupdaterignore', 'w') as f:
f.write('')



if __name__ == "__main__":
Expand Down
3 changes: 1 addition & 2 deletions dgupdater/cli_commands/init/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from .func.check_mongo_string import check_mongo_string
from .func.check_app_exists import check_app_exists
from .func.find_files import find_files
from .func.create_entry_in_mongodb import create_entry_in_mongodb
from .func.create_configuration_files import create_configuration_files

Expand Down Expand Up @@ -57,7 +56,7 @@ def init(name: str, version: str, mongodbstrd: str, mongodbstrc: str) -> None:
dgupdaterconf_json["app_name"] = name
dgupdaterconf_json["version"] = version
dgupdaterconf_json["mongodb_connection_string_client"] = mongodbstrc
dgupdaterconf_json["files_in_latest_version"] = find_files()
dgupdaterconf_json["files_in_latest_version"] = 'Files will be listed after publishing the changes.'

create_entry_in_mongodb(dgupdaterconf_json, mongodbstrd, over_write)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
'Development Status :: 2 - Pre-Alpha',
"Programming Language :: Python :: 3.12",
"License :: OSI Approved :: MIT License",
"Operating System :: Microsoft :: Windows",
"Operating System :: Platform Independent",
'Topic :: Utilities'
],
python_requires='>=3.6',
Expand Down

0 comments on commit 8c2c7a0

Please sign in to comment.