File: convert_all.py

package info (click to toggle)
azure-cli 2.82.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,359,416 kB
  • sloc: python: 1,910,381; sh: 1,343; makefile: 406; cs: 145; javascript: 74; sql: 37; xml: 21
file content (228 lines) | stat: -rw-r--r-- 8,933 bytes parent folder | download | duplicates (4)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import sys
import os
import subprocess


def get_repo_root():
    """
    Returns the root path to this repository. The root is where .git folder is.
    """
    import os.path
    here = os.path.dirname(os.path.realpath(__file__))

    while not os.path.exists(os.path.join(here, '.git')):
        here = os.path.dirname(here)

    return here


def comment_import_help(init_file, out_file):
    f_out =  open(out_file, "w")

    output = ""
    updated = False
    with open(init_file, "r") as f_in:
        for line in f_in:
            if "import" in line and "_help" in line and not updated:
                updated = True
                line = "# " + line
            output += line

    f_out.write(output)
    f_out.close()
    return updated

def decomment_import_help(init_file, out_file):
    f_out =  open(out_file, "w")

    output = ""
    updated = False
    with open(init_file, "r") as f_in:
        for line in f_in:
            if "import" in line and "_help" in line and not updated:
                updated = True
                line = line.lstrip("# ")
            output += line
    f_out.write(output)
    f_out.close()
    return updated

def install_extension(ext_name):
    command = "az extension add -n " + ext_name
    completed = subprocess.run(command.split())
    if completed.returncode == 0:
        print("{} was successfully installed.".format(ext_name))
        return True
    else:
        print("{} was not installed.".format(ext_name))
        return False

def uninstall_extension(ext_name):
    command = "az extension remove -n " + ext_name
    completed = subprocess.run(command.split())
    if completed.returncode == 0:
        print("{} was successfully uninstalled.".format(ext_name))
        return True
    else:
        print("{} was not uninstalled.".format(ext_name))
        return False

if __name__ == "__main__":
    args = sys.argv[1:]

    if args:
        if args[0].lower() == "--core":
            test = False
            try:
                if args[1].lower() == "--test":
                    test = True
            except IndexError:
                pass

            subprocess.run(["python", "./help_convert.py", "--get-all-mods"])

            module_names = []
            with open("mod.txt", "r") as f:
                for line in f:
                    module_names.append(line)
            if "sqlvm" not in module_names:
                module_names.append("sqlvm")
            os.remove("mod.txt")
            successes = 0
            with open(os.devnull, 'w') as devnull: # silence stdout by redirecting to devnull
                for mod in module_names:
                    args = ["python", "./help_convert.py", mod]
                    if test:
                        args.append("--test")
                    completed_process = subprocess.run(args, stdout=devnull)
                    if completed_process.returncode == 0:
                        successes += 1

            if successes:
                print("\n----------------------------------------------------------"
                      "\nSuccessfuly converted {} help.py files to help.yaml files."
                      "\n----------------------------------------------------------".format(successes))

        elif args[0].lower() == "--extensions":
            pass

        elif args[0].lower() == "--count":
            # Get info about help.py modules and converted help.yaml modules.

            src_root = os.path.join(get_repo_root(), "src", "command_modules")

            py_count = 0
            yaml_count = 0

            for root, dirs, files in os.walk(src_root):
                for file in files:
                    if file.endswith("_help.py") and os.path.dirname(root).endswith("command_modules") and os.path.join("build", "lib") not in root:
                        print("Found {}\n".format(os.path.join(root, file)))
                        py_count +=1
                    if file.endswith("help.yaml") and os.path.dirname(root).endswith("command_modules"):
                        print("Found {}\n".format(os.path.join(root, file)))
                        yaml_count +=1

            print("Found {} _help.py files\n".format(py_count))
            print("Found {} help.yaml files\n".format(yaml_count))

        elif args[0].lower() == "--move-py":

            src_root = os.path.join(get_repo_root(), "src", "command_modules")

            py_count = 0
            yaml_count = 0
            failures = 0

            for root, dirs, files in os.walk(src_root):
                for file in files:
                    if file.endswith("_help.py") and os.path.dirname(root).endswith("command_modules") and os.path.join("build", "lib") not in root:
                        src = os.path.join(root, file)
                        dst = os.path.join(root, "foo.py")
                        print("Found {}\n".format(src))
                        print("Renaming {}\n\tto {}\n.".format(src, dst))
                        os.rename(src, dst)
                        py_count +=1

                        src = os.path.join(root, "__init__.py")
                        dst = os.path.join(root, "__init__2.py")

                        success = comment_import_help(src, dst)

                        if success:
                            os.remove(src)
                            os.rename(dst, src)
                            print("Commented out import in {}\n".format(src))
                        else:
                            os.remove(dst)
                            print("Failed to comment out import in {}\n".format(src))
                            failures+=1


            print("Renamed {} _help.py files to foo.py.\n".format(py_count))
            print("There were {} failures to decomment import statements\n".format(failures))

        elif args[0].lower() == "--move-foo":
            src_root = os.path.join(get_repo_root(), "src", "command_modules")

            py_count = 0
            yaml_count = 0
            failures = 0

            for root, dirs, files in os.walk(src_root):
                for file in files:
                    if file.endswith("foo.py") and os.path.dirname(root).endswith("command_modules") and os.path.join("build", "lib") not in root:
                        src = os.path.join(root, file)
                        dst = os.path.join(root, "_help.py")
                        print("Found {}\n".format(src))
                        print("Renaming {}\n\tto {}\n.".format(src, dst))
                        os.rename(src, dst)
                        py_count +=1

                        src = os.path.join(root, "__init__.py")
                        dst = os.path.join(root, "__init__2.py")

                        success = decomment_import_help(src, dst)

                        if success:
                            os.remove(src)
                            os.rename(dst, src)
                            print("De-commented out import in {}\n".format(src))
                        else:
                            os.remove(dst)
                            print("Failed to de-comment out import in {}\n".format(src))
                            failures+=1

            print("Renamed {} foo.py files to _help.py.\n".format(py_count))
            print("There were {} failures to decomment import statements\n".format(failures))

        elif args[0].lower() == "--add-extensions":
            command = "az extension list-available --query [].name -o tsv"
            completed = subprocess.run(command.split(), stdout=subprocess.PIPE, universal_newlines=True)
            if completed.returncode == 0:
                num_installed = 0
                extensions = completed.stdout.splitlines()
                for ext in extensions:
                    success = install_extension(ext)
                    if success:
                        num_installed += 1

                print("Installed {} extensions".format(num_installed))

        elif args[0].lower() == "--remove-extensions":
            command = "az extension list --query [].name -o tsv"
            completed = subprocess.run(command.split(), stdout=subprocess.PIPE, universal_newlines=True)
            if completed.returncode == 0:
                num_installed = 0
                extensions = completed.stdout.splitlines()
                for ext in extensions:
                    success = uninstall_extension(ext)
                    if success:
                        num_installed += 1

                print("Uninstalled {} extensions".format(num_installed))