1. #!/bin/env python
    2. # -*- coding: utf-8 -*-
    3. import datetime
    4. import os
    5. import fileinput
    6. import psycopg2
    7. import tarfile
    8. import shutil
    9. import logging
    10. import time
    11. logging.basicConfig(level=logging.DEBUG,
    12. format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', # 日志格式
    13. datefmt='%Y-%m-%d %H:%M:%S', # 时间格式:2018-11-12 23:50:21
    14. filename='/home/hjpt/App/hplatform/logs/hplatform_local_scan_del_lsms.log', # 日志的输出路径
    15. filemode='a')
    16. #获取目录下的所有文件
    17. def scan_file(infor):
    18. tar_file = []
    19. remote_path = infor[1]
    20. for files in os.listdir(remote_path):
    21. print files
    22. file_name = remote_path + '/' + files
    23. print file_name
    24. if os.path.splitext(file_name)[1] == '.gz':
    25. tar_file.append(files)
    26. print tar_file
    27. return tar_file
    28. #删除文件的第一行
    29. def del_file_first(del_file):
    30. for line in fileinput.input(del_file, inplace=1):
    31. if not fileinput.isfirstline():
    32. print line.replace("\n","")
    33. #解压缩模块
    34. def un_tar(file_names):
    35. un_tar_file = []
    36. ufile = tarfile.open(file_names)
    37. names = ufile.getnames()
    38. for tt in names:
    39. ufile.extract(tt, path=remote_path)
    40. un_tar_file.append(tt)
    41. print un_tar_file
    42. ufile.close()
    43. return un_tar_file
    44. if __name__ == '__main__':
    45. sql = '''
    46. SELECT
    47. scan_group_id,
    48. remote_path,
    49. remote_bk_path,
    50. local_path,
    51. file_regular,
    52. chkf_regular,
    53. mv_flag,
    54. data_source,
    55. table_attr,
    56. prov_id
    57. FROM
    58. public.conf_path_info
    59. WHERE
    60. is_valid = '0' and scan_group_id = 'XXXX'
    61. '''
    62. try:
    63. pg_conn = psycopg2.connect(database='XX', user='XX', password='XXXX', host='XXXX', port='XXXX')
    64. cur = pg_conn.cursor()
    65. cur.execute(sql)
    66. rows = cur.fetchall()
    67. print(rows)
    68. pg_conn.close()
    69. except Exception as e:
    70. print e
    71. else:
    72. while True:
    73. for i in rows:
    74. remote_path = i[1]
    75. remote_bk_path = i[2]
    76. local_path = i[3]
    77. files = scan_file(i)
    78. #获取压缩文件名称
    79. for filename in files:
    80. print filename
    81. file = remote_path + '/' + filename
    82. bak_file = remote_bk_path + '/' + filename
    83. try:
    84. if os.path.getsize(file) == 0:
    85. os.remove(file)
    86. logging.info(file + 'file size is : 0 ,delete from local.')
    87. else:
    88. sss = os.path.getsize(file)
    89. #解压压缩文件
    90. un_tar_file=un_tar(file)
    91. logging.info(file + 'file size is :' + str(sss) + ': untar file successful !')
    92. #将文件从正式目录挪到bak目录
    93. shutil.move(file,bak_file)
    94. for unfilename in un_tar_file:
    95. dfilename = remote_path + '/' + unfilename
    96. tmpfilename = local_path + '/' + unfilename
    97. #删除文件的第一行
    98. del_file_first(dfilename)
    99. logging.info(dfilename + 'delete first line successful !')
    100. shutil.move(dfilename,tmpfilename)
    101. del un_tar_file[0]
    102. except Exception as e:
    103. print e