Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be compatible with python 2 #14

Merged
merged 9 commits into from Jan 20, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -72,3 +72,4 @@ ENV/

tests/test_data.py
tests/data/large_file
scenarios/test_config.yaml
15 changes: 15 additions & 0 deletions .travis.yml
@@ -0,0 +1,15 @@
language: python

python:
- '2.6'
- '2.7'
- '3.3'
- '3.4'
- '3.5'

install:
- pip install pytest mock wheel
- python setup.py sdist bdist_wheel --universal
- pip install dist/*.whl

script: py.test tests
9 changes: 8 additions & 1 deletion Makefile
Expand Up @@ -12,6 +12,7 @@ all: unit build

unit:
@echo "run unit test"
pip install pytest mock
py.test
@echo "ok"

Expand All @@ -21,6 +22,12 @@ tox:
tox
@echo "ok"

test:
@echo "run service test"
pip install -r scenarios/requirements.txt
behave scenarios/features
@echo "ok"

clean:
@echo "clean build and dist files"
rm -rf build dist qsctl.egg-info
Expand All @@ -33,5 +40,5 @@ build: clean

format:
@echo "format code with google style"
yapf -i -r ./qingstor ./tests
yapf -i -r ./qingstor ./tests ./scenarios
@echo "ok"
1 change: 1 addition & 0 deletions bin/qsctl
@@ -1,4 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# =========================================================================
# Copyright (C) 2016 Yunify, Inc.
# -------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions bin/qsctl.cmd
Expand Up @@ -33,6 +33,7 @@ goto :EOF
# ===================================================

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# =========================================================================
# Copyright (C) 2016 Yunify, Inc.
# -------------------------------------------------------------------------
Expand Down
29 changes: 18 additions & 11 deletions qingstor/qsctl/commands/base.py
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# =========================================================================
# Copyright (C) 2016 Yunify, Inc.
# -------------------------------------------------------------------------
Expand All @@ -14,6 +15,8 @@
# limitations under the License.
# =========================================================================

from __future__ import unicode_literals

import os
import sys
import argparse
Expand Down Expand Up @@ -62,7 +65,7 @@ def add_transfer_arguments(cls, parser):
@classmethod
def get_argument_parser(cls):
parser = argparse.ArgumentParser(
prog='qsctl %s' % cls.command,
prog="qsctl %s" % cls.command,
usage=cls.usage,
description=cls.description
)
Expand Down Expand Up @@ -138,7 +141,7 @@ def validate_qs_path(cls, qs_path):
qs_path = to_unix_path(qs_path)
if qs_path.startswith("qs://"):
qs_path = qs_path[5:]
qs_path_split = qs_path.split('/', 1)
qs_path_split = qs_path.split("/", 1)
if len(qs_path_split) == 1:
bucket, prefix = qs_path_split[0], ""
elif len(qs_path_split) == 2:
Expand Down Expand Up @@ -184,17 +187,15 @@ def remove_multiple_keys(cls, bucket, prefix="", options=None):
bucket, marker=marker, prefix=prefix
)
for item in keys:
key = item["key"] if sys.version > "3" else item["key"].encode(
'utf8'
)
key = item["key"]
if cls.confirm_key_remove(key[len(prefix):], options):
cls.remove_key(bucket, key)
if marker == "":
break

@classmethod
def list_multiple_keys(
cls, bucket, prefix="", delimiter="", marker="", limit=200
cls, bucket, prefix="", delimiter="", marker="", limit="200"
):
cls.validate_bucket(bucket)
current_bucket = cls.client.Bucket(bucket, cls.bucket_map[bucket])
Expand All @@ -213,13 +214,19 @@ def main(cls, args):
options = parser.parse_args(args)

# Load config file
conf = load_conf(options.config)
config_path = [
options.config, "~/.qingstor/config.yaml",
"~/.qingcloud/config.yaml"
]
for path in config_path:
conf = load_conf(path)
if conf is not None:
# Get client of qingstor
cls.client = cls.get_client(conf)
break

if conf is None:
if cls.client is None:
sys.exit(-1)

# Get client of qingstor
cls.client = cls.get_client(conf)

# Send request
return cls.send_request(options)
3 changes: 3 additions & 0 deletions qingstor/qsctl/commands/cp.py
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# =========================================================================
# Copyright (C) 2016 Yunify, Inc.
# -------------------------------------------------------------------------
Expand All @@ -14,6 +15,8 @@
# limitations under the License.
# =========================================================================

from __future__ import unicode_literals

from .transfer import TransferCommand


Expand Down
7 changes: 5 additions & 2 deletions qingstor/qsctl/commands/ls.py
@@ -1,3 +1,4 @@
# -*- coding: UTF-8 -*-
# =========================================================================
# Copyright (C) 2016 Yunify, Inc.
# -------------------------------------------------------------------------
Expand All @@ -14,6 +15,8 @@
# limitations under the License.
# =========================================================================

from __future__ import unicode_literals

import sys
import time

Expand Down Expand Up @@ -104,13 +107,13 @@ def list_keys(cls, options):
bucket, prefix = cls.validate_qs_path(options.qs_path)

delimiter = ""
limit = 200
limit = "200"
marker = ""

if options.recursive is False:
delimiter = "/"
if options.page_size is not None:
limit = options.page_size
limit = str(options.page_size)

while True:
keys, marker, dirs = cls.list_multiple_keys(
Expand Down
3 changes: 3 additions & 0 deletions qingstor/qsctl/commands/mb.py
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# =========================================================================
# Copyright (C) 2016 Yunify, Inc.
# -------------------------------------------------------------------------
Expand All @@ -14,6 +15,8 @@
# limitations under the License.
# =========================================================================

from __future__ import unicode_literals

import sys

from .base import BaseCommand
Expand Down
3 changes: 3 additions & 0 deletions qingstor/qsctl/commands/mv.py
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# =========================================================================
# Copyright (C) 2016 Yunify, Inc.
# -------------------------------------------------------------------------
Expand All @@ -14,6 +15,8 @@
# limitations under the License.
# =========================================================================

from __future__ import unicode_literals

import os

from .transfer import TransferCommand
Expand Down
3 changes: 3 additions & 0 deletions qingstor/qsctl/commands/rb.py
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# =========================================================================
# Copyright (C) 2016 Yunify, Inc.
# -------------------------------------------------------------------------
Expand All @@ -14,6 +15,8 @@
# limitations under the License.
# =========================================================================

from __future__ import unicode_literals

import sys

from .base import BaseCommand
Expand Down
3 changes: 3 additions & 0 deletions qingstor/qsctl/commands/rm.py
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# =========================================================================
# Copyright (C) 2016 Yunify, Inc.
# -------------------------------------------------------------------------
Expand All @@ -14,6 +15,8 @@
# limitations under the License.
# =========================================================================

from __future__ import unicode_literals

import sys

from .base import BaseCommand
Expand Down
3 changes: 3 additions & 0 deletions qingstor/qsctl/commands/sync.py
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# =========================================================================
# Copyright (C) 2016 Yunify, Inc.
# -------------------------------------------------------------------------
Expand All @@ -14,6 +15,8 @@
# limitations under the License.
# =========================================================================

from __future__ import unicode_literals

import os
import sys
import time
Expand Down
30 changes: 18 additions & 12 deletions qingstor/qsctl/commands/transfer.py
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# =========================================================================
# Copyright (C) 2016 Yunify, Inc.
# -------------------------------------------------------------------------
Expand All @@ -14,10 +15,14 @@
# limitations under the License.
# =========================================================================

from __future__ import unicode_literals

import os
import sys
import errno

from tqdm import tqdm

from .base import BaseCommand

from ..constants import (
Expand All @@ -40,7 +45,6 @@


class TransferCommand(BaseCommand):

command = ""
usage = ""

Expand Down Expand Up @@ -134,8 +138,8 @@ def upload_files(cls, options):
key_path = to_unix_path(key_path)
key = prefix + key_path
if (is_pattern_match(key_path, options.exclude, options.include)
and cls.confirm_key_upload(options, local_path, bucket,
key)):
and cls.confirm_key_upload(options, local_path, bucket,
key)):
cls.put_directory(bucket, key)

for f in files:
Expand All @@ -144,8 +148,8 @@ def upload_files(cls, options):
key_path = to_unix_path(key_path)
key = prefix + key_path
if (is_pattern_match(key_path, options.exclude, options.include)
and cls.confirm_key_upload(options, local_path, bucket,
key)):
and cls.confirm_key_upload(options, local_path, bucket,
key)):
cls.send_local_file(local_path, bucket, key)

cls.cleanup("LOCAL_TO_QS", options, bucket, prefix)
Expand Down Expand Up @@ -179,14 +183,16 @@ def download_files(cls, options):
bucket, marker=marker, prefix=prefix
)
for item in keys:
key = item["key"] if sys.version > "3" else item["key"].encode(
'utf8'
)
key = item["key"]
key_name = key[len(prefix):]
local_path = join_local_path(options.dest_path, key_name)
if (is_pattern_match(key_name, options.exclude, options.include)
and cls.confirm_key_download(options, local_path,
item["modified"])):
is_match = is_pattern_match(
key_name, options.exclude, options.include
)
is_confirmed_key_download = cls.confirm_key_download(
options, local_path, item["modified"]
)
if local_path and is_match and is_confirmed_key_download:
cls.write_local_file(local_path, bucket, key)
if marker == "":
break
Expand All @@ -206,7 +212,7 @@ def download_file(cls, options):
local_path = join_local_path(options.dest_path, key)
else:
local_path = options.dest_path
if cls.confirm_key_download(options, local_path):
if local_path and cls.confirm_key_download(options, local_path):
cls.write_local_file(local_path, bucket, key)

@classmethod
Expand Down
8 changes: 8 additions & 0 deletions qingstor/qsctl/driver.py
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# =========================================================================
# Copyright (C) 2016 Yunify, Inc.
# -------------------------------------------------------------------------
Expand All @@ -14,11 +15,14 @@
# limitations under the License.
# =========================================================================

from __future__ import unicode_literals

import sys
import argparse
import pkg_resources
from difflib import get_close_matches

from qingstor.qsctl.utils import is_python2
from qingstor.qsctl.helper import get_renderer
from qingstor.qsctl.commands.ls import LsCommand
from qingstor.qsctl.commands.cp import CpCommand
Expand Down Expand Up @@ -55,6 +59,10 @@ def exit_due_to_invalid_command(suggest_commands=None):


def check_argument(args):
if is_python2:
for i in range(len(args)):
args[i] = args[i].decode("utf-8")

if len(args) < 2:
exit_due_to_invalid_command()

Expand Down
2 changes: 2 additions & 0 deletions qingstor/qsctl/helper/qsdocutils.py
@@ -1,3 +1,5 @@
# -*- coding: UTF-8 -*-

import os
import signal
import contextlib
Expand Down