-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpost_gen_project.py
More file actions
139 lines (104 loc) · 4.78 KB
/
post_gen_project.py
File metadata and controls
139 lines (104 loc) · 4.78 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import subprocess
import shutil
import json
import os
import shutil
def get_date_fields():
# Run git commands and capture as string
output = subprocess.run(['git', 'log', '--date=iso', '--pretty=%cI', '--max-parents=0', '-n', '1'], capture_output=True, text=True)
# Store string and strip of leading / trailing whitespace
date = output.stdout.strip()
# Create a dictionary for date information to be pushed to JSON
date_information = {"created": f"{date}",
"lastModified": "{% now 'utc', '%Y-%m-%dT%H:%M:%S%z' %}",
"metadataLastUpdated": "{% now 'utc', '%Y-%m-%dT%H:%M:%S%z' %}"}
return date_information
def get_scc_labor_hours():
if shutil.which('scc') is not None:
try:
#Run scc and load results into a dictionary
#assuming we are in the .git directory of the repo
cmd = ['scc', '..', '--format', 'json2', '--exclude-file']
# Currently only supports specific files
files_to_exclude = [
"checks.yml,auto-changelog.yml,contributors.yml,repoStructure.yml,code.json,checklist.md,checklist.pdf,README.md,CONTRIBUTING.md,LICENSE,repolinter.json,SECURITY.md,CODE_OF_CONDUCT.md,CODEOWNERS.md,COMMUNITY.md,GOVERNANCE.md"
]
cmd.extend(files_to_exclude)
d = json.loads(subprocess.run(cmd,check=True, capture_output=True).stdout)
l_hours = d['estimatedScheduleMonths'] * 730.001
return round(l_hours,2)
except (subprocess.CalledProcessError, KeyError) as e:
print(e)
return None
else:
print("scc (https://github.com/boyter/scc) not found on system")
#Otherwise just use previous value as a default value.
return None
def prompt_exemption_text(exemptionType):
print(f"ℹ️ You have selected {exemptionType} for your Usage Type.")
return input("Please provide a one or two sentence justification for the exemption used. For more information on Usage Type, visit github.com/DSACMS/gov-codejson: ")
def format_multi_select_fields(text):
if text == "":
return []
new_text = text.split(",")
new_text = [text.strip() for text in new_text]
return new_text
def update_code_json(json_file_path):
# Read the JSON
with open(json_file_path, 'r') as file:
data = json.load(file)
# Add date_information and labor hours to the JSON
data['date'] = get_date_fields()
# Calculate labor hours
hours = get_scc_labor_hours()
if hours:
data['laborHours'] = hours
else:
data['laborHours'] = None
# Check if usageType is an exemption
if data['permissions']['usageType'].startswith('exempt'):
exemption_text = prompt_exemption_text(data['permissions']['usageType'])
data['permissions']['exemptionText'] = exemption_text
else:
del data['permissions']['exemptionText']
# Format multi-select options
multi_select_fields = ["platforms", "categories", "languages", "tags", "feedbackMechanisms", "projects", "systems", "upstream", "subsetInHealthcare", "userType"]
for field in multi_select_fields:
data[field] = format_multi_select_fields(data[field][0])
# Format integer fields
if data['reuseFrequency']['forks'].isdigit():
data['reuseFrequency']['forks'] = int(data['reuseFrequency']['forks'])
# Update the JSON
with open(json_file_path, 'w') as file:
json.dump(data, file, indent = 2)
def main():
try:
# Change to the parent directory
os.chdir('..')
# Define the codejson directory to remove
dir_name = "codejson"
# Check if codejson directory exists and remove it
if os.path.exists(dir_name):
shutil.rmtree(dir_name)
# Get the project name from cookiecutter
sub_project_dir = "{{cookiecutter.project_name}}"
codejson_file = "code.json"
project_root_dir = os.path.abspath('..')
json_file_path = os.path.join(sub_project_dir, codejson_file)
if os.path.exists(json_file_path):
# Move code.json file to parent directory
new_json_path = os.path.join(project_root_dir, codejson_file)
shutil.move(json_file_path, new_json_path)
# Remove the source directory
shutil.rmtree(sub_project_dir)
# Update the json with date and scc information
update_code_json(new_json_path)
print("Succesfully generated code.json file!")
else:
print(f"Error: {codejson_file} not found in {sub_project_dir}")
except OSError as error:
print(f"Error during OS operations: {error}")
except shutil.Error as error:
print(f"Error during shutil operations: {error}")
if __name__ == "__main__":
main()