From 7b07297ef20c4d002c70bc1062821527739fef9a Mon Sep 17 00:00:00 2001 From: 88lex <36984295+88lex@users.noreply.github.com> Date: Thu, 20 Aug 2020 09:17:27 +0800 Subject: [PATCH] Created using Colaboratory --- safire.ipynb | 141 +++++++++++++++++++++++++-------------------------- 1 file changed, 68 insertions(+), 73 deletions(-) diff --git a/safire.ipynb b/safire.ipynb index add99e7..d099982 100644 --- a/safire.ipynb +++ b/safire.ipynb @@ -7,7 +7,13 @@ "provenance": [], "private_outputs": true, "collapsed_sections": [ - "Ov-m1nFU9jry" + "Ov-m1nFU9jry", + "1FV-VHZsLkhG", + "WmnTaw3f9DF3", + "2NcemUMdp92X", + "0882m2pipfVU", + "FEwTp1m6pA_-", + "eojC1KSyFlf3" ], "toc_visible": true, "include_colab_link": true @@ -28,47 +34,6 @@ "\"Open" ] }, - { - "cell_type": "markdown", - "metadata": { - "id": "dAku_zP_KxPG", - "colab_type": "text" - }, - "source": [ - "### Commands" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "OeQm5tl7dLxF", - "colab_type": "code", - "cellView": "form", - "colab": {} - }, - "source": [ - "command1 = \"list projects\" #@param {type:\"string\"}\n", - "command2 = \"\" #@param {type:\"string\"}\n", - "command3 = \"\" #@param {type:\"string\"}\n", - "command4 = \"\" #@param {type:\"string\"}\n", - "command5 = \"\" #@param {type:\"string\"}\n", - "\n", - "\n", - "\n" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "yiEEfpW_zeB9", - "colab_type": "text" - }, - "source": [ - "Enter command(s) and select Runtime, Run All from the menu or ctrl-f9. See `Output` for results" - ] - }, { "cell_type": "markdown", "metadata": { @@ -97,7 +62,6 @@ "colab_type": "text" }, "source": [ - "\n", "1. If using Colab:\n", " - Save copy of the ipynb file to your drive (Colab Notebooks folder)\n", " - Run the init code cell to auth to your My Drive account. This gives you access to Drive where you will store your creds, svcaccts and xlsx/csv files.\n", @@ -128,7 +92,8 @@ " - `add projects 5 1 myproj` will create 5 projects starting with number 0001 using a prefix of myproj. Result: myproj0001, myproj0002 .. myproj0005\n", "\n", "7. Run commands using `Runtime - Run All` or `Ctrl-F9`\n", - " - NOTE: If you run only the Command cell it will do nothing. You need to Run All in order to run all of the code.\n" + " - NOTE: If you run only the Command cell it will do nothing. You need to Run All in order to run all of the code.\n", + "\n" ] }, { @@ -148,7 +113,7 @@ "metadata": { "id": "e2xK3xi2MlXs", "colab_type": "code", - "cellView": "both", + "cellView": "form", "colab": {} }, "source": [ @@ -162,6 +127,7 @@ "from google.auth.transport.requests import Request\n", "from google_auth_oauthlib.flow import InstalledAppFlow\n", "from googleapiclient.discovery import build\n", + "from shutil import copyfile\n", "\n", "if importlib.util.find_spec('fire') is None: os.system('pip install fire')\n", "import fire\n", @@ -174,13 +140,33 @@ " drive.mount(gdrive_mount)\n", " safire_dir = gdrive_mount + gd_output_dir\n", "except:\n", - " #@markdown Root directory when safire running locally (dropdown choices):\n", - " local_safire_dir = '/opt/safire/safire' #@param ['/opt/safire/safire', '~/safire', '~/../opt/safire/safire', \"safire\"] {allow-input: true}\n", - " safire_dir = local_safire_dir\n", - "#@markdown External config file located in safire_dir. Use any name. Def: config.py:\n", - "use_external_config = True #@param {type:\"boolean\"}\n", - "ext_conf_file = \"/config.py\" #@param [\"/config.py\"] {allow-input: true}\n", - "ext_conf_file = safire_dir + ext_conf_file" + " base_path = str(Path.home())\n", + " safire_dir = os.path.join(base_path, \"safire\")\n", + " os.makedirs(safire_dir, exist_ok=True)\n", + " cfg_file = os.path.join(safire_dir, \"config.py\")\n", + " if os.path.exists(cfg_file):\n", + " copyfile(cfg_file, \"config.py\")\n", + " else:\n", + " copyfile(\"default_config.py\", \"config.py\")\n", + " copyfile(\"default_config.py\", cfg_file)\n", + "\n", + " ## @markdown Root directory when safire running locally (dropdown choices):\n", + " # local_safire_dir = '~/safire' #@param ['~/safire', '/opt/safire/safire', '~/../opt/safire/safire', \"safire\"] {allow-input: true}\n", + " # safire_dir = local_safire_dir\n", + "# @markdown External config file located in local_safire_dir. Default: config.py:\n", + "use_external_config = True # @param {type:\"boolean\"}\n", + "ext_conf_file = \"config.py\" # @param [\"config.py\"] {allow-input: true}\n", + "ext_conf_file = os.path.join(safire_dir, ext_conf_file)\n", + "\n", + "\n", + "# except:\n", + "# #@markdown Root directory when safire running locally (dropdown choices):\n", + "# local_safire_dir = '~/safire' #@param ['~/safire', '/opt/safire/safire', '~/../opt/safire/safire', \"safire\"] {allow-input: true}\n", + "# safire_dir = local_safire_dir\n", + "# #@markdown External config file located in local_safire_dir. Default: config.py:\n", + "# use_external_config = True #@param {type:\"boolean\"}\n", + "# ext_conf_file = \"/config.py\" #@param [\"/config.py\"] {allow-input: true}\n", + "# ext_conf_file = safire_dir + ext_conf_file" ], "execution_count": null, "outputs": [] @@ -413,8 +399,9 @@ " \"https://www.googleapis.com/auth/drive\",\n", " \"https://www.googleapis.com/auth/cloud-platform\",\n", " \"https://www.googleapis.com/auth/iam\",\n", - " \"https://www.googleapis.com/auth/spreadsheets\",\n", " ]\n", + " # \"https://www.googleapis.com/auth/spreadsheets\",\n", + "\n", " self.scopes_group = [\n", " \"https://www.googleapis.com/auth/admin.directory.group\",\n", " \"https://www.googleapis.com/auth/admin.directory.group.member\",\n", @@ -1096,14 +1083,14 @@ " if not files:\n", " return f\"No json keys found in {sa_path}, Nothing to delete\"\n", " delete_files = [i for i in sorted(files) if \".json\" and str(filter) in i]\n", - " print(f\"Files to be deleted:\\n\", *delete_files, sep=\"\\n\")\n", - " yes_no = input(\"Confirm you want to delete all of these files. y/n: \")\n", - " if yes_no.lower() in [\"y\", \"yes\"]:\n", - " for file in delete_files:\n", - " print(f\"Removing {file}\")\n", - " os.remove(f\"{sa_path}/{file}\")\n", - " else:\n", - " print(\"Deletion of json files aborted\")\n", + " # print(f\"Files to be deleted:\\n\", *delete_files, sep=\"\\n\")\n", + " # yes_no = input(\"Confirm you want to delete all of these files. y/n: \")\n", + " # if yes_no.lower() in [\"y\", \"yes\"]:\n", + " for file in delete_files:\n", + " print(f\"Removing {file}\")\n", + " os.remove(f\"{sa_path}/{file}\")\n", + " # else:\n", + " # print(\"Deletion of json files aborted\")\n", "\n", " def members(\n", " self,\n", @@ -1255,7 +1242,18 @@ "colab_type": "text" }, "source": [ - "## Output" + "## Commands" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "yiEEfpW_zeB9", + "colab_type": "text" + }, + "source": [ + "- Enter command(s) and select `Runtime, Run All` from the top menu in Colab (or use ctrl-f9 to run all). \n", + "- See below for results of each command. All results are also exported to xlsx and csv files in the /data directory" ] }, { @@ -1267,42 +1265,39 @@ "colab": {} }, "source": [ - "%%time\n", - "# %%timeit\n", - "\n", - "#@markdown\n", - "if \"safire.py\" == sys.argv[0] and __name__ == \"__main__\":\n", + "if \"safire_ip.py\" == sys.argv[0] and __name__ == \"__main__\":\n", " fire.Fire(Commands)\n", "else:\n", + " %%time\n", + " command1 = \"\" #@param {type:\"string\"}\n", " if command1:\n", " sys.argv = [\"\"] + list(command1.split(\" \"))\n", " print(f\"safire command: {command1} \\n\\nOutput: \\n\")\n", " fire.Fire(Commands)\n", "\n", + " command2 = \"\" #@param {type:\"string\"}\n", " if command2:\n", " sys.argv = [\"\"] + list(command2.split(\" \"))\n", " print(f\"safire command: {command2} \\n\\nOutput: \\n\")\n", " fire.Fire(Commands)\n", "\n", + " command3 = \"\" #@param {type:\"string\"}\n", " if command3:\n", " sys.argv = [\"\"] + list(command3.split(\" \"))\n", " print(f\"safire command: {command3} \\n\\nOutput: \\n\")\n", " fire.Fire(Commands)\n", "\n", + " command4 = \"\" #@param {type:\"string\"}\n", " if command4:\n", " sys.argv = [\"\"] + list(command4.split(\" \"))\n", " print(f\"safire command: {command4} \\n\\nOutput: \\n\")\n", " fire.Fire(Commands)\n", "\n", + " command5 = \"\" #@param {type:\"string\"}\n", " if command5:\n", " sys.argv = [\"\"] + list(command5.split(\" \"))\n", " print(f\"safire command: {command5} \\n\\nOutput: \\n\")\n", - " fire.Fire(Commands)\n", - "# else:\n", - "# if __name__ == \"__main__\":\n", - "# fire.Fire(Commands)\n", - "\n", - "\n" + " fire.Fire(Commands)\n" ], "execution_count": null, "outputs": []