07-22-2020, 02:36 PM
Hi All!
I decided to do a thread where i can post some code that may be useful for someone who uses Tactic.
1. Useful piece of code if you want rename snapshot's files, preserving all comments and dates, but change files location physically, also for update Naming if it's changed:
I decided to do a thread where i can post some code that may be useful for someone who uses Tactic.
1. Useful piece of code if you want rename snapshot's files, preserving all comments and dates, but change files location physically, also for update Naming if it's changed:
Code:
import json
from pyasm.biz import Project
from pyasm.biz import Snapshot
from pyasm.search import Search, SearchType
from pyasm.checkin import FileAppendCheckin
import shutil
import os
server.set_project('PROJECT_CODE_HERE')
# Replace files depending on their UPDATED naming
# Here goes routine for stanpshots you need to "rebase"
_snapshot = Search.get_by_search_key("sthpw/snapshot?code=SNAPSHOT00006653")
file_objects = _snapshot.get_all_file_objects()
parent_sobject = _snapshot.get_parent()
snapshot_type = _snapshot.get_value('snapshot_type')
snapshot_context = _snapshot.get_value('context')
description = _snapshot.get_description()
version = _snapshot.get_version()
snapshot = Snapshot.create(parent_sobject, snapshot_type=snapshot_type, context=snapshot_context, description=description,
is_revision=is_revision, commit=False, version=version)
file_naming = Project.get_file_naming()
file_naming.set_sobject(parent_sobject)
file_naming.set_snapshot(snapshot)
dir_naming = Project.get_dir_naming()
dir_naming.set_sobject(parent_sobject)
dir_naming.set_snapshot(snapshot)
file_paths = []
dest_paths = []
file_types = []
for file_object in file_objects:
new_file_object = SearchType.create("sthpw/file")
new_file_object.set_value("type", file_object.get_value('type'))
new_file_object.set_value("metadata", file_object.get_value('metadata'))
dir_naming.set_file_object(new_file_object)
file_naming.set_file_object(new_file_object)
file_naming.set_ext(file_object.get_extension(file_object.get_file_name()))
new_file_object.set_value("file_name", file_naming.get_file_name())
file_paths.append(u'{0}/{1}'.format(file_object.get_value('checkin_dir'), file_object.get_file_name()).replace("//", "/"))
file_types.append(file_object.get_value('type'))
dest_paths.append(u'{0}/{1}'.format(dir_naming.get_lib_dir(), file_naming.get_file_name()).replace("//", "/"))
file_object.set_value("file_name", u'{0}'.format(file_naming.get_file_name()))
file_object.set_value("checkin_dir", dir_naming.get_lib_dir())
file_object.set_value("relative_dir", snapshot.get_dir('relative', file_object.get_value('type'), new_file_object))
file_object.commit(triggers=False, log_transaction=False)
for file_path, dest_path in zip(file_paths, dest_paths):
if os.path.isfile(file_path):
if file_path != dest_path:
shutil.move(file_path, dest_path)
checkin = FileAppendCheckin(_snapshot.get_code(), dest_paths, file_types,
keep_file_name=False, mode='move', source_paths=file_paths,
checkin_type='auto', do_update_versionless=False)
checkin.append_snapshot = snapshot
xmls = checkin.create_snapshot_xml(file_objects)
_snapshot.set_value('snapshot', xmls)
_snapshot.commit(triggers=False, log_transaction=False)
_snapshot.update_versionless('latest')
return json.dumps(xmls)