3
3
4
4
# Standard library
5
5
import json
6
+ import logging
6
7
import os
7
8
8
9
# Third-party
9
10
import git
10
11
12
+ # First-party/Local
13
+ import log
14
+
11
15
GIT_USER_NAME = "CC creativecommons.github.io Bot"
12
16
GIT_USER_EMAIL = "cc-creativecommons-github-io-bot@creativecommons.org"
13
17
14
18
GITHUB_USERNAME = "cc-creativecommons-github-io-bot"
15
19
GITHUB_ORGANIZATION = "creativecommons"
16
20
GITHUB_REPO_NAME = "creativecommons.github.io-source"
17
21
18
-
19
22
GITHUB_TOKEN = os .environ ["ADMIN_GITHUB_TOKEN" ]
20
23
GITHUB_REPO_URL_WITH_CREDENTIALS = (
21
24
f"https://{ GITHUB_USERNAME } :{ GITHUB_TOKEN } "
26
29
GIT_WORKING_DIRECTORY = f"{ WORKING_DIRECTORY } /{ GITHUB_REPO_NAME } "
27
30
JSON_FILE_DIRECTORY = f"{ GIT_WORKING_DIRECTORY } /databags"
28
31
32
+ log .set_up_logging ()
33
+ logger = logging .getLogger ("push_data_to_ccos" )
34
+ log .reset_handler ()
35
+
29
36
30
37
def set_up_repo ():
31
38
if not os .path .isdir (GIT_WORKING_DIRECTORY ):
32
- print ( "Cloning repo..." )
39
+ logger . log ( logging . INFO , "Cloning repo..." )
33
40
repo = git .Repo .clone_from (
34
41
url = GITHUB_REPO_URL_WITH_CREDENTIALS , to_path = GIT_WORKING_DIRECTORY
35
42
)
36
43
else :
37
- print ( "Setting up repo..." )
44
+ logger . log ( logging . INFO , "Setting up repo..." )
38
45
repo = git .Repo (GIT_WORKING_DIRECTORY )
39
46
origin = repo .remotes .origin
40
- print ( "Pulling latest code..." )
47
+ logger . log ( logging . INFO , "Pulling latest code..." )
41
48
origin .pull ()
42
49
return f"{ WORKING_DIRECTORY } /{ GITHUB_REPO_NAME } "
43
50
44
51
45
52
def set_up_git_user ():
46
- print ( "Setting up git user..." )
53
+ logger . log ( logging . INFO , "Setting up git user..." )
47
54
os .environ ["GIT_AUTHOR_NAME" ] = GIT_USER_NAME
48
55
os .environ ["GIT_AUTHOR_EMAIL" ] = GIT_USER_EMAIL
49
56
os .environ ["GIT_COMMITTER_NAME" ] = GIT_USER_NAME
50
57
os .environ ["GIT_COMMITTER_EMAIL" ] = GIT_USER_EMAIL
51
58
52
59
53
60
def generate_json_file (data , filename ):
54
- print ( "Generating JSON file..." )
61
+ logger . log ( logging . INFO , "Generating JSON file..." )
55
62
json_filename = f"{ JSON_FILE_DIRECTORY } /{ filename } "
56
63
with open (json_filename , "w" ) as json_file :
57
64
json .dump (data , json_file , sort_keys = True , indent = 4 )
@@ -65,10 +72,10 @@ def commit_and_push_changes(json_filename):
65
72
repo .index .add (items = f"{ json_filename } " )
66
73
repo .index .commit (message = "Syncing new data changes." )
67
74
origin = repo .remotes .origin
68
- print ( "Pushing latest code..." )
75
+ logger . log ( logging . INFO , "Pushing latest code..." )
69
76
origin .push ()
70
77
else :
71
- print ( "No changes to push..." )
78
+ logger . log ( logging . INFO , "No changes to push..." )
72
79
73
80
74
81
def push_data (data , filename ):
0 commit comments