본문 바로가기

Language/python

jython - Protex SDK 예제 모음

Jython 2.7.1과 Protex SDK를 이용하여 간단한 예제를 구현해 보았다.

보고서를 만드는 프로그램을 만들어야 하는데 Java가 익숙지 않아 손에 익은 Python 문법 및 모듈을 사용하기 위하여 Jython을 선택하였다.


protex_server.py


# -*- coding: utf-8 -*-

import os
import sys
import protex_sdk

protex_sdk.load_protex_sdk(r"Z:\libs\Protex-7.8.2-SDK-V7.0\lib")

from com.blackducksoftware.sdk.protex.client.util import ProtexServerProxy
from com.blackducksoftware.sdk.protex.util import PageFilterFactory
from com.blackducksoftware.sdk.protex.project import ProjectColumn
from com.blackducksoftware.sdk.protex.project import ProjectPageFilter

class ProtexServer:
	def __init__(self, server, username, userpass):
		self.__server = server
		self.__username = username
		self.__userpass = userpass
		self.__myServer = None
		
	def connect(self):
		self.__myServer = ProtexServerProxy(self.__server, self.__username, self.__userpass)
		self.__projectApi = self.__myServer.getProjectApi()
		self.__bom_api = self.__myServer.getBomApi()
	
	def get_bom_api(self):
		return self.__bom_api
	
	def get_project_list(self):
		pageFilter = PageFilterFactory.getAllRows(ProjectColumn.PROJECT_NAME)
		projects = self.__projectApi.getProjects(pageFilter)
		
		py_projects = []
		
		for project in projects:
			t = {}
			t["name"] = project.getName()
			t["id"] = project.getProjectId()
			t["license_id"] = project.getLicenseId()			
			py_projects.append(t)
		
		return py_projects



protex_bom.py

  • BOM 결과를 Python List로 반환하는 클래스


# -*- coding: utf-8 -*-

import os
import sys
import protex_sdk

protex_sdk.load_protex_sdk(r"Z:\libs\Protex-7.8.2-SDK-V7.0\lib")

from com.blackducksoftware.sdk.fault import SdkFault;
from com.blackducksoftware.sdk.protex.common import ApprovalState;
from com.blackducksoftware.sdk.protex.common import UsageLevel;
from com.blackducksoftware.sdk.protex.project.bom import BomApi;
from com.blackducksoftware.sdk.protex.project.bom import BomComponent;

class BomReader:
	def __init__(self, bom_api):
		self.__bom_api = bom_api
		
	def get_bom(self, project_id):
		bom_list = []
		
		bomComponents = self.__bom_api.getBomComponents(project_id)
		
		for component in bomComponents:
			c = {}
			
			c["component_name"] = component.getComponentName()
			component_version = component.getVersionName()
			if component_version == None:
				c["version_name"] = "Unknown"
			else:
				c["version_name"] = component_version
			
			c["component_type"] = component.getComponentType()
			c["license_info"] = component.getLicenseInfo().getName()
			
			if component.isHasDeclaredLicenseConflict() == True:
				c["declared_conflict"] = "Declared Conflict"
			elif component.isHasDeclaredLicenseConflict() == True:
				c["component_conflict"] = "Component Conflict"
			else:
				c["declared_conflict"] = "None"
				c["component_conflict"] = "None"
			
			c["search_count"] = component.getFileCounts().getStringSearches()
			
			bom_list.append(c)
			
		return bom_list
	



protex_sdk.py

  • protex sdk의 *.jar 파일을 Run-time으로 로드하기 위함
  • CLASSPATH에도 넣어보고 했는데 해결이 안되어 만든 함수
# -*- coding: utf-8 -*-

import os
import sys

def load_protex_sdk(lib_path):
	jar_files = os.listdir(lib_path)
	
	for jar_file in jar_files:
		sys.path.append(os.path.join(lib_path, jar_file))



query_project_list.py

  • 서버에 접속하여 BOM을 excel로 저장하는 간단한 예제


# -*- coding: utf-8 -*-

import os
import sys
import openpyxl
import protex_sdk
import protex_server
import protex_bom

def print_protex_project_list(project_list):
	for project in project_list:
		print("%s (%s / %s)" % (project["name"], project["id"], project["license_id"]))
		
def print_protex_bom_list(bom_list):
	for bom in bom_list:
		print("%s (%s / %s)" % (bom["component_name"], bom["version_name"], bom["license_info"]))
		
def save_excel(bom_list):
	wbk = openpyxl.Workbook()
	ws = wbk.create_sheet(title="bom list")

	ws.cell(row=1, column=1, value="Component Name")
	ws.cell(row=1, column=2, value="Version Name")
	ws.cell(row=1, column=3, value="License")
	
	i = 2
	for bom in bom_list:
		ws.cell(row=i, column=1, value=bom["component_name"])
		ws.cell(row=i, column=2, value=bom["version_name"])
		ws.cell(row=i, column=3, value=bom["license_info"])
		i = i + 1
	
	wbk.save("bom_result.xlsx")
	

if __name__ == "__main__":
	protex_sdk.load_protex_sdk(r"Z:\libs\Protex-7.8.2-SDK-V7.0\lib")
	protex_server = protex_server.ProtexServer("http://OOO.kr", "OOO", "OOO")
	
	protex_server.connect()	

	bom_reader = protex_bom.BomReader(protex_server.get_bom_api())	
	bom_list = bom_reader.get_bom("OOO")	
	save_excel(bom_list)