1414import selectors
1515import logging
1616import platform
17- import urllib .request
1817
1918logging .basicConfig (
2019 level = logging .INFO ,
@@ -75,13 +74,13 @@ def container_call(args):
7574
7675 last_line_stdout = None
7776 last_line_stderr = None
78- stderr_open , stderr_open = True , True
79- while stderr_open or stderr_open :
77+ stdout_open , stderr_open = True , True
78+ while stdout_open or stderr_open :
8079 for key , _ in sel .select ():
8180 data = key .fileobj .readline ()
8281 if data == '' :
8382 if key .fileobj is proc .stdout :
84- stdin_open = False
83+ stdout_open = False
8584 elif key .fileobj is proc .stderr :
8685 stderr_open = False
8786 continue
@@ -118,9 +117,11 @@ def build_mpr_builder(mx_version, dotnet, artifacts_repository=None):
118117 builder_image_url = None
119118 if artifacts_repository is not None :
120119 builder_image_url = f"{ artifacts_repository } :{ builder_image_tag } "
121- image_hash = pull_image (builder_image_url )
122- if image_hash is not None :
123- return builder_image_url
120+ image_url = pull_image (builder_image_url )
121+ if image_url is not None :
122+ return image_url
123+ else :
124+ builder_image_url = f"mendix-buildpack:{ builder_image_tag } "
124125
125126 prefix = ''
126127 if platform .machine () == 'arm64' and dotnet == 'dotnet' :
@@ -130,33 +131,32 @@ def build_mpr_builder(mx_version, dotnet, artifacts_repository=None):
130131 mxbuild_url = f"https://download.mendix.com/runtimes/{ mxbuild_filename } "
131132
132133 build_args = ['--build-arg' , f"MXBUILD_DOWNLOAD_URL={ mxbuild_url } " ,
133- '--file' , f"mxbuild/{ dotnet } .dockerfile" ]
134- if artifacts_repository is not None :
135- build_args += ['--tag' , builder_image_url ]
134+ '--file' , os .path .join ('mxbuild' , f"{ dotnet } .dockerfile" ),
135+ '--tag' , builder_image_url ]
136136
137- image_id = container_call (['image' , 'build' ] + build_args + ['mxbuild' ])
137+ container_call (['image' , 'build' ] + build_args + ['mxbuild' ])
138138 if artifacts_repository is not None :
139139 try :
140140 container_call (['image' , 'push' , builder_image_url ])
141141 except Exception as e :
142142 logging .warning ('Failed to push mxbuild into artifacts repository: {}; continuing with the build' .format (e ))
143- return image_id
143+ return builder_image_url
144144
145145def get_git_commit (source_dir ):
146146 git_head = os .path .join (source_dir , '.git' , 'HEAD' )
147147 if not os .path .isfile (git_head ):
148- return None
148+ raise Exception ( 'Project source doesn \' t contain git metadata' )
149149 with open (git_head ) as git_head :
150150 git_head_line = git_head .readline ().split ()
151151 if len (git_head_line ) == 1 :
152152 # Detached commit
153153 return git_head_line [0 ]
154154 if len (git_head_line ) > 2 :
155- return Exception (f"Unsupported Git HEAD format { git_head_line } " )
155+ raise Exception (f"Unsupported Git HEAD format { git_head_line } " )
156156 git_branch = git_head_line [1 ].split ('/' )
157157 git_branch_file = os .path .join (* ([source_dir , '.git' ] + git_branch ))
158158 if not os .path .isfile (git_branch_file ):
159- return Exception ('Git branch file doesn\' t exist' )
159+ raise Exception ('Git branch file doesn\' t exist' )
160160 with open (git_branch_file ) as git_branch_file :
161161 return git_branch_file .readline ()
162162
@@ -169,9 +169,12 @@ def build_mpr(source_dir, mpr_file, destination, artifacts_repository=None):
169169 logging .debug ('Detected Mendix version {}' .format ('.' .join (map (str ,mx_version_value ))))
170170 dotnet = 'dotnet' if mx_version_value >= (10 , 0 , 0 , 0 ) else 'mono'
171171 builder_image = build_mpr_builder (mx_version , dotnet , artifacts_repository )
172- model_version = get_git_commit (source_dir )
173- model_version = 'unversioned' if model_version is None else model_version
174-
172+ model_version = None
173+ try :
174+ model_version = get_git_commit (source_dir )
175+ except Exception as e :
176+ model_version = 'unversioned'
177+ logging .warning ('Cannot determine git commit ({}), will set model version to unversioned' .format (e ))
175178 container_id = container_call (['container' , 'create' , builder_image , os .path .basename (mpr_file ), model_version ])
176179 atexit .register (delete_container , container_id )
177180 container_call (['container' , 'cp' , os .path .abspath (source_dir )+ '/.' , f"{ container_id } :/workdir/project" ])
@@ -212,7 +215,9 @@ def prepare_mda(source_path, destination_path, artifacts_repository=None):
212215 mda_file = find_default_file (source_path , '.mda' )
213216 if mda_file is not None :
214217 with zipfile .ZipFile (mda_file ) as zip_file :
215- zip_file .extractall (project_path )
218+ zip_file .extractall (destination_path )
219+ elif os .path .isdir (source_path ):
220+ shutil .copytree (source_path , destination_path , dirs_exist_ok = True )
216221 extracted_mda_file = get_metadata_value (destination_path )
217222 if extracted_mda_file is not None :
218223 return destination_path
@@ -225,15 +230,14 @@ def build_image(mda_dir):
225230 mda_metadata = get_metadata_value (mda_path )
226231 mx_version = mda_metadata ['RuntimeVersion' ]
227232 java_version = mda_metadata .get ('JavaVersion' , 11 )
228- print (mda_metadata ['RuntimeVersion' ])
229- print (mda_metadata ['JavaVersion' ])
233+ logging .debug ("Detected Mendix {} Java {}" .format (mx_version , java_version ))
230234
231235if __name__ == '__main__' :
232236 parser = argparse .ArgumentParser (description = 'Build a Mendix app' )
233237 parser .add_argument ('--source' , metavar = 'source' , required = True , nargs = '?' , type = pathlib .Path , help = 'Path to source Mendix app (MDA file, MPK file, MPR directory or extracted MDA directory)' )
234238 parser .add_argument ('--destination' , metavar = 'destination' , required = True , nargs = '?' , type = pathlib .Path , help = 'Destination for MDA' )
235239 parser .add_argument ('--artifacts-repository' , required = False , nargs = '?' , metavar = 'artifacts_repository' , type = str , help = 'Repository to use for caching build images' )
236- parser .add_argument ('action' , metavar = 'action' , choices = ['build-mda' ], help = 'Action to perform' )
240+ parser .add_argument ('action' , metavar = 'action' , choices = ['build-mda-dir ' ], help = 'Action to perform' )
237241
238242 args = parser .parse_args ()
239243
@@ -244,4 +248,3 @@ def build_image(mda_dir):
244248 stop_processes ()
245249 raise
246250 # build_image(args.destination)
247-
0 commit comments