Skip to content

Instantly share code, notes, and snippets.

@dmoruzzi
Created September 17, 2022 06:35
Show Gist options
  • Save dmoruzzi/d4121866abe0e5334d552878c2760c91 to your computer and use it in GitHub Desktop.
Save dmoruzzi/d4121866abe0e5334d552878c2760c91 to your computer and use it in GitHub Desktop.
Dynamic Terminal Commands
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1>Dynamic Terminal Commands</h1>\n",
"<p>Version: 0.1.0 (2022.09.17)</p>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"import os"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Get current working directory\n",
"cwd_filepath_str: str = os.getcwd()\n",
"print(cwd_filepath_str)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Get list of files in cwd_filepath_str\n",
"cwd_files_lst = os.listdir(cwd_filepath_str)\n",
"print(cwd_files_lst)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>Algorithm: a process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer. Synonyms would be process, procedure, or logic.</p>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Define filter algorithm; return {'appName': ['dependency1', 'dependency2', 'etc..']}\n",
"def filter_dependencies(files_lst: list):\n",
" '''\n",
" Filter MSIX dependencies & installer from from a list of files\n",
"\n",
" :param files_lst: list of strings of fully qualified file names\n",
" :return: dict of schema {'appName': ['dependency1', 'dependency2', 'etc..']}\n",
" '''\n",
" appxpackage_name_lst, dependencies_lst = list(), list() # initialize vars\n",
" for file_str in files_lst: # iterate through list; lst -> str0, str1, str2, etc.\n",
" file_str = file_str.lower() # standardize search queries\n",
" if '.msix' in file_str:\n",
" if 'microsoft' in file_str:\n",
" # All dependencies contain 'microsoft' in file name\n",
" dependencies_lst.append(file_str)\n",
" else:\n",
" appxpackage_name_lst.append(file_str)\n",
" # No ELSE for '.msix' check because disregard non-msix files\n",
" if len(appxpackage_name_lst) > 1:\n",
" raise Exception('[ERROR] More than one application name')\n",
" return dict({str(appxpackage_name_lst[0]): dependencies_lst})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Define scribe algorithm; return string of terminal command\n",
"def scribe_terminal_command(appx_name_str: str, dependencies_lst: list, filepath_str: str):\n",
" '''\n",
" Scribe and return terminal command string \n",
"\n",
" :param appx_name_str:\n",
" :param dependencies_lst:\n",
" :param filepath_str:\n",
" :return string of terminal command\n",
" '''\n",
" if appx_name_str == '': # If no application name, there is a critical error.\n",
" raise Exception('[ERROR] Appx_name_str blank!')\n",
" s = f'Add-AppxPackage -Path \"{filepath_str}\\{appx_name_str}\" '\n",
" for dependency_str in dependencies_lst:\n",
" s += f'-DependencyPath \"{filepath_str}\\{dependency_str}\" '\n",
" return str(s)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Call filter_dependencies function and inspect results\n",
"dependencies_dict = filter_dependencies(cwd_files_lst) \n",
"print(dependencies_dict)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Call scribe_terminal_command and inspects results\n",
"appx_name_str = str(list(dependencies_dict.keys())[0]) # Python typecasting is weird\n",
"dependencies_lst = list(dependencies_dict.values())[0] # Python typecasting is weird\n",
"terminal_str = scribe_terminal_command(appx_name_str, dependencies_lst, cwd_filepath_str)\n",
"print(terminal_str)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>Final Remarks</h2>\n",
"<p>This explanation was designed to illustrate how this may be approached. This is not an end-all be-all solution. If this is a repetitive task, you may want to create a directories list and then iterate through that list. In the iteration cycle, execute the commands listed above for repetition.</p>"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.10.4 64-bit",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "901b79e026e03396fd1ffa7133844e9ea80e258ce34c66e1aabb5896bcb18463"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
# %% [markdown]
# <h1>Dynamic Terminal Commands</h1>
# <p>Version: 0.1.0 (2022.09.17)</p>
# %%
# imports
import os
# %%
# Get current working directory
cwd_filepath_str: str = os.getcwd()
print(cwd_filepath_str)
# %%
# Get list of files in cwd_filepath_str
cwd_files_lst = os.listdir(cwd_filepath_str)
print(cwd_files_lst)
# %% [markdown]
# <p>Algorithm: a process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer. Synonyms would be process, procedure, or logic.</p>
# %%
# Define filter algorithm; return {'appName': ['dependency1', 'dependency2', 'etc..']}
def filter_dependencies(files_lst: list):
'''
Filter MSIX dependencies & installer from from a list of files
:param files_lst: list of strings of fully qualified file names
:return: dict of schema {'appName': ['dependency1', 'dependency2', 'etc..']}
'''
appxpackage_name_lst, dependencies_lst = list(), list() # initialize vars
for file_str in files_lst: # iterate through list; lst -> str0, str1, str2, etc.
file_str = file_str.lower() # standardize search queries
if '.msix' in file_str:
if 'microsoft' in file_str:
# All dependencies contain 'microsoft' in file name
dependencies_lst.append(file_str)
else:
appxpackage_name_lst.append(file_str)
# No ELSE for '.msix' check because disregard non-msix files
if len(appxpackage_name_lst) > 1:
raise Exception('[ERROR] More than one application name')
return dict({str(appxpackage_name_lst[0]): dependencies_lst})
# %%
# Define scribe algorithm; return string of terminal command
def scribe_terminal_command(appx_name_str: str, dependencies_lst: list, filepath_str: str):
'''
Scribe and return terminal command string
:param appx_name_str:
:param dependencies_lst:
:param filepath_str:
:return string of terminal command
'''
if appx_name_str == '': # If no application name, there is a critical error.
raise Exception('[ERROR] Appx_name_str blank!')
s = f'Add-AppxPackage -Path "{filepath_str}\{appx_name_str}" '
for dependency_str in dependencies_lst:
s += f'-DependencyPath "{filepath_str}\{dependency_str}" '
return str(s)
# %%
# Call filter_dependencies function and inspect results
dependencies_dict = filter_dependencies(cwd_files_lst)
print(dependencies_dict)
# %%
# Call scribe_terminal_command and inspects results
appx_name_str = str(list(dependencies_dict.keys())[0]) # Python typecasting is weird
dependencies_lst = list(dependencies_dict.values())[0] # Python typecasting is weird
terminal_str = scribe_terminal_command(appx_name_str, dependencies_lst, cwd_filepath_str)
print(terminal_str)
# %% [markdown]
# <h2>Final Remarks</h2>
# <p>This explanation was designed to illustrate how this may be approached. This is not an end-all be-all solution. If this is a repetitive task, you may want to create a directories list and then iterate through that list. In the iteration cycle, execute the commands listed above for repetition.</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment