def SetUp(self): self.package_parent = os.path.join(self.temp_path, 'package_root') files.CopyTree( self.Resource('tests', 'unit', 'command_lib', 'ml_engine', 'test_data', 'package_root'), self.package_parent) self.package_dir = os.path.join(self.package_parent, 'test_package') self.output_dir = os.path.join(self.temp_path, 'output')
def _CopyIfNotWritable(source_dir, temp_dir): """Returns a writable directory with the same contents as source_dir. If source_dir is writable, it is used. Otherwise, a directory 'dest' inside of temp_dir is used. Args: source_dir: str, the directory to (potentially) copy temp_dir: str, the path to a writable temporary directory in which to store any copied code. Returns: str, the path to a writable directory with the same contents as source_dir (i.e. source_dir, if it's writable, or a copy otherwise). Raises: UploadFailureError: if the command exits non-zero. """ if files.HasWriteAccessInDir(source_dir): return source_dir if files.IsDirAncestorOf(source_dir, temp_dir): raise UncopyablePackageError( 'Cannot copy directory since working directory [{}] is inside of ' 'source directory [{}].'.format(temp_dir, source_dir)) dest_dir = os.path.join(temp_dir, 'dest') log.debug('Copying local source tree from [%s] to [%s]', source_dir, dest_dir) try: files.CopyTree(source_dir, dest_dir) except OSError: raise UncopyablePackageError( 'Cannot write to working location [{}]'.format(dest_dir)) return dest_dir
def Execute(self, scenario_context): del scenario_context resource_path = sdk_test_base.SdkBase.Resource(*self._path.split('/')) dest_path = os.path.join(os.getcwd(), os.path.basename(resource_path)) if os.path.isdir(resource_path): files.CopyTree(resource_path, dest_path) else: shutil.copy(resource_path, dest_path)
def Run(self, staging_area, descriptor, app, appyaml=None): # Copy the application in tmp area, and copy the optional app.yaml in it. scratch_area = os.path.join(staging_area, 'scratch') if os.path.isdir(app): files.CopyTree(app, scratch_area) else: os.mkdir(scratch_area) shutil.copy2(app, scratch_area) if appyaml: shutil.copyfile(appyaml, os.path.join(scratch_area, 'app.yaml')) return scratch_area
def Run(self, staging_area, config_file, project_dir, explicit_appyaml=None): # Logic is: copy/symlink the project in the staged area, and create a # simple file app.yaml for runtime: java11 if it does not exist. # If it exists in the standard and documented default location # (in project_dir/src/main/appengine/app.yaml), copy it in the staged # area. appenginewebxml = os.path.join(project_dir, 'src', 'main', 'webapp', 'WEB-INF', 'appengine-web.xml') if os.path.exists(appenginewebxml): raise self.error() if explicit_appyaml: shutil.copyfile(explicit_appyaml, os.path.join(staging_area, 'app.yaml')) else: appyaml = os.path.join(project_dir, 'src', 'main', 'appengine', 'app.yaml') if os.path.exists(appyaml): # Put the user app.yaml at the root of the staging directory to deploy # as required by the Cloud SDK. shutil.copy2(appyaml, staging_area) else: # Create a very simple 1 liner app.yaml for Java11 runtime. files.WriteFileContents(os.path.join(staging_area, 'app.yaml'), 'runtime: java11\n') for name in os.listdir(project_dir): # Do not deploy locally built artifacts, buildpack will clean this anyway. if name == self.ignore: continue srcname = os.path.join(project_dir, name) dstname = os.path.join(staging_area, name) try: os.symlink(srcname, dstname) except (AttributeError, OSError): # AttributeError can occur if this is a Windows machine with an older # version of Python, in which case os.symlink is not defined. If this is # a newer version of Windows, but the user is not allowed to create # symlinks, we'll get an OSError saying "symbolic link privilege not # held." In both cases, we just fall back to copying the files. log.debug( 'Could not symlink files in staging directory, falling back ' 'to copying') if os.path.isdir(srcname): files.CopyTree(srcname, dstname) else: shutil.copy2(srcname, dstname) return staging_area
def SetUp(self): self.exec_mock = self.StartObjectPatch( execution_utils, 'Exec', side_effect=self._MakeFakeRunSetupTools()) self.package_root = os.path.join(self.temp_path, 'my-package') package = self.Resource('tests', 'unit', 'command_lib', 'ml_engine', 'test_data', 'package_root') files.CopyTree(package, self.package_root) self.output_path = os.path.join(self.temp_path, 'output') self.package_dir = os.path.join(self.package_root, 'test_package') self.get_python_mock = self.StartObjectPatch(execution_utils, 'GetPythonExecutable') self.get_python_mock.return_value = 'current/python'
def Run(self, staging_area, config_file, project_dir, explicit_appyaml=None): # Logic is: copy/symlink the project in the staged area, and create a # simple file app.yaml for runtime: java11 if it does not exist. # If it exists in the standard and documented default location # (in project_dir/src/main/appengine/app.yaml), copy it in the staged # area. appenginewebxml = os.path.join(project_dir, 'src', 'main', 'webapp', 'WEB-INF', 'appengine-web.xml') if os.path.exists(appenginewebxml): raise self.error() if explicit_appyaml: shutil.copyfile(explicit_appyaml, os.path.join(staging_area, 'app.yaml')) else: appyaml = os.path.join(project_dir, 'src', 'main', 'appengine', 'app.yaml') if os.path.exists(appyaml): # Put the user app.yaml at the root of the staging directory to deploy # as required by the Cloud SDK. shutil.copy2(appyaml, staging_area) else: # Create a very simple 1 liner app.yaml for Java11 runtime. files.WriteFileContents( os.path.join(staging_area, 'app.yaml'), 'runtime: java11\n') for name in os.listdir(project_dir): # Do not deploy locally built artifacts, buildpack will clean this anyway. if name == self.ignore: continue srcname = os.path.join(project_dir, name) dstname = os.path.join(staging_area, name) if os.path.isdir(srcname): if hasattr(os, 'symlink'): os.symlink(srcname, dstname) else: files.CopyTree(srcname, dstname) else: if hasattr(os, 'symlink'): os.symlink(srcname, dstname) else: shutil.copy2(srcname, dstname) return staging_area