Skip to content

Commit 80fbc11

Browse files
authored
Merge pull request #125 from rajdesai24/push_data_to_ccos
logging changes in push_data_to_ccos
2 parents 9b2b92e + 7d56273 commit 80fbc11

File tree

5 files changed

+50
-22
lines changed

5 files changed

+50
-22
lines changed

normalize_repos/log.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ def set_up_logging():
148148
def reset_handler():
149149
"""
150150
Reset the formatter on the handler on the root logger. This causes the next
151-
call to the logger can repopulate them based on the new stack in a new file.
151+
call to the logger can repopulate them based on the new stack in a new
152+
file.
152153
"""
153154

154155
handlers = logging.root.handlers

push_data_to_ccos/get_community_team_data.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,22 @@
66
"""
77

88
# Standard library
9+
import logging
910
import os
1011

1112
# Third-party
1213
import asana
1314

15+
# First-party/Local
16+
import log
17+
1418
ASANA_CLIENT = asana.Client.access_token(os.environ["ADMIN_ASANA_TOKEN"])
1519
ASANA_PROJECT_GID = "1172465506923661"
1620

21+
log.set_up_logging()
22+
logger = logging.getLogger("push_data_to_ccos")
23+
log.reset_handler()
24+
1725

1826
def generate_databag():
1927
"""
@@ -49,15 +57,15 @@ def generate_databag():
4957
]
5058
}
5159
"""
52-
print("Pulling from Asana and generating databag...")
60+
logger.log(logging.INFO, "Pulling from Asana and generating databag...")
5361
databag = {"projects": [], "community_builders": []}
5462

5563
members = ASANA_CLIENT.tasks.find_by_section(
5664
ASANA_PROJECT_GID, opt_fields=["name", "custom_fields"]
5765
)
58-
print(" Team members pulled.")
66+
logger.log(logging.INFO, "Team members pulled.")
5967

60-
print(" Processing team members...")
68+
logger.log(logging.INFO, "Processing team members...")
6169
for member in members:
6270
if member["name"] == "":
6371
continue # Sometimes blank names come up
@@ -92,8 +100,7 @@ def generate_databag():
92100
)
93101
break
94102

95-
print(" Done.")
96-
print("Pull successful.")
103+
logger.log(logging.INFO, "Done.")
97104
return databag
98105

99106

push_data_to_ccos/get_repo_data.py

+20-8
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,46 @@
11
#!/usr/bin/env python3
22
# vim: set fileencoding=utf-8:
33

4+
# Standard library
5+
import logging
6+
47
# Third-party
58
import emoji
69
import yaml
710
from github import Github
811
from github.GithubException import GithubException, UnknownObjectException
912

1013
# First-party/Local
14+
import log
1115
from push_data_via_git import GITHUB_ORGANIZATION, GITHUB_TOKEN
1216

1317
CC_METADATA_FILE_NAME = ".cc-metadata.yml"
1418

19+
log.set_up_logging()
20+
logger = logging.getLogger("push_data_to_ccos")
21+
log.reset_handler()
22+
1523

1624
def set_up_github_client():
17-
print("Setting up GitHub client...")
25+
logger.log(logging.INFO, "Setting up GitHub client...")
1826
github_client = Github(GITHUB_TOKEN)
1927
return github_client
2028

2129

2230
def get_cc_organization(github_client):
23-
print("Getting CC's GitHub organization...")
31+
logger.log(logging.INFO, "Getting CC's GitHub organization...")
2432
cc = github_client.get_organization(GITHUB_ORGANIZATION)
2533
return cc
2634

2735

2836
def get_repositories(organization):
29-
print("Getting CC's repos...")
37+
logger.log(logging.INFO, "Getting CC's repos...")
3038
repos = organization.get_repos()
3139
return repos
3240

3341

3442
def get_repo_github_data(repo):
35-
print("\tGetting data for this repo...")
43+
logger.log(logging.INFO, "Getting data for this repo...")
3644
repo_github_data = {
3745
"id": repo.id,
3846
"name": repo.name,
@@ -59,10 +67,10 @@ def get_repo_github_data(repo):
5967

6068

6169
def get_repo_cc_metadata(repo):
62-
print("\tGetting CC metadata for this repo...")
70+
logger.log(logging.INFO, "Getting CC metadata for this repo...")
6371
try:
6472
cc_metadata_file = repo.get_contents(CC_METADATA_FILE_NAME)
65-
except (UnknownObjectException, GithubException) as e:
73+
except (UnknownObjectException, GithubException):
6674
return {}
6775
cc_metadata = yaml.safe_load(cc_metadata_file.decoded_content)
6876
if "technologies" in cc_metadata:
@@ -79,7 +87,9 @@ def get_repo_data_list(repos):
7987
total = repos.totalCount
8088

8189
for repo in repos:
82-
print(f"Processing {count} of {total}{repo.name}")
90+
logger.log(
91+
logging.INFO, f"Processing {count} of {total}{repo.name}"
92+
)
8393
if not repo.private:
8494
repo_cc_metadata = get_repo_cc_metadata(repo)
8595
is_engineering_project = repo_cc_metadata.get(
@@ -92,7 +102,9 @@ def get_repo_data_list(repos):
92102
repo_data = {**repo_github_data, **repo_cc_metadata}
93103
repo_data_list.append(repo_data)
94104
else:
95-
print("\tNot an active engineering project, skipping")
105+
logger.log(
106+
logging.INFO, "Not an active engineering project, skipping"
107+
)
96108
count += 1
97109
return sorted(repo_data_list, key=lambda k: k["name"].lower())
98110

push_data_to_ccos/log.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../normalize_repos/log.py

push_data_to_ccos/push_data_via_git.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,22 @@
33

44
# Standard library
55
import json
6+
import logging
67
import os
78

89
# Third-party
910
import git
1011

12+
# First-party/Local
13+
import log
14+
1115
GIT_USER_NAME = "CC creativecommons.github.io Bot"
1216
GIT_USER_EMAIL = "cc-creativecommons-github-io-bot@creativecommons.org"
1317

1418
GITHUB_USERNAME = "cc-creativecommons-github-io-bot"
1519
GITHUB_ORGANIZATION = "creativecommons"
1620
GITHUB_REPO_NAME = "creativecommons.github.io-source"
1721

18-
1922
GITHUB_TOKEN = os.environ["ADMIN_GITHUB_TOKEN"]
2023
GITHUB_REPO_URL_WITH_CREDENTIALS = (
2124
f"https://{GITHUB_USERNAME}:{GITHUB_TOKEN}"
@@ -26,32 +29,36 @@
2629
GIT_WORKING_DIRECTORY = f"{WORKING_DIRECTORY}/{GITHUB_REPO_NAME}"
2730
JSON_FILE_DIRECTORY = f"{GIT_WORKING_DIRECTORY}/databags"
2831

32+
log.set_up_logging()
33+
logger = logging.getLogger("push_data_to_ccos")
34+
log.reset_handler()
35+
2936

3037
def set_up_repo():
3138
if not os.path.isdir(GIT_WORKING_DIRECTORY):
32-
print("Cloning repo...")
39+
logger.log(logging.INFO, "Cloning repo...")
3340
repo = git.Repo.clone_from(
3441
url=GITHUB_REPO_URL_WITH_CREDENTIALS, to_path=GIT_WORKING_DIRECTORY
3542
)
3643
else:
37-
print("Setting up repo...")
44+
logger.log(logging.INFO, "Setting up repo...")
3845
repo = git.Repo(GIT_WORKING_DIRECTORY)
3946
origin = repo.remotes.origin
40-
print("Pulling latest code...")
47+
logger.log(logging.INFO, "Pulling latest code...")
4148
origin.pull()
4249
return f"{WORKING_DIRECTORY}/{GITHUB_REPO_NAME}"
4350

4451

4552
def set_up_git_user():
46-
print("Setting up git user...")
53+
logger.log(logging.INFO, "Setting up git user...")
4754
os.environ["GIT_AUTHOR_NAME"] = GIT_USER_NAME
4855
os.environ["GIT_AUTHOR_EMAIL"] = GIT_USER_EMAIL
4956
os.environ["GIT_COMMITTER_NAME"] = GIT_USER_NAME
5057
os.environ["GIT_COMMITTER_EMAIL"] = GIT_USER_EMAIL
5158

5259

5360
def generate_json_file(data, filename):
54-
print("Generating JSON file...")
61+
logger.log(logging.INFO, "Generating JSON file...")
5562
json_filename = f"{JSON_FILE_DIRECTORY}/{filename}"
5663
with open(json_filename, "w") as json_file:
5764
json.dump(data, json_file, sort_keys=True, indent=4)
@@ -65,10 +72,10 @@ def commit_and_push_changes(json_filename):
6572
repo.index.add(items=f"{json_filename}")
6673
repo.index.commit(message="Syncing new data changes.")
6774
origin = repo.remotes.origin
68-
print("Pushing latest code...")
75+
logger.log(logging.INFO, "Pushing latest code...")
6976
origin.push()
7077
else:
71-
print("No changes to push...")
78+
logger.log(logging.INFO, "No changes to push...")
7279

7380

7481
def push_data(data, filename):

0 commit comments

Comments
 (0)