Drew Short
3 years ago
7 changed files with 154 additions and 108 deletions
-
109acm.py
-
81acm/asyncio.py
-
18acm/config.py
-
8acm/utility.py
-
31acm/version.py
-
14poetry.lock
-
1pyproject.toml
@ -0,0 +1,81 @@ |
|||
import asyncio |
|||
import logging |
|||
import typing |
|||
|
|||
LOG = logging.getLogger("acm.async") |
|||
|
|||
async def run_command_shell( |
|||
command, |
|||
stdout=asyncio.subprocess.PIPE, |
|||
stderr=asyncio.subprocess.PIPE, |
|||
on_success: typing.List[typing.Callable] = [()] |
|||
): |
|||
"""Run command in subprocess (shell). |
|||
|
|||
Note: |
|||
This can be used if you wish to execute e.g. "copy" |
|||
on Windows, which can only be executed in the shell. |
|||
""" |
|||
process = await asyncio.create_subprocess_shell( |
|||
command, stdout=stdout, stderr=stderr |
|||
) |
|||
|
|||
process_stdout, process_stderr = await process.communicate() |
|||
|
|||
if process.returncode == 0: |
|||
for success_callable in on_success: |
|||
success_callable() |
|||
|
|||
if stdout != asyncio.subprocess.DEVNULL: |
|||
result = process_stdout.decode().strip() |
|||
return result |
|||
else: |
|||
return None |
|||
|
|||
|
|||
def make_chunks(tasks, chunk_size): |
|||
"""Yield successive chunk_size-sized chunks from tasks. |
|||
|
|||
Note: |
|||
Taken from https://stackoverflow.com/a/312464 |
|||
modified for python 3 only |
|||
""" |
|||
for i in range(0, len(tasks), chunk_size): |
|||
yield tasks[i: i + chunk_size] |
|||
|
|||
|
|||
def run_asyncio_commands(tasks, max_concurrent_tasks=0): |
|||
"""Run tasks asynchronously using asyncio and return results. |
|||
|
|||
If max_concurrent_tasks are set to 0, no limit is applied. |
|||
|
|||
Note: |
|||
By default, Windows uses SelectorEventLoop, which does not support |
|||
subprocesses. Therefore ProactorEventLoop is used on Windows. |
|||
https://docs.python.org/3/library/asyncio-eventloops.html#windows |
|||
""" |
|||
all_results = [] |
|||
|
|||
if max_concurrent_tasks == 0: |
|||
chunks = [tasks] |
|||
num_chunks = len(chunks) |
|||
else: |
|||
chunks = make_chunks(tasks=tasks, chunk_size=max_concurrent_tasks) |
|||
num_chunks = len( |
|||
list(make_chunks(tasks=tasks, chunk_size=max_concurrent_tasks))) |
|||
|
|||
if asyncio.get_event_loop().is_closed(): |
|||
asyncio.set_event_loop(asyncio.new_event_loop()) |
|||
if platform.system() == "Windows": |
|||
asyncio.set_event_loop(asyncio.ProactorEventLoop()) |
|||
loop = asyncio.get_event_loop() |
|||
|
|||
chunk = 1 |
|||
for tasks_in_chunk in chunks: |
|||
commands = asyncio.gather(*tasks_in_chunk) |
|||
results = loop.run_until_complete(commands) |
|||
all_results += results |
|||
chunk += 1 |
|||
|
|||
loop.close() |
|||
return all_results |
@ -0,0 +1,31 @@ |
|||
import importlib.metadata |
|||
import logging |
|||
import os |
|||
import pathlib |
|||
|
|||
import toml |
|||
|
|||
LOG = logging.getLogger("acm.version") |
|||
|
|||
|
|||
def __get_version(): |
|||
""" |
|||
Automatically determine the version of the application being run |
|||
""" |
|||
# Attempt to read the installed package information |
|||
try: |
|||
return importlib.metadata.version('asset-compression-manager') |
|||
except importlib.metadata.PackageNotFoundError: |
|||
LOG.debug("The package is not installed, reading the version from another source") |
|||
|
|||
# Fallback on parsing the pyproject.toml file |
|||
root_dir = pathlib.Path(__file__).parent.parent.resolve() |
|||
with open(os.path.join(root_dir, "pyproject.toml"), "r") as project_file: |
|||
project = toml.load(project_file) |
|||
return project["tool"]["poetry"]["version"] |
|||
|
|||
LOG.debug("Falling back on UNKNOWN version identifier") |
|||
return "UNKNOWN" |
|||
|
|||
# Application Version |
|||
VERSION = __get_version() |
Write
Preview
Loading…
Cancel
Save
Reference in new issue