|
1 | 1 | import os |
2 | 2 | import json |
| 3 | +import shutil |
3 | 4 | import requests |
4 | 5 | import tempfile |
| 6 | +import subprocess |
5 | 7 | from pkg_resources import resource_filename |
6 | 8 | from geopy.geocoders import Nominatim |
7 | 9 | from geopy.location import Location |
@@ -213,13 +215,75 @@ def write_colors() -> None: |
213 | 215 |
|
214 | 216 |
|
215 | 217 | def write_ccfdataset() -> None: |
216 | | - ccfdataset = {} |
| 218 | + ccfdataset = get_ccfdataset() |
217 | 219 |
|
218 | 220 | ccfdataset_file = resource_filename("resotodata", "data/co2.json") |
219 | 221 | print(f"Writing CCF dataset to {ccfdataset_file}") |
220 | 222 | with open(ccfdataset_file, "w") as f: |
221 | 223 | json.dump(ccfdataset, f, indent=4) |
222 | 224 | f.write("\n") |
223 | 225 |
|
| 226 | + |
| 227 | +def get_ccfdataset() -> dict: |
| 228 | + print("Checking if git and npm are installed") |
| 229 | + for tool in ("git", "npm"): |
| 230 | + if not shutil.which(tool): |
| 231 | + raise RuntimeError(f"{tool} not found in path") |
| 232 | + |
| 233 | + ccfdataset = {} |
| 234 | + ccfrepo = "https://github.com/cloud-carbon-footprint/cloud-carbon-footprint.git" |
| 235 | + |
| 236 | + export_ts = """import { |
| 237 | + AWS_CLOUD_CONSTANTS, |
| 238 | + AWS_EMISSIONS_FACTORS_METRIC_TON_PER_KWH, |
| 239 | +} from './packages/aws/src/domain/AwsFootprintEstimationConstants' |
| 240 | +import { GCP_CLOUD_CONSTANTS, getGCPEmissionsFactors } from './packages/gcp/src/domain/GcpFootprintEstimationConstants' |
| 241 | +import { |
| 242 | + AZURE_CLOUD_CONSTANTS, |
| 243 | + AZURE_EMISSIONS_FACTORS_METRIC_TON_PER_KWH, |
| 244 | +} from './packages/azure/src/domain/AzureFootprintEstimationConstants' |
| 245 | +import { configLoader } from '@cloud-carbon-footprint/common' |
| 246 | +
|
| 247 | +const combinedDictionary: { [key: string]: any } = { |
| 248 | + aws: { |
| 249 | + AWS_CLOUD_CONSTANTS: AWS_CLOUD_CONSTANTS, |
| 250 | + AWS_EMISSIONS_FACTORS_METRIC_TON_PER_KWH: AWS_EMISSIONS_FACTORS_METRIC_TON_PER_KWH, |
| 251 | + }, |
| 252 | + gcp: { |
| 253 | + GCP_CLOUD_CONSTANTS: GCP_CLOUD_CONSTANTS, |
| 254 | + GCP_EMISSIONS_FACTORS_METRIC_TON_PER_KWH: getGCPEmissionsFactors(), |
| 255 | + }, |
| 256 | + azure: { |
| 257 | + AZURE_CLOUD_CONSTANTS: AZURE_CLOUD_CONSTANTS, |
| 258 | + AZURE_EMISSIONS_FACTORS_METRIC_TON_PER_KWH: AZURE_EMISSIONS_FACTORS_METRIC_TON_PER_KWH, |
| 259 | + }, |
| 260 | +} |
| 261 | +configLoader().GCP.USE_CARBON_FREE_ENERGY_PERCENTAGE = true |
| 262 | +combinedDictionary['gcp']['GCP_EMISSIONS_FACTORS_METRIC_TON_PER_KWH_CFE'] = getGCPEmissionsFactors() |
| 263 | +
|
| 264 | +console.log(JSON.stringify(combinedDictionary, null, 2)) |
| 265 | +""" |
| 266 | + |
| 267 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 268 | + print(f"Cloning {ccfrepo} to {tmpdir}") |
| 269 | + subprocess.run(["git", "clone", ccfrepo, tmpdir], check=True) |
| 270 | + print(f"Installing dependencies in {tmpdir}") |
| 271 | + subprocess.run(["npm", "install", "--legacy-peer-deps", "--silent"], cwd=tmpdir, check=True) |
| 272 | + print("Installing ts-node") |
| 273 | + subprocess.run(["npm", "install", "--silent", "ts-node"], cwd=tmpdir, check=True) |
| 274 | + |
| 275 | + export_file = os.path.join(tmpdir, "export.ts") |
| 276 | + with open(export_file, "w") as f: |
| 277 | + f.write(export_ts) |
| 278 | + |
| 279 | + print("Exporting CCF dataset constants") |
| 280 | + result = subprocess.run( |
| 281 | + ["./node_modules/.bin/ts-node", "export.ts"], cwd=tmpdir, check=True, capture_output=True, text=True |
| 282 | + ) |
| 283 | + |
| 284 | + ccfdataset = json.loads(result.stdout) |
| 285 | + return ccfdataset |
| 286 | + |
| 287 | + |
224 | 288 | if __name__ == "__main__": |
225 | 289 | main() |
0 commit comments