|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2006 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing some common utility functions for the subversion package. |
|
8 """ |
|
9 |
|
10 import os |
|
11 import contextlib |
|
12 |
|
13 import Utilities |
|
14 |
|
15 from .Config import DefaultConfig, DefaultIgnores |
|
16 |
|
17 |
|
18 def getServersPath(): |
|
19 """ |
|
20 Module function to get the filename of the servers file. |
|
21 |
|
22 @return filename of the servers file (string) |
|
23 """ |
|
24 if Utilities.isWindowsPlatform(): |
|
25 appdata = os.environ["APPDATA"] |
|
26 return os.path.join(appdata, "Subversion", "servers") |
|
27 else: |
|
28 homedir = Utilities.getHomeDir() |
|
29 return os.path.join(homedir, ".subversion", "servers") |
|
30 |
|
31 |
|
32 def getConfigPath(): |
|
33 """ |
|
34 Module function to get the filename of the config file. |
|
35 |
|
36 @return filename of the config file (string) |
|
37 """ |
|
38 if Utilities.isWindowsPlatform(): |
|
39 appdata = os.environ["APPDATA"] |
|
40 return os.path.join(appdata, "Subversion", "config") |
|
41 else: |
|
42 homedir = Utilities.getHomeDir() |
|
43 return os.path.join(homedir, ".subversion", "config") |
|
44 |
|
45 |
|
46 def createDefaultConfig(): |
|
47 """ |
|
48 Module function to create a default config file suitable for eric. |
|
49 """ |
|
50 config = getConfigPath() |
|
51 with contextlib.suppress(OSError): |
|
52 os.makedirs(os.path.dirname(config)) |
|
53 with contextlib.suppress(OSError), open(config, "w") as f: |
|
54 f.write(DefaultConfig) |
|
55 |
|
56 |
|
57 def amendConfig(): |
|
58 """ |
|
59 Module function to amend the config file. |
|
60 """ |
|
61 config = getConfigPath() |
|
62 try: |
|
63 with open(config, "r") as f: |
|
64 configList = f.read().splitlines() |
|
65 except OSError: |
|
66 return |
|
67 |
|
68 newConfig = [] |
|
69 ignoresFound = False |
|
70 amendList = [] |
|
71 for line in configList: |
|
72 if line.find("global-ignores") in [0, 2]: |
|
73 ignoresFound = True |
|
74 if line.startswith("# "): |
|
75 line = line[2:] |
|
76 newConfig.append(line) |
|
77 for amend in DefaultIgnores: |
|
78 if amend not in line: |
|
79 amendList.append(amend) |
|
80 elif ignoresFound: |
|
81 if line.startswith("##"): |
|
82 ignoresFound = False |
|
83 if amendList: |
|
84 newConfig.append(" " + " ".join(amendList)) |
|
85 newConfig.append(line) |
|
86 continue |
|
87 elif line.startswith("# "): |
|
88 line = line[2:] |
|
89 newConfig.append(line) |
|
90 oldAmends = amendList[:] |
|
91 amendList = [] |
|
92 for amend in oldAmends: |
|
93 if amend not in line: |
|
94 amendList.append(amend) |
|
95 else: |
|
96 newConfig.append(line) |
|
97 |
|
98 if newConfig != configList: |
|
99 with contextlib.suppress(OSError), open(config, "w") as f: |
|
100 f.write("\n".join(newConfig)) |