-
- Notifications
You must be signed in to change notification settings - Fork 19
Add cocoapods support to package.py #119
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
Changes from 8 commits
3768d2e 801e740 3b01c28 536f5ed 1f5ff88 666e594 9b3a113 82f2a1e 1eb1bc3 5226943 550c654 e1d9073 3b38ed8 File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -32,7 +32,10 @@ | |
| from fetchcode.package_util import GitHubSource | ||
| from fetchcode.package_util import MiniupnpPackagesGitHubSource | ||
| from fetchcode.package_util import OpenSSLGitHubSource | ||
| from fetchcode.package_util import construct_cocoapods_package | ||
| from fetchcode.package_util import get_cocoapod_tags | ||
| from fetchcode.packagedcode_models import Package | ||
| from fetchcode.utils import get_hashed_path | ||
| from fetchcode.utils import get_response | ||
| | ||
| router = Router() | ||
| | @@ -362,6 +365,50 @@ def get_gnu_data_from_purl(purl): | |
| ) | ||
| | ||
| | ||
| @router.route("pkg:cocoapods/.*") | ||
| def get_cocoapods_data_from_purl(purl): | ||
| purl = PackageURL.from_string(purl) | ||
| Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Put this in try/except block, given input may not be a valid PURL Member Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you @TG1999 . I'm in the midst of refactoring but will add this to the updated code. One note: there are nearly a dozen other uses of that same syntax by other supported PURL types in Member Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @TG1999 On second thought, purldb-toolkit's | ||
| name = purl.name | ||
| cocoapods_org_url = f"https://cocoapods.org/pods/{name}" | ||
| api = "https://cdn.cocoapods.org" | ||
| hashed_path = get_hashed_path(name) | ||
| hashed_path_underscore = hashed_path.replace("/", "_") | ||
| file_prefix = "all_pods_versions_" | ||
| spec = f"{api}/{file_prefix}{hashed_path_underscore}.txt" | ||
| data_list = get_cocoapod_tags(spec, name) | ||
| | ||
| for tag in data_list: | ||
| if purl.version and tag != purl.version: | ||
| continue | ||
| | ||
| gh_repo_owner = None | ||
| gh_repo_name = name | ||
| podspec_api_url = f"https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/{hashed_path}/{name}/{tag}/{name}.podspec.json" | ||
| podspec_api_response = get_response(podspec_api_url) | ||
| podspec_homepage = podspec_api_response.get('homepage') | ||
| | ||
| if podspec_homepage.startswith("https://github.com/"): | ||
| podspec_homepage_remove_gh_prefix = podspec_homepage.replace("https://github.com/", "") | ||
| podspec_homepage_split = podspec_homepage_remove_gh_prefix.split("/") | ||
| gh_repo_owner = podspec_homepage_split[0] | ||
| gh_repo_name = podspec_homepage_split[-1] | ||
| | ||
| tag_pkg = construct_cocoapods_package( | ||
| purl, | ||
| name, | ||
| hashed_path, | ||
| cocoapods_org_url, | ||
| gh_repo_owner, | ||
| gh_repo_name, | ||
| tag | ||
| ) | ||
| | ||
| yield tag_pkg | ||
| | ||
| if purl.version: | ||
| break | ||
| | ||
| | ||
| @dataclasses.dataclass | ||
| class DirectoryListedSource: | ||
| source_url: str = dataclasses.field( | ||
| | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -14,7 +14,11 @@ | |
| # CONDITIONS OF ANY KIND, either express or implied. See tshe License for the | ||
| # specific language governing permissions and limitations under the License. | ||
| | ||
| import hashlib | ||
| import os | ||
| import sys | ||
| from functools import partial | ||
| | ||
| import requests | ||
| from dateutil import parser as dateparser | ||
| from dateutil.parser import ParserError | ||
| | @@ -166,11 +170,76 @@ def get_github_rest(url): | |
| | ||
| | ||
| def get_response(url, headers=None): | ||
| """ | ||
| Generate `Package` object for a `url` string | ||
| """ | ||
| resp = requests.get(url, headers=headers) | ||
| if resp.status_code == 200: | ||
| return resp.json() | ||
| | ||
| raise Exception(f"Failed to fetch: {url}") | ||
| | ||
| | ||
| def get_text_response(url, headers=None): | ||
| resp = requests.get(url, headers=headers) | ||
| if resp.status_code == 200: | ||
| return resp.text | ||
| | ||
| raise Exception(f"Failed to fetch: {url}") | ||
| | ||
| | ||
| def make_head_request(url, headers=None): | ||
| try: | ||
| resp = requests.head(url, headers=headers) | ||
| return resp | ||
| except: | ||
| raise Exception(f"Failed to fetch: {url}") | ||
| | ||
| | ||
| def get_hashed_path(name): | ||
| """ | ||
| Returns a string with a part of the file path derived from the md5 hash. | ||
| | ||
| From https://github.com/CocoaPods/cdn.cocoapods.org: | ||
| "There are a set of known prefixes for all Podspec paths, you take the | ||
| name of the pod, create a hash (using md5) of it and take the first | ||
| three characters." | ||
| | ||
| """ | ||
| if not name: | ||
| return | ||
| podname = get_podname_proper(name) | ||
| if name != podname: | ||
| name_to_hash = podname | ||
| else: | ||
| name_to_hash = name | ||
| | ||
| hash_init = get_first_three_md5_hash_characters(name_to_hash) | ||
| hashed_path = "/".join(list(hash_init)) | ||
| | ||
| return hashed_path | ||
| | ||
| | ||
| # for FIPS support | ||
| sys_v0 = sys.version_info[0] | ||
| sys_v1 = sys.version_info[1] | ||
| if sys_v0 == 3 and sys_v1 >= 9: | ||
| md5_hasher = partial(hashlib.md5, usedforsecurity=False) | ||
| else: | ||
| md5_hasher = hashlib.md5 | ||
| Comment on lines +220 to +226 Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this even needed? I don't think we're using FIPS enabled system. Member Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @keshav-space . I copied this over from https://github.com/nexB/scancode-toolkit/blob/develop/src/packagedcode/cocoapods.py#L89-L118 at the start of the project. Don't know whether this is needed or not. Shall I remove this Member Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pombredanne For the time being I'm leaving the @keshav-space had raised the question of whether we need the | ||
| | ||
| | ||
| def get_podname_proper(podname): | ||
| """ | ||
| Podnames in cocoapods sometimes are files inside a pods package (like 'OHHTTPStubs/Default') | ||
| This returns proper podname in those cases. | ||
| """ | ||
| if "/" in podname: | ||
| return podname.split("/")[0] | ||
| return podname | ||
| | ||
| | ||
| def get_first_three_md5_hash_characters(podname): | ||
| """ | ||
| From https://github.com/CocoaPods/cdn.cocoapods.org: | ||
| "There are a set of known prefixes for all Podspec paths, you take the name of the pod, | ||
| create a hash (using md5) of it and take the first three characters." | ||
| """ | ||
| return md5_hasher(podname.encode("utf-8")).hexdigest()[0:3] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@johnmhoran after refactoring
get_cocoapods_data_from_purlinto multiple functions, please put those functions inpackage_util.pyand only keep the top-levelget_cocoapods_data_from_purlfunction inpackage.pyfileThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @keshav-space -- I was wondering about that, given how the other existing, relatively short
@router.route()functions inpackage.pyhave related functions in bothpackage_util.pyandutils.py. I've already added a handful of utilities toutils.pyfor cocoapods support (siblings of existing utilities, but these do not throw exceptions because that stops the purlclimetadatacommand, which we don't want to do) and will do as you suggest with the now 4 additional functions for cocoapods created by my almost-finished refactoring. And then I have 3 or 4 mock tests to create.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@keshav-space Moving these related functions to
package_util.pyraises one question: in order to facilitate the collection and sharing of cocoapods data from a number of different sources, I've created a dictionary at the top ofpackage.pywhich all functions can access. When I move some functions topackage_util.py, will continued access be as simple as importing that dictionary frompackage.pyintopackage_util.py? That's my plan atm.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@keshav-space I am having trouble importing and accessing in
package_util.pytheloggerI've defined and use widely in mypackage.pycode. I'll dig into this soon, but meanwhile, do you have any guidance on how to share a logging function -- this prints to screen and to the "errors"/"warnings" keys in the JSON output. I now import inpackage_util.pywithfrom fetchcode.package import loggerbut get this error runningmetadata:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@johnmhoran please don't share the same logger across different files. Define a new logger for
package_util.pyand avoid any circular dependencies i.e. don't import anything frompackage.pyinpackage_util.py. The error above is due to a circular dependency.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @keshav-space . I've defined the logger in each of
package.pyandpackage_util.py(configured inget_cocoapods_data_from_purl()), and have defined thepod_summarydictionary inpackage_util.pyand import it intopackage.py(pod_summaryis shared among functions in both files), and everything seems to still work as desired. 👍