[Notes] [Git][BuildStream/buildstream][chandan/sourcetransform] 3 commits: Allow source plugins to access previous sources



Title: GitLab

Chandan Singh pushed to branch chandan/sourcetransform at BuildStream / buildstream

Commits:

25 changed files:

Changes:

  • buildstream/_loader/loader.py
    ... ... @@ -506,14 +506,15 @@ class Loader():
    506 506
             element = Element._new_from_meta(meta_element, platform.artifactcache)
    
    507 507
             element._preflight()
    
    508 508
     
    
    509
    -        for source in element.sources():
    
    509
    +        sources = list(element.sources())
    
    510
    +        for idx, source in enumerate(sources):
    
    510 511
                 # Handle the case where a subproject needs to be fetched
    
    511 512
                 #
    
    512 513
                 if source.get_consistency() == Consistency.RESOLVED:
    
    513 514
                     if self._fetch_subprojects:
    
    514 515
                         if ticker:
    
    515 516
                             ticker(filename, 'Fetching subproject from {} source'.format(source.get_kind()))
    
    516
    -                    source._fetch()
    
    517
    +                    source._fetch(sources[0:idx])
    
    517 518
                     else:
    
    518 519
                         detail = "Try fetching the project with `bst fetch {}`".format(filename)
    
    519 520
                         raise LoadError(LoadErrorReason.SUBPROJECT_FETCH_NEEDED,
    

  • buildstream/_scheduler/queues/fetchqueue.py
    ... ... @@ -40,8 +40,10 @@ class FetchQueue(Queue):
    40 40
             self._skip_cached = skip_cached
    
    41 41
     
    
    42 42
         def process(self, element):
    
    43
    +        previous_sources = []
    
    43 44
             for source in element.sources():
    
    44
    -            source._fetch()
    
    45
    +            source._fetch(previous_sources)
    
    46
    +            previous_sources.append(source)
    
    45 47
     
    
    46 48
         def status(self, element):
    
    47 49
             # state of dependencies may have changed, recalculate element state
    

  • buildstream/_versions.py
    ... ... @@ -23,7 +23,7 @@
    23 23
     # This version is bumped whenever enhancements are made
    
    24 24
     # to the `project.conf` format or the core element format.
    
    25 25
     #
    
    26
    -BST_FORMAT_VERSION = 11
    
    26
    +BST_FORMAT_VERSION = 12
    
    27 27
     
    
    28 28
     
    
    29 29
     # The base BuildStream artifact version
    

  • buildstream/element.py
    ... ... @@ -1253,6 +1253,12 @@ class Element(Plugin):
    1253 1253
                 # Prepend provenance to the error
    
    1254 1254
                 raise ElementError("{}: {}".format(self, e), reason=e.reason) from e
    
    1255 1255
     
    
    1256
    +        # Ensure that the first source does not need access to previous soruces
    
    1257
    +        if self.__sources and self.__sources[0]._is_transform():
    
    1258
    +            raise ElementError("{}: {} cannot be the first source of an element "
    
    1259
    +                               "as it requires access to previous sources"
    
    1260
    +                               .format(self, self.__sources[0]))
    
    1261
    +
    
    1256 1262
             # Preflight the sources
    
    1257 1263
             for source in self.sources():
    
    1258 1264
                 source._preflight()
    
    ... ... @@ -1296,9 +1302,9 @@ class Element(Plugin):
    1296 1302
         #
    
    1297 1303
         def _track(self):
    
    1298 1304
             refs = []
    
    1299
    -        for source in self.__sources:
    
    1305
    +        for index, source in enumerate(self.__sources):
    
    1300 1306
                 old_ref = source.get_ref()
    
    1301
    -            new_ref = source._track()
    
    1307
    +            new_ref = source._track(self.__sources[0:index])
    
    1302 1308
                 refs.append((source._get_unique_id(), new_ref))
    
    1303 1309
     
    
    1304 1310
                 # Complimentary warning that the new ref will be unused.
    

  • buildstream/plugins/sources/pip.py
    1
    +#
    
    2
    +#  Copyright Bloomberg Finance LP
    
    3
    +#
    
    4
    +#  This program is free software; you can redistribute it and/or
    
    5
    +#  modify it under the terms of the GNU Lesser General Public
    
    6
    +#  License as published by the Free Software Foundation; either
    
    7
    +#  version 2 of the License, or (at your option) any later version.
    
    8
    +#
    
    9
    +#  This library is distributed in the hope that it will be useful,
    
    10
    +#  but WITHOUT ANY WARRANTY; without even the implied warranty of
    
    11
    +#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    
    12
    +#  Lesser General Public License for more details.
    
    13
    +#
    
    14
    +#  You should have received a copy of the GNU Lesser General Public
    
    15
    +#  License along with this library. If not, see <http://www.gnu.org/licenses/>.
    
    16
    +#
    
    17
    +#  Authors:
    
    18
    +#        Chandan Singh <csingh43 bloomberg net>
    
    19
    +
    
    20
    +"""
    
    21
    +pip - stage python packages using pip
    
    22
    +=====================================
    
    23
    +
    
    24
    +**Host depndencies:**
    
    25
    +
    
    26
    +  * ``virtualenv`` CLI tool or ``venv`` python module
    
    27
    +
    
    28
    +**Usage:**
    
    29
    +
    
    30
    +.. code:: yaml
    
    31
    +
    
    32
    +   # Specify the pip source kind
    
    33
    +   kind: pip
    
    34
    +
    
    35
    +   # Optionally specify the python executable, defaults to system "python"
    
    36
    +   # Note that either the venv module or the virtualenv CLI tool must be
    
    37
    +   # available
    
    38
    +   python-exe: python3.6
    
    39
    +
    
    40
    +   # Optionally specify index url, defaults to PyPi
    
    41
    +   index-url: https://mypypi.example.com/simple
    
    42
    +
    
    43
    +   # Optionally specify the path to requirements files
    
    44
    +   # Note that either 'requirements-files' or 'packages' must be defined
    
    45
    +   requirements-files:
    
    46
    +   - requirements.txt
    
    47
    +
    
    48
    +   # Optionally specify a list of additional packages
    
    49
    +   # Note that either 'requirements-files' or 'packages' must be defined
    
    50
    +   packages:
    
    51
    +   - flake8
    
    52
    +
    
    53
    +   # Optionally specify a relative staging directory
    
    54
    +   directory: path/to/stage
    
    55
    +
    
    56
    +   # Specify the ref. It is a list of strings of format
    
    57
    +   # `<package-name>==<version>`, one per line.
    
    58
    +   # Usually this will be contents of a requirements.txt file where all
    
    59
    +   # package versions have been frozen.
    
    60
    +   ref: "flake8==3.5.0\\nmccabe==0.6.1\\npkg-resources==0.0.0\\npycodestyle==2.3.1\\npyflakes==1.6.0"
    
    61
    +
    
    62
    +.. note::
    
    63
    +
    
    64
    +   The ``pip`` plugin is available since :ref:`format version 12 <project_format_version>`
    
    65
    +
    
    66
    +"""
    
    67
    +
    
    68
    +import hashlib
    
    69
    +import os
    
    70
    +
    
    71
    +from buildstream import Consistency, Source, SourceError, utils
    
    72
    +
    
    73
    +_PYPI_INDEX_URL = 'https://pypi.org/simple/'
    
    74
    +
    
    75
    +
    
    76
    +class PipSource(Source):
    
    77
    +    # pylint: disable=attribute-defined-outside-init
    
    78
    +
    
    79
    +    # We need access to previous sources at track time to use requirements.txt
    
    80
    +    # but not at fetch time as self.ref should contain sufficient information
    
    81
    +    # for this plugin
    
    82
    +    requires_previous_sources_track = True
    
    83
    +
    
    84
    +    def configure(self, node):
    
    85
    +        self.node_validate(node, ['index-url', 'packages', 'python-exe', 'ref', 'requirements-files'] +
    
    86
    +                           Source.COMMON_CONFIG_KEYS)
    
    87
    +        self.ref = self.node_get_member(node, str, 'ref', None)
    
    88
    +        self.python_exe = self.node_get_member(node, str, 'python-exe', 'python')
    
    89
    +        self.index_url = self.node_get_member(node, str, 'index-url', _PYPI_INDEX_URL)
    
    90
    +        self.packages = self.node_get_member(node, list, 'packages', [])
    
    91
    +        self.requirements_files = self.node_get_member(node, list, 'requirements-files', [])
    
    92
    +
    
    93
    +        if not (self.packages or self.requirements_files):
    
    94
    +            raise SourceError("{}: Either 'packages' or 'requirements-files' must be specified". format(self))
    
    95
    +
    
    96
    +    def preflight(self):
    
    97
    +        # Try to find a way to open virtual environments on the host
    
    98
    +        try:
    
    99
    +            # Look for the virtualenv CLI first
    
    100
    +            venv = utils.get_host_tool('virtualenv')
    
    101
    +            self.venv_cmd = [venv, '--python', self.python_exe]
    
    102
    +        except utils.ProgramNotFoundError:
    
    103
    +            # Fall back to venv module if it is installed
    
    104
    +            python_exe = utils.get_host_tool(self.python_exe)
    
    105
    +            rc = self.call([python_exe, '-m', 'venv', '--help'])
    
    106
    +            if rc == 0:
    
    107
    +                self.venv_cmd = [python_exe, '-m', 'venv']
    
    108
    +            else:
    
    109
    +                raise SourceError("{}: venv module not found using python: {}"
    
    110
    +                                  .format(self, python_exe))
    
    111
    +
    
    112
    +    def get_unique_key(self):
    
    113
    +        return [self.venv_cmd, self.index_url, self.ref]
    
    114
    +
    
    115
    +    def get_consistency(self):
    
    116
    +        if not self.ref:
    
    117
    +            return Consistency.INCONSISTENT
    
    118
    +        # FIXME add a stronger consistency check
    
    119
    +        # Currently we take the presence of "something" as an indication that
    
    120
    +        # we have the right things in cache
    
    121
    +        if os.path.exists(self.mirror) and os.listdir(self.mirror):
    
    122
    +            return Consistency.CACHED
    
    123
    +        return Consistency.RESOLVED
    
    124
    +
    
    125
    +    def get_ref(self):
    
    126
    +        return self.ref
    
    127
    +
    
    128
    +    def load_ref(self, node):
    
    129
    +        self.ref = self.node_get_member(node, str, 'ref', None)
    
    130
    +
    
    131
    +    def set_ref(self, ref, node):
    
    132
    +        node['ref'] = self.ref = ref
    
    133
    +
    
    134
    +    def track(self, previous_sources_dir):
    
    135
    +        # XXX pip does not offer any public API other than the CLI tool so it
    
    136
    +        # is not feasible to correctly parse the requirements file or to check
    
    137
    +        # which package versions pip is going to install.
    
    138
    +        # See https://pip.pypa.io/en/stable/user_guide/#using-pip-from-your-program
    
    139
    +        # for details.
    
    140
    +        # As a result, we have to wastefully install the packages during track.
    
    141
    +        with self.tempdir() as tmpdir:
    
    142
    +            pip = self._venv_pip(tmpdir)
    
    143
    +
    
    144
    +            install_args = [pip, 'install', '--index-url', self.index_url]
    
    145
    +            for requirement_file in self.requirements_files:
    
    146
    +                fpath = os.path.join(previous_sources_dir, requirement_file)
    
    147
    +                install_args += ['-r', fpath]
    
    148
    +            install_args += self.packages
    
    149
    +
    
    150
    +            self.call(install_args, fail="Failed to install python packages")
    
    151
    +            _, reqs = self.check_output([pip, 'freeze'])
    
    152
    +
    
    153
    +        return reqs.strip()
    
    154
    +
    
    155
    +    def fetch(self):
    
    156
    +        with self.tempdir() as tmpdir:
    
    157
    +            pip = self._venv_pip(tmpdir)
    
    158
    +            packages = self.ref.strip().split('\n')
    
    159
    +            self.call([pip, 'install',
    
    160
    +                       '--index-url', self.index_url,
    
    161
    +                       '--prefix', self.mirror] +
    
    162
    +                      packages,
    
    163
    +                      fail="Failed to install python packages: {}".format(packages))
    
    164
    +
    
    165
    +    def stage(self, directory):
    
    166
    +        with self.timed_activity("Staging Python packages", silent_nested=True):
    
    167
    +            utils.copy_files(self.mirror, directory)
    
    168
    +
    
    169
    +    @property
    
    170
    +    def mirror(self):
    
    171
    +        """Directory where this source should stage its files
    
    172
    +
    
    173
    +        """
    
    174
    +        path = os.path.join(self.get_mirror_directory(),
    
    175
    +                            self.index_url,
    
    176
    +                            hashlib.sha256(self.ref.encode()).hexdigest())
    
    177
    +        os.makedirs(path, exist_ok=True)
    
    178
    +        return path
    
    179
    +
    
    180
    +    def _venv_pip(self, directory):
    
    181
    +        """Open a virtual environment in given directory and return pip path
    
    182
    +
    
    183
    +        """
    
    184
    +        self.call(self.venv_cmd + [directory], fail="Failed to initialize virtual environment")
    
    185
    +        pip_exe = os.path.join(directory, 'bin', 'pip')
    
    186
    +        if not os.path.isfile(pip_exe):
    
    187
    +            raise SourceError("Failed to initialize virtual environment")
    
    188
    +        return pip_exe
    
    189
    +
    
    190
    +
    
    191
    +def setup():
    
    192
    +    return PipSource

  • buildstream/source.py
    ... ... @@ -156,7 +156,7 @@ class SourceFetcher():
    156 156
         #############################################################
    
    157 157
         #                      Abstract Methods                     #
    
    158 158
         #############################################################
    
    159
    -    def fetch(self, alias_override=None):
    
    159
    +    def fetch(self, alias_override=None, **kwargs):
    
    160 160
             """Fetch remote sources and mirror them locally, ensuring at least
    
    161 161
             that the specific reference is cached locally.
    
    162 162
     
    
    ... ... @@ -209,6 +209,28 @@ class Source(Plugin):
    209 209
         __defaults = {}          # The defaults from the project
    
    210 210
         __defaults_set = False   # Flag, in case there are not defaults at all
    
    211 211
     
    
    212
    +    requires_previous_sources_track = False
    
    213
    +    """Whether access to previous sources is required during track
    
    214
    +
    
    215
    +    When set to True:
    
    216
    +      * all sources listed before this source in the given element will be
    
    217
    +        fetched before this source is tracked
    
    218
    +      * Source.track() will be called with an additional keywork argument
    
    219
    +        `previous_sources_dir` that will contain a list of sources
    
    220
    +      * this source can not be the first source for an element
    
    221
    +    """
    
    222
    +
    
    223
    +    requires_previous_sources_fetch = False
    
    224
    +    """Whether access to previous sources is required during fetch
    
    225
    +
    
    226
    +    When set to True:
    
    227
    +      * all sources listed before this source in the given element will be
    
    228
    +        fetched before this source is fetched
    
    229
    +      * Source.fetch() will be called with an additional keyword argument
    
    230
    +        `previous_sources_dir` that will contain a list of sources
    
    231
    +      * this source can not be the first source for an element
    
    232
    +    """
    
    233
    +
    
    212 234
         def __init__(self, context, project, meta, *, alias_override=None):
    
    213 235
             provenance = _yaml.node_get_provenance(meta.config)
    
    214 236
             super().__init__("{}-{}".format(meta.element_name, meta.element_index),
    
    ... ... @@ -303,7 +325,7 @@ class Source(Plugin):
    303 325
             """
    
    304 326
             raise ImplError("Source plugin '{}' does not implement set_ref()".format(self.get_kind()))
    
    305 327
     
    
    306
    -    def track(self):
    
    328
    +    def track(self, **kwargs):
    
    307 329
             """Resolve a new ref from the plugin's track option
    
    308 330
     
    
    309 331
             Returns:
    
    ... ... @@ -324,7 +346,7 @@ class Source(Plugin):
    324 346
             # Allow a non implementation
    
    325 347
             return None
    
    326 348
     
    
    327
    -    def fetch(self):
    
    349
    +    def fetch(self, **kwargs):
    
    328 350
             """Fetch remote sources and mirror them locally, ensuring at least
    
    329 351
             that the specific reference is cached locally.
    
    330 352
     
    
    ... ... @@ -517,46 +539,59 @@ class Source(Plugin):
    517 539
     
    
    518 540
         # Wrapper function around plugin provided fetch method
    
    519 541
         #
    
    520
    -    def _fetch(self):
    
    521
    -        project = self._get_project()
    
    522
    -        source_fetchers = self.get_source_fetchers()
    
    523
    -        if source_fetchers:
    
    524
    -            for fetcher in source_fetchers:
    
    525
    -                alias = fetcher._get_alias()
    
    526
    -                success = False
    
    542
    +    # Args:
    
    543
    +    #   previous_sources (list): List of Sources listed prior to this source
    
    544
    +    #
    
    545
    +    def _fetch(self, previous_sources):
    
    546
    +        def __fetch(**kwargs):
    
    547
    +            project = self._get_project()
    
    548
    +            source_fetchers = self.get_source_fetchers()
    
    549
    +            if source_fetchers:
    
    550
    +                for fetcher in source_fetchers:
    
    551
    +                    alias = fetcher._get_alias()
    
    552
    +                    success = False
    
    553
    +                    for uri in project.get_alias_uris(alias):
    
    554
    +                        try:
    
    555
    +                            fetcher.fetch(uri)
    
    556
    +                        # FIXME: Need to consider temporary vs. permanent failures,
    
    557
    +                        #        and how this works with retries.
    
    558
    +                        except BstError as e:
    
    559
    +                            last_error = e
    
    560
    +                            continue
    
    561
    +                        success = True
    
    562
    +                        break
    
    563
    +                    if not success:
    
    564
    +                        raise last_error
    
    565
    +            else:
    
    566
    +                alias = self._get_alias()
    
    567
    +                if not project.mirrors or not alias:
    
    568
    +                    self.fetch(**kwargs)
    
    569
    +                    return
    
    570
    +
    
    571
    +                context = self._get_context()
    
    572
    +                source_kind = type(self)
    
    527 573
                     for uri in project.get_alias_uris(alias):
    
    574
    +                    new_source = source_kind(context, project, self.__meta,
    
    575
    +                                             alias_override=(alias, uri))
    
    576
    +                    new_source._preflight()
    
    528 577
                         try:
    
    529
    -                        fetcher.fetch(uri)
    
    578
    +                        new_source.fetch(**kwargs)
    
    530 579
                         # FIXME: Need to consider temporary vs. permanent failures,
    
    531 580
                         #        and how this works with retries.
    
    532 581
                         except BstError as e:
    
    533 582
                             last_error = e
    
    534 583
                             continue
    
    535
    -                    success = True
    
    536
    -                    break
    
    537
    -                if not success:
    
    538
    -                    raise last_error
    
    584
    +                    return
    
    585
    +                raise last_error
    
    586
    +
    
    587
    +        if self.requires_previous_sources_fetch:
    
    588
    +            self.__ensure_previous_sources(previous_sources)
    
    589
    +            with self.tempdir() as staging_directory:
    
    590
    +                for src in previous_sources:
    
    591
    +                    src._stage(staging_directory)
    
    592
    +                __fetch(previous_sources_dir=staging_directory)
    
    539 593
             else:
    
    540
    -            alias = self._get_alias()
    
    541
    -            if not project.mirrors or not alias:
    
    542
    -                self.fetch()
    
    543
    -                return
    
    544
    -
    
    545
    -            context = self._get_context()
    
    546
    -            source_kind = type(self)
    
    547
    -            for uri in project.get_alias_uris(alias):
    
    548
    -                new_source = source_kind(context, project, self.__meta,
    
    549
    -                                         alias_override=(alias, uri))
    
    550
    -                new_source._preflight()
    
    551
    -                try:
    
    552
    -                    new_source.fetch()
    
    553
    -                # FIXME: Need to consider temporary vs. permanent failures,
    
    554
    -                #        and how this works with retries.
    
    555
    -                except BstError as e:
    
    556
    -                    last_error = e
    
    557
    -                    continue
    
    558
    -                return
    
    559
    -            raise last_error
    
    594
    +            __fetch()
    
    560 595
     
    
    561 596
         # Wrapper for stage() api which gives the source
    
    562 597
         # plugin a fully constructed path considering the
    
    ... ... @@ -762,8 +797,19 @@ class Source(Plugin):
    762 797
     
    
    763 798
         # Wrapper for track()
    
    764 799
         #
    
    765
    -    def _track(self):
    
    766
    -        new_ref = self.__do_track()
    
    800
    +    # Args:
    
    801
    +    #   previous_sources (list): List of Sources listed prior to this source
    
    802
    +    #
    
    803
    +    def _track(self, previous_sources):
    
    804
    +        if self.requires_previous_sources_track:
    
    805
    +            self.__ensure_previous_sources(previous_sources)
    
    806
    +            with self.tempdir() as staging_directory:
    
    807
    +                for src in previous_sources:
    
    808
    +                    src._stage(staging_directory)
    
    809
    +                new_ref = self.__do_track(previous_sources_dir=staging_directory)
    
    810
    +        else:
    
    811
    +            new_ref = self.__do_track()
    
    812
    +
    
    767 813
             current_ref = self.get_ref()
    
    768 814
     
    
    769 815
             if new_ref is None:
    
    ... ... @@ -775,6 +821,18 @@ class Source(Plugin):
    775 821
     
    
    776 822
             return new_ref
    
    777 823
     
    
    824
    +    # _is_transform()
    
    825
    +    #
    
    826
    +    # A plugin is considered a source transform plugin if it requires access to
    
    827
    +    # previous sources for its tracking or fetching. Such sources cannot be the
    
    828
    +    # first source of any element.
    
    829
    +    #
    
    830
    +    # Returns:
    
    831
    +    #   (bool): Whetner this is a source transform plugin.
    
    832
    +    #
    
    833
    +    def _is_transform(self):
    
    834
    +        return self.requires_previous_sources_track or self.requires_previous_sources_fetch
    
    835
    +
    
    778 836
         # Returns the alias if it's defined in the project
    
    779 837
         def _get_alias(self):
    
    780 838
             alias = self.__expected_alias
    
    ... ... @@ -791,12 +849,12 @@ class Source(Plugin):
    791 849
         #############################################################
    
    792 850
     
    
    793 851
         # Tries to call track for every mirror, stopping once it succeeds
    
    794
    -    def __do_track(self):
    
    852
    +    def __do_track(self, **kwargs):
    
    795 853
             project = self._get_project()
    
    796 854
             # If there are no mirrors, or no aliases to replace, there's nothing to do here.
    
    797 855
             alias = self._get_alias()
    
    798 856
             if not project.mirrors or not alias:
    
    799
    -            return self.track()
    
    857
    +            return self.track(**kwargs)
    
    800 858
     
    
    801 859
             context = self._get_context()
    
    802 860
             source_kind = type(self)
    
    ... ... @@ -808,7 +866,7 @@ class Source(Plugin):
    808 866
                                          alias_override=(alias, uri))
    
    809 867
                 new_source._preflight()
    
    810 868
                 try:
    
    811
    -                ref = new_source.track()
    
    869
    +                ref = new_source.track(**kwargs)
    
    812 870
                 # FIXME: Need to consider temporary vs. permanent failures,
    
    813 871
                 #        and how this works with retries.
    
    814 872
                 except BstError as e:
    
    ... ... @@ -849,3 +907,14 @@ class Source(Plugin):
    849 907
             _yaml.node_final_assertions(config)
    
    850 908
     
    
    851 909
             return config
    
    910
    +
    
    911
    +    # Ensures that previous sources have been tracked and fetched.
    
    912
    +    #
    
    913
    +    def __ensure_previous_sources(self, previous_sources):
    
    914
    +        for index, src in enumerate(previous_sources):
    
    915
    +            if src.get_consistency() == Consistency.RESOLVED:
    
    916
    +                src._fetch(previous_sources[0:index])
    
    917
    +            elif src.get_consistency() == Consistency.INCONSISTENT:
    
    918
    +                new_ref = src._track(previous_sources[0:index])
    
    919
    +                src._save_ref(new_ref)
    
    920
    +                src._fetch(previous_sources[0:index])

  • doc/source/core_plugins.rst
    ... ... @@ -58,6 +58,7 @@ Sources
    58 58
        sources/ostree
    
    59 59
        sources/patch
    
    60 60
        sources/deb
    
    61
    +   sources/pip
    
    61 62
     
    
    62 63
     
    
    63 64
     External plugins
    

  • tests/integration/pip.pytests/integration/pip_element.py

  • tests/integration/pip_source.py
    1
    +import os
    
    2
    +import pytest
    
    3
    +
    
    4
    +from buildstream import _yaml
    
    5
    +
    
    6
    +from tests.testutils import cli_integration as cli
    
    7
    +from tests.testutils.integration import assert_contains
    
    8
    +
    
    9
    +
    
    10
    +pytestmark = pytest.mark.integration
    
    11
    +
    
    12
    +
    
    13
    +DATA_DIR = os.path.join(
    
    14
    +    os.path.dirname(os.path.realpath(__file__)),
    
    15
    +    "project"
    
    16
    +)
    
    17
    +
    
    18
    +
    
    19
    +@pytest.mark.datafiles(DATA_DIR)
    
    20
    +def test_pip_source(cli, tmpdir, datafiles):
    
    21
    +    project = os.path.join(datafiles.dirname, datafiles.basename)
    
    22
    +    checkout = os.path.join(cli.directory, 'checkout')
    
    23
    +    element_path = os.path.join(project, 'elements')
    
    24
    +    element_name = 'pip/hello.bst'
    
    25
    +
    
    26
    +    element = {
    
    27
    +        'kind': 'import',
    
    28
    +        'sources': [
    
    29
    +            {
    
    30
    +                'kind': 'local',
    
    31
    +                'path': 'files/pip-source'
    
    32
    +            },
    
    33
    +            {
    
    34
    +                'kind': 'pip',
    
    35
    +                'python-exe': 'python3',
    
    36
    +                'index-url': 'file://{}'.format(os.path.realpath(os.path.join(project, 'files', 'pypi-repo'))),
    
    37
    +                'requirements-files': ['myreqs.txt'],
    
    38
    +                'packages': ['app2']
    
    39
    +            }
    
    40
    +        ]
    
    41
    +    }
    
    42
    +    os.makedirs(os.path.dirname(os.path.join(element_path, element_name)), exist_ok=True)
    
    43
    +    _yaml.dump(element, os.path.join(element_path, element_name))
    
    44
    +
    
    45
    +    result = cli.run(project=project, args=['track', element_name])
    
    46
    +    assert result.exit_code == 0
    
    47
    +
    
    48
    +    result = cli.run(project=project, args=['build', element_name])
    
    49
    +    assert result.exit_code == 0
    
    50
    +
    
    51
    +    result = cli.run(project=project, args=['checkout', element_name, checkout])
    
    52
    +    assert result.exit_code == 0
    
    53
    +
    
    54
    +    assert_contains(checkout, ['/bin', '/bin/app1', '/bin/app2'])

  • tests/integration/project/files/pip-source/myreqs.txt
    1
    +app1

  • tests/integration/project/files/pypi-repo/app1/App1-0.1.tar.gz
    No preview for this file type
  • tests/integration/project/files/pypi-repo/app1/index.html
    1
    +<html>
    
    2
    +  <head>
    
    3
    +    <title>Links for app1</title>
    
    4
    +  </head>
    
    5
    +  <body>
    
    6
    +    <a href="">'App1-0.1.tar.gz'>App1-0.1.tar.gz</a><br />
    
    7
    +  </body>
    
    8
    +</html>

  • tests/integration/project/files/pypi-repo/app2/App2-0.1.tar.gz
    No preview for this file type
  • tests/integration/project/files/pypi-repo/app2/index.html
    1
    +<html>
    
    2
    +  <head>
    
    3
    +    <title>Links for app1</title>
    
    4
    +  </head>
    
    5
    +  <body>
    
    6
    +    <a href="">'App2-0.1.tar.gz'>App2-0.1.tar.gz</a><br />
    
    7
    +  </body>
    
    8
    +</html>

  • tests/sources/pip.py
    1
    +import os
    
    2
    +import pytest
    
    3
    +
    
    4
    +from buildstream._exceptions import ErrorDomain
    
    5
    +from buildstream import _yaml
    
    6
    +from tests.testutils import cli
    
    7
    +
    
    8
    +DATA_DIR = os.path.join(
    
    9
    +    os.path.dirname(os.path.realpath(__file__)),
    
    10
    +    'pip',
    
    11
    +)
    
    12
    +
    
    13
    +
    
    14
    +def generate_project(project_dir, tmpdir):
    
    15
    +    project_file = os.path.join(project_dir, "project.conf")
    
    16
    +    _yaml.dump({'name': 'foo'}, project_file)
    
    17
    +
    
    18
    +
    
    19
    +# Test that without ref, consistency is set appropriately.
    
    20
    +@pytest.mark.datafiles(os.path.join(DATA_DIR, 'no-ref'))
    
    21
    +def test_no_ref(cli, tmpdir, datafiles):
    
    22
    +    project = os.path.join(datafiles.dirname, datafiles.basename)
    
    23
    +    generate_project(project, tmpdir)
    
    24
    +    assert cli.get_element_state(project, 'target.bst') == 'no reference'
    
    25
    +
    
    26
    +
    
    27
    +# Test that pip is not allowed to be the first source
    
    28
    +@pytest.mark.datafiles(os.path.join(DATA_DIR, 'first-source-pip'))
    
    29
    +def test_first_source(cli, tmpdir, datafiles):
    
    30
    +    project = os.path.join(datafiles.dirname, datafiles.basename)
    
    31
    +    generate_project(project, tmpdir)
    
    32
    +    result = cli.run(project=project, args=[
    
    33
    +        'show', 'target.bst'
    
    34
    +    ])
    
    35
    +    result.assert_main_error(ErrorDomain.ELEMENT, None)
    
    36
    +
    
    37
    +
    
    38
    +# Test that error is raised when neither packges nor requirements files
    
    39
    +# have been specified
    
    40
    +@pytest.mark.datafiles(os.path.join(DATA_DIR, 'no-packages'))
    
    41
    +def test_no_packages(cli, tmpdir, datafiles):
    
    42
    +    project = os.path.join(datafiles.dirname, datafiles.basename)
    
    43
    +    generate_project(project, tmpdir)
    
    44
    +    result = cli.run(project=project, args=[
    
    45
    +        'show', 'target.bst'
    
    46
    +    ])
    
    47
    +    result.assert_main_error(ErrorDomain.SOURCE, None)

  • tests/sources/pip/first-source-pip/target.bst
    1
    +kind: import
    
    2
    +description: pip should not be allowed to be the first source
    
    3
    +sources:
    
    4
    +- kind: pip
    
    5
    +  python-exe: python3
    
    6
    +  packages:
    
    7
    +  - flake8

  • tests/sources/pip/no-packages/file
    1
    +Hello World!

  • tests/sources/pip/no-packages/target.bst
    1
    +kind: import
    
    2
    +description: The kind of this element is irrelevant.
    
    3
    +sources:
    
    4
    +- kind: local
    
    5
    +  path: file
    
    6
    +- kind: pip
    
    7
    +  python-exe: python3

  • tests/sources/pip/no-ref/file
    1
    +Hello World!

  • tests/sources/pip/no-ref/target.bst
    1
    +kind: import
    
    2
    +description: The kind of this element is irrelevant.
    
    3
    +sources:
    
    4
    +- kind: local
    
    5
    +  path: file
    
    6
    +- kind: pip
    
    7
    +  python-exe: python3
    
    8
    +  packages:
    
    9
    +  - flake8

  • tests/sources/previous_source_access.py
    1
    +import os
    
    2
    +import pytest
    
    3
    +
    
    4
    +from tests.testutils import cli
    
    5
    +
    
    6
    +DATA_DIR = os.path.join(
    
    7
    +    os.path.dirname(os.path.realpath(__file__)),
    
    8
    +    'previous_source_access'
    
    9
    +)
    
    10
    +
    
    11
    +
    
    12
    +##################################################################
    
    13
    +#                              Tests                             #
    
    14
    +##################################################################
    
    15
    +# Test that plugins can access data from previous sources
    
    16
    +@pytest.mark.datafiles(DATA_DIR)
    
    17
    +def test_custom_transform_source(cli, tmpdir, datafiles):
    
    18
    +    project = os.path.join(datafiles.dirname, datafiles.basename)
    
    19
    +
    
    20
    +    # Ensure we can track
    
    21
    +    result = cli.run(project=project, args=[
    
    22
    +        'track', 'target.bst'
    
    23
    +    ])
    
    24
    +    result.assert_success()
    
    25
    +
    
    26
    +    # Ensure we can fetch
    
    27
    +    result = cli.run(project=project, args=[
    
    28
    +        'fetch', 'target.bst'
    
    29
    +    ])
    
    30
    +    result.assert_success()
    
    31
    +
    
    32
    +    # Ensure we get correct output from foo_transform
    
    33
    +    result = cli.run(project=project, args=[
    
    34
    +        'build', 'target.bst'
    
    35
    +    ])
    
    36
    +    destpath = os.path.join(cli.directory, 'checkout')
    
    37
    +    result = cli.run(project=project, args=[
    
    38
    +        'checkout', 'target.bst', destpath
    
    39
    +    ])
    
    40
    +    result.assert_success()
    
    41
    +    # Assert that files from both sources exist, and that they have
    
    42
    +    # the same content
    
    43
    +    assert os.path.exists(os.path.join(destpath, 'file'))
    
    44
    +    assert os.path.exists(os.path.join(destpath, 'filetransform'))
    
    45
    +    with open(os.path.join(destpath, 'file')) as file1:
    
    46
    +        with open(os.path.join(destpath, 'filetransform')) as file2:
    
    47
    +            assert file1.read() == file2.read()

  • tests/sources/previous_source_access/elements/target.bst
    1
    +kind: import
    
    2
    +
    
    3
    +sources:
    
    4
    +- kind: local
    
    5
    +  path: files/file
    
    6
    +- kind: foo_transform

  • tests/sources/previous_source_access/files/file
    1
    +Hello World!

  • tests/sources/previous_source_access/plugins/sources/foo_transform.py
    1
    +"""
    
    2
    +foo_transform - transform "file" from previous sources into "filetransform"
    
    3
    +===========================================================================
    
    4
    +
    
    5
    +This is a test source plugin that looks for a file named "file" staged by
    
    6
    +previous sources, and copies its contents to a file called "filetransform".
    
    7
    +
    
    8
    +"""
    
    9
    +
    
    10
    +import os
    
    11
    +import hashlib
    
    12
    +
    
    13
    +from buildstream import Consistency, Source, SourceError, utils
    
    14
    +
    
    15
    +
    
    16
    +class FooTransformSource(Source):
    
    17
    +
    
    18
    +    # We need access to previous both at track time and fetch time
    
    19
    +    requires_previous_sources_track = True
    
    20
    +    requires_previous_sources_fetch = True
    
    21
    +
    
    22
    +    @property
    
    23
    +    def mirror(self):
    
    24
    +        """Directory where this source should stage its files
    
    25
    +
    
    26
    +        """
    
    27
    +        path = os.path.join(self.get_mirror_directory(), self.name,
    
    28
    +                            self.ref.strip())
    
    29
    +        os.makedirs(path, exist_ok=True)
    
    30
    +        return path
    
    31
    +
    
    32
    +    def configure(self, node):
    
    33
    +        self.node_validate(node, ['ref'] + Source.COMMON_CONFIG_KEYS)
    
    34
    +        self.ref = self.node_get_member(node, str, 'ref', None)
    
    35
    +
    
    36
    +    def preflight(self):
    
    37
    +        pass
    
    38
    +
    
    39
    +    def get_unique_key(self):
    
    40
    +        return (self.ref,)
    
    41
    +
    
    42
    +    def get_consistency(self):
    
    43
    +        if self.ref is None:
    
    44
    +            return Consistency.INCONSISTENT
    
    45
    +        # If we have a file called "filetransform", verify that its checksum
    
    46
    +        # matches our ref. Otherwise, it resolved but not cached.
    
    47
    +        fpath = os.path.join(self.mirror, 'filetransform')
    
    48
    +        try:
    
    49
    +            with open(fpath, 'rb') as f:
    
    50
    +                if hashlib.sha256(f.read()).hexdigest() == self.ref.strip():
    
    51
    +                    return Consistency.CACHED
    
    52
    +        except Exception:
    
    53
    +            pass
    
    54
    +        return Consistency.RESOLVED
    
    55
    +
    
    56
    +    def get_ref(self):
    
    57
    +        return self.ref
    
    58
    +
    
    59
    +    def set_ref(self, ref, node):
    
    60
    +        self.ref = node['ref'] = ref
    
    61
    +
    
    62
    +    def track(self, previous_sources_dir):
    
    63
    +        # Store the checksum of the file from previous source as our ref
    
    64
    +        fpath = os.path.join(previous_sources_dir, 'file')
    
    65
    +        with open(fpath, 'rb') as f:
    
    66
    +            return hashlib.sha256(f.read()).hexdigest()
    
    67
    +
    
    68
    +    def fetch(self, previous_sources_dir):
    
    69
    +        fpath = os.path.join(previous_sources_dir, 'file')
    
    70
    +        # Verify that the checksum of the file from previous source matches
    
    71
    +        # our ref
    
    72
    +        with open(fpath, 'rb') as f:
    
    73
    +            if hashlib.sha256(f.read()).hexdigest() != self.ref.strip():
    
    74
    +                raise SourceError("Element references do not match")
    
    75
    +
    
    76
    +        # Copy "file" as "filetransform"
    
    77
    +        newfpath = os.path.join(self.mirror, 'filetransform')
    
    78
    +        utils.safe_copy(fpath, newfpath)
    
    79
    +
    
    80
    +    def stage(self, directory):
    
    81
    +        # Simply stage the "filetransform" file
    
    82
    +        utils.safe_copy(os.path.join(self.mirror, 'filetransform'),
    
    83
    +                        os.path.join(directory, 'filetransform'))
    
    84
    +
    
    85
    +
    
    86
    +def setup():
    
    87
    +    return FooTransformSource

  • tests/sources/previous_source_access/project.conf
    1
    +# Project with local source plugins
    
    2
    +name: foo
    
    3
    +
    
    4
    +element-path: elements
    
    5
    +
    
    6
    +plugins:
    
    7
    +- origin: local
    
    8
    +  path: plugins/sources
    
    9
    +  sources:
    
    10
    +    foo_transform: 0



  • [Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]