From cf2817de19b69bf2ae7a78f983a20c712c2ba2e8 Mon Sep 17 00:00:00 2001 From: memsharded Date: Wed, 19 Apr 2017 22:20:20 +0200 Subject: [PATCH] changed regex for validation --- conans/__init__.py | 3 +- conans/model/ref.py | 68 +++++++++++++---------- conans/model/username.py | 30 +--------- conans/test/integration/conan_env_test.py | 10 ++-- conans/test/model/ref_test.py | 17 +++++- conans/test/model/username_test.py | 11 ++-- 6 files changed, 70 insertions(+), 69 deletions(-) diff --git a/conans/__init__.py b/conans/__init__.py index c26974fe1dc..70861864c6b 100644 --- a/conans/__init__.py +++ b/conans/__init__.py @@ -15,5 +15,4 @@ COMPLEX_SEARCH_CAPABILITY = "complex_search" SERVER_CAPABILITIES = [COMPLEX_SEARCH_CAPABILITY, ] -__version__ = '0.22.1' - +__version__ = '0.22.2' diff --git a/conans/model/ref.py b/conans/model/ref.py index f07f63b200a..ec578e6dcc2 100644 --- a/conans/model/ref.py +++ b/conans/model/ref.py @@ -2,41 +2,49 @@ import re from conans.errors import ConanException, InvalidNameException from conans.model.version import Version -from conans.model.username import Username -def _validate_conan_name(name, version=False): - """Check for name compliance with pattern rules""" - if name == "*": - return - if ConanFileReference.validation_pattern.match(name) is None: - if version and name.startswith("[") and name.endswith("]"): - return - if len(name) > ConanFileReference.max_chars: - message = "'%s' is too long. Valid names must " \ - "contain at most %s characters." % (name, - ConanFileReference.max_chars) - elif len(name) < ConanFileReference.min_chars: - message = "'%s' is too short. Valid names must contain"\ - " at least %s characters." % (name, ConanFileReference.min_chars) +class ConanName(object): + _max_chars = 40 + _min_chars = 2 + _validation_pattern = re.compile("^[a-zA-Z0-9_][a-zA-Z0-9_\+\.-]{%s,%s}$" + % (_min_chars - 1, _max_chars)) + + @staticmethod + def invalid_name_message(name): + if len(name) > ConanName._max_chars: + message = ("'%s' is too long. Valid names must contain at most %s characters." + % (name, ConanName._max_chars)) + elif len(name) < ConanName._min_chars: + message = ("'%s' is too short. Valid names must contain at least %s characters." + % (name, ConanName._min_chars)) else: - message = "'%s' is an invalid name. Valid names MUST begin with a "\ - "letter or number, have between %s-%s chars, including "\ - "letters, numbers, underscore,"\ - " dot and dash" % (name, ConanFileReference.min_chars, - ConanFileReference.max_chars) + message = ("'%s' is an invalid name. Valid names MUST begin with a " + "letter or number, have between %s-%s chars, including " + "letters, numbers, underscore, dot and dash" + % (name, ConanName._min_chars, ConanName._max_chars)) raise InvalidNameException(message) + @staticmethod + def validate_user(username): + if ConanName._validation_pattern.match(username) is None: + ConanName.invalid_name_message(username) + + @staticmethod + def validate_name(name, version=False): + """Check for name compliance with pattern rules""" + if name == "*": + return + if ConanName._validation_pattern.match(name) is None: + if version and name.startswith("[") and name.endswith("]"): + return + ConanName.invalid_name_message(name) + class ConanFileReference(namedtuple("ConanFileReference", "name version user channel")): - """ Full reference of a conans, e.g.: + """ Full reference of a package recipes, e.g.: opencv/2.4.10@lasote/testing """ - max_chars = 40 - min_chars = 2 - base_er = "[a-zA-Z0-9_]+[a-zA-Z0-9_\+\.-]{%s,%s}" % (min_chars - 1, max_chars) - regular_expression = "^%s$" % base_er - validation_pattern = re.compile(regular_expression) whitespace_pattern = re.compile(r"\s+") sep_pattern = re.compile("@|/") @@ -45,10 +53,10 @@ def __new__(cls, name, version, user, channel): @param name: string containing the desired name @param validate: checks for valid complex name. default True """ - _validate_conan_name(name) - _validate_conan_name(version, True) - Username.validate(user, pattern=True) - _validate_conan_name(channel) + ConanName.validate_name(name) + ConanName.validate_name(version, True) + ConanName.validate_name(user) + ConanName.validate_name(channel) version = Version(version) return super(cls, ConanFileReference).__new__(cls, name, version, user, channel) diff --git a/conans/model/username.py b/conans/model/username.py index 6d2198bded0..f7e96d2a872 100644 --- a/conans/model/username.py +++ b/conans/model/username.py @@ -1,38 +1,12 @@ -from conans.errors import InvalidNameException -import re +from conans.model.ref import ConanName class Username(str): - max_len = 30 - min_len = 2 - base_er = "[a-zA-Z][a-zA-Z0-9_-]{%s,%s}" % (min_len-1, max_len-1) - pattern = re.compile("^%s$" % base_er) - def __new__(cls, name): """Simple name creation. @param name: string containing the desired name - @param validate: checks for valid simple name. default True """ - Username.validate(name) + ConanName.validate_user(name) return str.__new__(cls, name) - - @staticmethod - def validate(name, pattern=False): - """Check for name compliance with pattern rules. User names can be - with upper/lower case - """ - if Username.pattern.match(name) is None: - if pattern and name == "*": - return - if len(name) > Username.max_len: - message = "'%s' is too long. Valid names must contain at most %s characters." \ - "" % (name, Username.max_len) - elif len(name) < Username.min_len: - message = "'%s' is too short. Valid names must contain at least %s characters." \ - "" % (name, Username.min_len) - else: - message = "'%s' is an invalid name. "\ - "Valid names should begin with alphanumerical characters, '_' and '-'." % name - raise InvalidNameException(message) diff --git a/conans/test/integration/conan_env_test.py b/conans/test/integration/conan_env_test.py index 12acd14af8a..dc4fd9b15a8 100644 --- a/conans/test/integration/conan_env_test.py +++ b/conans/test/integration/conan_env_test.py @@ -8,6 +8,7 @@ from conans.test.utils.tools import TestClient from conans.test.utils.cpp_test_files import cpp_hello_conan_files from conans.util.files import load +from conans import tools class ConanEnvTest(unittest.TestCase): @@ -161,10 +162,11 @@ class HelloConan(ConanFile): client.save(files, clean_first=True) client.run("install --profile ./myprofile --build missing") - if platform.system() != "Windows": - ret = os.system("cd '%s' && chmod +x %s && ./%s" % (client.current_folder, "activate.sh", "activate.sh")) - else: - ret = os.system('cd "%s" && %s' % (client.current_folder, "activate.bat")) + with tools.chdir(client.current_folder): + if platform.system() != "Windows": + ret = os.system("chmod +x activate.sh && ./activate.sh") + else: + ret = os.system("activate.bat") self.assertEquals(ret, 0) def conan_env_deps_test(self): diff --git a/conans/test/model/ref_test.py b/conans/test/model/ref_test.py index 8a0dbf72350..c74bdd3b259 100644 --- a/conans/test/model/ref_test.py +++ b/conans/test/model/ref_test.py @@ -19,6 +19,13 @@ def basic_test(self): self.assertEqual(ref.channel, "testing") self.assertEqual(str(ref), "opencv_lite/2.4.10@phil_lewis/testing") + ref = ConanFileReference.loads("opencv/2.4.10@3rd-party/testing") + self.assertEqual(ref.name, "opencv") + self.assertEqual(ref.version, "2.4.10") + self.assertEqual(ref.user, "3rd-party") + self.assertEqual(ref.channel, "testing") + self.assertEqual(str(ref), "opencv/2.4.10@3rd-party/testing") + def errors_test(self): self.assertRaises(ConanException, ConanFileReference.loads, "") self.assertRaises(ConanException, ConanFileReference.loads, "opencv/2.4.10") @@ -26,4 +33,12 @@ def errors_test(self): self.assertRaises(ConanException, ConanFileReference.loads, "opencv??/2.4.10@laso/testing") self.assertRaises(ConanException, ConanFileReference.loads, ".opencv/2.4.10@lasote/testing") self.assertRaises(ConanException, ConanFileReference.loads, "o/2.4.10 @ lasote/testing") - self.assertRaises(ConanException, ConanFileReference.loads, "lib/1.0@user.surname/channel") + self.assertRaises(ConanException, ConanFileReference.loads, "lib/1.0@user&surname/channel") + self.assertRaises(ConanException, ConanFileReference.loads, + "opencv%s/2.4.10@laso/testing" % "A" * 40) + self.assertRaises(ConanException, ConanFileReference.loads, + "opencv/2.4.10%s@laso/testing" % "A" * 40) + self.assertRaises(ConanException, ConanFileReference.loads, + "opencv/2.4.10@laso%s/testing" % "A" * 40) + self.assertRaises(ConanException, ConanFileReference.loads, + "opencv/2.4.10@laso/testing%s" % "A" * 40) diff --git a/conans/test/model/username_test.py b/conans/test/model/username_test.py index 7fe7a7ef4ce..15dafcf88e9 100644 --- a/conans/test/model/username_test.py +++ b/conans/test/model/username_test.py @@ -9,12 +9,15 @@ class UsernameTest(unittest.TestCase): def username_test(self): Username("userwith-hypens") self.assertRaises(ConanException, Username, "") - self.assertRaises(ConanException, Username, "A"*31) + self.assertRaises(ConanException, Username, "a") + self.assertRaises(ConanException, Username, "A"*42) Username("A"*30) - self.assertRaises(ConanException, Username, "1A") - self.assertRaises(ConanException, Username, "_A") + self.assertRaises(ConanException, Username, "-A") + self.assertRaises(ConanException, Username, "&A") Username("A1") + Username("1A") + Username("_A") Username("a_") - self.assertRaises(ConanException, Username, "$$") \ No newline at end of file + self.assertRaises(ConanException, Username, "$$")