I have a jenkins pipeline script, where I am collecting the data as json format. I want all the data to be updated in a string and finally write to a json file. But in my script, the value is over written.
for(String item: jsonObj.devices) {
os = item.os
if(!os.contains("\\.")) {
osV = os + ".0"
} else{
osV = os
}
def osValues = osV.split('\\.')
if(values[0].toInteger() <= osValues[0].toInteger()) {
name = item.name
def data = [
name: "$name",
os: "$os",
]
def json_str = JsonOutput.toJson(data)
def json_beauty = JsonOutput.prettyPrint(json_str)
writeJSON(file: 'message1.json', json: json_beauty)
}
}
I want all the data to be collected and finally write to sample.json. Please let me know where I am wrong.
Related
I have two array list in that form:
def AllowedExtensions = [ '.mxf', '.mov', '.mp4']
def myPaths = [C:\Temp\Media\Media.c2v, C:\Temp\Media\Media.V2C, C:\Temp\Media\toto\test\巨塔の終わり.mp4, C:\Temp\Media\toto\toto.mxf]
I'm trying to build a new list mySelectedPath from myPaths only if it matches one of the extensions from AllowedExtensions
I'm looking for the groovy way to do it but can't get it working correctly.
AllowedExtensions.each { mySelectedPath = myPaths.findAll { it }}
Here is my expected result:
[C:\Temp\MediaNavigator\toto\toto.mxf, C:\Temp\MediaNavigator\toto\test\巨塔の終わり.mp4]
Thank you for any input !
Another solution is to use findResults
allowedExtensions.findResults { ext -> myPaths.find { it.endsWith ext } }
If you want multiple results per extension, try:
allowedExtensions.findResults { ext -> myPaths.findAll { it.endsWith ext } }.flatten()
def AllowedExtensions = [ '.mxf', '.mov', '.mp4']
def myPaths = [
'C:\\Temp\\Media\\Media.c2v',
'C:\\Temp\\Media\\Media.V2C',
'C:\\Temp\\Media\\toto\\test\\巨塔の終わり.mp4',
'C:\\Temp\\Media\\toto\\toto.mxf'
]
println myPaths.findAll { it.toLowerCase()[it.lastIndexOf('.')..-1] in AllowedExtensions }
Or regex-based:
def AllowedExtensions = [ '.mxf', '.mov', '.mp4']
def myPaths = ['C:\\Temp\\Media\\Media.c2v', 'C:\\Temp\\Media\\Media.V2C', 'C:\\Temp\\Media\\toto\\test\\巨塔の終わり.mp4', 'C:\\Temp\\Media\\toto\\toto.mxf']
def regex = /^.*\.(${AllowedExtensions.join( '|' ).replace( '.', '' )})$/
def res = myPaths.grep{ it ==~ regex }
assert res.toString() == '[C:\\Temp\\Media\\toto\\test\\巨塔の終わり.mp4, C:\\Temp\\Media\\toto\\toto.mxf]'
I need to take my output from the for loop below and add it to a single array. The list object can either be ["envName-inactive-1", "active-1", "inactive-1", "envName-active-1"] or ["envName-inactive-2", "", "", "envName-active-2"]
My code:
if (appendVersion) {
for (elements in list) {
test = (elements + "-" + branch)
println(test)
}
} else {
println(list)
}
output:
envName-inactive-1-v2
active-1-v2
inactive-1-v2
envName-active-1-v2
and
envName-inactive-2-v2
-v2
-v2
envName-active-2-v2
desired output:
["envName-inactive-1-v2", "active-1-v2", "inactive-1-v2", "envName-active-1-v2"]
and
["envName-inactive-2-v2", "", "", "envName-active-2-v2"]
You desired format seems to be json. In Jenkins you have option to use writeJSON to convert list to json format.
def branch = "v2"
def list = ["envName-inactive-1", "active-1", "inactive-1", "envName-active-1"]
def versionedList = list.collect{it ? it+'-'+branch : ''}
def json = writeJSON returnText: true, json: versionedList
println json
the same in plain groovy:
def branch = "v2"
def list = ["envName-inactive-1", "active-1", "inactive-1", "envName-active-1"]
def versionedList = list.collect{it ? it+'-'+branch : ''}
def json = new groovy.json.JsonBuilder(versionedList).toString()
println json
result:
["envName-inactive-1-v2","active-1-v2","inactive-1-v2","envName-active-1-v2"]
I am calling the Azure cognitive API for OCR text-recognization and I am passing 10-images at the same time simultaneously (as the code below only accepts one image at a time-- that is 10-independent requests in parallel) which is not efficient to me, regardin processing point of view, as I need to use extra modules i.e: Celery and multiprocessing.
So, is there a way to send all the 10-images in a single request and get the output at once then do post processing?
import time
from io import BytesIO
import cv2
import requests
from PIL import Image as PILImage
from PIL import Image
file_list = []
headers = {
"Ocp-Apim-Subscription-Key": "<API-KEY>",
'Content-Type': 'application/octet-stream'}
p = "symbol_sample.jpg"
print(p,"p")
def recognise_text(p):
p = cv2.imread(p)
cropped_image = PILImage.fromarray(p)
buffer = BytesIO()
cropped_image.save(buffer, format="JPEG")
image_bytes = buffer.getvalue()
try:
response = requests.post(
"https://centralindia.api.cognitive.microsoft.com/vision/v2.0/recognizeText?mode=Printed",
headers=headers,
data=image_bytes
)
header_link = str(response.headers['Operation-Location'])
while (True):
headers_get = {
"Ocp-Apim-Subscription-Key": "<API-KEY>"",
'Content-Type': 'application/json'
}
result = requests.get(
url=header_link,
headers=headers_get
)
response_r = result.json()
if response_r["status"] == "Succeeded":
return response_r
else:
time.sleep(4)
except Exception as e:
print(e)
return ""
image1="symbol_sample.jpg"
o = recognise_text(image1)
print(o)
Any help would be really appreciated.
I guess you are looking for Batch Read File
public class BatchReadFileSample
{
public static async Task RunAsync(string endpoint, string key)
{
ComputerVisionClient computerVision = new ComputerVisionClient(new ApiKeyServiceClientCredentials(key))
{
Endpoint = endpoint
};
const int numberOfCharsInOperationId = 36;
string localImagePath = #"Images\handwritten_text.jpg"; // See this repo's readme.md for info on how to get these images. Alternatively, you can just set the path to any appropriate image on your machine.
string remoteImageUrl = "https://github.com/Azure-Samples/cognitive-services-sample-data-files/raw/master/ComputerVision/Images/printed_text.jpg";
Console.WriteLine("Text being batch read ...");
await BatchReadFileFromStreamAsync(computerVision, localImagePath, numberOfCharsInOperationId);
await BatchReadFileFromUrlAsync(computerVision, remoteImageUrl, numberOfCharsInOperationId);
}
// Read text from a remote image
private static async Task BatchReadFileFromUrlAsync(ComputerVisionClient computerVision, string imageUrl, int numberOfCharsInOperationId)
{
if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute))
{
Console.WriteLine("\nInvalid remote image url:\n{0} \n", imageUrl);
return;
}
// Start the async process to read the text
BatchReadFileHeaders textHeaders = await computerVision.BatchReadFileAsync(imageUrl);
await GetTextAsync(computerVision, textHeaders.OperationLocation, numberOfCharsInOperationId);
}
// Recognize text from a local image
private static async Task BatchReadFileFromStreamAsync(ComputerVisionClient computerVision, string imagePath, int numberOfCharsInOperationId)
{
if (!File.Exists(imagePath))
{
Console.WriteLine("\nUnable to open or read local image path:\n{0} \n", imagePath);
return;
}
using (Stream imageStream = File.OpenRead(imagePath))
{
// Start the async process to recognize the text
BatchReadFileInStreamHeaders textHeaders = await computerVision.BatchReadFileInStreamAsync(imageStream);
await GetTextAsync(computerVision, textHeaders.OperationLocation, numberOfCharsInOperationId);
}
}
Here is the Full Code
Is there a way we can retrieve artefacts in date descending order?
I currently have below script here as an example:
import org.sonatype.nexus.repository.storage.Asset
import org.sonatype.nexus.repository.storage.Query
import org.sonatype.nexus.repository.storage.StorageFacet
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
def request = new JsonSlurper().parseText(args)
assert request.groupId: 'groupId parameter is required'
assert request.repoName: 'repoName parameter is required'
assert request.startDate: 'startDate parameter is required, format: yyyy-mm-dd'
log.info("Gathering Asset list for repository: ${request.repoName} as of startDate: ${request.startDate}")
def repo = repository.repositoryManager.get(request.repoName)
StorageFacet storageFacet = repo.facet(StorageFacet)
def tx = storageFacet.txSupplier().get()
tx.begin()
Iterable<Asset> assets = tx.
findAssets(Query.builder()
.where('group = ').param(request.groupId)
.and('last_updated > ').param(request.startDate)
.build(), [repo])
def urls = assets.collect { "/repository/${repo.name}/${it.name()}" }
tx.commit()
def result = JsonOutput.toJson([
assets : urls,
since : request.startDate,
repoName: request.repoName
])
return result
with:
Query.builder()
.where('group = ').param(request.groupId)
.and('last_updated > ').param(request.startDate)
.build()
def urls = assets.collect { "/repository/${repo.name}/${it.name()}" }
Is there a way we can change above script to retrieve things in date descending order?
You can simply add a suffix.
Query.builder()
.where('group = ').param(request.groupId)
.and('last_updated > ').param(request.startDate)
.suffix('order by last_updated desc')
.build()
def urls = assets.collect { "/repository/${repo.name}/${it.name()}" }
Nexus is using OrientDB behind the scene. You can find query examples here:
https://orientdb.com/docs/2.0/orientdb.wiki/Tutorial-SQL.html
I have an XML file that I am trying to parse through with Groovy.
<?xml version = '1.0' encoding = 'UTF-8'?>
<ServerList>
<Server>server1.me.com</Server>
<CleanUpTest>TESTING</CleanUpTest>
<Server>server2.me.com</Server>
</ServerList>
This code works and gives me the output Result: [server1.me.com]:
def serverList = new XmlSlurper().parse("E:\\Program Files(x86)\\Jenkins\\jobs\\first_servers.xml")
def output = []
serverList.Server.find { it == 'server1.me.com' }.each{
output.add(it)
}
return output
But when I try to get the value in CleanUpTest it is not working.
def serverList = new XmlSlurper().parse("E:\\Program Files(x86)\\Jenkins\\jobs\\first_servers.xml")
def output = []
serverList.Server.find { it == 'server1.me.com' }.CleanUpTest.each{
output.add(it)
}
return output
What do I have wrong? I am expecting Result: [TESTING]