|  | 
|  | 1 | +import re | 
|  | 2 | + | 
|  | 3 | +from portage.dbapi.dep_expand import dep_expand | 
|  | 4 | +from portage.versions import cpv_getversion | 
|  | 5 | +import portage.exception as pe | 
|  | 6 | + | 
|  | 7 | + | 
| 1 | 8 | class AtomException(Exception): | 
| 2 | 9 |     pass | 
| 3 | 10 | 
 | 
| 4 | 11 | 
 | 
| 5 | 12 | class Atom(object): | 
| 6 | 13 | 
 | 
| 7 | 14 |     def __init__(self, atom): | 
| 8 |  | -        # We expect an atom of the form [=]CATEGORY/PACKAGE[-VERSION]. | 
| 9 |  | -        self.category = None | 
| 10 |  | -        self.package = None | 
| 11 |  | -        self.version = None | 
|  | 15 | +        try: | 
|  | 16 | +            # We expect an atom of the form [=]CATEGORY/PACKAGE[-VERSION]. | 
|  | 17 | +            self.atom = dep_expand(str(atom)) | 
|  | 18 | +        except pe.InvalidAtom: | 
|  | 19 | +            raise AtomException( | 
|  | 20 | +                "ATOM has to be of the form [=]SECTION/PACKAGE[-VERSION]") | 
| 12 | 21 | 
 | 
| 13 |  | -        # We don't store the optional '='. | 
| 14 |  | -        temp = atom.split("=") | 
| 15 |  | -        self.atom = temp[-1] | 
|  | 22 | +        self.category = self.atomCategory() | 
|  | 23 | +        self.package = self.atomName() | 
|  | 24 | +        self.version = self.atomVersion() | 
|  | 25 | +        self.repo = self.atomRepo() | 
| 16 | 26 | 
 | 
| 17 |  | -        try: | 
| 18 |  | -            self.category, self.package = self.atom.split("/") | 
| 19 |  | -        except ValueError: | 
|  | 27 | +    def _splitPackage(self): | 
|  | 28 | +        return self.atom.split("/", 1) | 
|  | 29 | + | 
|  | 30 | +    def atomCategory(self): | 
|  | 31 | +        """Returns the package category without name""" | 
|  | 32 | +        category = re.sub('^(=|<=|>=|<|>)', '', self._splitPackage()[0]) | 
|  | 33 | +        if category == 'null': | 
| 20 | 34 |             raise AtomException( | 
| 21 | 35 |                 "ATOM has to be of the form [=]SECTION/PACKAGE[-VERSION]") | 
| 22 | 36 | 
 | 
| 23 |  | -        # Split off version. | 
| 24 |  | -        try: | 
| 25 |  | -            temp = self.package.index("-") | 
| 26 |  | -            if temp > -1: | 
| 27 |  | -                self.version = self.package[temp + 1:] | 
| 28 |  | -                self.package = self.package[:temp] | 
| 29 |  | -        except ValueError: | 
| 30 |  | -            pass | 
|  | 37 | +        return category | 
| 31 | 38 | 
 | 
| 32 |  | -    def __str__(self): | 
|  | 39 | +    def atomName(self): | 
|  | 40 | +        """Returns the package name without category""" | 
|  | 41 | +        pkg = self._splitPackage()[1] | 
|  | 42 | +        suffix = ['-' + str(self.atomVersion()), | 
|  | 43 | +                  '::' + str(self.atomRepo())] | 
|  | 44 | +        for s in suffix: | 
|  | 45 | +            pkg = pkg.replace(s, '') | 
|  | 46 | + | 
|  | 47 | +        return pkg | 
|  | 48 | + | 
|  | 49 | +    def atomVersion(self): | 
|  | 50 | +        """Returns the package version""" | 
|  | 51 | + | 
|  | 52 | +        return cpv_getversion(self.atom) | 
|  | 53 | + | 
|  | 54 | +    def atomRepo(self): | 
|  | 55 | +        """Returns the package repository""" | 
|  | 56 | +        pkg = self._splitPackage()[1].split("::", 1) | 
|  | 57 | +        if len(pkg) == 2: | 
|  | 58 | +            return pkg[1] | 
|  | 59 | + | 
|  | 60 | +    def atomCatName(self): | 
|  | 61 | +        """Returns the package category and name without version""" | 
|  | 62 | +        return "/".join([self.category, self.package]) | 
|  | 63 | + | 
|  | 64 | +    def atomComplete(self): | 
|  | 65 | +        """Returns a portage compatible string representation""" | 
|  | 66 | +        suff = [] | 
|  | 67 | +        pref = '' | 
| 33 | 68 |         if self.version is not None: | 
| 34 |  | -            prefix = "=" | 
| 35 |  | -            suffix = "-" + self.version | 
| 36 |  | -        else: | 
| 37 |  | -            prefix = "" | 
| 38 |  | -            suffix = "" | 
| 39 |  | -        return prefix + self.category + "/" + self.package + suffix | 
|  | 69 | +            suff += ['-' + self.version] | 
|  | 70 | +            pref = '=' | 
|  | 71 | + | 
|  | 72 | +        if self.repo is not None: | 
|  | 73 | +            suff += ['::' + self.repo] | 
|  | 74 | + | 
|  | 75 | +        return (pref + self.category + '/' + self.package + | 
|  | 76 | +                ''.join([str(s) for s in suff])) | 
|  | 77 | + | 
|  | 78 | +    def __str__(self): | 
|  | 79 | +        return self.atomComplete() | 
| 40 | 80 | 
 | 
| 41 | 81 |     def __eq__(self, other): | 
| 42 |  | -        result = (self.atom == other.atom) | 
|  | 82 | +        result = (self.category == other.category | 
|  | 83 | +                  and self.package == other.package | 
|  | 84 | +                  and self.version == other.version) | 
|  | 85 | + | 
| 43 | 86 |         return result | 
| 44 | 87 | 
 | 
| 45 | 88 |     def __repr__(self): | 
|  | 
0 commit comments