6Triple-use minimum working example for PyXMake. This script can be
7executed in three different ways in varying levels of accessibility
9@note: Create a Python package from an OpenAPI specification. Optionally, create a
10portable installer instead.
15----------------------------------------------------------------------------------------------
19@author: garb_ma [DLR-FA,STM Braunschweig]
20----------------------------------------------------------------------------------------------
34 sys.path.insert(0,os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
43except ImportError:
pass
51 source="https://stmlab.fa-services.intra.dlr.de/2/openapi.json
",
57 Main function to execute the script.
60 delimn =
"."; result = -1; repeat = 0; client = kwargs.get(
"client",
"python")
62 url = posixpath.join(kwargs.get(
"base_url",
"https://stmlab.fa-services.intra.dlr.de/2/PyXMake/api/client/zip"))
64 if not output: output=os.getcwd()
66 data = {
"URI":source,
"ClientID": client,
67 "CLI": [
"--skip-validate-spec",
68 "--additional-properties=packageName=%s" % str(BuildID),
69 "--additional-properties=packageVersion=%s" % str(kwargs.get(
"version",
"1.0.0")),
70 "--additional-properties=packageUrl=%s" % posixpath.dirname(source)]}
72 filename = kwargs.get(
"filename",delimn.join([BuildID,kwargs.get(
"ext",posixpath.basename(url))]))
75 if not requests.head(url).ok:
raise ValueError
76 except:
raise ConnectionError(
"The given url cannot be reached: %s" % str(url))
81 with Utility.TemporaryDirectory()
as _, requests.patch(url, params=data, stream=
True)
as r:
83 with open(filename,
'wb')
as f:
84 for chunk
in r.iter_content(chunk_size=8192): f.write(chunk)
86 if kwargs.get(
"build",
False):
88 with zipfile.ZipFile(filename,
'r')
as zip_ref: zip_ref.extractall()
91 subprocess.check_call([sys.executable,
"-m",
"build"]);
93 Utility.CreateArchive(filename, os.path.join(os.path.abspath(os.getcwd()),client,
"dist"))
95 if os.listdir(os.getcwd()):
99 shutil.copy(filename, output);
break
104 if repeat >= 3:
break;
109 except ImportError:
pass
113if __name__ ==
"__main__":
117 parser = argparse.ArgumentParser(description=
'CLI wrapper options for OpenAPI client generator.')
118 parser.add_argument(
'name', type=str, nargs=1, help=
"Name of the project")
119 parser.add_argument(
'source', type=str, nargs=1, help=
"URL to an OpenAPI specification")
120 parser.add_argument(
'--version', type=str, nargs=1, help=
"Version used during package creation. Defaults to 1.0.0")
121 parser.add_argument(
"--output", type=str, nargs=1, help=
"Absolute path to output directory. Defaults to current project folder.")
122 parser.add_argument(
"--file", type=str, nargs=1, help=
"Output file name. Defaults to Name.zip")
123 parser.add_argument(
"--build", type=Utility.GetBoolean, const=
True, default=
True, nargs=
'?',
124 help=
"Check public PyPi repository to verify the results. Defaults to True.")
132 args, _ = parser.parse_known_args()
134 project = args.name[0]
136 source = args.source[0] ;
138 try: version = args.version[0]
139 except: version =
"1.0.0"
141 try: output = args.output[0]
142 except: output = os.path.abspath(os.getcwd())
144 try: build = args.build[0]
147 try: filename = args.file[0]
148 except: filename = delimn.join([project,
"zip"])
151 except Exception
as _:
153 if AllowDefaultMakeOption:
156 main(
"pyc_client", source=
"https://stmlab.fa-services.intra.dlr.de/1/openapi.json", output=os.getcwd(), version=PyCODAC.__version__, build=
True)
159 main(
"pyx_client", source=
"https://stmlab.fa-services.intra.dlr.de/2/openapi.json", output=os.getcwd(), version=PyXMake.__version__, build=
True)
162 main(project, source, output, filename=filename, version=version, build=build)
165 print(
"==================================")
166 print(
"Finished building API client")
167 print(
"==================================")
Create a make object to define the building environment.
main(BuildID, source="https://stmlab.fa-services.intra.dlr.de/2/openapi.json", output=None, **kwargs)