From b7b1cf6aad1936c598c084060e91c812bb007db2 Mon Sep 17 00:00:00 2001 From: Thibaut Chavet Date: Thu, 1 Sep 2022 10:52:24 +0200 Subject: [PATCH] Exclude unnecessary directories when sending code --- Readme.rst | 4 +- changelog.txt | 18 ++++---- pyproject.toml | 4 +- src/appollo/helpers.py | 94 ++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 105 insertions(+), 15 deletions(-) diff --git a/Readme.rst b/Readme.rst index 98c8f69..59df029 100644 --- a/Readme.rst +++ b/Readme.rst @@ -5,10 +5,10 @@ | -.. image:: https://img.shields.io/badge/version-0.2.0-blue +.. image:: https://img.shields.io/badge/version-0.2.1-blue :target: https://github.com/Appollo-CLI/Appollo/ -.. image:: https://img.shields.io/pypi/l/appollo +.. image:: https://img.shields.io/github/license/appollo-cli/appollo :target: https://github.com/Appollo-CLI/Appollo/blob/master/LICENSE .. image:: https://img.shields.io/librariesio/release/pypi/appollo diff --git a/changelog.txt b/changelog.txt index d836d23..c143a30 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,9 +1,11 @@ +v0.2.1 + - CLI: Exclude unnecessary directories when sending code v0.2.0 - - CLI : Added choice picker for some options when no value was provided - - Documentation : Improved and finished a first version of the tutorial - - Documentation : Removed the Usage section - - CLI : While building added a "progress bar" to show the progress of the build in realtime - - CLI : Minor improvements and cleanup of the code - - CLI : Added a GIF showing a quick Appollo demo - - CLI : Added flutter version list - - CLI : Added more precise build progress status \ No newline at end of file + - CLI: Added choice picker for some options when no value was provided + - Documentation: Improved and finished a first version of the tutorial + - Documentation: Removed the Usage section + - CLI: While building added a "progress bar" to show the progress of the build in realtime + - CLI: Minor improvements and cleanup of the code + - CLI: Added a GIF showing a quick Appollo demo + - CLI: Added flutter version list + - CLI: Added more precise build progress status \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index c3531e3..4eb78e6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "appollo" -version = "0.2.0" +version = "0.2.1" authors = [ {name="Appollo", email="hello@appollo.space"} ] @@ -16,7 +16,7 @@ keywords = ["Flutter", "iOS"] classifiers = [ "Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License", - "Development Status :: 3 - Alpha", + "Development Status :: 4 - Beta", "Operating System :: OS Independent", "Environment :: Console", "Natural Language :: English", diff --git a/src/appollo/helpers.py b/src/appollo/helpers.py index 0ab9129..a97e6bf 100644 --- a/src/appollo/helpers.py +++ b/src/appollo/helpers.py @@ -1,5 +1,4 @@ import os -import shutil from functools import update_wrapper import click @@ -9,7 +8,9 @@ def zip_directory(directory_path): """ Archives a directory in a zip file and returns his name.""" - return shutil.make_archive(os.path.join(os.getcwd(), '.app'), "zip", directory_path) + if os.path.exists(os.path.join(os.getcwd(), '.app.zip')): + os.remove(os.path.join(os.getcwd(), '.app.zip')) + return make_zip(os.path.join(os.getcwd(), '.app'), directory_path, ["build", ".dart_tool", ".pub-cache", ".pub", ".git"]) @@ -80,9 +81,96 @@ def terminal_menu(api_route, prompt_text, api_params=None, key_fieldname="key", choices=terminal_ready_list, qmark="", ).ask() - if not menu_entry_index: # When ctrl-C, exit + print("menu_entry_index: "+str(menu_entry_index)) + if menu_entry_index is None: # When ctrl-C, exit exit() value = item_list[menu_entry_index][key_fieldname] return value + + +### Copied from shutil to add directory exlusion +def _make_zipfile(base_name, base_dir, exclude_dir=None, verbose=0, dry_run=0, logger=None): + """Create a zip file from all the files under 'base_dir'. + + The output zip file will be named 'base_name' + ".zip". Returns the + name of the output zip file. + """ + import zipfile # late import for breaking circular dependency + + zip_filename = base_name + ".zip" + archive_dir = os.path.dirname(base_name) + + if archive_dir and not os.path.exists(archive_dir): + if logger is not None: + logger.info("creating %s", archive_dir) + if not dry_run: + os.makedirs(archive_dir) + + if logger is not None: + logger.info("creating '%s' and adding '%s' to it", + zip_filename, base_dir) + + if not dry_run: + with zipfile.ZipFile(zip_filename, "w", + compression=zipfile.ZIP_DEFLATED) as zf: + path = os.path.normpath(base_dir) + if path != os.curdir: + zf.write(path, path) + if logger is not None: + logger.info("adding '%s'", path) + for dirpath, dirnames, filenames in os.walk(base_dir, topdown=True): + if exclude_dir is not None: + dirnames[:] = [d for d in dirnames if d not in exclude_dir] + for name in sorted(dirnames): + path = os.path.normpath(os.path.join(dirpath, name)) + zf.write(path, path) + if logger is not None: + logger.info("adding '%s'", path) + for name in filenames: + path = os.path.normpath(os.path.join(dirpath, name)) + if os.path.isfile(path): + zf.write(path, path) + if logger is not None: + logger.info("adding '%s'", path) + + return zip_filename + + +def make_zip(base_name, root_dir=None, exclude_dir=None, base_dir=None, verbose=0, + dry_run=0, logger=None): + """Create a zip archive file + + 'base_name' is the name of the file to create, minus any format-specific + extension. + + 'root_dir' is a directory that will be the root directory of the + archive; ie. we typically chdir into 'root_dir' before creating the + archive. 'base_dir' is the directory where we start archiving from; + ie. 'base_dir' will be the common prefix of all files and + directories in the archive. 'root_dir' and 'base_dir' both default + to the current directory. Returns the name of the archive file. + """ + save_cwd = os.getcwd() + if root_dir is not None: + if logger is not None: + logger.debug("changing into '%s'", root_dir) + base_name = os.path.abspath(base_name) + if not dry_run: + os.chdir(root_dir) + + if base_dir is None: + base_dir = os.curdir + + kwargs = {'dry_run': dry_run, 'logger': logger} + + try: + filename = _make_zipfile(base_name, base_dir, exclude_dir, **kwargs) + finally: + if root_dir is not None: + if logger is not None: + logger.debug("changing back to '%s'", save_cwd) + os.chdir(save_cwd) + + return filename