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 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 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_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 __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 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(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 unzip_file(my, from_path, to_dir): zip_util = ZipUtil() return zip_util.extract(from_path, to_dir)