|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# |
| 3 | +# http://nexb.com and https://github.com/nexB/scancode.io |
| 4 | +# The ScanCode.io software is licensed under the Apache License version 2.0. |
| 5 | +# Data generated with ScanCode.io is provided as-is without warranties. |
| 6 | +# ScanCode is a trademark of nexB Inc. |
| 7 | +# |
| 8 | +# You may not use this software except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 |
| 10 | +# Unless required by applicable law or agreed to in writing, software distributed |
| 11 | +# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | +# CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | +# specific language governing permissions and limitations under the License. |
| 14 | +# |
| 15 | +# Data Generated with ScanCode.io is provided on an "AS IS" BASIS, WITHOUT WARRANTIES |
| 16 | +# OR CONDITIONS OF ANY KIND, either express or implied. No content created from |
| 17 | +# ScanCode.io should be considered or used as legal advice. Consult an Attorney |
| 18 | +# for any legal advice. |
| 19 | +# |
| 20 | +# ScanCode.io is a free software code scanning tool from nexB Inc. and others. |
| 21 | +# Visit https://github.com/nexB/scancode.io for support and download. |
| 22 | + |
| 23 | +from source_inspector import symbols_ctags |
| 24 | + |
| 25 | +from scanpipe.pipes import LoopProgress |
| 26 | + |
| 27 | + |
| 28 | +class UniversalCtagsNotFound(Exception): |
| 29 | + pass |
| 30 | + |
| 31 | + |
| 32 | +def collect_and_store_resource_symbols(project, logger=None): |
| 33 | + """ |
| 34 | + Collect symbols from codebase files using Ctags and store |
| 35 | + them in the extra data field. |
| 36 | + """ |
| 37 | + if not symbols_ctags.is_ctags_installed(): |
| 38 | + raise UniversalCtagsNotFound( |
| 39 | + "``Universal Ctags`` not found." |
| 40 | + "Install ``Universal Ctags`` to use this pipeline." |
| 41 | + ) |
| 42 | + |
| 43 | + project_files = project.codebaseresources.files() |
| 44 | + |
| 45 | + resources = project_files.filter( |
| 46 | + is_binary=False, |
| 47 | + is_archive=False, |
| 48 | + is_media=False, |
| 49 | + ) |
| 50 | + |
| 51 | + resources_count = resources.count() |
| 52 | + |
| 53 | + resource_iterator = resources.iterator(chunk_size=2000) |
| 54 | + progress = LoopProgress(resources_count, logger) |
| 55 | + |
| 56 | + for resource in progress.iter(resource_iterator): |
| 57 | + _collect_and_store_resource_symbols(resource) |
| 58 | + |
| 59 | + |
| 60 | +def _collect_and_store_resource_symbols(resource): |
| 61 | + """ |
| 62 | + Collect symbols from a resource using Ctags and store |
| 63 | + them in the extra data field. |
| 64 | + """ |
| 65 | + symbols = symbols_ctags.collect_symbols(resource.location) |
| 66 | + tags = [symbol["name"] for symbol in symbols if "name" in symbol] |
| 67 | + resource.update_extra_data({"source_symbols": tags}) |
0 commit comments