-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvimcc.py
executable file
·88 lines (68 loc) · 2.83 KB
/
vimcc.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
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python3
import sys
import os
import configparser
import shutil
import subprocess
def find_project_config_file(directory, max_levels = 5):
if max_levels == 0 or directory == "/" or directory == "":
return None #Config file not found
fileName = os.path.join(directory, "config/config")
if os.path.isfile(fileName):
return fileName
else:
return find_project_config_file(os.path.dirname(directory), max_levels-1)
#Returns true: success, false: no success
def copy_file_from_subprojects(file_name):
abs_file_name = os.path.abspath(file_name)
#Find project config file
cfgFile = find_project_config_file(os.path.dirname(abs_file_name))
if not cfgFile:
print("Could not find the config file of the WinCC project for '{}'!"
"".format(file_name))
return False
config = configparser.RawConfigParser(dict_type=MultiOrderedDict, strict=False)
if not config.read(cfgFile):
print("Could not parse config file '{}'!".format(cfgFile))
return False
projDirs = config.get("general", "proj_path", fallback=None)
if not projDirs:
print("Could not parse general:proj_path from '{}'!".format(cfgFile))
return False
projDirs = [ directory.replace('"', '') for directory in projDirs]
#Main and sub project directories
mainDir = projDirs.pop(-1)
subDirs = projDirs
rel_file_name = abs_file_name.replace(mainDir+"/", "")
for subDir in subDirs[::-1]: #subDirs are listed from least to highest priority
new_file_name = os.path.join(subDir, rel_file_name)
if os.path.isfile(new_file_name):
destDir = os.path.dirname(file_name)
if destDir != "":
os.makedirs(os.path.dirname(file_name), exist_ok=True) #Create dir if needed
shutil.copy2(new_file_name, file_name)
print("Copied '{}' to '{}'!".format(new_file_name, file_name))
return True
print("File '{}' not found in subprojects.".format(file_name))
return False
#Necessary to load duplicate keys/options from config file
from collections import OrderedDict
class MultiOrderedDict(OrderedDict):
def __setitem__(self, key, value):
if isinstance(value, list) and key in self:
self[key].extend(value)
else:
super(OrderedDict, self).__setitem__(key, value)
def keys(self):
return super(OrderedDict, self).keys()
if __name__ == "__main__":
allFiles = sys.argv[1:] #All provided files
#Existing and missing files in current directory
existingFiles = []
missingFiles = []
for file in allFiles:
if os.path.isfile(file) or copy_file_from_subprojects(file):
existingFiles.append(file)
else:
missingFiles.append(file)
subprocess.run(["vim"]+existingFiles+missingFiles)