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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
# %% [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