Skip to content

Commit

Permalink
commit command complete
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashif4354 committed Jul 28, 2024
1 parent 8c2c7a0 commit 203a551
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dgupdaterconf.json
dgupdater.egg-info/
dist/
build/
release/
dgupdater_release/

# Pycache
*.pyc
25 changes: 12 additions & 13 deletions dgupdater/cli_commands/commit/func/commit_changes.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
from os import makedirs, getcwd
from json import load, dump, dumps
from .find_files import find_files
from shutil import rmtree

from .find_files import find_files
from .create_chunks import create_chunks

def commit_changes() -> None:

makedirs('release/chunks', exist_ok = True)
try:
rmtree('dgupdater_release/chunks') # Removing the chunks directory and its contents if it exists
except FileNotFoundError as _:
pass

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

release_json = {}

Expand All @@ -26,26 +32,19 @@ def commit_changes() -> None:
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_files'] = len(release_files)
dgupdaterconf_json['update_ready'] = False
dgupdaterconf_json['no_of_chunks'] = no_of_chunks
dgupdaterconf_json['files_in_latest_version'] = release_files

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


Expand Down
38 changes: 22 additions & 16 deletions dgupdater/cli_commands/commit/func/create_chunks.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
from json import load, dump
from json import dump
from math import ceil
from tqdm import tqdm


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

max_chunk_size = 1000000 # 10 Lakh (or) 1 Million
max_chunk_size = 1_000_000 # 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

with tqdm(total = no_of_chunks, desc = "Creating Chunks", ncols = 110, unit='chunks') as pbar:

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 = {
'obj_type': 'chunk',
'_id': chunk_name,
'chunk_data': chunk
}

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

start += max_chunk_size
pbar.update(1)

return no_of_chunks

Expand Down
2 changes: 1 addition & 1 deletion dgupdater/cli_commands/commit/func/find_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def get_ignore_list() -> list:
try:
with open('.dgupdaterignore', 'r') as file:
return [line.split('\n')[0] for line in file.readlines()]
except FileNotFoundError:
except FileNotFoundError as _:
return []

if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion dgupdater/cli_commands/init/func/check_app_exists.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def check_app_exists(name: str, mongostr: str) -> bool:
with MongoClient(mongostr) as client:
db = client['DGUPDATER']
collections = [collection.lower() for collection in db.list_collection_names()]
except Exception as e:
except Exception as _:
echo("Some error occured. Please try again.")

if name.lower() in collections:
Expand Down
2 changes: 1 addition & 1 deletion dgupdater/cli_commands/init/func/check_mongo_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def check_mongo_string(ctx, param, value: str) -> str|None:

echo("Verified !!\n")
return value + '*VERIFIEDONCE*' # *VERIFIEDONCE* is added to the end of the string to detect second execution of callback of click.options()
except ConnectionFailure as e:
except ConnectionFailure as _:
print()
raise BadParameter("Enter a Valid MongoDB Connection String")
except Exception as e:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def create_configuration_files(data: dict, app_name: str, mongodbstrd: str) -> N
if exists(file):
with open(file, 'r') as f:
dgupdaterconf_json = load(f)
except JSONDecodeError as e:
except JSONDecodeError as _:
pass

dgupdaterconf_json['mongodbstrds'][app_name] = mongodbstrd
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def create_entry_in_mongodb(dgupdaterconf_json: dict[str:any], mongodbstrd: str,


collection.insert_one(dgupdaterconf_json)
except Exception as e:
except Exception as _:
raise UsageError("Error while creating entry in MongoDB: ")


Expand Down
8 changes: 4 additions & 4 deletions dgupdater/cli_commands/init/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#template
dgupdaterconf_json = {
"obj_type": "config",
"_id": "",
"app_name": "",
"version": "",
Expand All @@ -35,11 +36,10 @@


@command()
@option("--name", "-n", required = True, prompt = arguments["name"]["prompt"], help = arguments["name"]["help"])
@option("--version", "-v", required = True, prompt = arguments["version"]["prompt"], help = arguments["version"]["help"])
@option("--name", "-n", required = True, prompt = arguments["name"]["prompt"], help = arguments["name"]["help"])
@option("--mongodbstrd", "-md", callback = check_mongo_string, required = True, prompt = arguments["mongodb_connection_string_write"]["prompt"], help = arguments["mongodb_connection_string_write"]["help"])
@option("--mongodbstrc", "-mc", callback = check_mongo_string, required = True, prompt = arguments["mongodb_connection_string_read"]["prompt"], help = arguments["mongodb_connection_string_read"]["help"])
def init(name: str, version: str, mongodbstrd: str, mongodbstrc: str) -> None:
def init(name: str, mongodbstrd: str, mongodbstrc: str) -> None:
# print(name, version, mongodbstrd, mongodbstrc)

echo("\nInitializing this directory for autoupdation...")
Expand All @@ -54,7 +54,7 @@ def init(name: str, version: str, mongodbstrd: str, mongodbstrc: str) -> None:

dgupdaterconf_json["_id"] = name + '_config'
dgupdaterconf_json["app_name"] = name
dgupdaterconf_json["version"] = version
dgupdaterconf_json["version"] = "Version will be updated after publishing the changes."
dgupdaterconf_json["mongodb_connection_string_client"] = mongodbstrc
dgupdaterconf_json["files_in_latest_version"] = 'Files will be listed after publishing the changes.'

Expand Down

0 comments on commit 203a551

Please sign in to comment.