Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,4 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.idea/
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/FileAutomation.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions dev.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Rename to dev version
# This is dev version
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[project]
name = "automation_file_dev"
version = "0.0.1"
authors = [
{ name = "JE-Chen", email = "zenmailman@gmail.com" },
]
description = ""
readme = { file = "README.md", content-type = "text/markdown" }
requires-python = ">=3.8"
license = { text = "MIT" }
classifiers = [
"Programming Language :: Python :: 3.7",
"Development Status :: 2 - Pre-Alpha",
"Environment :: Win32 (MS Windows)",
"Environment :: MacOS X",
"Environment :: X11 Applications",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent"
]

[project.urls]
"Homepage" = "https://github.com/JE-Chen/Integration-testing-environment"

[tool.setuptools.packages]
find = { namespaces = false }
11 changes: 11 additions & 0 deletions file_automation/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from file_automation.dir.dir_process import copy_dir, create_dir, rename_dir, remove_dir_tree
from file_automation.file.file_process import copy_file, rename_file, remove_file, \
copy_all_file_to_dir, copy_specify_extension_file
from file_automation.zip.zip_process import zip_dir, zip_file, zip_info, zip_file_info, set_zip_password, \
unzip_file, read_zip_file, unzip_all
__all__ = [
"copy_file", "rename_file", "remove_file", "copy_all_file_to_dir", "copy_specify_extension_file",
"copy_dir", "create_dir", "copy_specify_extension_file", "remove_dir_tree",
"zip_dir", "zip_file", "zip_info", "zip_file_info", "set_zip_password", "unzip_file", "read_zip_file",
"unzip_all"
]
File renamed without changes.
43 changes: 43 additions & 0 deletions file_automation/dir/dir_process.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import os
import shutil
import sys
from pathlib import Path

from file_automation.utils.exception.exceptions import DirNotExistsException


def copy_dir(dir_path: str, target_dir_path: str):
dir_path = Path(dir_path)
target_dir_path = Path(target_dir_path)
if dir_path.is_dir():
try:
shutil.copytree(dir_path, target_dir_path, dirs_exist_ok=True)
except shutil.Error as error:
print(repr(error))
else:
print(repr(DirNotExistsException), file=sys.stderr)


def remove_dir_tree(dir_path: str):
dir_path = Path(dir_path)
if dir_path.is_dir():
try:
shutil.rmtree(dir_path)
except shutil.Error as error:
print(repr(error))


def rename_dir(origin_dir_path, target_dir: str):
origin_dir_path = Path(origin_dir_path)
if origin_dir_path.exists() and origin_dir_path.is_dir():
try:
Path.rename(origin_dir_path, target_dir)
except Exception as error:
print(repr(error))
else:
print(repr(DirNotExistsException), file=sys.stderr)


def create_dir(dir_path: str):
dir_path = Path(dir_path)
dir_path.mkdir(exist_ok=True)
File renamed without changes.
61 changes: 61 additions & 0 deletions file_automation/file/file_process.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import shutil
import sys
from pathlib import Path

from file_automation.utils.exception.exceptions import FileNotExistsException, DirNotExistsException


def copy_file(file_path: str, target_path: str):
file_path = Path(file_path)
if file_path.is_file() and file_path.exists():
try:
shutil.copy2(file_path, target_path)
except shutil.Error as error:
print(repr(error))
else:
print(repr(FileNotExistsException), file=sys.stderr)


def copy_specify_extension_file(file_dir_path: str, target_extension: str, target_path: str):
file_dir_path = Path(file_dir_path)
if file_dir_path.exists() and file_dir_path.is_dir():
for file in file_dir_path.glob(f"**/*.{target_extension}"):
copy_file(str(file), target_path)
else:
print(repr(DirNotExistsException), file=sys.stderr)


def copy_all_file_to_dir(dir_path: str, target_dir_path: str):
dir_path = Path(dir_path)
target_dir_path = Path(target_dir_path)
if dir_path.is_dir() and target_dir_path.is_dir():
try:
shutil.move(str(dir_path), str(target_dir_path))
except shutil.Error as error:
print(repr(error), file=sys.stderr)
else:
print(repr(DirNotExistsException), file=sys.stderr)


def rename_file(origin_file_path, target_name: str, file_extension=None):
origin_file_path = Path(origin_file_path)
if origin_file_path.exists() and origin_file_path.is_dir():
if file_extension is None:
file_list = list(origin_file_path.glob("**/*"))
else:
file_list = list(origin_file_path.glob(f"**/*.{file_extension}"))
try:
file_index = 0
for file in file_list:
file.rename(Path(origin_file_path, target_name))
file_index = file_index + 1
except Exception as error:
print(repr(error))
else:
print(repr(DirNotExistsException), file=sys.stderr)


def remove_file(file_path: str):
file_path = Path(file_path)
if file_path.exists() and file_path.is_file():
file_path.unlink(missing_ok=True)
10 changes: 10 additions & 0 deletions file_automation/utils/exception/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class FileNotExistsException(Exception):
pass


class DirNotExistsException(Exception):
pass


class ZIPGetWrongFileException(Exception):
pass
Empty file.
File renamed without changes.
69 changes: 69 additions & 0 deletions file_automation/zip/zip_process.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import sys
import zipfile
from pathlib import Path
from shutil import make_archive
from typing import List

from file_automation.utils.exception.exceptions import ZIPGetWrongFileException


def zip_dir(dir_we_want_to_zip: str, zip_name: str):
make_archive(root_dir=dir_we_want_to_zip, base_name=zip_name, format="zip")


def zip_file(zip_file_path: str, file: [str, List[str]]):
current_zip = zipfile.ZipFile(zip_file_path, mode="w")
if isinstance(file, str):
file_name = Path(file)
current_zip.write(file, file_name.name)
else:
if isinstance(file, list):
for writeable in file:
file_name = Path(writeable)
current_zip.write(writeable, file_name.name)
else:
print(repr(ZIPGetWrongFileException), file=sys.stderr)
current_zip.close()


def read_zip_file(zip_file_path: str, file_name: str, password: [str, None] = None):
current_zip = zipfile.ZipFile(zip_file_path, mode="r")
data = None
with current_zip.open(name=file_name, mode="r", pwd=password, force_zip64=True) as read_file:
data = read_file.read()
current_zip.close()
return data


def unzip_file(zip_file_path: str, extract_member, extract_path: [str, None] = None, password: [str, None] = None):
current_zip = zipfile.ZipFile(zip_file_path, mode="r")
current_zip.extract(member=extract_member, path=extract_path, pwd=password)
current_zip.close()


def unzip_all(
zip_file_path: str, extract_member: [str, None] = None,
extract_path: [str, None] = None, password: [str, None] = None):
current_zip = zipfile.ZipFile(zip_file_path, mode="r")
current_zip.extractall(members=extract_member, path=extract_path, pwd=password)
current_zip.close()


def zip_info(zip_file_path: str):
current_zip = zipfile.ZipFile(zip_file_path, mode="r")
info_list = current_zip.infolist()
current_zip.close()
return info_list


def zip_file_info(zip_file_path: str):
current_zip = zipfile.ZipFile(zip_file_path, mode="r")
name_list = current_zip.namelist()
current_zip.close()
return name_list


def set_zip_password(zip_file_path: str, password: bytes):
current_zip = zipfile.ZipFile(zip_file_path)
current_zip.setpassword(pwd=password)
current_zip.close()
34 changes: 34 additions & 0 deletions tests/unit_test/dir/dir_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from pathlib import Path

from file_automation import copy_dir, remove_dir_tree, rename_dir, create_dir

copy_dir_path = Path(str(Path.cwd()) + "/test_dir")
rename_dir_path = Path(str(Path.cwd()) + "/rename_dir")
first_file_dir = Path(str(Path.cwd()) + "/first_file_dir")
second_file_dir = Path(str(Path.cwd()) + "/second_file_dir")


def test_create_dir():
create_dir(str(copy_dir_path))


def test_copy_dir():
copy_dir(str(first_file_dir), str(copy_dir_path))


def test_rename_dir():
rename_dir(str(copy_dir_path), str(rename_dir_path))


def test_remove_dir_tree():
remove_dir_tree(str(rename_dir_path))


def test():
test_copy_dir()
test_rename_dir()
test_remove_dir_tree()


if __name__ == "__main__":
test()
1 change: 1 addition & 0 deletions tests/unit_test/dir/first_file_dir/test_file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
1 change: 1 addition & 0 deletions tests/unit_test/dir/second_file_dir/test_file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
1 change: 1 addition & 0 deletions tests/unit_test/file/first_file_dir/test_file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
1 change: 1 addition & 0 deletions tests/unit_test/file/second_file_dir/test_file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
49 changes: 49 additions & 0 deletions tests/unit_test/file/test_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from pathlib import Path

from file_automation import copy_file, copy_specify_extension_file, copy_all_file_to_dir, rename_file, remove_file

first_file_dir = Path(str(Path.cwd()) + "/first_file_dir")
second_file_dir = Path(str(Path.cwd()) + "/second_file_dir")
test_file_dir = Path(str(Path.cwd()) + "/test_file")
test_file_path = Path(str(Path.cwd()) + "/test_file/test_file")

with open(str(test_file_path), "w+") as file:
file.write("test")

with open(str(test_file_path) + ".test", "w+") as file:
file.write("test")

with open(str(test_file_path) + ".txt", "w+") as file:
file.write("test")


def test_copy_file():
copy_file(str(test_file_path), str(first_file_dir))


def test_copy_specify_extension_file():
copy_specify_extension_file(str(test_file_dir), "txt", str(second_file_dir))


def test_copy_all_file_to_dir():
copy_all_file_to_dir(str(test_file_dir), str(first_file_dir))


def test_rename_file():
rename_file(str(test_file_dir), "rename", file_extension="txt")


def test_remove_file():
remove_file(str(Path(test_file_dir, "rename")))


def test():
test_copy_file()
test_copy_specify_extension_file()
test_copy_all_file_to_dir()
test_rename_file()
test_remove_file()


if __name__ == "__main__":
test()
1 change: 1 addition & 0 deletions tests/unit_test/file/test_file/test_file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
1 change: 1 addition & 0 deletions tests/unit_test/file/test_file/test_file.test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
1 change: 1 addition & 0 deletions tests/unit_test/zip/dir_to_zip/test3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test3
1 change: 1 addition & 0 deletions tests/unit_test/zip/dir_to_zip/test4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test4
1 change: 1 addition & 0 deletions tests/unit_test/zip/file_to_zip.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
file_to_zip
Binary file added tests/unit_test/zip/test.zip
Binary file not shown.
Binary file added tests/unit_test/zip/test_generate.zip
Binary file not shown.
Loading