portal.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import subprocess
  2. import time
  3. from datetime import datetime
  4. from pathlib import Path
  5. import config
  6. cfg = config.config()
  7. report_dir = cfg.portal_dir + '\\System\\Report'
  8. publish_dir = cfg.portal_dir + '\\daten'
  9. def portal(portal_file):
  10. print(f"== Portal '{portal_file}.xml' ==")
  11. full_filename = f'{cfg.xml_dir}\\{portal_file}.xml'
  12. if not Path(full_filename).exists():
  13. print(f"!! Datei '{full_filename}' existiert nicht !!")
  14. return
  15. prepare_report_temp()
  16. pub_dir = Path(f'{publish_dir}\\{portal_file}')
  17. if not pub_dir.exists():
  18. pub_dir.mkdir()
  19. with open(f'{cfg.log_dir}\\{portal_file}.mac.log', 'w') as logfile:
  20. export_files('jpg', pub_dir, logfile)
  21. export_files('xls', pub_dir, logfile)
  22. export_files('pdf', pub_dir, logfile)
  23. def export_files(export_format, pub_dir, logfile):
  24. rep_dir = f'{report_dir}\\{export_format}'
  25. cmd = f'"{cfg.cognos_dir}\\runmac32.exe" "{cfg.tools_dir}\\publish-reports.mac" "{rep_dir}","{export_format}","{pub_dir}"'
  26. print(f"Exportiere '{rep_dir}' nach '{pub_dir}'...")
  27. start = datetime.now().timestamp()
  28. p = subprocess.Popen(cmd, stdout=logfile, stderr=logfile)
  29. reports = sorted(Path(rep_dir).glob('*.pp[rx]'))
  30. exports = [translate_filename(f.name, export_format, str(pub_dir)) for f in reports]
  31. exports_remaining = exports
  32. while p.poll() is None:
  33. exports_remaining = [f for f in exports_remaining if not f.exists() or f.stat().st_mtime < start]
  34. print('Fortschritt: ' + str(len(exports) - len(exports_remaining)) + '/' + str(len(exports)) + '...', end='\r')
  35. time.sleep(5)
  36. print('Abgeschlossen. ')
  37. if export_format == 'jpg':
  38. rename_files(pub_dir)
  39. cleanup_dir(pub_dir)
  40. def translate_filename(filename, export_format, pub_dir):
  41. if export_format == 'jpg':
  42. return Path(pub_dir + '\\' + filename[:-4] + 'graph1.jpg')
  43. return Path(pub_dir + '\\' + filename[:-4] + '_0.' + export_format)
  44. def rename_files(pub_dir):
  45. for f in pub_dir.glob('*.jpg'):
  46. if f.exists():
  47. new_name = str(f).replace('graph', '_')
  48. f.replace(Path(new_name))
  49. def cleanup_dir(pub_dir):
  50. for f in pub_dir.glob('*.htm'):
  51. f.unlink()
  52. def prepare_report_temp():
  53. pass
  54. if __name__ == '__main__':
  55. portal('GAPS_neu')