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

Replace python with python3 in hashbang and drop dependency on PyGithub (part 1) #2037

Closed
wants to merge 6 commits into from

Conversation

iarspider
Copy link
Contributor

  • process-pull-request, query-and-process-prs - used in cms-bot
  • cmsdist-comp-pr-process.py - used in comp-bot
  • jenkins-jobs/es-cmssw-afs-eos.py - used in es-cmssw-afs-eos

@cmsbuild
Copy link
Contributor

A new Pull Request was created by @iarspider for branch master.

@cmsbuild, @smuzaffar, @aandvalenzuela, @iarspider can you please review it and eventually sign? Thanks.
@perrotta, @dpiparo, @rappoccio you are the release manager for this.
cms-bot commands are listed here

@iarspider iarspider marked this pull request as draft August 15, 2023 16:12
@cmsbuild
Copy link
Contributor

Pull request #2037 was updated.

@cmsbuild
Copy link
Contributor

Pull request #2037 was updated.

@iarspider iarspider marked this pull request as ready for review August 18, 2023 10:10
@cmsbuild
Copy link
Contributor

Pull request #2037 was updated.

@smuzaffar
Copy link
Contributor

@iarspider , all of these changes will fail at from github import Github as we do not have github for python3

@iarspider iarspider marked this pull request as draft August 23, 2023 21:30
@iarspider iarspider changed the title Replace python with python3 in hashbang (part 1) Replace python with python3 in hashbang and drop dependency on PyGithub (part 1) Aug 24, 2023
@cmsbuild
Copy link
Contributor

Pull request #2037 was updated.

@iarspider iarspider marked this pull request as ready for review August 25, 2023 10:07
setdefaulttimeout(120)
SCRIPT_DIR = dirname(abspath(argv[0]))

def process_pr(gh, repo, issue, dryRun):
def process_pr(repo: str, issue: dict, dryRun: bool):
Copy link
Contributor

Choose a reason for hiding this comment

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

@iarspider , this does not look right, you are passing prId as 2nd arg while signature here expacs a dict.

@smuzaffar
Copy link
Contributor

@iarspider , how about we convert the json dict to python object ( some like like https://stackoverflow.com/questions/6578986/how-to-convert-json-data-into-a-python-object ) . For example so some of the github_utils functions we can pass an extra args to convert the dict to object

import json
from types import SimpleNamespace

def get_issue(....)
  return json.loads(json.dumps(data), object_hook=lambda d: SimpleNamespace(**d))

and then one can use issue.state instead of issue["state"]

@iarspider
Copy link
Contributor Author

@smuzaffar thanks, fixed that.

@cmsbuild
Copy link
Contributor

Pull request #2037 was updated.

@smuzaffar
Copy link
Contributor

smuzaffar commented Aug 25, 2023

check this #2037 (comment) , I would suggest to make use of it instead of converting all object.property to object["property"]

@iarspider
Copy link
Contributor Author

@iarspider , how about we convert the json dict to python object ( some like like https://stackoverflow.com/questions/6578986/how-to-convert-json-data-into-a-python-object ) . For example so some of the github_utils functions we can pass an extra args to convert the dict to object

import json
from types import SimpleNamespace

def get_issue(....)
  return json.loads(json.dumps(data), object_hook=lambda d: SimpleNamespace(**d))

and then one can use issue.state instead of issue["state"]

Using dot-access makes handling optional fields harder - one need to wrap the statement in try-catch or use getattr(...). We can, of course, write our own version of SimpleNamespace that returns None instead of raising AttributeError, but I'm not sure if it will be easier to maintain.

@smuzaffar
Copy link
Contributor

smuzaffar commented Aug 25, 2023

do you have an example of optional field?

@iarspider
Copy link
Contributor Author

Sure:

  • milestone
  • user's name
  • pull request information

@smuzaffar
Copy link
Contributor

point me to the code in cms-bot please

@iarspider
Copy link
Contributor Author

iarspider commented Aug 25, 2023

$ curl -s -L https://api.github.com/repos/cms-sw/cmssw/issues/42662 | jq ".milestone"

{
  "url": "https://api.github.com/repos/cms-sw/cmssw/milestones/95",
  "html_url": "https://github.com/cms-sw/cmssw/milestone/95",
  "labels_url": "https://api.github.com/repos/cms-sw/cmssw/milestones/95/labels",
  "id": 9667493,
  "node_id": "MI_kwDOAKdhz84Ak4Ol",
  "number": 95,
  "title": "CMSSW_13_3_X",
  "description": null,
  "creator": {
    "login": "smuzaffar",
    "id": 4115138,
    "node_id": "MDQ6VXNlcjQxMTUxMzg=",
    "avatar_url": "https://avatars.githubusercontent.com/u/4115138?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/smuzaffar",
    "html_url": "https://github.com/smuzaffar",
    "followers_url": "https://api.github.com/users/smuzaffar/followers",
    "following_url": "https://api.github.com/users/smuzaffar/following{/other_user}",
    "gists_url": "https://api.github.com/users/smuzaffar/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/smuzaffar/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/smuzaffar/subscriptions",
    "organizations_url": "https://api.github.com/users/smuzaffar/orgs",
    "repos_url": "https://api.github.com/users/smuzaffar/repos",
    "events_url": "https://api.github.com/users/smuzaffar/events{/privacy}",
    "received_events_url": "https://api.github.com/users/smuzaffar/received_events",
    "type": "User",
    "site_admin": false
  },
  "open_issues": 76,
  "closed_issues": 189,
  "state": "open",
  "created_at": "2023-07-18T08:40:50Z",
  "updated_at": "2023-08-25T09:11:57Z",
  "due_on": null,
  "closed_at": null
}

$ curl -s -L https://api.github.com/repos/cms-sw/cms-bot/issues/2037 | jq ".milestone"

null

$ curl -s -L https://api.github.com/repos/cms-sw/cms-bot/issues/2037 | jq ".pull_request"

{
  "url": "https://api.github.com/repos/cms-sw/cms-bot/pulls/2037",
  "html_url": "https://github.com/cms-sw/cms-bot/pull/2037",
  "diff_url": "https://github.com/cms-sw/cms-bot/pull/2037.diff",
  "patch_url": "https://github.com/cms-sw/cms-bot/pull/2037.patch",
  "merged_at": null
}

$ curl -s -L https://api.github.com/repos/cms-sw/cms-bot/issues/1033 | jq ".milestones"

null

@smuzaffar
Copy link
Contributor

all of this can be simply fixed by adding get method to SimpleNamespace derived class or using hadattr . This will be much easier instead of fixing every obj.property to obj["property"]

@iarspider
Copy link
Contributor Author

all of this can be simply fixed by adding get method to SimpleNamespace derived class or using hadattr . This will be much easier instead of fixing every obj.property to obj["property"]

Yes. But using the latter syntax helps moving from member functions to external ones easier, since IDE will highlight missing member functions.

@iarspider
Copy link
Contributor Author

And rewriting obj.property with obj["property"] once is not that complicated.

@smuzaffar
Copy link
Contributor

I am not saying that it is complicated to do the change but there are many obj.property with obj["property"] changes you have to make. If you just use

class CMSSimpleNamespace(SimpleNamespace):
  def get(self, name, default=None):
    if hasattr(self,name): return getattr(self,name)
    return default

then all these changes can be avoided. Calling of member functions needs to be done in both cases anyway. I guess we can maintain these few lines of code

@iarspider iarspider closed this Aug 25, 2023
@smuzaffar smuzaffar deleted the no-more-py2-pt1 branch September 25, 2023 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants