-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename.py
executable file
·63 lines (49 loc) · 2.19 KB
/
rename.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#! /usr/bin/env python3
import sys
import os
import logging
import argparse
import csv
import shutil
import subprocess
def main():
path = f'/var/vmail/vmail1/{args.domain}/'
try:
with open(args.file) as csv_file:
logging.debug(f'Read file: {args.file}')
user_reader = csv.DictReader(csv_file)
for row in user_reader:
logging.debug(f'E-mail from file {row}')
new_email = row['new_email'].replace('@' + args.domain, "")
old_email = row['old_email'].replace('@' + args.domain, "")
logging.debug(f'\n{old_email}\n{new_email}\n{path}')
try:
os.rename(path + old_email, path + new_email)
except OSError:
logging.debug(f'Directory not empty {new_email}')
try:
subprocess.check_output(['cp', '-rf', path + old_email + '/Maildir', path + new_email])
except subprocess.CalledProcessError:
logging.debug(f'Directory not found {old_email}')
continue
try:
subprocess.check_output(['mv', path + old_email, '/var/vmail/vmail1/delete'])
except subprocess.CalledProcessError:
logging.debug(f'Directory not empty /var/vmail/vmail1/delete/{old_email}')
'''
try:
shutil.rmtree(path + old_email)
except FileNotFoundError:
continue
'''
except FileNotFoundError:
logging.info(f'Cannot open the file: {args.file}')
sys.exit(0)
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(process)d %(name)s %(levelname)s %(message)s',
datefmt='%d-%b-%y %H:%M:%S')
parser = argparse.ArgumentParser(description='Rename e-mail', formatter_class=argparse.MetavarTypeHelpFormatter)
parser.add_argument('--file', type=str, help='file with new and old e-mail')
parser.add_argument('--domain', type=str, help='e-mail domain')
args = parser.parse_args()
sys.exit(main())