Skip to content
This repository was archived by the owner on Feb 21, 2025. It is now read-only.

Commit 6c56493

Browse files
authored
Merge pull request #15 from cms-DQM/backport/pr-12-13-14
2 parents 1e7a6dd + 2bf7522 commit 6c56493

File tree

7 files changed

+243
-23
lines changed

7 files changed

+243
-23
lines changed

.github/workflows/test_package.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ jobs:
77
runs-on: ubuntu-latest
88
strategy:
99
matrix:
10-
python-version: ["3.9", "3.10", "3.11"]
10+
python-version: ["3.9", "3.10", "3.11", "3.12"]
1111

1212
steps:
1313
- uses: actions/checkout@v4
14+
with:
15+
fetch-depth: 0
1416
- name: Set up Python ${{ matrix.python-version }}
1517
uses: actions/setup-python@v5
1618
with:
@@ -34,4 +36,8 @@ jobs:
3436
SSO_CLIENT_SECRET: ${{ secrets.SSO_CLIENT_SECRET }}
3537
ENVIRONMENT: ${{ vars.ENVIRONMENT }}
3638
run: |
37-
pytest tests -s
39+
pytest tests --cov=runregistry --cov-report xml --retries 2
40+
- name: Upload results to Codecov
41+
uses: codecov/codecov-action@v4
42+
with:
43+
token: ${{ secrets.CODECOV_TOKEN }}

runregistry/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1+
from .runregistry import __version__
12
from .runregistry import *
2-
3-
# To update:
4-
# pip install wheel && pip install twine && python setup.py bdist_wheel && twine upload --skip-existing dist/*
5-
__version__ = "1.4.1"

runregistry/runregistry.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import os
2+
import sys
23
import time
34
import json
45
import requests
56
from dotenv import load_dotenv
6-
from cernrequests import get_api_token, get_with_token
7+
from cernrequests import get_api_token
78
from runregistry.utils import (
89
transform_to_rr_run_filter,
910
transform_to_rr_dataset_filter,
1011
__parse_runs_arg,
1112
)
1213

14+
__version__ = "1.5.0"
15+
1316
# Look for .env file in the directory of the caller
1417
# first. If it exists, use it.
1518
if os.path.exists(os.path.join(os.getcwd(), ".env")):
@@ -32,12 +35,15 @@
3235
email = "api@api"
3336
client_id = os.environ.get("SSO_CLIENT_ID")
3437
client_secret = os.environ.get("SSO_CLIENT_SECRET")
38+
target_application = ""
39+
target_name = ""
3540

3641

3742
def setup(target):
3843
global api_url
3944
global target_application
4045
global use_cookies
46+
global target_name
4147

4248
if target == "local":
4349
api_url = "http://localhost:9500"
@@ -46,15 +52,17 @@ def setup(target):
4652
elif target == "development":
4753
api_url = "https://dev-cmsrunregistry.web.cern.ch/api"
4854
use_cookies = True
49-
target_application = "webframeworks-paas-dev-cmsrunregistry"
50-
# elif target == "qa":
51-
# api_url = "https://cmsrunregistry-qa.web.cern.ch/api" # Temporary new SSO Proxy for production
52-
# use_cookies = True
53-
# target_application = "webframeworks-paas-qa-cmsrunregistry"
55+
target_application = "dev-cmsrunregistry-sso-proxy"
5456
elif target == "production":
5557
api_url = "https://cmsrunregistry.web.cern.ch/api"
5658
use_cookies = True
57-
target_application = "webframeworks-paas-cmsrunregistry"
59+
target_application = "cmsrunregistry-sso-proxy"
60+
61+
target_name = target
62+
63+
64+
def _get_user_agent():
65+
return f"runregistry_api_client/{__version__} ({_get_target()}, python {sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}, requests {requests.__version__})"
5866

5967

6068
def _get_headers(token: str = ""):
@@ -63,21 +71,26 @@ def _get_headers(token: str = ""):
6371
headers["email"] = email
6472
if token:
6573
headers["Authorization"] = "Bearer " + token
74+
headers["User-Agent"] = _get_user_agent()
6675
return headers
6776

6877

6978
setup(os.environ.get("ENVIRONMENT", "production"))
7079

7180

81+
def _get_target():
82+
return target_name
83+
84+
7285
def _get_token():
7386
# if not use_cookies:
7487
# return {"dummy": "yammy"}
7588
"""
7689
Gets the token required to query RR API through the CERN SSO.
7790
:return: the token required to query Run Registry API. In particular 'connect.sid' is the one we are interested in
7891
"""
79-
# if os.getenv("ENVIRONMENT") == "development":
80-
# return None
92+
if _get_target() == "local":
93+
return ""
8194
token, expiration_date = get_api_token(
8295
client_id=client_id,
8396
client_secret=client_secret,
@@ -100,7 +113,7 @@ def _get_page(
100113
query_filter = transform_to_rr_run_filter(run_filter=query_filter)
101114
elif data_type == "datasets" and not ignore_filter_transformation:
102115
query_filter = transform_to_rr_dataset_filter(dataset_filter=query_filter)
103-
if os.getenv("ENVIRONMENT") == "development":
116+
if _get_target() in ["development", "local"]:
104117
print(url)
105118
print(query_filter)
106119
payload = json.dumps(
@@ -121,7 +134,7 @@ def get_dataset_names_of_run(run_number, **kwargs):
121134
:return: Array of dataset names of the specified run_number
122135
"""
123136
url = "{}/get_all_dataset_names_of_run/{}".format(api_url, run_number)
124-
return get_with_token(url, target_application=target_application).json()
137+
return requests.get(url, headers=_get_headers(token=_get_token())).json()
125138

126139

127140
def get_run(run_number, **kwargs):
@@ -259,7 +272,7 @@ def get_datasets(limit=40000, compress_attributes=True, **kwargs) -> list:
259272
def get_cycles():
260273
url = "{}/cycles/global".format(api_url)
261274
headers = _get_headers(token=_get_token())
262-
if os.getenv("ENVIRONMENT") == "development":
275+
if _get_target() in ["development", "local"]:
263276
print(url)
264277
return requests.get(url, headers=headers).json()
265278

runregistry/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ def __parse_runs_arg(runs):
2424
return []
2525
elif isinstance(runs, list):
2626
return runs
27+
else:
28+
return []
2729

2830

2931
def transform_to_rr_run_filter(run_filter):
@@ -32,7 +34,7 @@ def transform_to_rr_run_filter(run_filter):
3234
:param run_filter: a filter that the user inputs into the api client
3335
:return: returns a filter that runregistry back end understands.
3436
"""
35-
if not run_filter:
37+
if not run_filter or not isinstance(run_filter, dict):
3638
return {}
3739
transformed_filter = {}
3840
for key, value in run_filter.items():
@@ -91,7 +93,7 @@ def transform_to_rr_dataset_filter(dataset_filter):
9193
:param dataset_filter: a filter that the user inputs into the api client
9294
:return: returns a filter that runregistry back end understands.
9395
"""
94-
if not dataset_filter:
96+
if not dataset_filter or not isinstance(dataset_filter, dict):
9597
return {}
9698
transformed_filter = {}
9799
for key, value in dataset_filter.items():

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def find_version(*file_paths):
2424

2525
setup(
2626
name="runregistry",
27-
version=find_version("runregistry", "__init__.py"),
27+
version=find_version("runregistry", "runregistry.py"),
2828
packages=find_packages(),
2929
author="Fabio Espinosa",
3030
description="CMS Run Registry client",

testing-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pytest>=3.6
2+
pytest-retry
23
pytest-cov
34
codecov
45
-e .

0 commit comments

Comments
 (0)