def test_good_cached_package(self): ext_dep_file_path = os.path.join(test_dir, "hw_ext_dep.json") with open(ext_dep_file_path, "w+") as ext_dep_file: ext_dep_file.write(hw_json_template % good_version) ext_dep_descriptor = EDF.ExternDepDescriptor( ext_dep_file_path).descriptor_contents ext_dep = NugetDependency(ext_dep_descriptor) # # Create a cache with a good cached package. # # First, create the cache. cache_dir = os.path.join(test_dir, 'nuget_test_good_cache') os.mkdir(cache_dir) ext_dep.global_cache_path = cache_dir # Then create the directories inside the cache that should hold the contents. package_cache_dir = os.path.join(cache_dir, hw_package_name.lower(), good_version) os.makedirs(package_cache_dir) # Create a directory that doesn't match the heuristic. test_cache_contents = os.path.join(package_cache_dir, hw_package_name, "working_blah") os.makedirs(test_cache_contents) self.assertTrue(ext_dep._fetch_from_cache(hw_package_name)) # Make sure that the contents were copied correctly. final_path = os.path.join(ext_dep.contents_dir, "working_blah") self.assertTrue(os.path.isdir(final_path)) self.assertTrue(ext_dep.verify())
def test_bad_cached_package(self): ext_dep_file_path = os.path.join(test_dir, "hw_ext_dep.json") with open(ext_dep_file_path, "w+") as ext_dep_file: ext_dep_file.write(hw_json_template % good_version) ext_dep_descriptor = EDF.ExternDepDescriptor( ext_dep_file_path).descriptor_contents ext_dep = NugetDependency(ext_dep_descriptor) # # Create a cache with a bad cached package. # # First, create the cache. cache_dir = os.path.join(test_dir, 'nuget_test_bad_cache') os.mkdir(cache_dir) ext_dep.global_cache_path = cache_dir # Then create the directories inside the cache that should hold the contents. package_cache_dir = os.path.join(cache_dir, hw_package_name.lower(), good_version) os.makedirs(package_cache_dir) # There are no package directories inside the cache. self.assertFalse(ext_dep._fetch_from_cache(hw_package_name)) # Create a directory that doesn't match the heuristic. test_cache_contents = os.path.join(package_cache_dir, "contents", "blah") os.makedirs(test_cache_contents) self.assertFalse(ext_dep._fetch_from_cache(hw_package_name))
def test_download_missing_nuget(self): ext_dep_file_path = os.path.join(test_dir, "hw_ext_dep.json") with open(ext_dep_file_path, "w+") as ext_dep_file: ext_dep_file.write(hw_json_template % missing_version) ext_dep_descriptor = EDF.ExternDepDescriptor(ext_dep_file_path).descriptor_contents ext_dep = NugetDependency(ext_dep_descriptor) with self.assertRaises(RuntimeError): ext_dep.fetch() self.assertFalse(ext_dep.verify()) self.assertEqual(ext_dep.version, missing_version)
def test_download_bad_nuget(self): ext_dep_file_path = os.path.join(test_dir, "hw_ext_dep.json") with open(ext_dep_file_path, "w+") as ext_dep_file: ext_dep_file.write(hw_json_template % bad_version) ext_dep_descriptor = EDF.ExternDepDescriptor(ext_dep_file_path).descriptor_contents ext_dep = NugetDependency(ext_dep_descriptor) with self.assertRaises(RuntimeError): # we should throw an exception because we don't know how to parse the version ext_dep.fetch() self.assertFalse(ext_dep.verify())
def test_cache_path_not_found(self): ext_dep_file_path = os.path.join(test_dir, "hw_ext_dep.json") with open(ext_dep_file_path, "w+") as ext_dep_file: ext_dep_file.write(hw_json_template % good_version) ext_dep_descriptor = EDF.ExternDepDescriptor( ext_dep_file_path).descriptor_contents ext_dep = NugetDependency(ext_dep_descriptor) ext_dep.global_cache_path = "not_a_real_path" self.assertFalse(ext_dep._fetch_from_cache(hw_package_name))
def test_can_get_nuget_path(self): nuget_cmd = NugetDependency.GetNugetCmd() nuget_cmd += ["locals", "global-packages", "-list"] ret = RunCmd(nuget_cmd[0], ' '.join(nuget_cmd[1:]), outstream=sys.stdout) self.assertEqual(ret, 0) # make sure we have a zero return code
def Push(self, nuPackage, apikey): if (not os.path.isfile(nuPackage)): raise Exception("Invalid file path for NuPkg file") logging.debug("Pushing %s file to server %s" % (nuPackage, self.ConfigData["server_url"])) cmd = NugetDependency.GetNugetCmd() cmd += ["push", nuPackage] cmd += ["-Verbosity", "detailed"] # cmd += ["-NonInteractive"] cmd += ["-Source", self.ConfigData["server_url"]] cmd += ["-ApiKey", apikey] output_buffer = StringIO() ret = RunCmd(cmd[0], " ".join(cmd[1:]), outstream=output_buffer) if (ret != 0): # Rewind the buffer and capture the contents. output_buffer.seek(0) output_contents = output_buffer.read() # Check for the API message. if "API key is invalid".lower() in output_contents.lower(): logging.critical( "API key is invalid. Please use --ApiKey to provide a valid key." ) # Generic error. logging.error("Failed on nuget commend. RC = 0x%x" % ret) return ret
def Pack(self, version, OutputDirectory, ContentDir, RelNotesText=None): self.NewVersion = version # content must be absolute path in nuspec otherwise it is assumed # relative to nuspec file. cdir = os.path.abspath(ContentDir) # make nuspec file xmlstring = self._MakeNuspecXml(cdir, RelNotesText) nuspec = os.path.join(OutputDirectory, self.Name + ".nuspec") self.TempFileToDelete.append(nuspec) f = open(nuspec, "wb") f.write(xmlstring) f.close() # run nuget cmd = NugetDependency.GetNugetCmd() cmd += ["pack", nuspec] cmd += ["-OutputDirectory", '"' + OutputDirectory + '"'] cmd += ["-Verbosity", "detailed"] # cmd += ["-NonInteractive"] ret = RunCmd(cmd[0], " ".join(cmd[1:])) if (ret != 0): logging.error("Failed on nuget commend. RC = 0x%x" % ret) return ret self.NuPackageFile = os.path.join( OutputDirectory, self._GetNuPkgFileName(self.NewVersion)) self.TempFileToDelete.append(self.NuPackageFile) return ret
def _GetNextVersion(self, force_version=None): # first get the last version # TODO: get the source from the JSON file that configures this old_version_raw = _GetLatestNugetVersion(self.nuget_package_name) old_version = NugetDependency.normalize_version(old_version_raw) logging.info(f"Found old version {old_version}") # Get a temporary folder to download the nuget package into temp_nuget_path = tempfile.mkdtemp() # Download the Nuget Package self._DownloadNugetPackageVersion(self.nuget_package_name, old_version, temp_nuget_path) # Unpack and read the previous release notes, skipping the header, also get hashes old_notes, old_hashes = self._GetOldReleaseNotesAndHashes( os.path.join(temp_nuget_path, self.nuget_package_name, self.nuget_package_name, self.release_notes_filename)) # Get the current hashes of open ssl and ourself curr_hashes = _GetCommitHashes(self.ws) # Figure out what release branch we are in logging.info(curr_hashes) logging.info("OLD") logging.info(old_hashes) current_release = self._GetReleaseForCommit(curr_hashes["MU_BASECORE"]) # Put that as the first two pieces of our version if force_version is not None: new_version = force_version else: new_version = current_release[0:4] + \ "." + current_release[4:] + "." # Calculate the newest version new_version += _GetSubVersions(old_version, current_release, curr_hashes, old_hashes) # make sure to normalize the version new_version = NugetDependency.normalize_version(new_version) if new_version == old_version: raise RuntimeError( "We are unable to republish the same version that was published last" ) # Create the release note from this branch, currently the commit message on the head? release_note = _GetReleaseNote() # Create our release notes, appending the old ones self._CreateReleaseNotes(release_note, new_version, old_notes, curr_hashes) # Clean up the temporary nuget file? logging.critical("Creating new version:" + new_version) return new_version
def test_normalize_version(self): version1 = "5.10.05.0" proper_version1 = "5.10.5" self.assertEqual(proper_version1, NugetDependency.normalize_version(version1)) version2 = "6.10" proper_version2 = "6.10.0" self.assertEqual(proper_version2, NugetDependency.normalize_version(version2)) version3 = "6" proper_version3 = "6.0.0" self.assertEqual(proper_version3, NugetDependency.normalize_version(version3)) # try some bad cases version4 = "not a number" with self.assertRaises(ValueError): NugetDependency.normalize_version(version4) with self.assertRaises(ValueError): NugetDependency.normalize_version("") with self.assertRaises(ValueError): NugetDependency.normalize_version(bad_version)
def ExtDepFactory(descriptor): # Add all supported external dependencies here to avoid import errors. from edk2toolext.environment.extdeptypes.web_dependency import WebDependency from edk2toolext.environment.extdeptypes.nuget_dependency import NugetDependency from edk2toolext.environment.extdeptypes.git_dependency import GitDependency if descriptor['type'] == NugetDependency.TypeString: return NugetDependency(descriptor) elif descriptor['type'] == WebDependency.TypeString: return WebDependency(descriptor) elif descriptor['type'] == GitDependency.TypeString: return GitDependency(descriptor) raise ValueError("Unknown extdep type '%s' requested!" % descriptor['type'])
def test_download_good_nuget(self): ext_dep_file_path = os.path.join(test_dir, "hw_ext_dep.json") with open(ext_dep_file_path, "w+") as ext_dep_file: ext_dep_file.write(hw_json_template % good_version) ext_dep_descriptor = EDF.ExternDepDescriptor(ext_dep_file_path).descriptor_contents ext_dep = NugetDependency(ext_dep_descriptor) ext_dep.fetch() self.assertTrue(ext_dep.verify()) self.assertEqual(ext_dep.version, good_version) # make sure we clean up after ourselves ext_dep.clean()
def _DownloadNugetPackageVersion(self, package_name: str, version: str, destination: os.PathLike, source=None): cmd = NugetDependency.GetNugetCmd() cmd += ["install", package_name] if source is not None: cmd += ["-Source", source] cmd += ["-ExcludeVersion"] cmd += ["-Version", version] cmd += ["-Verbosity", "detailed"] cmd += ["-OutputDirectory", '"' + destination + '"'] ret = RunCmd(cmd[0], " ".join(cmd[1:])) if ret != 0: return False else: return True
def _GetLatestNugetVersion(package_name, source=None): cmd = NugetDependency.GetNugetCmd() cmd += ["list"] cmd += [package_name] if source is not None: cmd += ["-Source", source] return_buffer = StringIO() if (RunCmd(cmd[0], " ".join(cmd[1:]), outstream=return_buffer) == 0): # Seek to the beginning of the output buffer and capture the output. return_buffer.seek(0) return_string = return_buffer.readlines() return_buffer.close() for line in return_string: line = line.strip() if line.startswith(package_name): return line.replace(package_name, "").strip() else: return "0.0.0.0"
def test_normalize_version(self): version1 = "5.10.05.0" proper_version1 = "5.10.5" self.assertEqual(proper_version1, NugetDependency.normalize_version(version1)) version2 = "6.10" proper_version2 = "6.10.0" self.assertEqual(proper_version2, NugetDependency.normalize_version(version2)) version3 = "6" proper_version3 = "6.0.0" self.assertEqual(proper_version3, NugetDependency.normalize_version(version3)) version4 = "6-beta" proper_version4 = "6.0.0-beta" self.assertEqual(proper_version4, NugetDependency.normalize_version(version4)) version5 = "3.2.1.-alpha" proper_version5 = "3.2.1-alpha" self.assertEqual(proper_version5, NugetDependency.normalize_version(version5)) # try some bad cases with self.assertRaises(ValueError): NugetDependency.normalize_version("not a number") with self.assertRaises(ValueError): NugetDependency.normalize_version("6.0-beta-beta") with self.assertRaises(ValueError): NugetDependency.normalize_version("6.0-bad") with self.assertRaises(ValueError): NugetDependency.normalize_version("6.0-") with self.assertRaises(ValueError): NugetDependency.normalize_version("--") with self.assertRaises(ValueError): NugetDependency.normalize_version("6-") with self.assertRaises(ValueError): NugetDependency.normalize_version("") with self.assertRaises(ValueError): NugetDependency.normalize_version(bad_version)