[2412] | 1 | #!/usr/bin/env python |
---|
| 2 | # -*- coding: utf-8 -*- |
---|
[2411] | 3 | |
---|
[2412] | 4 | # this must come first |
---|
| 5 | from __future__ import print_function, unicode_literals, division |
---|
[2411] | 6 | |
---|
[2412] | 7 | # standard library imports |
---|
| 8 | from argparse import ArgumentParser |
---|
[2413] | 9 | import json |
---|
| 10 | import shutil |
---|
| 11 | import os |
---|
[2412] | 12 | import os.path |
---|
[2413] | 13 | import subprocess |
---|
| 14 | # import datetime as dt |
---|
[2411] | 15 | |
---|
[2413] | 16 | # Application library imports |
---|
| 17 | from gencmip6 import * |
---|
| 18 | from gencmip6_path import * |
---|
[2411] | 19 | |
---|
[2413] | 20 | |
---|
[2412] | 21 | ######################################## |
---|
[2413] | 22 | def get_storedir(login): |
---|
[2411] | 23 | |
---|
[2413] | 24 | command = ["ccc_home", "-A", "-u", login] |
---|
| 25 | try : |
---|
| 26 | res = subprocess.check_output(command) |
---|
[2421] | 27 | except Exception as rc: |
---|
| 28 | print(rc) |
---|
[2413] | 29 | res = None |
---|
| 30 | |
---|
[2424] | 31 | return res.strip() |
---|
[2413] | 32 | |
---|
| 33 | |
---|
| 34 | ######################################## |
---|
| 35 | def get_dirsize(dirname): |
---|
| 36 | |
---|
| 37 | command = ["du", "-sbh", dirname] |
---|
| 38 | try : |
---|
[2421] | 39 | res = subprocess.check_output(command) |
---|
| 40 | res = res.split()[0] |
---|
[2413] | 41 | except Exception as rc : |
---|
| 42 | print(rc) |
---|
| 43 | res = None |
---|
| 44 | |
---|
| 45 | return res |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | # ######################################## |
---|
| 49 | # def get_dirlist(dirname): |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | # return output |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | ######################################## |
---|
| 56 | def parse_myproject(filename): |
---|
| 57 | |
---|
| 58 | project = {} |
---|
| 59 | project["project"] = "gencmip6" |
---|
[2412] | 60 | logins = {} |
---|
[2411] | 61 | |
---|
[2413] | 62 | if where_we_run() == "curie": |
---|
| 63 | try : |
---|
| 64 | res = subprocess.check_output("ccc_myproject") |
---|
| 65 | except Exception as rc : |
---|
| 66 | print(rc) |
---|
| 67 | exit() |
---|
[2421] | 68 | with open(os.path.join(DIR["DATA"], OUT["CCCMP"]), "w") as fileout: |
---|
[2413] | 69 | fileout.write(res) |
---|
| 70 | |
---|
[2412] | 71 | with open(filename, "r") as filein: |
---|
| 72 | # Skip lines until we find project name. |
---|
| 73 | # Then extract script date |
---|
| 74 | for ligne in filein: |
---|
| 75 | if project["project"] in ligne: |
---|
| 76 | today = ligne.split()[-1] |
---|
[2413] | 77 | today = string_to_date(today) |
---|
| 78 | |
---|
[2412] | 79 | break |
---|
[2411] | 80 | |
---|
[2412] | 81 | # Skip next two lines : first is blank and second is login titles |
---|
| 82 | for _ in xrange(1): |
---|
| 83 | next(filein) |
---|
[2411] | 84 | |
---|
[2413] | 85 | # Login list, until blank line |
---|
[2412] | 86 | for ligne in filein: |
---|
| 87 | if not ligne.strip(): |
---|
| 88 | break |
---|
| 89 | login, conso = ligne.split() |
---|
| 90 | logins[login] = float(conso) |
---|
[2411] | 91 | |
---|
[2413] | 92 | # Skip until we find consumed time (hours) |
---|
[2412] | 93 | for ligne in filein: |
---|
| 94 | if "Total" in ligne: |
---|
| 95 | total = float(ligne.split()[-1]) |
---|
| 96 | break |
---|
[2411] | 97 | |
---|
[2413] | 98 | # Skip until we find allocated time (hours) |
---|
[2412] | 99 | for ligne in filein: |
---|
| 100 | if "Allocated" in ligne: |
---|
| 101 | project["alloc"] = float(ligne.split()[-1]) |
---|
| 102 | break |
---|
[2411] | 103 | |
---|
[2413] | 104 | # Skip until we find theoratical use (%) |
---|
[2412] | 105 | for ligne in filein: |
---|
| 106 | if "Suggested use at this time" in ligne: |
---|
| 107 | utheo = float(ligne.split()[-1].strip("%")) |
---|
| 108 | break |
---|
[2411] | 109 | |
---|
[2413] | 110 | # Skip until we find real use (%) |
---|
[2412] | 111 | for ligne in filein: |
---|
| 112 | if "Real use at this time" in ligne: |
---|
| 113 | ureal = float(ligne.split()[-1].strip("%")) |
---|
| 114 | break |
---|
[2411] | 115 | |
---|
[2413] | 116 | # Skip until we find deadline |
---|
[2412] | 117 | for ligne in filein: |
---|
| 118 | if "Project deadline" in ligne: |
---|
| 119 | project["deadline"] = ligne.split()[-1] |
---|
| 120 | break |
---|
[2411] | 121 | |
---|
[2413] | 122 | return project, logins, today, total, utheo, ureal |
---|
[2411] | 123 | |
---|
| 124 | |
---|
[2412] | 125 | ######################################## |
---|
| 126 | def write_param(filename, project): |
---|
[2411] | 127 | |
---|
[2412] | 128 | if args.dryrun: |
---|
| 129 | print(json.dumps(project, indent=2)) |
---|
| 130 | else: |
---|
| 131 | with open(filename, "w") as fileout: |
---|
| 132 | json.dump(project, fileout, indent=2) |
---|
[2411] | 133 | |
---|
| 134 | |
---|
[2412] | 135 | ######################################## |
---|
| 136 | def write_bilan(filename, today, total, ureal, utheo): |
---|
| 137 | """ |
---|
| 138 | Conso totale par jour |
---|
| 139 | --------------------- |
---|
| 140 | on garde le total, date en tete en accumulant dans le fichier : |
---|
| 141 | OUT_CONSO_BILAN |
---|
| 142 | """ |
---|
[2411] | 143 | |
---|
[2412] | 144 | title_str = "{:10s} {:12s} {:11s} {:11s}\n".format( |
---|
| 145 | "date", |
---|
| 146 | "conso(hours)", |
---|
| 147 | "real_use(%)", |
---|
| 148 | "theo_use(%)", |
---|
| 149 | ) |
---|
[2413] | 150 | result_str = "{:%Y-%m-%d} {:12.2f} {:11.2f} {:11.2f}\n".format( |
---|
[2412] | 151 | today, |
---|
| 152 | total, |
---|
| 153 | ureal, |
---|
| 154 | utheo, |
---|
| 155 | ) |
---|
| 156 | |
---|
| 157 | if args.dryrun: |
---|
| 158 | print(title_str.strip()) |
---|
| 159 | print(result_str.strip()) |
---|
| 160 | else: |
---|
| 161 | if not os.path.isfile(filename): |
---|
| 162 | with open(filename, "w") as fileout: |
---|
| 163 | fileout.write(title_str) |
---|
| 164 | with open(filename, "a") as fileout: |
---|
| 165 | fileout.write(result_str) |
---|
| 166 | |
---|
| 167 | |
---|
| 168 | ######################################## |
---|
| 169 | def write_utheo(filename, today, utheo): |
---|
| 170 | """ |
---|
| 171 | Conso théorique par jour |
---|
| 172 | ------------------------ |
---|
| 173 | OUT_CONSO_THEO |
---|
| 174 | """ |
---|
| 175 | |
---|
| 176 | title_str = "{:10s} {:11s}\n".format( |
---|
| 177 | "date", |
---|
| 178 | "theo_use(%)", |
---|
| 179 | ) |
---|
[2413] | 180 | result_str = "{:%Y-%m-%d} {:11.2f}\n".format( |
---|
[2412] | 181 | today, |
---|
| 182 | utheo, |
---|
| 183 | ) |
---|
| 184 | |
---|
| 185 | if args.dryrun: |
---|
| 186 | print(title_str.strip()) |
---|
| 187 | print(result_str.strip()) |
---|
| 188 | else: |
---|
| 189 | if not os.path.isfile(filename): |
---|
| 190 | with open(filename, "w") as fileout: |
---|
| 191 | fileout.write(title_str) |
---|
| 192 | with open(filename, "a") as fileout: |
---|
| 193 | fileout.write(result_str) |
---|
| 194 | |
---|
| 195 | |
---|
| 196 | ######################################## |
---|
| 197 | def write_login(filename, today, logins): |
---|
| 198 | """ |
---|
| 199 | Conso par login (HOME) |
---|
| 200 | ---------------------- |
---|
| 201 | on garde la trace de chaque login, date en tete, en remplacant |
---|
| 202 | le fichier a chaque fois : OUT_CONSO_LOGIN |
---|
| 203 | """ |
---|
| 204 | |
---|
| 205 | title_str = "{:10s} {:10s} {:12s}\n".format( |
---|
| 206 | "date", |
---|
| 207 | "login", |
---|
| 208 | "conso(hours)", |
---|
| 209 | ) |
---|
| 210 | |
---|
| 211 | with open(filename, "w") as fileout: |
---|
| 212 | if args.dryrun: |
---|
| 213 | print(title_str.strip()) |
---|
| 214 | else: |
---|
| 215 | fileout.write(title_str) |
---|
| 216 | |
---|
| 217 | for key in sorted(logins): |
---|
[2413] | 218 | result_str = "{:%Y-%m-%d} {:10s} {:12.2f}\n".format( |
---|
[2412] | 219 | today, |
---|
| 220 | key, |
---|
| 221 | logins[key], |
---|
| 222 | ) |
---|
| 223 | if args.dryrun: |
---|
| 224 | print(result_str.strip()) |
---|
| 225 | else: |
---|
| 226 | fileout.write(result_str) |
---|
| 227 | |
---|
| 228 | |
---|
| 229 | ######################################## |
---|
[2417] | 230 | def write_store(filename, today, logins): |
---|
[2412] | 231 | """ |
---|
| 232 | volume cree sur STORE |
---|
| 233 | --------------------- |
---|
| 234 | par login qui a consomme, en remplacant le fichier a chaque fois : |
---|
| 235 | OUT_CONSO_STORE |
---|
| 236 | """ |
---|
| 237 | |
---|
[2413] | 238 | items = (login for login, conso in logins.iteritems() |
---|
| 239 | if conso > 0.) |
---|
[2412] | 240 | |
---|
[2417] | 241 | title_str = "{:10s} {:10s} {:>7s} {:s}\n".format( |
---|
| 242 | "date", |
---|
| 243 | "login", |
---|
| 244 | "dirsize", |
---|
| 245 | "dirname", |
---|
| 246 | ) |
---|
[2412] | 247 | |
---|
[2417] | 248 | with open(filename, "w") as fileout: |
---|
| 249 | if args.dryrun: |
---|
| 250 | print(title_str.strip()) |
---|
| 251 | else: |
---|
| 252 | fileout.write(title_str) |
---|
[2413] | 253 | |
---|
[2417] | 254 | for login in items: |
---|
| 255 | if args.verbose: |
---|
| 256 | print(login) |
---|
| 257 | storedir = get_storedir(login) |
---|
| 258 | if not storedir: |
---|
| 259 | break |
---|
| 260 | igcm_out = os.path.join(storedir, "IGCM_OUT") |
---|
[2413] | 261 | |
---|
[2421] | 262 | if not os.path.isdir(igcm_out): |
---|
| 263 | break |
---|
[2417] | 264 | |
---|
[2421] | 265 | dirlist = [] |
---|
| 266 | try: |
---|
| 267 | dirlist = os.listdir(igcm_out) |
---|
| 268 | except OSError as rc: |
---|
| 269 | print("Error on os.listdir({}):\n{}".format(igcm_out, rc)) |
---|
| 270 | |
---|
| 271 | for dirname in dirlist: |
---|
[2417] | 272 | result_str = "{:%Y-%m-%d} {:10s} {:>7s} {:s}\n".format( |
---|
| 273 | today, |
---|
| 274 | login, |
---|
[2421] | 275 | get_dirsize(os.path.join(igcm_out, dirname)), |
---|
| 276 | os.path.join(igcm_out, dirname) |
---|
[2417] | 277 | ) |
---|
[2421] | 278 | |
---|
| 279 | if args.dryrun or args.verbose: |
---|
[2417] | 280 | print(result_str.strip()) |
---|
[2421] | 281 | |
---|
| 282 | if not args.dryrun: |
---|
[2417] | 283 | fileout.write(result_str) |
---|
| 284 | |
---|
| 285 | |
---|
[2412] | 286 | ######################################## |
---|
[2417] | 287 | def save_files(OUT, today): |
---|
| 288 | |
---|
| 289 | if not args.dryrun: |
---|
| 290 | suffix = "{:%Y%m%d}".format(today) |
---|
| 291 | for filename in OUT.itervalues(): |
---|
| 292 | filein = os.path.join(DIR["DATA"], filename) |
---|
| 293 | if os.path.isfile(filein): |
---|
[2425] | 294 | fileout = os.path.join(DIR["SAVEDATA"], |
---|
[2417] | 295 | "_".join((filename, suffix))) |
---|
| 296 | shutil.copy(filein, fileout) |
---|
| 297 | |
---|
| 298 | |
---|
| 299 | ######################################## |
---|
[2412] | 300 | if __name__ == '__main__': |
---|
| 301 | |
---|
| 302 | # Get arguments from command line |
---|
| 303 | # =============================== |
---|
| 304 | parser = ArgumentParser() |
---|
| 305 | parser.add_argument("-v", "--verbose", action="store_true", |
---|
| 306 | help="Verbose mode") |
---|
| 307 | parser.add_argument("-d", "--dryrun", action="store_true", |
---|
| 308 | help="dry run, no file produced") |
---|
| 309 | parser.add_argument("-a", "--all", action="store_false", |
---|
| 310 | help="produce all files (default)") |
---|
| 311 | parser.add_argument("-b", "--bilan", action="store_true", |
---|
| 312 | help="produce all files (default)") |
---|
| 313 | parser.add_argument("-l", "--login", action="store_true", |
---|
| 314 | help="produce all files (default)") |
---|
| 315 | parser.add_argument("-s", "--store", action="store_true", |
---|
| 316 | help="produce all files (default)") |
---|
| 317 | |
---|
| 318 | args = parser.parse_args() |
---|
| 319 | if args.verbose: |
---|
| 320 | print(os.path.basename(__file__)) |
---|
[2413] | 321 | print(where_we_run()) |
---|
[2412] | 322 | print(args) |
---|
| 323 | |
---|
[2417] | 324 | if args.bilan or args.login or args.store: |
---|
| 325 | args.all = False |
---|
[2412] | 326 | |
---|
| 327 | if args.verbose: |
---|
[2413] | 328 | print(DIR["DATA"]) |
---|
[2425] | 329 | print(DIR["SAVEDATA"]) |
---|
[2412] | 330 | |
---|
[2413] | 331 | (project, logins, today, total, utheo, ureal) = \ |
---|
[2417] | 332 | parse_myproject(os.path.join(DIR["DATA"], OUT["CCCMP"])) |
---|
[2412] | 333 | |
---|
| 334 | if args.verbose: |
---|
| 335 | print(today, utheo, ureal) |
---|
| 336 | print(project) |
---|
| 337 | print(logins) |
---|
| 338 | |
---|
| 339 | # Produce files |
---|
| 340 | # ============= |
---|
| 341 | |
---|
| 342 | # 1- Parametres du projet |
---|
| 343 | # ----------------------- |
---|
[2417] | 344 | if args.verbose: |
---|
| 345 | print("=> write_param") |
---|
[2412] | 346 | |
---|
[2417] | 347 | write_param(os.path.join(DIR["DATA"], OUT["PARAM"]), project) |
---|
| 348 | |
---|
[2412] | 349 | # 2- Conso totale par jour |
---|
| 350 | # ------------------------ |
---|
[2417] | 351 | if args.verbose: |
---|
| 352 | print("=> write_bilan") |
---|
| 353 | |
---|
[2412] | 354 | write_bilan( |
---|
[2417] | 355 | os.path.join(DIR["DATA"], OUT["BILAN"]), |
---|
[2412] | 356 | today, |
---|
| 357 | total, |
---|
| 358 | ureal, |
---|
| 359 | utheo |
---|
| 360 | ) |
---|
| 361 | |
---|
| 362 | # 2b- Conso théorique par jour |
---|
| 363 | # ---------------------------- |
---|
[2417] | 364 | if args.verbose: |
---|
| 365 | print("=> write_utheo") |
---|
[2412] | 366 | |
---|
[2417] | 367 | write_utheo(os.path.join(DIR["DATA"], OUT["UTHEO"]), today, utheo) |
---|
| 368 | |
---|
[2412] | 369 | # 3- Conso par login (HOME) |
---|
| 370 | # ------------------------- |
---|
[2417] | 371 | if args.verbose: |
---|
| 372 | print("=> write_login") |
---|
[2412] | 373 | |
---|
[2417] | 374 | write_login(os.path.join(DIR["DATA"], OUT["LOGIN"]), today, logins) |
---|
| 375 | |
---|
[2412] | 376 | # 4- volume cree sur STORE |
---|
| 377 | # ------------------------ |
---|
[2417] | 378 | if args.verbose: |
---|
| 379 | print("=> write_store") |
---|
| 380 | |
---|
[2413] | 381 | # if where_we_run() == "curie": |
---|
[2417] | 382 | # write_store(os.path.join(DIR["DATA"], OUT["STORE"])) |
---|
| 383 | write_store(os.path.join(DIR["DATA"], OUT["STORE"]), today, logins) |
---|
[2412] | 384 | |
---|
| 385 | # Save files (on WORKDIR) |
---|
| 386 | # ======================= |
---|
[2417] | 387 | if args.verbose: |
---|
| 388 | print("=> Save files") |
---|
[2412] | 389 | if not args.dryrun: |
---|
[2417] | 390 | save_files(OUT, today) |
---|