Repository navigation
Expand file tree
/
Copy pathreimport.py
More file actions
executable file
·114 lines (101 loc) · 3.33 KB
/
Copy pathreimport.py
File metadata and controls
executable file
·114 lines (101 loc) · 3.33 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python
# Needed for antipackage with python 2
from __future__ import absolute_import
import datetime
import fnmatch
import glob
import io
import json
import os
import os.path
import re
import socket
import subprocess
import sys
from collections import OrderedDict
from os.path import expandvars
def reimport(*paths):
for p in paths:
if os.path.isfile(p):
print('Reimporting file: ' + p)
_reimport(p)
elif os.path.isdir(p):
print('Reimporting dir: ' + p)
for dir, _, files in os.walk(p):
for f in fnmatch.filter(files, '*.go'):
_reimport(dir + '/' + f)
else:
for f in glob.glob(p):
print('Reimporinting file: ' + f)
_reimport(f)
BEGIN_IMPORT_REGEX = ur'import \(\s*'
END_IMPORT_REGEX = ur'\)\s*'
PKG_MAP = {
'k8s.io/apimachinery/pkg/api/testing/roundtrip': ['k8s.io/apimachinery/pkg/api/apitesting/roundtrip']
}
def _detect_pkg_alias(line):
parts = line.strip().split()
if len(parts) == 2:
alias = parts[0]
pkg = parts[1].strip('"')
else:
pkg = parts[0].strip('"')
dirs = pkg.rsplit('/')
alias = dirs[len(dirs) - 1]
return pkg, alias
def _reimport(fname):
with open(fname, 'r+') as f:
content = f.readlines()
out = []
replacements = {}
import_block = False
for line in content:
c = line.strip()
if import_block:
if c == '':
continue
elif re.match(END_IMPORT_REGEX, c) is not None:
import_block = False
else:
cur_pkg, cur_alias = _detect_pkg_alias(c)
if cur_pkg in PKG_MAP:
rep = PKG_MAP[cur_pkg]
if len(rep) == 2:
out.append('%s "%s"\n' % (rep[1], rep[0]))
if rep[1] != cur_alias:
replacements[cur_alias] = rep[1]
else:
nu_pkg, nu_alias = _detect_pkg_alias(rep[0])
out.append('"%s"\n' % (rep[0]))
if nu_alias != cur_alias:
replacements[cur_alias] = nu_alias
continue
elif re.match(BEGIN_IMPORT_REGEX, c) is not None:
import_block = True
out.append(line)
content = out
out = []
for line in content:
c = line.strip()
if import_block:
if c == '':
continue
elif re.match(END_IMPORT_REGEX, c) is not None:
import_block = False
elif re.match(BEGIN_IMPORT_REGEX, c) is not None:
import_block = True
else:
for cur, nu in replacements.iteritems():
line = line.replace(cur+'.', nu+'.')
out.append(line)
f.seek(0)
f.writelines(out)
f.truncate()
if __name__ == "__main__":
reimport(*sys.argv[1:])
# if len(sys.argv) > 1:
# # http://stackoverflow.com/a/834451
# # http://stackoverflow.com/a/817296
# globals()[sys.argv[1]](*sys.argv[2:])
# else:
# help()