Skip to content

Commit

Permalink
Make CLI code nicer (cvat-ai#60)
Browse files Browse the repository at this point in the history
* Return created task id from function

* Add pbar for annotation dump

* Add testing requirements list

* Remove resources properly in tests

* Add backup dump progress bar

* Refactor code

* Set up logging in CLI

* Add annotations uploading  progress with tus

* Refactor code

* Add tqdm dependency

* Update changelog

* Add some comments to the implementation

* Remove extra code

* Update ci container

* Add progress bars to task import

* Add tests, refactor code

* Add progressbar for task creation

* Remove extra line

* Change exception type

* Move requirements files

* Fix dockerfile

* Revert extra change

* Isolate test directories

* Move cli package

* Update cli package references

* Move package files into a directory

* Move files

* Update requirements and dockerfiles

* Add cvat-cli package

* Autoformat CLI code

* Add developer guide

* Update readme

* Add Black check on CI

* Add isort check on CI

* Merge branch 'develop' into zm/cli-package

* Update package

* Change paths in cli code

* Move files

* Update docs

* Update dockerfile

* Update changelog

* Fix linter issues

* Fix linter issues

* Add dev requirements

* Update ci

Co-authored-by: Nikita Manovich <[email protected]>
  • Loading branch information
zhiltsov-max and nmanovic authored Jun 21, 2022
1 parent ca8150e commit 26d78fe
Show file tree
Hide file tree
Showing 11 changed files with 488 additions and 441 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/black.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Linter
on: pull_request
jobs:
Black:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- id: files
uses: jitterbit/get-changed-files@v1
continue-on-error: true

- name: Run checks
env:
PR_FILES_AM: ${{ steps.files.outputs.added_modified }}
PR_FILES_RENAMED: ${{ steps.files.outputs.renamed }}
run: |
PR_FILES="$PR_FILES_AM $PR_FILES_RENAMED"
for FILE in $PR_FILES; do
EXTENSION="${FILE##*.}"
DIRECTORY="${FILE%%/*}"
if [[ "$EXTENSION" == "py" && "$DIRECTORY" == "cvat-cli" ]]; then
CHANGED_FILES+=" $FILE"
fi
done
if [[ ! -z $CHANGED_FILES ]]; then
sudo apt-get --no-install-recommends install -y build-essential curl python3-dev python3-pip python3-venv
python3 -m venv .env
. .env/bin/activate
pip install -U pip wheel setuptools
pip install $(egrep "black.*" ./cvat-cli/requirements/development.txt)
mkdir -p black_report
echo "Black version: "$(black --version)
echo "The files will be checked: "$(echo $CHANGED_FILES)
black --check --config ./cvat-cli/pyproject.toml $CHANGED_FILES > ./black_report/black_checks.txt || EXIT_CODE=$(echo $?) || true
deactivate
exit $EXIT_CODE
else
echo "No files with the \"py\" extension found"
fi
- name: Upload artifacts
if: failure()
uses: actions/upload-artifact@v2
with:
name: black_report
path: black_report
48 changes: 48 additions & 0 deletions .github/workflows/isort.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Linter
on: pull_request
jobs:
isort:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- id: files
uses: jitterbit/get-changed-files@v1
continue-on-error: true

- name: Run checks
env:
PR_FILES_AM: ${{ steps.files.outputs.added_modified }}
PR_FILES_RENAMED: ${{ steps.files.outputs.renamed }}
run: |
PR_FILES="$PR_FILES_AM $PR_FILES_RENAMED"
for FILE in $PR_FILES; do
EXTENSION="${FILE##*.}"
DIRECTORY="${FILE%%/*}"
if [[ "$EXTENSION" == "py" && "$DIRECTORY" == "cvat-cli" ]]; then
CHANGED_FILES+=" $FILE"
fi
done
if [[ ! -z $CHANGED_FILES ]]; then
sudo apt-get --no-install-recommends install -y build-essential curl python3-dev python3-pip python3-venv
python3 -m venv .env
. .env/bin/activate
pip install -U pip wheel setuptools
pip install $(egrep "isort.*" ./cvat-cli/requirements/development.txt)
mkdir -p isort_report
echo "isort version: "$(isort --version)
echo "The files will be checked: "$(echo $CHANGED_FILES)
isort --check --sp ./cvat-cli/pyproject.toml $CHANGED_FILES > ./isort_report/isort_checks.txt || EXIT_CODE=$(echo $?) || true
deactivate
exit $EXIT_CODE
else
echo "No files with the \"py\" extension found"
fi
- name: Upload artifacts
if: failure()
uses: actions/upload-artifact@v2
with:
name: isort_report
path: isort_report
8 changes: 8 additions & 0 deletions cvat-cli/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"

[tool.isort]
profile = "black"
forced_separate = ["tests"]
line_length = 100

[tool.black]
line-length = 100
target-version = ['py38']
5 changes: 5 additions & 0 deletions cvat-cli/requirements/development.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-r base.txt

black>=22.1.0
isort>=5.10.1
pylint>=2.7.0
6 changes: 3 additions & 3 deletions cvat-cli/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os.path as osp
import re

import setuptools
from setuptools import find_packages, setup


def find_version(project_dir=None):
Expand Down Expand Up @@ -42,15 +42,15 @@ def parse_requirements(filename=BASE_REQUIREMENTS_FILE):
with open("README.md", "r") as fh:
long_description = fh.read()

setuptools.setup(
setup(
name="cvat-cli",
version=find_version(project_dir="src/cvat_cli"),
description="Command-line client for CVAT",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/openvinotoolkit/cvat/",
package_dir={"": "src"},
packages=setuptools.find_packages(where='src'),
packages=find_packages(where="src"),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
Expand Down
40 changes: 23 additions & 17 deletions cvat-cli/src/cvat_cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
# SPDX-License-Identifier: MIT

import logging
import requests
import sys
from http.client import HTTPConnection

import requests

from cvat_cli.core.core import CLI, CVAT_API_V2
from cvat_cli.core.definition import parser

log = logging.getLogger(__name__)


def config_log(level):
log = logging.getLogger('core')
formatter = logging.Formatter('[%(asctime)s] %(levelname)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S', style='%')
log = logging.getLogger("core")
formatter = logging.Formatter(
"[%(asctime)s] %(levelname)s: %(message)s", datefmt="%Y-%m-%d %H:%M:%S", style="%"
)
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(formatter)
log.addHandler(handler)
Expand All @@ -25,29 +29,31 @@ def config_log(level):

def main():
actions = {
'create': CLI.tasks_create,
'delete': CLI.tasks_delete,
'ls': CLI.tasks_list,
'frames': CLI.tasks_frame,
'dump': CLI.tasks_dump,
'upload': CLI.tasks_upload,
'export': CLI.tasks_export,
'import': CLI.tasks_import,
"create": CLI.tasks_create,
"delete": CLI.tasks_delete,
"ls": CLI.tasks_list,
"frames": CLI.tasks_frame,
"dump": CLI.tasks_dump,
"upload": CLI.tasks_upload,
"export": CLI.tasks_export,
"import": CLI.tasks_import,
}
args = parser.parse_args()
config_log(args.loglevel)
with requests.Session() as session:
api = CVAT_API_V2('%s:%s' % (args.server_host, args.server_port), args.https)
api = CVAT_API_V2("%s:%s" % (args.server_host, args.server_port), args.https)
cli = CLI(session, api, args.auth)
try:
actions[args.action](cli, **args.__dict__)
except (requests.exceptions.HTTPError,
requests.exceptions.ConnectionError,
requests.exceptions.RequestException) as e:
except (
requests.exceptions.HTTPError,
requests.exceptions.ConnectionError,
requests.exceptions.RequestException,
) as e:
log.critical(e)

return 0


if __name__ == '__main__':
if __name__ == "__main__":
sys.exit(main())
2 changes: 1 addition & 1 deletion cvat-cli/src/cvat_cli/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
#
# SPDX-License-Identifier: MIT

from .definition import parser, ResourceType # noqa
from .core import CLI, CVAT_API_V2 # noqa
from .definition import ResourceType, parser # noqa
Loading

0 comments on commit 26d78fe

Please sign in to comment.