From 36c1397fef3656cfdfcafc9398794f78397dd77b Mon Sep 17 00:00:00 2001 From: jgstew Date: Thu, 10 Feb 2022 14:23:14 -0500 Subject: [PATCH] add option to export all sites to besapi and bescli --- src/besapi/__init__.py | 2 +- src/besapi/besapi.py | 18 ++++++++++++++++++ src/bescli/bescli.py | 4 ++++ tests/tests.py | 3 ++- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/besapi/__init__.py b/src/besapi/__init__.py index 08c32b6..f8f0f93 100644 --- a/src/besapi/__init__.py +++ b/src/besapi/__init__.py @@ -6,4 +6,4 @@ from . import besapi -__version__ = "1.1.1" +__version__ = "1.1.2" diff --git a/src/besapi/besapi.py b/src/besapi/besapi.py index b584672..3a49261 100644 --- a/src/besapi/besapi.py +++ b/src/besapi/besapi.py @@ -203,6 +203,8 @@ def export_site_contents( print("export_site_contents()") # Iterate Over All Site Content content = self.get("site/" + site_path + "/content") + if verbose: + print(content) if content.request.status_code == 200: print( "Archiving %d items from %s..." % (content().countchildren(), site_path) @@ -251,6 +253,22 @@ def export_site_contents( ) as bes_file: bes_file.write(content.text.encode("utf-8")) + def export_all_sites( + self, include_external=False, export_folder="./", name_trim=70, verbose=False + ): + """export all bigfix sites to a folder""" + results_sites = self.get("sites") + if verbose: + print(results_sites) + if results_sites.request.status_code == 200: + for item in results_sites().iterchildren(): + site_path = item.attrib["Resource"].split("/api/site/", 1)[1] + if include_external or "external/" not in site_path: + print("Exporting Site:", site_path) + self.export_site_contents( + site_path, export_folder, name_trim, verbose + ) + __call__ = login # https://stackoverflow.com/q/40536821/861745 __enter__ = login diff --git a/src/bescli/bescli.py b/src/bescli/bescli.py index 6d86b17..7385001 100644 --- a/src/bescli/bescli.py +++ b/src/bescli/bescli.py @@ -300,6 +300,10 @@ def do_export_site(self, site_path): """export site contents to current folder""" self.bes_conn.export_site_contents(site_path, verbose=True) + def do_export_all_sites(self, statement=None): + """export site contents to current folder""" + self.bes_conn.export_all_sites(verbose=False) + complete_upload = Cmd.path_complete def do_upload(self, file_path): diff --git a/tests/tests.py b/tests/tests.py index b88b21b..1eee2a3 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -50,7 +50,8 @@ # set working directory to src folder in parent folder os.chdir("../src") - # Test file upload: print(bigfix_cli.bes_conn.upload("./besapi/__init__.py", "test_file.txt")) + # Test file upload: + print(bigfix_cli.bes_conn.upload("./besapi/__init__.py", "test_besapi_upload.txt")) if os.name == "nt": subprocess.run(