Skip to content

Commit

Permalink
Merge pull request #10 from paulocoutinhox/fix-file-create-dir
Browse files Browse the repository at this point in the history
fix create dir for invalid dir
  • Loading branch information
paulocoutinhox committed Feb 2, 2024
2 parents 7b389d6 + c03ccb6 commit b075a80
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 36 deletions.
22 changes: 13 additions & 9 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
name: Build

on: [push]
on:
push:
paths-ignore:
- '**.md'
- 'docs/**'
- 'extras/images/**'

jobs:
build:
Expand All @@ -13,24 +18,23 @@ jobs:
- { name: "Linux", os: ubuntu-latest }
- { name: "Windows", os: windows-latest }
- { name: "macOS", os: macos-latest }
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10"]
exclude:
- { config: { os: macos-latest }, python-version: "3.6" }
- { config: { os: windows-latest }, python-version: "3.6" }

python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Upgrade PIP
run: python3 -m pip install --upgrade pip setuptools wheel

- name: Install Poetry
uses: snok/install-poetry@v1
- name: Install poetry
uses: abatilo/actions-poetry@v2

- name: Display Python version
run: python3 --version
Expand Down
9 changes: 6 additions & 3 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,25 @@ jobs:
publish:
name: Publish
runs-on: "ubuntu-latest"
defaults:
run:
shell: bash
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v1
uses: actions/setup-python@v5
with:
python-version: "3.10"

- name: Upgrade PIP
run: python3 -m pip install --upgrade pip setuptools wheel

- name: Install Poetry
uses: snok/install-poetry@v1
- name: Install poetry
uses: abatilo/actions-poetry@v2

- name: Display Python version
run: python3 --version
Expand Down
9 changes: 6 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,25 @@ jobs:
autorelease:
name: Release
runs-on: "ubuntu-latest"
defaults:
run:
shell: bash
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v1
uses: actions/setup-python@v5
with:
python-version: "3.10"

- name: Upgrade PIP
run: python3 -m pip install --upgrade pip setuptools wheel

- name: Install Poetry
uses: snok/install-poetry@v1
- name: Install poetry
uses: abatilo/actions-poetry@v2

- name: Display Python version
run: python3 --version
Expand Down
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ Python package that group a lot of classes and functions that help software deve

### Requirements

* Python 3.6+
* Python 3.7+

### How To Use

To use in your project, install `pygemstones` module:

```
pip install pygemstones
python3 -m pip install pygemstones
```

or:
Expand Down Expand Up @@ -62,7 +62,7 @@ There are several implemented modules for you to use:

These are the requirements for local development:

* Python 3.6+
* Python 3.7+
* Poetry (https://python-poetry.org/)

You can install locally:
Expand Down Expand Up @@ -132,3 +132,13 @@ git push origin v0.0.1
```

After release action finish, publish the release on Github `releases` page and Github Action will run `publish steps` automatically.

## Buy me a coffee

<a href='https://ko-fi.com/paulocoutinho' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://az743702.vo.msecnd.net/cdn/kofi1.png?v=2' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a>

## License

[MIT](http://opensource.org/licenses/MIT)

Copyright (c) 2021-2024, Paulo Coutinho
2 changes: 1 addition & 1 deletion pygemstones/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.13"
__version__ = "0.0.14"
16 changes: 12 additions & 4 deletions pygemstones/io/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os
import shutil
import stat
from distutils.dir_util import copy_tree


# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -297,8 +296,9 @@ def create_dir(path):
None
"""

if not os.path.isdir(path):
os.makedirs(path, exist_ok=True)
if path:
if not os.path.isdir(path):
os.makedirs(path, exist_ok=True)


# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -428,7 +428,15 @@ def copy_all(src_path, dst_path):
"""

create_dir(dst_path)
copy_tree(src_path, dst_path, update=1, preserve_symlinks=True)

for item in os.listdir(src_path):
src_item = os.path.join(src_path, item)
dst_item = os.path.join(dst_path, item)

if os.path.isdir(src_item):
shutil.copytree(src_item, dst_item, dirs_exist_ok=True, symlinks=True)
else:
shutil.copy2(src_item, dst_item)


# -----------------------------------------------------------------------------
Expand Down
18 changes: 9 additions & 9 deletions pygemstones/io/pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,25 @@ def unpack(src_path, dst_path):
".gz",
]:
with tarfile.open(src_path, "r:*") as archive:

def is_within_directory(directory, target):

abs_directory = os.path.abspath(directory)
abs_target = os.path.abspath(target)

prefix = os.path.commonprefix([abs_directory, abs_target])

return prefix == abs_directory

def safe_extract(tar, path=".", members=None, *, numeric_owner=False):

for member in tar.getmembers():
member_path = os.path.join(path, member.name)
if not is_within_directory(path, member_path):
raise Exception("Attempted Path Traversal in Tar File")

tar.extractall(path, members, numeric_owner=numeric_owner)



tar.extractall(path, members, numeric_owner=numeric_owner)

safe_extract(archive, dst_path)
archive.close()
else:
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
description = "Python package that group a lot of classes and functions that help software development."
name = "pygemstones"
version = "0.0.13"
version = "0.0.14"

authors = ["Paulo Coutinho <paulocoutinhox@gmail.com>"]
license = "MIT"
Expand All @@ -17,12 +17,12 @@ keywords = []
classifiers = ["Topic :: Software Development :: Libraries :: Python Modules"]

[tool.poetry.dependencies]
python = "^3.6.2"
python = "^3.7.0"
colorama = "^0.4.4"
boto3 = "^1.20.10"

[tool.poetry.dev-dependencies]
black = "^21.11b0"
black = "^22.3.0"
pytest = "^6.2.5"
pytest-cov = "^3.0.0"
isort = "^5.10.1"
Expand Down
11 changes: 11 additions & 0 deletions tests/io/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ def test_create_dir(tmp_path):
assert f.dir_exists(target_path)


# -----------------------------------------------------------------------------
def test_create_dir_invalid(tmp_path):
target_path = ""
f.create_dir(target_path)

target_path = None
f.create_dir(target_path)

assert True


# -----------------------------------------------------------------------------
def test_create_dir_that_already_exists(tmp_path):
target_path = os.path.join(tmp_path, "new-dir")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_pygems.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

# -----------------------------------------------------------------------------
def test_version():
assert __version__ == "0.0.13"
assert __version__ == "0.0.14"

0 comments on commit b075a80

Please sign in to comment.