PhoenixMock  1.8.7
Tools to split/merge/print mock used in Phoenix
createReleaseCurl.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # coding: utf-8
3 
4 import sys
5 import os
6 import subprocess
7 import json
8 import glob
9 import argparse
10 
11 server="gitlab.in2p3.fr"
12 apiProject="/api/v4/projects/"
13 
14 
15 def getListArchiveFile(inputDirectory, projectName):
16  '''
17  Get the list of the archives file for a given project name
18  Parameters:
19  inputDirectory : directory (for one OS) where all the binary packages are created
20  projectName : name of the project
21  Return:
22  list of corresponding binary packages
23  '''
24  listPackage = glob.glob(inputDirectory + "/*/" + projectName + "-*" )
25  print(listPackage)
26  return listPackage
27 
28 
29 #curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" --form "file=@dk.png" "https://gitlab.example.com/api/v4/projects/5/uploads"
30 def uploadFileCommand(projectIdOwn, fileName, token):
31  '''
32  Upload a file in the given gitlab project
33  Parameters:
34  projectIdOwn : id of the project
35  fileName : name of the file to be uploaded
36  token : token to be used to create the release
37  Return:
38  url of the uploaded file
39  '''
40  print("Upload file",fileName)
41  URL = '"https://'+server+apiProject+str(projectIdOwn)+'/uploads"'
42  privateToken='--header "PRIVATE-TOKEN: '+token+'" '
43  fileCmd = '--form "file=@'+fileName+'" '
44  command = "curl --insecure --request POST " + privateToken + fileCmd + URL
45  #print("Updload file command :", command)
46  output = subprocess.getoutput(command)
47  print("Upload file output :",output)
48  outputDico = json.loads('{' + output.split("{")[1])
49  outputFile = outputDico["full_path"]
50  outputUrl = outputDico["full_path"]
51  print("Upload file output file ",outputFile, ", url :",outputUrl)
52  return outputFile, outputUrl
53 
54 
55 
56 #{ "name": "hoge", "url": "https://google.com", "filepath": "/binaries/linux-amd64", "link_type":"other" }
57 
58 def getListArchiveLinks(projectIdOwn, listPackage, useComma, token):
59  '''
60  Create the list of link to the archive uploaded binary packages
61  Parameters:
62  projectIdOwn : id of the project to be used
63  listPackage : list of the binary packages to be uploaded and linked
64  useComma : True to add a comma in the first place, false otherwise
65  token : token to be used to create the release
66  Return:
67  string of the list of archive links corresponding to the given list of packages
68  '''
69  linksData = ""
70  for packageName in listPackage:
71  if useComma:
72  linksData += ", "
73  baseName = os.path.basename(packageName)
74  packageOsName = packageName.split('/')[-2]
75  nameData = "\"name\": \"" + packageOsName + " " + baseName+"\""
76 
77  uploadedFullPath, outputUrl = uploadFileCommand(projectIdOwn, packageName, token)
78  print("outputUrl =",outputUrl)
79  fullPathToFile = 'https://'+server+"/"+uploadedFullPath
80  #filePathData = '"filepath": "'+fullPathToFile+'"'
81  linkType = '"link_type": "other" '
82  urlData = '"url": "'+fullPathToFile+'"'
83 
84  addDataLink = '{ '+nameData + ", " + urlData + ", " + linkType +'}'
85  print("addDataLink =",addDataLink)
86  linksData += addDataLink
87  useComma = True
88 
89  return linksData
90 
91 
92 def createReleaseCurlCommand(projectIdOwn, projectName, projectTagName, basePackageDir, token):
93  '''
94  Create a release of the given project
95  Parameters:
96  projectIdOwn : id of the project on gitlab
97  projectName : name of the project
98  projectTagName : tag name of the project
99  basePackageDir : base directory wher eto find binary packages
100  token : token to be used to create the release
101  '''
102  postURL="--request POST https://"+server+apiProject+str(projectIdOwn)+"/releases"
103  header="--header 'Content-Type: application/json' "
104  privateToken='--header "PRIVATE-TOKEN: '+token+'" '
105  versionName="version " + projectTagName
106  dataStr = "--data '{ \"name\": \"Release "+versionName+"\"," + "\"tag_name\": \""+projectTagName+"\","
107  dataStr += "\"description\": \"Automatic release, "+versionName+"\""
108 
109  useComma = False
110  linksData = ", \"assets\": { \"links\": ["
111  listArchiveFile = getListArchiveFile(basePackageDir + "/", projectName)
112  linksData += getListArchiveLinks(projectIdOwn, listArchiveFile, useComma, token)
113  linksData += "] } "
114  dataStr += linksData
115  dataStr += "}' "
116  command = "curl --insecure " + header + privateToken + dataStr + postURL
117  print("Create Release command :",command)
118  output = subprocess.getoutput(command)
119  print("Release Output :",output)
120 
121 
122 if __name__ == "__main__":
123  parser = argparse.ArgumentParser(description="Program which creates release with given tag")
124  parser.add_argument('-n', '--projectname', help="Name of the current projet", required=True, type=str)
125  parser.add_argument('-i', '--projectid', help="Id of the current projet", required=True, type=int)
126  parser.add_argument('-t', '--tag', help="Tag to be used to create the current release", required=True, type=str)
127  parser.add_argument('-p', '--token', help="Token to be used to create the release", required=True, type=str)
128  parser.add_argument('-d', '--packagedirectory', help="Directory where to find packages", required=False, type=str, default="./")
129  args = parser.parse_args()
130 
131  createReleaseCurlCommand(args.projectid, args.projectname, args.tag, args.packagedirectory, args.token)
132 
133 
134 
135 
createReleaseCurl.getListArchiveFile
def getListArchiveFile(inputDirectory, projectName)
Definition: createReleaseCurl.py:15
createReleaseCurl.uploadFileCommand
def uploadFileCommand(projectIdOwn, fileName, token)
Definition: createReleaseCurl.py:30
createReleaseCurl.str
str
Definition: createReleaseCurl.py:128
createReleaseCurl.createReleaseCurlCommand
def createReleaseCurlCommand(projectIdOwn, projectName, projectTagName, basePackageDir, token)
Definition: createReleaseCurl.py:92
createReleaseCurl.getListArchiveLinks
def getListArchiveLinks(projectIdOwn, listPackage, useComma, token)
Definition: createReleaseCurl.py:58