#!/usr/bin/env python3
import MySQLdb
import sys
import os
import imp


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print("Нужно имя файла дампа netflow")
        exit(1)

    FNAME = sys.argv[1]

    cur_dir = os.path.dirname(os.path.abspath(__file__))
    tmp_dir = '/tmp/djing_flow'
    tmp_ipuser_file = '/tmp/ipuser.txt'
    tmp_dump_file = '%s/djing_flow_dump.tmp' % tmp_dir

    os.chdir(cur_dir)
    if not os.path.isdir(tmp_dir):
        os.mkdir(tmp_dir)
    os.rename('/tmp/djing_flow/%s' % FNAME, tmp_dump_file)

    settings = imp.load_source('djing.settings.DATABASES', '../../djing/settings.py')

    db = MySQLdb.connect(
            host=settings.DATABASES['default']['HOST'],
            user=settings.DATABASES['default']['USER'],
            passwd=settings.DATABASES['default']['PASSWORD'],
            db=settings.DATABASES['default']['NAME'],
            charset='utf8'
        )
    cursor = db.cursor()

    sql = r'SELECT abonent.ip_address, acc.username FROM abonent LEFT JOIN accounts_app_userprofile AS acc ON (acc.id = abonent.userprofile_ptr_id) WHERE abonent.ip_address != 0'
    ln = cursor.execute(sql)
    with open(tmp_ipuser_file, 'w') as f:
        f.write("count: %d\n" % ln)
        while True:
            row = cursor.fetchone()
            if row is None:
                break
            f.write("%d-%s\n" % row)
    db.close()

    os.system('/usr/bin/bash -c "%(CUR_DIR)s/djing_flow %(TMP_IPUSER_FILE)s < %(TMP_DUMP)s | /usr/bin/mysql -u%(DB_USER)s -h %(HOST)s -p %(DB_NAME)s --password=%(DB_PASSW)s"' % {
        'CUR_DIR': cur_dir,
        'TMP_IPUSER_FILE': tmp_ipuser_file,
        'TMP_DUMP': tmp_dump_file,
        'DB_USER': settings.DATABASES['default']['USER'],
        'HOST': settings.DATABASES['default']['HOST'],
        'DB_NAME': settings.DATABASES['default']['NAME'],
        'DB_PASSW': settings.DATABASES['default']['PASSWORD']
    })

    os.remove(tmp_dump_file)
    os.remove(tmp_ipuser_file)


@@ -56,19 +56,43 @@
  os%0A
+from 
 import
+lib
  imp
-%0A
+ort import_module
 %0A%0Aif
@@ -161,37 +161,37 @@
 nt(%22
-%D0%9D%D1%83%D0%B6%D0%BD%D0%BE %D0%B8%D0%BC%D1%8F %D1%84%D0%B0%D0%B9%D0%BB%D0%B0 %D0%B4%D0%B0%D0%BC%D0%BF%D0%B0 netflow
+File name of netflow required
 %22)%0A 
@@ -568,83 +568,89 @@
    s
-ettings = imp.load_source('djing.settings.DATABASES', '../../djing/
+ys.path.append('../../')%0A    local_settings = import_module('djing.local_
 settings
 .py'
@@ -649,11 +649,8 @@
 ings
-.py
 ')%0A%0A
@@ -687,17 +687,19 @@
     
-    
 host=
+local_
 sett
@@ -745,17 +745,19 @@
     
-   
- 
 user=
+local_
 sett
@@ -803,19 +803,21 @@
     
-    
 passwd=
+local_
 sett
@@ -867,15 +867,17 @@
     
-    db=
+db=local_
 sett
@@ -919,20 +919,16 @@
         
-    
 charset=
@@ -938,20 +938,16 @@
 f8'%0A    
-   
- 
 )%0A    cu
@@ -1022,21 +1022,53 @@
 ame 
-FROM abonent 
+' %5C%0A          r'FROM abonent ' %5C%0A          r'
 LEFT
@@ -1146,16 +1146,32 @@
 ptr_id) 
+' %5C%0A          r'
 WHERE ab
@@ -1473,16 +1473,25 @@
 .system(
+%0A        
 '/usr/bi
@@ -1500,16 +1500,27 @@
 bash -c 
+'%0A        '
 %22%25(CUR_D
@@ -1572,16 +1572,27 @@
 UMP)s %7C 
+'%0A        '
 /usr/bin
@@ -1677,16 +1677,20 @@
         
+    
 'CUR_DIR
@@ -1705,24 +1705,28 @@
 ir,%0A        
+  
+  
 'TMP_IPUSER_
@@ -1753,24 +1753,28 @@
 le,%0A        
+    
 'TMP_DUMP': 
@@ -1792,24 +1792,28 @@
 le,%0A        
+   
+ 
 'DB_USER': s
@@ -1811,16 +1811,22 @@
 _USER': 
+local_
 settings
@@ -1868,16 +1868,20 @@
     
+ 
+   
 'HOST': 
 sett
@@ -1876,16 +1876,22 @@
 'HOST': 
+local_
 settings
@@ -1925,24 +1925,28 @@
 '%5D,%0A        
+    
 'DB_NAME': s
@@ -1944,16 +1944,22 @@
 _NAME': 
+local_
 settings
@@ -1993,24 +1993,28 @@
 '%5D,%0A        
+    
 'DB_PASSW': 
@@ -2013,16 +2013,22 @@
 PASSW': 
+local_
 settings
@@ -2061,16 +2061,20 @@
 SWORD'%5D%0A
+    
     %7D)%0A%0A
@@ -2129,13 +2129,12 @@
 puser_file)%0A
-%0A

Fix importing when changed settings path