1+: Slightly different version of "Reapply naming" code snippet:
4. Simple code snippet, searches for snapshots with files that contains ".log" extension and deletes this snapshots and files. (simple cleanup script):
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
_sobject = Search.get_by_search_key("tvs/asset?project=aquariki&code=ASSET00035")
snapshot_search = Search('sthpw/snapshot')
snapshot_search.add_relationship_filters([_sobject], op='in')
snapshot_search.add_op_filters([('version', 'not in', [0, -1])])
snapshots_sobjects = snapshot_search.get_sobjects()
# for single snapshot
#snapshots_sobjects = Search.get_by_search_key("sthpw/snapshot?code=SNAPSHOT00007971")
#snapshots_sobjects = [snapshots_sobjects ]
return_xmls = []
for _snapshot in snapshots_sobjects:
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)
from pyasm.security import Sudo
sudo = Sudo()
try:
for file_path, dest_path in zip(file_paths, dest_paths):
if os.path.isfile(file_path):
if file_path != dest_path:
full_abs_path = os.path.dirname(dest_path)
if not os.path.isdir(full_abs_path):
os.makedirs(full_abs_path)
shutil.move(file_path, dest_path)
finally:
sudo.exit()
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)
return_xmls.append(xmls)
_snapshot.set_value('snapshot', xmls)
_snapshot.commit(triggers=False, log_transaction=False)
_snapshot.update_versionless('latest')
return json.dumps(return_xmls)
4. Simple code snippet, searches for snapshots with files that contains ".log" extension and deletes this snapshots and files. (simple cleanup script):
Code:
from pyasm.search import Search
from tactic.ui.tools import DeleteCmd
import json
server.set_project('PROJECT_CODE_HERE')
snapshots = Search.eval("@SOBJECT(sthpw/file['file_name', 'like', '%.log'].sthpw/snapshot)")
all = []
errors = []
for snapshot in snapshots:
all.append(snapshot.get_value('description'))
try:
cmd = DeleteCmd(sobject=snapshot, auto_discover=True)
cmd.execute()
except:
errors.append(snapshot.get_value('code'))
return json.dumps({'fine': all, 'failed': errors})