-
Notifications
You must be signed in to change notification settings - Fork 2
/
rename-robot.py
77 lines (60 loc) · 2.52 KB
/
rename-robot.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
from __future__ import print_function
import os
import re
import argparse
def get_clean_name(name):
name = name.lower()
lower_name = name.replace('-', '_')
cc_name = ''.join(x.capitalize() or '_' for x in lower_name.split('_'))
return name, lower_name, cc_name
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Poppy daemon')
parser.add_argument('name', type=str, nargs='+',
help='name of your robot')
args = parser.parse_args()
args.name = '-'.join(args.name)
wd = os.path.dirname(os.path.realpath(__file__))
print('Working in directory "{}"...'.format(wd))
os.chdir(wd)
old_name, old_lower, old_cc = get_clean_name(os.path.basename(wd))
new_name, new_lower, new_cc = get_clean_name(os.path.basename(args.name))
od = os.path.join('software', old_lower)
nd = os.path.join('software', new_lower)
print('Rename folder "{}" to "{}"'.format(od, nd))
os.rename(od, nd)
od = os.path.join('software', new_lower, '{}.py'.format(old_lower))
nd = os.path.join('software', new_lower, '{}.py'.format(new_lower))
print('Rename module "{}" to "{}"'.format(od, nd))
os.rename(od, nd)
od = os.path.join('software', new_lower, 'configuration', '{}.json'.format(old_lower))
nd = os.path.join('software', new_lower, 'configuration', '{}.json'.format(new_lower))
print('Rename module "{}" to "{}"'.format(od, nd))
os.rename(od, nd)
print('Finally, rename root folder from "{}" to "{}"'.format(old_name, args.name))
os.rename(os.path.join('..', old_name),
os.path.join('..', new_name))
print('Start editing python modules...')
p = os.path.join('software', new_lower, '__init__.py')
print('Edit {}'.format(p))
with open(p) as f:
s = f.read()
s = re.sub(r'{}'.format(old_lower), '{}'.format(new_lower), s)
s = re.sub(r'{}'.format(old_cc), '{}'.format(new_cc), s)
with open(p, 'w') as f:
f.write(s)
p = os.path.join('software', new_lower, '{}.py'.format(new_lower))
print('Edit {}'.format(p))
with open(p) as f:
s = f.read()
s = re.sub(r'{}'.format(old_lower), '{}'.format(new_lower), s)
s = re.sub(r'{}'.format(old_cc), '{}'.format(new_cc), s)
with open(p, 'w') as f:
f.write(s)
p = os.path.join('software', 'setup.py')
print('Edit {}'.format(p))
with open(p) as f:
s = f.read()
s = re.sub(r'{}'.format(old_name), '{}'.format(new_name), s)
with open(p, 'w') as f:
f.write(s)