本文整理汇总了Python中son_editor.app.database.db_session函数的典型用法代码示例。如果您正苦于以下问题:Python db_session函数的具体用法?Python db_session怎么用?Python db_session使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了db_session函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: clone
def clone(ws_id: int, url: str, name: str = None):
"""
Clones a repository by url into given workspace
:param name: Optional name of the local repository name, otherwise the remote name is taken
:param user_data: Session data to get access token for GitHub
:param ws_id: Destination workspace to clone
:param url: URL of the source repository
:return: True if successful, otherwise NameConflict is thrown
"""
workspace = get_workspace(ws_id)
url_decode = parse.urlparse(url)
if is_github(url_decode.netloc):
# Take the suffix of url as first name candidate
github_project_name = name
if github_project_name is None:
github_project_name = _repo_name_from_url(url_decode)
dbsession = db_session()
pj = dbsession.query(Project).join(Workspace)\
.filter(Workspace.id == workspace.id).filter(
Project.name == github_project_name).first()
dbsession.commit()
# Error when the project name in given workspace already exists
if pj is not None:
raise NameConflict('A project with name {} already exists'.format(github_project_name))
project_target_path = os.path.join(workspace.path, PROJECT_REL_PATH, github_project_name)
logger.info('Cloning from github repo...')
# If url in GitHub domain, access by token
url_with_token = _get_repo_url(url_decode)
out, err, exitcode = git_command(['clone', url_with_token, project_target_path])
if exitcode is 0:
setup_git_user_email(project_target_path)
# Check if the project is a valid son project
check_son_validity(project_target_path)
# Create project and scan it.
dbsession = db_session()
try:
pj = Project(github_project_name, github_project_name, workspace)
pj.repo_url = url
sync_project_descriptor(pj)
dbsession.add(pj)
scan_project_dir(project_target_path, pj)
dbsession.commit()
# Check if the project is valid
result = create_info_dict(out=out)
result["id"] = pj.id
return result
except:
dbsession.rollback()
shutil.rmtree(project_target_path)
raise Exception("Scan project failed")
else:
return create_info_dict(err=err, exitcode=exitcode)
raise NotImplemented("Cloning from other is not implemented yet. Only github is supported for now.")
示例2: update_platform
def update_platform(workspace_id: int, platform_id: int, platform_data) -> dict:
"""
Update the platform entry
:param workspace_id:
:param platform_id:
:return: The updated platform definition
"""
platform_name = shlex.quote(platform_data['name'])
platform_url = shlex.quote(platform_data['url'])
session = db_session()
workspace = session.query(Workspace).filter(Workspace.id == workspace_id).first()
if workspace is None:
raise NotFound("workspace with id {} could not be found".format(workspace_id))
platform = session.query(Platform). \
filter(Platform.workspace == workspace). \
filter(Platform.id == platform_id). \
first()
if platform is None:
raise NotFound("Platform with id {} could not be found".format(platform_id))
if platform_name != platform.name:
existing_platforms = session.query(Platform). \
filter(Platform.workspace == workspace). \
filter(Platform.name == platform_data['name']). \
all()
if len(existing_platforms) > 0:
raise NameConflict("Platform with name {} already exists".format(platform_data['name']))
platform.name = platform_name
platform.url = platform_url
update_workspace_descriptor(platform.workspace)
session.commit()
return platform.as_dict()
示例3: create_platform
def create_platform(workspace_id: int, platform_data) -> dict:
"""
Create a new platform entry
:param workspace_id: The workspace ID
:param platform_data: The platform info
:return: The newly created platform descriptor
"""
platform_name = shlex.quote(platform_data['name'])
platform_url = shlex.quote(platform_data['url'])
session = db_session()
workspace = session.query(Workspace).filter(Workspace.id == workspace_id).first()
if workspace is None:
raise NotFound("workspace with id {} could not be found".format(workspace_id))
existing_platforms = session.query(Platform). \
filter(Platform.workspace == workspace). \
filter(Platform.name == platform_data['name']). \
all()
if len(existing_platforms) > 0:
raise NameConflict("Platform with name {} already exists".format(platform_data['name']))
platform = Platform(name=platform_name, url=platform_url, workspace=workspace)
session.add(platform)
update_workspace_descriptor(platform.workspace)
session.commit()
return platform.as_dict()
示例4: query_private_nsfs
def query_private_nsfs(ws_id, vendor, name, version, is_vnf):
"""
Finds a function in the private catalogue
:param ws_id:
:param is_vnf:
:param vendor:
:param name:
:param version:
:return:
"""
session = db_session()
if is_vnf:
descriptor = session.query(PrivateFunction).filter(PrivateFunction.name == name and
PrivateFunction.vendor == vendor and
PrivateFunction.version == version and
PrivateFunction.workspace.id == ws_id and
PrivateFunction.workspace.owner == get_user(
session['user_data'])).first()
else:
descriptor = session.query(PrivateService).filter(
PrivateService.name == name and
PrivateService.vendor == vendor and
PrivateService.version == version and
PrivateFunction.workspace.id == ws_id and
PrivateFunction.workspace.owner == get_user(session['user_data'])).first()
return descriptor
示例5: create_catalogue
def create_catalogue(workspace_id: int, catalogue_data):
"""
Creates a catalgoue in the given workspace. A catalogue is defined by its name and url. These are given as
json data
:param workspace_id: Workspace ID of the target workspace, where the catalogue should get created.
:return: Catalogue descriptor
"""
catalogue_name = shlex.quote(catalogue_data['name'])
catalogue_url = shlex.quote(catalogue_data['url'])
session = db_session()
workspace = session.query(Workspace).filter(Workspace.id == workspace_id).first()
if workspace is None:
raise NotFound("workspace with id {} could not be found".format(workspace_id))
existing_catalogues = session.query(Catalogue). \
filter(Catalogue.workspace == workspace). \
filter(Catalogue.name == catalogue_data['name']). \
all()
if len(existing_catalogues) > 0:
raise NameConflict("catalogue with name {} already exists".format(catalogue_data['name']))
catalogue = Catalogue(name=catalogue_name, url=catalogue_url, workspace=workspace)
session.add(catalogue)
session.commit()
update_workspace_descriptor(catalogue.workspace)
return catalogue.as_dict()
示例6: delete_image_file
def delete_image_file(ws_id, project_id, vnf_id, filename):
"""
Deletes the image file with the given name
:param ws_id: The workspace ID
:param project_id: The project ID
:param vnf_id: The VNF ID
:param filename: The name of the file to delete
:return: A success message
:raises NotFound: if the image file could not be located
"""
session = db_session()
function = session.query(Function). \
join(Project). \
join(Workspace). \
filter(Workspace.id == ws_id). \
filter(Project.id == project_id). \
filter(Function.id == vnf_id).first()
if function:
save_name = secure_filename(filename)
if not save_name == get_file_name(function):
file_path = get_file_path("vnf", function).replace(get_file_name(function), save_name)
if os.path.exists(file_path):
os.remove(file_path)
return "File {} was deleted".format(save_name)
else:
raise NotFound("File {} not found!".format(save_name))
else:
raise NotFound("Function with id " + vnf_id + " does not exist")
示例7: update_catalogue
def update_catalogue(workspace_id, catalogue_id, catalogue_data):
"""
Updates a specific catalogue by its id. The catalogue
applies the given name and url, that are in the json parameter.
:param workspace_id: The Workspace ID
:param catalogue_id: The Catalogue ID
:return: The updated Catalogue descriptor
"""
catalogue_name = shlex.quote(catalogue_data['name'])
catalogue_url = shlex.quote(catalogue_data['url'])
session = db_session()
workspace = session.query(Workspace).filter(Workspace.id == workspace_id).first()
if workspace is None:
raise NotFound("workspace with id {} could not be found".format(workspace_id))
catalogue = session.query(Catalogue). \
filter(Catalogue.workspace == workspace). \
filter(Catalogue.id == catalogue_id). \
first()
if catalogue is None:
raise NotFound("catalogue with id {} could not be found".format(catalogue_id))
if catalogue_name != catalogue.name:
existing_catalogues = session.query(catalogue). \
filter(catalogue.workspace == workspace). \
filter(catalogue.name == catalogue_data['name']). \
all()
if len(existing_catalogues) > 0:
raise NameConflict("catalogue with name {} already exists".format(catalogue_data['name']))
catalogue.name = catalogue_name
catalogue.url = catalogue_url
session.commit()
update_workspace_descriptor(catalogue.workspace)
return catalogue.as_dict()
示例8: save_image_file
def save_image_file(ws_id, project_id, function_id, file):
"""
Saves the vnf image file into the vnfs folder
:param ws_id: The workspace ID
:param project_id: The project ID
:param function_id: The function ID
:param file: The image file
:return: A success message
"""
if file.filename == '':
raise InvalidArgument("No file attached!")
if file:
filename = secure_filename(file.filename)
session = db_session()
function = session.query(Function). \
join(Project). \
join(Workspace). \
filter(Workspace.id == ws_id). \
filter(Project.id == project_id). \
filter(Function.id == function_id).first()
if function is not None:
file_path = get_file_path("vnf", function)
file_path = file_path.replace(get_file_name(function), filename)
file.save(file_path)
return "File {} successfully uploaded!".format(filename)
else:
raise NotFound("Function with id " + function_id + " does not exist")
示例9: get_image_files
def get_image_files(ws_id, project_id, function_id):
"""
Returns a list of image file names located in the vnf folder
:param ws_id: The Workspace ID
:param project_id: The project ID
:param function_id: The function ID
:return: A List of image file names for this VNF
"""
session = db_session()
function = session.query(Function). \
join(Project). \
join(Workspace). \
filter(Workspace.id == ws_id). \
filter(Project.id == project_id). \
filter(Function.id == function_id).first()
if function:
folder_path = get_file_path("vnf", function).replace(get_file_name(function), "")
image_files = []
for filename in os.listdir(folder_path):
if not Path(os.path.join(folder_path, filename)).is_dir():
if not filename == get_file_name(function):
image_files.append(filename)
return image_files
else:
raise NotFound("Function with id " + function_id + " does not exist")
示例10: update_project
def update_project(project_data, project_id):
"""
Update the Project
:param project_data: The project Data
:param project_id: The project ID to update
:return: The updated project descriptor
"""
session = db_session()
project = session.query(Project).filter(Project.id == project_id).first()
if project is None:
raise NotFound("Project with id {} could not be found".format(project_id))
# Update name
if 'name' in project_data and project_data['name'] != project.name:
if os.path.exists(get_project_path(project.workspace.path, project.rel_path)):
new_name = shlex.quote(project_data['name'])
old_path = get_project_path(project.workspace.path, project.rel_path)
new_path = rreplace(old_path, project.name, new_name, 1)
if os.path.exists(new_path):
raise NameConflict("Invalid name parameter, workspace '{}' already exists".format(new_name))
# Do not allow move directories outside of the workspaces_dir
if not new_path.startswith(WORKSPACES_DIR):
raise Exception("Invalid path parameter, you are not allowed to break out of {}".format(WORKSPACES_DIR))
else:
# Move the directory
shutil.move(old_path, new_path)
project.name = new_name
project.rel_path = new_name
set_data(project, project_data)
sync_project_descriptor(project)
db_session.commit()
return project.as_dict()
示例11: delete_function
def delete_function(ws_id: int, project_id: int, function_id: int) -> dict:
"""
Deletes the function
:param ws_id:
:param project_id:
:param function_id:
:return: the deleted function
"""
session = db_session()
function = session.query(Function). \
join(Project). \
join(Workspace). \
filter(Workspace.id == ws_id). \
filter(Project.id == project_id). \
filter(Function.id == function_id).first()
if function is not None:
session.delete(function)
else:
session.rollback()
raise NotFound("Function with id " + function_id + " does not exist")
try:
os.remove(get_file_path("vnf", function))
except:
session.rollback()
logger.exception("Could not delete function:")
raise
session.commit()
return function.as_dict()
示例12: delete_service
def delete_service(project_id: int, service_id: int) -> dict:
"""
Deletes the service from the Database and from the disk
:param project_id: The Projects ID
:param service_id: The Services ID
:return: The descriptor of the deleted service
"""
session = db_session()
project = session.query(Project).filter(Project.id == project_id).first()
if project is None:
raise NotFound("Could not delete service: project with id {} not found".format(service_id))
service = session.query(Service). \
filter(Service.id == service_id). \
filter(Service.project == project). \
first()
if service is None:
raise NotFound("Could not delete service: service with id {} not found".format(service_id))
session.delete(service)
try:
os.remove(get_file_path("nsd", service))
except:
session.rollback()
logger.exception("Could not delete service:")
raise
session.commit()
return service.as_dict()
示例13: query_private_nsfs
def query_private_nsfs(ws_id, vendor, name, version, is_vnf):
"""
Finds a function in the private catalogue
:param ws_id: The workspace ID
:param is_vnf: if descriptor is a VNF
:param vendor: the descriptors vendor
:param name: the descriptors name
:param version: the descriptors version
:return: The requested descriptor if found, None if nothing found
"""
session = db_session()
if is_vnf:
descriptor = session.query(PrivateFunction).filter(PrivateFunction.name == name and
PrivateFunction.vendor == vendor and
PrivateFunction.version == version and
PrivateFunction.workspace.id == ws_id and
PrivateFunction.workspace.owner == get_user(
session['user_data'])).first()
else:
descriptor = session.query(PrivateService).filter(
PrivateService.name == name and
PrivateService.vendor == vendor and
PrivateService.version == version and
PrivateFunction.workspace.id == ws_id and
PrivateFunction.workspace.owner == get_user(session['user_data'])).first()
return descriptor
示例14: test_pack_project
def test_pack_project(self):
session = db_session()
project = session.query(Project).filter(Project.id == self.pjid).first()
package_path = publishutil.pack_project(project)
self.assertTrue(os.path.exists(package_path))
self.assertTrue(os.path.isfile(package_path))
# create another service in project
request_dict = get_sample_ns("servicename", "vendorname", "0.1")
response = self.app.post(
'/' + WORKSPACES + '/' + self.wsid + '/' + PROJECTS + '/' + str(self.pjid) + '/' + SERVICES + '/',
data=json.dumps(request_dict),
content_type='application/json')
# should fail because the project name is invalid
project = session.query(Project).filter(Project.id == self.pjid).first()
try:
publishutil.pack_project(project)
except Exception as err:
self.assertTrue(isinstance(err, PackException))
# should fail as only one service can be packaged
project = session.query(Project).filter(Project.id == self.pjid2).first()
try:
publishutil.pack_project(project)
except Exception as err:
self.assertTrue(isinstance(err, PackException))
session.commit()
示例15: create_workspace
def create_workspace(login: str, workspace_data: dict) -> dict:
"""
Creates a workspace (on disk and in the database) from the given workspace data
:param workspace_data: The workspace configuration data
:return: The created workspace
"""
wsName = shlex.quote(workspace_data["name"])
session = db_session()
# test if ws Name exists in database
user = get_user(login)
existingWorkspaces = list(session.query(Workspace)
.filter(Workspace.owner == user)
.filter(Workspace.name == wsName))
if len(existingWorkspaces) > 0:
raise NameConflict("Workspace with name " + wsName + " already exists")
wsPath = path.join(WORKSPACES_DIR, user.name, wsName)
# prepare db insert
try:
ws = Workspace(name=wsName, path=wsPath, owner=user)
session.add(ws)
if 'platforms' in workspace_data:
for platform in workspace_data['platforms']:
ptf = Platform(platform['name'], platform['url'], True, ws)
if 'token' in platform:
ptf.token_path = create_token_file(platform['token'])
session.add(ptf)
test_url(platform['name'], platform['url'] + "/api/v2/packages")
if 'catalogues' in workspace_data:
for catalogue in workspace_data['catalogues']:
session.add(Catalogue(catalogue['name'], catalogue['url'], True, ws))
test_url(catalogue['name'], catalogue['url'])
except Exception as e:
logger.exception(e)
session.rollback()
raise
# create workspace on disk
proc = Popen(['son-workspace', '--init', '--workspace', wsPath], stdout=PIPE, stderr=PIPE)
out, err = proc.communicate()
exitcode = proc.returncode
if out.decode().find('existing') >= 0:
workspace_exists = True
else:
workspace_exists = False
if exitcode == 0 and not workspace_exists:
update_workspace_descriptor(ws)
session.commit()
return ws.as_dict()
else:
session.rollback()
if workspace_exists:
raise NameConflict(out.decode())
raise Exception(err, out)