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

Validate ConanFileReference only if requested #3623

Merged
merged 15 commits into from
Sep 27, 2018
Merged
36 changes: 22 additions & 14 deletions conans/model/ref.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,29 @@ class ConanFileReference(namedtuple("ConanFileReference", "name version user cha
sep_pattern = re.compile("@|/|#")
revision = None

def __new__(cls, name, version, user, channel, revision=None):
def __new__(cls, name, version, user, channel, revision=None, validate=True):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should validate defaulted to False and only validate in the ConanFileReference loading points?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel safer if we keep the previous behavior (always validating), at least in this pull request. Afterward, we can force it to be explicit and decide at each point if validation is needed or not.

"""Simple name creation.
@param name: string containing the desired name
@param version: string containing the desired version
@param user: string containing the user name
@param channel: string containing the user channel
@param revision: string containing the revision (optional)
"""
ConanName.validate_name(name, reference_token="package name")
ConanName.validate_name(version, True, reference_token="package version")
ConanName.validate_name(user, reference_token="user name")
ConanName.validate_name(channel, reference_token="channel")
version = Version(version)
obj = super(cls, ConanFileReference).__new__(cls, name, version, user, channel)
if revision:
ConanName.validate_name(revision, reference_token="revision")
obj.revision = revision
if validate:
obj.validate()
return obj

def validate(self):
ConanName.validate_name(self.name, reference_token="package name")
ConanName.validate_name(self.version, True, reference_token="package version")
ConanName.validate_name(self.user, reference_token="user name")
ConanName.validate_name(self.channel, reference_token="channel")
if self.revision:
ConanName.validate_name(self.revision, reference_token="revision")

def __eq__(self, value):
if not value:
return False
Expand All @@ -93,7 +97,7 @@ def __hash__(self):
return hash((self.name, self.version, self.user, self.channel, self.revision))

@staticmethod
def loads(text):
def loads(text, validate=True):
""" Parses a text string to generate a ConanFileReference object
"""
text = ConanFileReference.whitespace_pattern.sub("", text)
Expand All @@ -106,8 +110,7 @@ def loads(text):
except ValueError:
raise ConanException("Wrong package recipe reference %s\nWrite something like "
"OpenCV/1.0.6@user/stable" % text)
obj = ConanFileReference(name, version, user, channel)
obj.revision = revision
obj = ConanFileReference(name, version, user, channel, revision, validate=validate)
return obj

def __repr__(self):
Expand All @@ -134,25 +137,30 @@ class PackageReference(namedtuple("PackageReference", "conan package_id")):
"""
revision = None

def __new__(cls, conan, package_id):
def __new__(cls, conan, package_id, validate=True):
revision = None
if "#" in package_id:
package_id, revision = package_id.rsplit("#", 1)
ConanName.validate_name(revision, reference_token="revision")
obj = super(cls, PackageReference).__new__(cls, conan, package_id)
obj.revision = revision
if validate:
obj.validate()
return obj

def validate(self):
if self.revision:
ConanName.validate_name(self.revision, reference_token="revision")

@staticmethod
def loads(text):
def loads(text, validate=True):
text = text.strip()
tmp = text.split(":")
try:
conan = ConanFileReference.loads(tmp[0].strip())
package_id = tmp[1].strip()
except IndexError:
raise ConanException("Wrong package reference %s" % text)
return PackageReference(conan, package_id)
return PackageReference(conan, package_id, validate=validate)

def __repr__(self):
return "%s:%s" % (self.conan, self.package_id)
Expand Down