ift_import.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from pathlib import Path
  2. import subprocess
  3. cfg = {
  4. 'schema': 'temp',
  5. 'scripts_dir': 'E:\\GlobalCube\\Tasks\\scripts',
  6. 'query_dir': 'E:\\GlobalCube\\System\\NAVISION\\SQL\\exec',
  7. }
  8. target_tables = {
  9. 'A': 'IFT_Actuals',
  10. 'B': 'IFT_Budget',
  11. 'C': 'IFT_Commitment',
  12. 'K': 'IFT_Controlling_Documents',
  13. 'P': 'IFT_Plan'
  14. }
  15. def task(args):
  16. return subprocess.Popen(args, stdout=subprocess.DEVNULL).wait()
  17. def ift_import(csv_file, target_table):
  18. bulkcopy = str(Path(cfg['scripts_dir']) / 'bcp_import.bat')
  19. sqlquery = str(Path(cfg['scripts_dir']) / 'sqlquery.bat')
  20. sqlexec = str(Path(cfg['scripts_dir']) / 'sqlexec.bat')
  21. query_file = target_table + '.sql'
  22. task(f"{sqlquery} \"TRUNCATE TABLE [{cfg['schema']}].[{target_table}]\" ")
  23. task(f"{bulkcopy} [{cfg['schema']}].[{target_table}] \"{csv_file}\" ")
  24. task(f"{sqlexec} {query_file} \"{csv_file}\" ")
  25. def import_dir(base_dir=None):
  26. if base_dir is None:
  27. base_dir = 'E:\\IFT\\staging'
  28. for csv_file in Path(base_dir).glob('*.csv'):
  29. print(csv_file.name)
  30. if csv_file.name.count('_') == 2:
  31. _, filetype, _ = csv_file.name.split('_')
  32. ift_import(str(csv_file), target_tables[filetype])
  33. csv_file.unlink()
  34. if __name__ == '__main__':
  35. import_dir()