-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathshared.py
93 lines (75 loc) · 2.44 KB
/
shared.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Standard library
import argparse
import logging
import os
from datetime import datetime, timezone
# Third-party
from git import InvalidGitRepositoryError, NoSuchPathError, Repo
from pandas import PeriodIndex
def setup(current_file):
# Set up logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(module)s - %(message)s",
)
logger = logging.getLogger(__name__)
# Datetime
datetime_today = datetime.now(timezone.utc)
quarter = PeriodIndex([datetime_today.date()], freq="Q")[0]
# Paths
paths = {}
paths["repo"] = os.path.dirname(
os.path.abspath(os.path.realpath(os.path.join(__file__, "..")))
)
paths["dotenv"] = os.path.join(paths["repo"], ".env")
paths["data"] = os.path.dirname(
os.path.abspath(os.path.realpath(current_file))
)
phase = os.path.basename(
os.path.dirname(os.path.abspath(os.path.realpath(current_file)))
)
paths["data"] = os.path.join(paths["repo"], "data")
data_quarter = os.path.join(paths["data"], f"{quarter}")
paths["state"] = os.path.join(data_quarter, "state.yaml")
paths["data_phase"] = os.path.join(data_quarter, phase)
paths["data_quarter"] = data_quarter
return logger, paths
def log_paths(logger, paths):
paths_list = []
for label, path in paths.items():
label = f"{label}:"
paths_list.append(f"\n{' ' * 12}{label:<11} {path}")
paths_list = "".join(paths_list)
logger.info(f"PATHS:{paths_list}")
def commit_changes(message):
repo_path = os.getcwd()
try:
repo = Repo(repo_path)
except InvalidGitRepositoryError:
logging.error(f"Invalid Git repository at {repo_path}")
return
except NoSuchPathError:
logging.error(f"No such path: {repo_path}")
return
repo.git.add(update=True)
repo.index.commit(message)
origin = repo.remote(name="origin")
origin.push()
def main():
parser = argparse.ArgumentParser(description="Git operations script")
parser.add_argument(
"--operation",
type=str,
required=True,
help="Operation to perform: commit",
)
parser.add_argument(
"--message", type=str, required=True, help="Commit message"
)
args = parser.parse_args()
if args.operation == "commit":
commit_changes(args.message)
else:
raise ValueError("Unsupported operation")
if __name__ == "__main__":
main()