gebos_import.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. 'account2': 'GEBOS_Account',
  10. 'activity2': 'GEBOS_Activity',
  11. 'activitytype2': 'GEBOS_Activity_Type',
  12. 'costcenter2': 'GEBOS_Cost_Center',
  13. 'department2': 'GEBOS_Department',
  14. 'employee2': 'GEBOS_Employee',
  15. 'employeeaccount2': 'GEBOS_Employee_Account',
  16. 'employmenttype2': 'GEBOS_Employment_Type',
  17. 'timemodel2': 'GEBOS_Time_Model',
  18. 'timetracking2': 'GEBOS_Time_Tracking'
  19. }
  20. def task(args):
  21. return subprocess.Popen(args, stdout=subprocess.DEVNULL).wait()
  22. def gebos_import(csv_file, target_table):
  23. bulkcopy = str(Path(cfg['scripts_dir']) / 'bcp_import.bat')
  24. sqlquery = str(Path(cfg['scripts_dir']) / 'sqlquery.bat')
  25. sqlexec = str(Path(cfg['scripts_dir']) / 'sqlexec.bat')
  26. query_file = target_table + '.sql'
  27. task(f"{sqlquery} \"TRUNCATE TABLE [{cfg['schema']}].[{target_table}]\" ")
  28. task(f"{bulkcopy} [{cfg['schema']}].[{target_table}] \"{csv_file}\" ")
  29. task(f"{sqlexec} {query_file} \"{csv_file}\" ")
  30. def import_dir(base_dir=None):
  31. if base_dir is None:
  32. base_dir = 'E:\\GEBOS\\staging'
  33. for csv_file in Path(base_dir).glob('*.csv'):
  34. print(csv_file.name)
  35. for key, table in target_tables.items():
  36. if not csv_file.name.startswith(key):
  37. continue
  38. gebos_import(str(csv_file), table)
  39. csv_file.unlink()
  40. if __name__ == '__main__':
  41. import_dir()