Skip to content

Commit

Permalink
Add extracting zip function (#20)
Browse files Browse the repository at this point in the history
* Add extracting zip function

* add discover function as well

* Fix comment

* Formatting

* Update docs

* Now?

* Update docs again?

* Update 3RD Party file
  • Loading branch information
lukasmittag authored Jul 19, 2024
1 parent 5404aec commit d7c514b
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 17 deletions.
2 changes: 1 addition & 1 deletion NOTICE-3RD-PARTY-CONTENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Python
| Dependency | Version | License |
|:-----------|:-------:|--------:|
|types-requests|2.32.0.20240622|Apache 2.0|
|types-requests|2.32.0.20240712|Apache 2.0|
|urllib3|2.2.2|MIT|
## Workflows
| Dependency | Version | License |
Expand Down
2 changes: 2 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
## Functions

- [`velocitas_lib.create_log_file`](./velocitas_lib.md#function-create_log_file): Create a log file for the given service and runtime.
- [`velocitas_lib.discover_files_in_filetree`](./velocitas_lib.md#function-discover_files_in_filetree): Recursively search for files with a specific file type under the tree root.
- [`velocitas_lib.download_file`](./velocitas_lib.md#function-download_file)
- [`velocitas_lib.extract_zip`](./velocitas_lib.md#function-extract_zip): Extract a zip file.
- [`velocitas_lib.get_app_manifest`](./velocitas_lib.md#function-get_app_manifest)
- [`velocitas_lib.get_cache_data`](./velocitas_lib.md#function-get_cache_data): Return the data of the cache as Python object.
- [`velocitas_lib.get_log_file_name`](./velocitas_lib.md#function-get_log_file_name): Build the log file name for the given service and runtime.
Expand Down
85 changes: 71 additions & 14 deletions docs/velocitas_lib.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

---

<a href="../velocitas_lib/__init__.py#L24"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../velocitas_lib/__init__.py#L26"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `get_valid_arch`

Expand All @@ -35,7 +35,7 @@ Return a known architecture for the given `arch`.

---

<a href="../velocitas_lib/__init__.py#L41"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../velocitas_lib/__init__.py#L43"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `require_env`

Expand Down Expand Up @@ -66,7 +66,7 @@ Require and return an environment variable.

---

<a href="../velocitas_lib/__init__.py#L59"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../velocitas_lib/__init__.py#L61"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `get_workspace_dir`

Expand All @@ -79,7 +79,7 @@ Return the workspace directory.

---

<a href="../velocitas_lib/__init__.py#L64"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../velocitas_lib/__init__.py#L66"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `get_app_manifest`

Expand All @@ -94,7 +94,7 @@ get_app_manifest() → Dict[str, Any]

---

<a href="../velocitas_lib/__init__.py#L74"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../velocitas_lib/__init__.py#L76"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `get_script_path`

Expand All @@ -107,7 +107,7 @@ Return the absolute path to the directory the invoked Python script is located i

---

<a href="../velocitas_lib/__init__.py#L80"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../velocitas_lib/__init__.py#L82"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `get_package_path`

Expand All @@ -120,7 +120,7 @@ Return the absolute path to the package directory the invoked Python script belo

---

<a href="../velocitas_lib/__init__.py#L86"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../velocitas_lib/__init__.py#L88"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `get_project_cache_dir`

Expand All @@ -139,7 +139,7 @@ Return the project's cache directory.

---

<a href="../velocitas_lib/__init__.py#L95"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../velocitas_lib/__init__.py#L97"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `get_cache_data`

Expand All @@ -152,7 +152,7 @@ Return the data of the cache as Python object.

---

<a href="../velocitas_lib/__init__.py#L105"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../velocitas_lib/__init__.py#L107"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `get_log_file_name`

Expand All @@ -178,7 +178,7 @@ Build the log file name for the given service and runtime.

---

<a href="../velocitas_lib/__init__.py#L118"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../velocitas_lib/__init__.py#L120"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `get_programming_language`

Expand All @@ -191,7 +191,7 @@ Return the programming language of the project.

---

<a href="../velocitas_lib/__init__.py#L123"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../velocitas_lib/__init__.py#L125"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `create_log_file`

Expand All @@ -217,7 +217,7 @@ Create a log file for the given service and runtime.

---

<a href="../velocitas_lib/__init__.py#L138"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../velocitas_lib/__init__.py#L140"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `download_file`

Expand All @@ -232,7 +232,7 @@ download_file(uri: str, local_file_path: str) → None

---

<a href="../velocitas_lib/__init__.py#L146"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../velocitas_lib/__init__.py#L148"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `is_uri`

Expand All @@ -257,7 +257,7 @@ Check if the provided path is a URI.

---

<a href="../velocitas_lib/__init__.py#L158"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../velocitas_lib/__init__.py#L160"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `obtain_local_file_path`

Expand All @@ -284,6 +284,63 @@ Return the absolute path to the file, specified by a absolute/relative local pat
- <b>`str`</b>: The absolute path to the file.


---

<a href="../velocitas_lib/__init__.py#L195"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `extract_zip`

```python
extract_zip(file_path: str, extract_to: str) → str
```

Extract a zip file.



**Args:**

- <b>`file_path`</b> (str): The file path to the zip.
- <b>`extract_to`</b> (str): The file path to extract to.



**Raises:**
RuntimeError if the file_path is not a zip.



**Returns:**

- <b>`str`</b>: The file path to the extracted top level folder.


---

<a href="../velocitas_lib/__init__.py#L217"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `discover_files_in_filetree`

```python
discover_files_in_filetree(tree_root: str, file_type: str) → List[str]
```

Recursively search for files with a specific file type under the tree root.



**Args:**

- <b>`tree_root`</b> (str): The path to the tree root to search from.
- <b>`file_type`</b> (str): The file type that is searched for.



**Returns:**

- <b>`List[str]`</b>: A list of file paths, relative to the search tree root.




---
Expand Down
47 changes: 45 additions & 2 deletions velocitas_lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
import os
import sys
import re
import requests
import zipfile
from io import TextIOWrapper
from typing import Any, Dict, Optional
from typing import Any, Dict, List, Optional

import requests


def get_valid_arch(arch: str) -> str:
Expand Down Expand Up @@ -188,3 +190,44 @@ def obtain_local_file_path(

download_file(path_or_uri, download_path)
return download_path


def extract_zip(file_path: str, extract_to: str) -> str:
"""Extract a zip file.
Args:
file_path (str): The file path to the zip.
extract_to (str): The file path to extract to.
Raises:
RuntimeError if the file_path is not a zip.
Returns:
str: The file path to the extracted top level folder.
"""
if zipfile.is_zipfile(file_path):
with zipfile.ZipFile(file_path, "r") as zip_ref:
zip_ref.extractall(extract_to)

return extract_to
else:
raise RuntimeError(f"{file_path!r} is not a zip file!")


def discover_files_in_filetree(tree_root: str, file_type: str) -> List[str]:
"""
Recursively search for files with a specific file type under the tree root.
Args:
tree_root (str): The path to the tree root to search from.
file_type (str): The file type that is searched for.
Returns:
List[str]: A list of file paths, relative to the search tree root.
"""
files = []
for dir, _, potential_files in os.walk(tree_root):
for file in potential_files:
if file.endswith(file_type):
files.append(os.path.join(dir, file))
return files

0 comments on commit d7c514b

Please sign in to comment.