def execute(my): from pyasm.common import ZipUtil ziputil = ZipUtil() paths = my.kwargs.get("paths") upload_dir = Environment.get_upload_dir() template_dir = Environment.get_template_dir() for path in paths: path = path.replace("\\", "/") basename = os.path.basename(path) upload_path = "%s/%s" % (upload_dir, basename) if not upload_path.endswith(".zip"): continue print "upload: ", upload_path if not os.path.exists(upload_path): continue print "template_dir: ", template_dir shutil.move(upload_path, template_dir) to_path = "%s/%s" % (template_dir, basename) # unzip the file ziputil.extract(to_path)
def execute(self): from pyasm.common import ZipUtil ziputil = ZipUtil() paths = self.kwargs.get("paths") upload_dir = Environment.get_upload_dir() template_dir = Environment.get_template_dir() for path in paths: path = path.replace("\\", "/") basename = os.path.basename(path) upload_path = "%s/%s" % (upload_dir, basename) if not upload_path.endswith(".zip"): continue print "upload: ", upload_path if not os.path.exists(upload_path): continue print "template_dir: ", template_dir shutil.move(upload_path, template_dir) to_path = "%s/%s" % (template_dir, basename) # unzip the file ziputil.extract(to_path)
def handle_encrypted(my, base_dir, transaction_code, encrypted): key = my.ticket from_path = "%s/%s" % (base_dir, encrypted) tmp_dir = Environment.get_tmp_dir(include_ticket=True) if encrypted.endswith(".enc"): to_path = "%s/%s" % (tmp_dir, encrypted) to_path = to_path.replace(".enc", "") encrypt_util = EncryptUtil(key) encrypt_util.decrypt_file(from_path, to_path) zip_util = ZipUtil() to_dir = os.path.dirname(to_path) zip_util.extract(to_path) else: to_path = from_path to_dir = tmp_dir zip_util = ZipUtil() zip_util.extract(to_path, to_dir) dirname = encrypted.replace(".enc", "") dirname = dirname.replace(".zip", "") print "Running transaction: [%s]" % transaction_code my.handle_transaction(to_dir, transaction_code, dirname)
def handle_path(my, src_path): src_path = src_path.replace("\\", "/") # upload folder basename = os.path.basename(src_path) if my.mode =='copy': target_path = src_path target_dir = os.path.dirname(target_path) else: target_dir = Environment.get_upload_dir() target_path = "%s/%s" % (target_dir, basename) base_dir = Environment.get_template_dir() template_dir = "%s/%s" % (base_dir, my.project_code) if os.path.exists(template_dir): shutil.rmtree(template_dir) #raise TacticException("Template is already installed at [%s]" %template_dir) # unzip the file from pyasm.common import ZipUtil # this is fixed for windows if zipping doesn't use compression paths = ZipUtil.extract(target_path) # veryify that the paths extracted are the expected ones rootname, ext = os.path.splitext(basename) # check if it unzips at the templates folder directly unzip_at_template_dir = False # move the plugin zip file to the appropriate folder if my.mode == 'copy': # if they manually drop the zip file already here, skip if target_dir != base_dir: shutil.copy(target_path, base_dir) else: unzip_at_template_dir = True else: shutil.move(target_path, base_dir) # move unzipped files into the plugin area # remove any version info, only allow 1 particular version installed for now import re rootname = re.sub('(.*)(-)(\d.*)', r'\1', rootname) unzip_path = "%s/%s" % (target_dir, rootname) dest_dir = '%s/%s'%(base_dir, rootname) if not unzip_at_template_dir and os.path.exists(dest_dir): shutil.rmtree(dest_dir) shutil.move(unzip_path, dest_dir)
def handle_path(my, src_path): src_path = src_path.replace("\\", "/") # upload folder basename = os.path.basename(src_path) if my.mode == 'copy': target_path = src_path target_dir = os.path.dirname(target_path) else: target_dir = Environment.get_upload_dir() target_path = "%s/%s" % (target_dir, basename) base_dir = Environment.get_template_dir() template_dir = "%s/%s" % (base_dir, my.project_code) if os.path.exists(template_dir): shutil.rmtree(template_dir) #raise TacticException("Template is already installed at [%s]" %template_dir) # unzip the file from pyasm.common import ZipUtil # this is fixed for windows if zipping doesn't use compression paths = ZipUtil.extract(target_path) # veryify that the paths extracted are the expected ones rootname, ext = os.path.splitext(basename) # check if it unzips at the templates folder directly unzip_at_template_dir = False # move the plugin zip file to the appropriate folder if my.mode == 'copy': # if they manually drop the zip file already here, skip if target_dir != base_dir: shutil.copy(target_path, base_dir) else: unzip_at_template_dir = True else: shutil.move(target_path, base_dir) # move unzipped files into the plugin area # remove any version info, only allow 1 particular version installed for now import re rootname = re.sub('(.*)(-)(\d.*)', r'\1', rootname) unzip_path = "%s/%s" % (target_dir, rootname) dest_dir = '%s/%s' % (base_dir, rootname) if not unzip_at_template_dir and os.path.exists(dest_dir): shutil.rmtree(dest_dir) shutil.move(unzip_path, dest_dir)
def execute(self): import os path = self.kwargs.get("path") path = path.replace("\\", "/") basename = os.path.basename(path) upload_dir = Environment.get_upload_dir() path = "%s/%s" % (upload_dir, basename) paths = ZipUtil.extract(path) # TODO: why do we need to read the manifest here? # ... should be automatic manifest_path = "%s/transaction_log/manifest.xml" % upload_dir if not os.path.exists(manifest_path): raise TacticException("Cannot find manifest file [%s]" % manifest_path) f = codecs.open(manifest_path, 'r', 'utf-8') manifest_xml = f.read() f.close() creator = PluginInstaller(base_dir=upload_dir, manifest=manifest_xml) creator.execute() # run the transactions logs = creator.get_jobs() for log in logs: transaction_xml = log.get_value("transaction") cmd = RunTransactionCmd(transaction_xml=transaction_xml) cmd.execute() # This is needed here, because normaly, RunTransactionCmd # is run by a sync, so it blocks further syncs. When # a transaction session is installed, we need to propogate # this to the other machines cmd = TransactionQueueAppendCmd() input = { 'search_type': 'sthpw/transaction_log', 'sobject': log } cmd.input = input cmd.execute()
def execute(my): force = my.kwargs.get("force") clean = my.kwargs.get("clean") if clean in [False, 'false']: clean = False # ensure that plugin dir is empty if os.path.exists(my.plugin_dir): if clean: if force in ['true', True]: shutil.rmtree(my.plugin_dir) else: raise Exception("Plugin is already located at [%s]" % my.plugin_dir) if not os.path.exists(my.plugin_dir): os.makedirs(my.plugin_dir) # build the information from the manifest code = my.kwargs.get("code") if not code: code = my.xml.get_value("manifest/@code") version = my.kwargs.get("version") if not version: version = my.xml.get_value("manifest/@version") nodes = my.xml.get_nodes("manifest/*") # clean out all of the files my.delete_files(nodes) has_info = False sobjects = [] # handle all of the nodes in the manifest for i, node in enumerate(nodes): name = my.xml.get_node_name(node) if name == 'sobject': dumped_sobjects = my.handle_sobject(node) if not dumped_sobjects: dumped_sobjects = [] sobjects.extend(dumped_sobjects) elif name == 'search_type': my.handle_search_type(node) elif name == 'include': my.handle_include(node) elif name == 'python': my.handle_python(node) # make sure there is a data node and handle it data_node = my.xml.get_node("manifest/data") if data_node is None: root_node = my.xml.get_root_node() data_node = my.xml.create_element("data") child = my.xml.get_first_child(root_node) if child is None: my.xml.append_child(root_node, data_node) else: my.xml.insert_before(data_node, child) my.handle_data(data_node) manifest_path = "%s/manifest.xml" % (my.plugin_dir) file = codecs.getwriter('utf8')(open(manifest_path, 'wb')) file.write(my.xml.to_string()) file.close() # FIXME: leaving this out for now #my.handle_snapshots() dist_dir = my.kwargs.get("dist_dir") if not dist_dir: dist_dir = Environment.get_dist_dir() # get the basename of the code basecode = os.path.basename(my.code) # zip up the contents import zipfile if version: zip_path = "%s/%s-%s.zip" % (dist_dir, basecode, version) else: zip_path = "%s/%s.zip" % (dist_dir, basecode) print "Zipping up plugin file [%s]" % zip_path print " from [%s]" % my.plugin_dir from pyasm.common import ZipUtil ignore_dirs = ['.svn'] # ignore file #ignore_path = "%s/.ignore" % (my.plugin_dir) parts = my.code.split("/") root_dir = "%s/%s" % (my.base_dir, parts[0]) if len(parts) >= 2: include_dirs = ["/".join(parts[1:])] else: include_dirs = None ZipUtil.zip_dir(root_dir, zip_path, ignore_dirs=ignore_dirs, include_dirs=include_dirs) print "... done" #f = codecs.open(zip_path, 'w') #zip = zipfile.ZipFile(f, 'w', compression=zipfile.ZIP_DEFLATED) #my.zip_dir(zip, my.plugin_dir, "asset", rel_dir='') my.zip_path = zip_path # encrypt the file """ print "encrypting!!!!", zip_path my.enc_path = "%s.enc" % zip_path from pyasm.common import EncryptUtil ticket = "OMG" encrypt = EncryptUtil(ticket) encrypt.encrypt_file(zip_path) """ # register the plugin into the current project if my.kwargs.get("register") in [True, 'true']: # first check if a plugin with this code already exists plugin = Search.get_by_code("config/plugin", my.code) if plugin: raise TacticException("Plugin [%s] already existed in the project." % my.code) # create a new one plugin = SearchType.create("config/plugin") plugin.set_value("code", my.code) # update the information if version: plugin.set_value("version", version) # NOTE: not sure if this is desirable plugin.set_value("manifest", my.manifest) if my.plugin_dir.startswith(my.base_dir): rel_dir = my.plugin_dir.replace(my.base_dir, "") rel_dir = rel_dir.lstrip("/") plugin.set_value("rel_dir", rel_dir) plugin.commit() # record all of the sobject exported if plugin.get_value("type", no_exception=True) == "config": for sobject in sobjects: plugin_content = SearchType.create("config/plugin_content") plugin_content.set_value("search_type", sobject.get_search_type()) plugin_content.set_value("search_code", sobject.get_code()) plugin_content.set_value("plugin_code", my.code) plugin_content.commit()
def __init__(my, **kwargs): super(PluginBase,my).__init__(**kwargs) # plugin sobject (Not really used anymore?) my.search_key = my.kwargs.get("search_key") zip_path = my.kwargs.get("zip_path") upload_file_name = my.kwargs.get("upload_file_name") my.base_dir = my.kwargs.get("base_dir") my.plugin_dir = my.kwargs.get("plugin_dir") my.manifest = my.kwargs.get("manifest") my.code = my.kwargs.get("code") my.version = my.kwargs.get("version") relative_dir = my.kwargs.get("relative_dir") my.verbose = my.kwargs.get("verbose") not in [False, 'false'] # at the end of this, the following variables are needed in order to # define the plugin # # version: the version of the plugin # plugin_dir: the directory where the plugin definition is located # manifest: the description of what is in the plugin if zip_path: # assume the zip path is the same as the basename basename = os.path.basename(zip_path) basename, ext = os.path.splitext(basename) assert ext == '.zip' tmp_dir = Environment.get_tmp_dir() unzip_dir = "%s/%s" % (tmp_dir, basename) if os.path.exists(unzip_dir): shutil.rmtree(unzip_dir) # unzip the file in to the tmp_dir or plugin_dir (for install) zip_util = ZipUtil() zip_util.extract(zip_path, base_dir=tmp_dir) # assume zip path my.plugin_dir, ext = os.path.splitext(zip_path) # mv from temp if my.plugin_dir != unzip_dir: if os.path.exists(my.plugin_dir): shutil.rmtree(my.plugin_dir) shutil.move(unzip_dir, my.plugin_dir) manifest_path = "%s/manifest.xml" % my.plugin_dir f = open(manifest_path, 'r') my.manifest = f.read() f.close() elif upload_file_name: # The path is moved to the plugin dir, if this process is taking # "local" file (such as one uploaded) upload_dir = Environment.get_upload_dir() upload_path = "%s/%s" % (upload_dir, upload_file_name) plugin_base_dir = Environment.get_plugin_dir() dist_dir = Environment.get_dist_dir() if not os.path.exists(dist_dir): os.makedirs(dist_dir) basename = os.path.basename(upload_path) #if os.path.exists("%s/%s" % (plugin_base_dir, basename)): # os.unlink("%s/%s" % (plugin_base_dir, basename) ) #shutil.move(upload_path, plugin_base_dir) # copy to dist folder if os.path.exists("%s/%s" % (dist_dir, basename)): os.unlink("%s/%s" % (dist_dir, basename) ) shutil.move(upload_path, dist_dir) zip_path = "%s/%s" % (dist_dir, upload_file_name) zip_util = ZipUtil() zip_util.extract(zip_path, base_dir=plugin_base_dir) my.plugin_dir = "%s/%s" % (plugin_base_dir, basename) my.plugin_dir = my.plugin_dir[:-4] manifest_path = "%s/manifest.xml" % (my.plugin_dir) if os.path.exists(manifest_path): f = open(manifest_path, 'r') my.manifest = f.read() f.close() else: # when uploading, this will likely not be needed my.manifest = "<manifest/>" return elif relative_dir: plugin_base_dir = Environment.get_plugin_dir() my.plugin_dir = "%s/%s" % (plugin_base_dir, relative_dir) manifest_path = "%s/manifest.xml" % my.plugin_dir if not os.path.exists(manifest_path): plugin_base_dir = Environment.get_builtin_plugin_dir() my.plugin_dir = "%s/%s" % (plugin_base_dir, relative_dir) manifest_path = "%s/manifest.xml" % my.plugin_dir f = open(manifest_path, 'r') my.manifest = f.read() f.close() elif my.plugin_dir: manifest_path = "%s/manifest.xml" % (my.plugin_dir) f = open(manifest_path, 'r') my.manifest = f.read() f.close() # get the plugin sobject elif my.search_key: plugin = SearchKey.get_by_search_key(my.search_key) my.manifest = plugin.get_value("manifest") my.code = plugin.get_code() my.version = plugin.get_value("version") elif my.manifest: # everything is extracted from the manifest later pass elif my.code: search = Search("config/plugin") search.add_filter("code", my.code) plugin = search.get_sobject() # In case there is extra plugins folder which is the case when the user # is developing. relative_dir = plugin.get_value("rel_dir") plugin_base_dir = Environment.get_plugin_dir() my.plugin_dir = "%s/%s" % (plugin_base_dir, relative_dir) # TODO: fix the ZipUtil.zip_dir() manifest_path = "%s/manifest.xml" % my.plugin_dir if not os.path.exists(manifest_path): plugin_base_dir = Environment.get_builtin_plugin_dir() my.plugin_dir = "%s/%s" % (plugin_base_dir, relative_dir) manifest_path = "%s/manifest.xml" % my.plugin_dir if os.path.exists(manifest_path): f = open(manifest_path, 'r') my.manifest = f.read() f.close() else: # this condition happens likely for a versioned installed plugin from a zip file # where it starts with an extra folder "plugins" and the rel_dir has not been recorded properly my.manifest = plugin.get_value("manifest") my.code = plugin.get_code() my.version = plugin.get_value("version") else: raise Exception("No plugin found") # assertions assert my.manifest # read the xml my.xml = Xml() my.xml.read_string(my.manifest) # if code is passed in, then use that. if not my.code: my.code = my.xml.get_value("manifest/data/code") # old implementation if not my.code: my.code = my.xml.get_value("manifest/@code") if not my.version: my.version = my.xml.get_value("manifest/data/version") assert my.code if not my.base_dir: if my.code.startswith("TACTIC"): my.base_dir = Environment.get_builtin_plugin_dir() else: my.base_dir = Environment.get_plugin_dir() # set the base directory for this particular plugin if not my.plugin_dir: if my.version: my.plugin_dir = "%s/%s-%s" % (my.base_dir, my.code, my.version) else: my.plugin_dir = "%s/%s" % (my.base_dir, my.code)
def handle_file_mode(self, base_dir, transaction_code, paths, log, transaction_xml, ticket): # drop the transaction into a folder timestamp = log.get_value("timestamp") timestamp = parser.parse(timestamp) timestamp = timestamp.strftime("%Y%m%d_%H%M%S") asset_dir = Environment.get_asset_dir() # create the transactions dir if it does not exist if not os.path.exists("%s/transactions" % base_dir): os.makedirs("%s/transactions" % base_dir) base_dir = "%s/transactions/%s" % (base_dir, transaction_code) is_encrypted = True if is_encrypted == True: # put the transaction in a temp folder tmp_dir = Environment.get_tmp_dir(include_ticket=True) tmp_dir = "%s/%s-%s" % (tmp_dir, transaction_code, timestamp) else: tmp_dir = "%s/%s" % (base_dir, timestamp) if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) from pyasm.common import EncryptUtil encrypt = EncryptUtil(ticket) # create a simple manifest file f = open("%s/manifest.xml" % tmp_dir, 'wb') f.write('''<manifest code='transaction_log' version='1'>\n''') f.write(''' <sobject search_type="sthpw/transaction_log"/>\n''') f.write('''</manifest>\n''') f.close() tpath = "%s/sthpw_transaction_log.spt" % tmp_dir from pyasm.search import TableDataDumper dumper = TableDataDumper() dumper.set_delimiter("#-- Start Entry --#", "#-- End Entry --#") dumper.set_include_id(False) dumper.set_sobjects([log]) dumper.dump_tactic_inserts(tpath, mode='sobject') tpath = "%s/_transaction.xml" % tmp_dir #f = open(tpath, 'wb') f = codecs.getwriter('utf8')(open(tpath, 'wb')) f.write(transaction_xml.to_string()) f.close() # copy the checked in files for path in paths: rel_path = path.replace(asset_dir, "") rel_path = rel_path.lstrip("/") to_path = "%s/%s" % (tmp_dir, rel_path) to_dir = os.path.dirname(to_path) if not os.path.exists(to_dir): os.makedirs(to_dir) shutil.copy(path, to_dir) # zip up and encrypt the transaction if is_encrypted: zip_path = "%s.zip" % (tmp_dir) from pyasm.common import ZipUtil zip = ZipUtil() zip.zip_dir("%s" % (tmp_dir), zip_path) encrypt.encrypt_file(zip_path) shutil.move("%s.enc" % zip_path, "%s-%s.zip.enc" % (base_dir, timestamp)) rmdir = os.path.dirname(tmp_dir) shutil.rmtree(rmdir) #os.unlink("%s.zip" % tmp_dir) job = self.kwargs.get("job") job.set_value("error_log", "") job.commit() return
def execute(my): # This will go to a registered remote server and get the project #server_code = my.kwargs.get("server") #assert server_code ## get the registered server #server = Search.get_by_code("sthpw/sync_server", server_code) #assert server project_code = my.kwargs.get("project_code") assert project_code version = my.kwargs.get("version") if not version: version = "1.0.0" # build the project names template_name = "%s-%s.zip" % (project_code, version) data_name = "%s-data-%s.zip" % (project_code, version) file_name = "%s-files-%s.zip" % (project_code, version) tmp_dir = Environment.get_tmp_dir() to_dir = tmp_dir to_template_path = "%s/%s-%s.zip" % (to_dir, project_code, version) to_data_path = "%s/%s-data-%s.zip" % (to_dir, project_code, version) to_file_path = "%s/%s-files-%s.zip" % (to_dir, project_code, version) #sync_mode = server.get_value("sync_mode") sync_mode = "file" if sync_mode == "file": base_dir = my.kwargs.get("base_dir") from_template_path = "%s/%s" % (base_dir, template_name) from_data_path = "%s/%s" % (base_dir, data_name) from_file_path = "%s/%s" % (base_dir, file_name) to_file_path = from_file_path # copy the files # ???? why are we copying here? shutil.copy(from_template_path, to_template_path) shutil.copy(from_data_path, to_data_path) else: # TEST TEST TEST ticket = server.get_value("server") remote_server = TacticServerStub( protocol='xmlrpc', server=server, project=project_code, #user=user, #password=password, ticket=ticket, ) class_name = 'tactic.ui.sync.SyncCreateTemplateCmd' kwargs = {'project_code': project_code} remote_server.execute_cmd(class_name, kwargs) base_url = "http://%s/assets/_temp/" % server # download and install the files from_template_path = "%s/%s_template-%s.zip" % ( base_url, project_code, version) from_data_path = "%s/%s_data-%s.zip" % (base_url, project_code, version) remote_server.download(from_template_path, to_dir) remote_server.download(from_data_path, to_dir) # This makes the pretty big assumption that this is an official template if not os.path.exists(to_template_path): raise Exception("Path [%s] does not exist" % to_template_path) template_code = project_code try: new_project = False from tactic.command import ProjectTemplateInstallerCmd cmd = ProjectTemplateInstallerCmd(project_code=project_code, path=to_template_path, template_code=template_code, new_project=new_project, version=version) cmd.execute() Project.set_project(project_code) project = Project.get() # NOTE: this avoids breaking on search.py, line 203, where it checks # for tables. The caching mechanism for what tables are in the # database need to be refreshed once a template is imported from pyasm.search import DatabaseImpl, DbContainer DatabaseImpl.clear_table_cache() # import from a plugin kwargs = { #'base_dir': base_dir, #'manifest': my.xml.to_string(), 'code': "%s_data" % project_code, 'zip_path': to_data_path, } from tactic.command import PluginInstaller cmd = PluginInstaller(**kwargs) cmd.execute() # create zip of the project files if os.path.exists(to_file_path): from pyasm.common import ZipUtil zip_util = ZipUtil() asset_dir = Environment.get_asset_dir() to_file_dir = os.path.dirname(to_file_path) zip_util.extract(to_file_path, base_dir=asset_dir) except Exception, e: print "Error: ", str(e) raise
def execute(my): import datetime now = datetime.datetime.now() version = now.strftime("%Y%m%d_%H%M%S") project_code = my.kwargs.get("project_code") if project_code: project = Project.get_by_code(project_code) else: project = Project.get() project_code = project.get_code() server_code = my.kwargs.get("server") assert server_code if not isinstance(server_code, basestring): server_code = server_code.get_value("code") base_dir = my.kwargs.get('base_dir') ticket = Environment.get_ticket() tmp_dir = "%s/sync_%s" % (Environment.get_tmp_dir(), ticket) if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) server = Search.get_by_code("sthpw/sync_server", server_code) if server.get_value("sync_mode") == "file": if not base_dir: base_dir = server.get_value("base_dir") else: raise Exception("sync mode [%s] not support" % sync_mode) # FIXME: not sure if this is valid anymore asset_dir = Environment.get_asset_dir() base_dir = "%s/_temp" % asset_dir # create the project template from tactic.command import ProjectTemplateCreatorCmd cmd = ProjectTemplateCreatorCmd(project_code=project_code, version=version, base_dir=tmp_dir) cmd.execute() project_path = cmd.get_zip_path() # create zip of the project files from pyasm.common import ZipUtil zip_util = ZipUtil() asset_dir = Environment.get_asset_dir() project_dir = "%s/%s" % (asset_dir, project_code) zip_dir = "%s/%s" % (tmp_dir, project_code) file_path = "%s-files-%s.zip" % (zip_dir, version) if os.path.exists(file_path): os.unlink(file_path) zip_util.zip_dir2(project_dir, zip_path=file_path) # create a manifest for all the data in the project. xml = Xml() my.xml = xml xml.create_doc("manifest") manifest_node = xml.get_root_node() xml.set_attribute(manifest_node, "code", "%s-data" % project_code) search_types = project.get_search_types() # just dump the data for search_type in search_types: data_node = xml.create_element("sobject") xml.append_child(manifest_node, data_node) xml.set_attribute(data_node, "search_type", search_type.get_value("search_type")) # dump the note entries data_node = xml.create_element("sobject") xml.append_child(manifest_node, data_node) xml.set_attribute( data_node, "expression", "@SOBJECT(sthpw/note['project_code','%s'])" % project_code) xml.set_attribute(data_node, "search_type", "sthpw/note") # dump the task entries data_node = xml.create_element("sobject") xml.append_child(manifest_node, data_node) xml.set_attribute( data_node, "expression", "@SOBJECT(sthpw/task['project_code','%s'])" % project_code) xml.set_attribute(data_node, "search_type", "sthpw/task") # dump the snapshot entries data_node = xml.create_element("sobject") xml.append_child(manifest_node, data_node) xml.set_attribute( data_node, "expression", "@SOBJECT(sthpw/snapshot['project_code','%s'])" % project_code) xml.set_attribute(data_node, "search_type", "sthpw/snapshot") # dump the file entries data_node = xml.create_element("sobject") xml.append_child(manifest_node, data_node) xml.set_attribute( data_node, "expression", "@SOBJECT(sthpw/file['project_code','%s'])" % project_code) xml.set_attribute(data_node, "search_type", "sthpw/file") manifest = xml.to_string() # create a virtual plugin sobject from tactic.command import PluginCreator plugin = SearchType.create("config/plugin") plugin.set_value("version", version) plugin.set_value("code", "%s_project" % project_code) creator = PluginCreator(base_dir=tmp_dir, plugin=plugin, manifest=manifest, version=version) creator.execute() data_path = creator.get_zip_path() # move the files to the appropriate base_dir import shutil if not os.path.exists(base_dir): os.makedirs(base_dir) if os.path.exists(project_path): basename = os.path.basename(project_path) if os.path.exists("%s/%s" % (base_dir, basename)): os.unlink("%s/%s" % (base_dir, basename)) shutil.move(project_path, base_dir) if os.path.exists(data_path): basename = os.path.basename(data_path) if os.path.exists("%s/%s" % (base_dir, basename)): os.unlink("%s/%s" % (base_dir, basename)) shutil.move(data_path, base_dir) if os.path.exists(file_path): basename = os.path.basename(file_path) if os.path.exists("%s/%s" % (base_dir, basename)): os.unlink("%s/%s" % (base_dir, basename)) shutil.move(file_path, base_dir) shutil.rmtree(tmp_dir) my.handle_manifest(base_dir, project_code, version)
def execute(self): # This will go to a registered remote server and get the project #server_code = self.kwargs.get("server") #assert server_code ## get the registered server #server = Search.get_by_code("sthpw/sync_server", server_code) #assert server project_code = self.kwargs.get("project_code") assert project_code version = self.kwargs.get("version") if not version: version = "1.0.0" # build the project names template_name = "%s-%s.zip" % (project_code, version) data_name = "%s-data-%s.zip" % (project_code, version) file_name = "%s-files-%s.zip" % (project_code, version) tmp_dir = Environment.get_tmp_dir() to_dir = tmp_dir to_template_path = "%s/%s-%s.zip" % (to_dir, project_code, version) to_data_path = "%s/%s-data-%s.zip" % (to_dir, project_code, version) to_file_path = "%s/%s-files-%s.zip" % (to_dir, project_code, version) #sync_mode = server.get_value("sync_mode") sync_mode = "file" if sync_mode == "file": base_dir = self.kwargs.get("base_dir") from_template_path = "%s/%s" % (base_dir, template_name) from_data_path = "%s/%s" % (base_dir, data_name) from_file_path = "%s/%s" % (base_dir, file_name) to_file_path = from_file_path # copy the files # ???? why are we copying here? shutil.copy(from_template_path, to_template_path) shutil.copy(from_data_path, to_data_path) else: # TEST TEST TEST ticket = server.get_value("server") remote_server = TacticServerStub( protocol='xmlrpc', server=server, project=project_code, #user=user, #password=password, ticket=ticket, ) class_name = 'tactic.ui.sync.SyncCreateTemplateCmd' kwargs = { 'project_code': project_code } remote_server.execute_cmd(class_name, kwargs) base_url = "http://%s/assets/_temp/" % server # download and install the files from_template_path = "%s/%s_template-%s.zip" % (base_url, project_code, version) from_data_path = "%s/%s_data-%s.zip" % (base_url, project_code, version) remote_server.download(from_template_path, to_dir) remote_server.download(from_data_path, to_dir) # This makes the pretty big assumption that this is an official template if not os.path.exists(to_template_path): raise Exception("Path [%s] does not exist" % to_template_path) template_code = project_code try: new_project = False from tactic.command import ProjectTemplateInstallerCmd cmd = ProjectTemplateInstallerCmd(project_code=project_code, path=to_template_path,template_code=template_code, new_project=new_project, version=version) cmd.execute() Project.set_project(project_code) project = Project.get() # NOTE: this avoids breaking on search.py, line 203, where it checks # for tables. The caching mechanism for what tables are in the # database need to be refreshed once a template is imported from pyasm.search import DatabaseImpl, DbContainer DatabaseImpl.clear_table_cache() # import from a plugin kwargs = { #'base_dir': base_dir, #'manifest': self.xml.to_string(), 'code': "%s_data" % project_code, 'zip_path': to_data_path, } from tactic.command import PluginInstaller cmd = PluginInstaller(**kwargs) cmd.execute() # create zip of the project files if os.path.exists(to_file_path): from pyasm.common import ZipUtil zip_util = ZipUtil() asset_dir = Environment.get_asset_dir() to_file_dir = os.path.dirname(to_file_path) zip_util.extract(to_file_path, base_dir=asset_dir) except Exception as e: print("Error: ", str(e)) raise
def execute(self): import datetime now = datetime.datetime.now() version = now.strftime("%Y%m%d_%H%M%S") project_code = self.kwargs.get("project_code") if project_code: project = Project.get_by_code(project_code) else: project = Project.get() project_code = project.get_code() server_code = self.kwargs.get("server") assert server_code if not isinstance(server_code, basestring): server_code = server_code.get_value("code") base_dir = self.kwargs.get('base_dir') ticket = Environment.get_ticket() tmp_dir = "%s/sync_%s" % (Environment.get_tmp_dir(), ticket) if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) server = Search.get_by_code("sthpw/sync_server", server_code) if server.get_value("sync_mode") == "file": if not base_dir: base_dir = server.get_value("base_dir") else: raise Exception("sync mode [%s] not support" % sync_mode) # FIXME: not sure if this is valid anymore asset_dir = Environment.get_asset_dir() base_dir = "%s/_temp" % asset_dir # create the project template from tactic.command import ProjectTemplateCreatorCmd cmd = ProjectTemplateCreatorCmd(project_code=project_code, version=version, base_dir=tmp_dir) cmd.execute() project_path = cmd.get_zip_path() # create zip of the project files from pyasm.common import ZipUtil zip_util = ZipUtil() asset_dir = Environment.get_asset_dir() project_dir = "%s/%s" % (asset_dir, project_code) zip_dir = "%s/%s" % (tmp_dir, project_code) file_path = "%s-files-%s.zip" % (zip_dir, version) if os.path.exists(file_path): os.unlink(file_path) zip_util.zip_dir2(project_dir, zip_path=file_path) # create a manifest for all the data in the project. xml = Xml() self.xml = xml xml.create_doc("manifest") manifest_node = xml.get_root_node() xml.set_attribute(manifest_node, "code", "%s-data" % project_code) search_types = project.get_search_types() # just dump the data for search_type in search_types: data_node = xml.create_element("sobject") xml.append_child(manifest_node, data_node) xml.set_attribute(data_node, "search_type", search_type.get_value("search_type")) # dump the note entries data_node = xml.create_element("sobject") xml.append_child(manifest_node, data_node) xml.set_attribute(data_node, "expression", "@SOBJECT(sthpw/note['project_code','%s'])" % project_code) xml.set_attribute(data_node, "search_type", "sthpw/note") # dump the task entries data_node = xml.create_element("sobject") xml.append_child(manifest_node, data_node) xml.set_attribute(data_node, "expression", "@SOBJECT(sthpw/task['project_code','%s'])" % project_code) xml.set_attribute(data_node, "search_type", "sthpw/task") # dump the snapshot entries data_node = xml.create_element("sobject") xml.append_child(manifest_node, data_node) xml.set_attribute(data_node, "expression", "@SOBJECT(sthpw/snapshot['project_code','%s'])" % project_code) xml.set_attribute(data_node, "search_type", "sthpw/snapshot") # dump the file entries data_node = xml.create_element("sobject") xml.append_child(manifest_node, data_node) xml.set_attribute(data_node, "expression", "@SOBJECT(sthpw/file['project_code','%s'])" % project_code) xml.set_attribute(data_node, "search_type", "sthpw/file") manifest = xml.to_string() # create a virtual plugin sobject from tactic.command import PluginCreator plugin = SearchType.create("config/plugin") plugin.set_value("version", version) plugin.set_value("code", "%s_project" % project_code) creator = PluginCreator( base_dir=tmp_dir, plugin=plugin, manifest=manifest, version=version ) creator.execute() data_path = creator.get_zip_path() # move the files to the appropriate base_dir import shutil if not os.path.exists(base_dir): os.makedirs(base_dir) if os.path.exists(project_path): basename = os.path.basename(project_path) if os.path.exists("%s/%s" % (base_dir, basename)): os.unlink("%s/%s" % (base_dir, basename)) shutil.move(project_path, base_dir) if os.path.exists(data_path): basename = os.path.basename(data_path) if os.path.exists("%s/%s" % (base_dir, basename)): os.unlink("%s/%s" % (base_dir, basename)) shutil.move(data_path, base_dir) if os.path.exists(file_path): basename = os.path.basename(file_path) if os.path.exists("%s/%s" % (base_dir, basename)): os.unlink("%s/%s" % (base_dir, basename)) shutil.move(file_path, base_dir) shutil.rmtree(tmp_dir) self.handle_manifest(base_dir, project_code, version)
def unzip_file(my, from_path, to_dir): zip_util = ZipUtil() return zip_util.extract(from_path, to_dir)