def cms_rm(arg): """ CMS rm command works with local files/dirs and CMS storate elements. Examples: cmssh> rm local_file cmssh> rm -rf local_dir cmssh> rm T3_US_Cornell:/xrootdfs/cms/store/user/user_name/file.root """ arg = arg.strip() try: debug = get_ipython().debug except: debug = 0 if not arg: print_error("Usage: rm <options> source_file") dst = arg.split()[-1] if os.path.exists(dst) or len(glob.glob(dst)): cmd = "rm %s" % arg run(cmd) else: if pat_lfn.match(arg.split(':')[-1]): status = rm_lfn(arg, verbose=debug) print_status(status) else: if not os.path.exists(dst): print_error('File %s does not exists' % dst) else: raise Exception('Not implemented yet')
def cms_apt(arg=''): "Execute apt commands" if '-cache' in arg or '-get' in arg: cmd = 'apt%s' % arg else: msg = 'Not supported apt command' raise Exception(msg) run(cmd)
def cms_cp(arg): """ cmssh cp command copies local files/dirs to/from local files/dirs or CMS storate elements. Examples: cmssh> cp file1 file2 cmssh> cp file.root T3_US_Cornell:/store/user/name cmssh> cp /store/mc/file.root T3_US_Cornell:/store/user/name cmssh> cp T3_US_Cornell:/store/user/name/file.root T3_US_Omaha """ check_voms_proxy() background = False orig_arg = arg arg = arg.strip() try: last_arg = arg.split(' ')[-1].strip() if last_arg == '&': background = True arg = arg.replace('&', '').strip() src, dst = arg.rsplit(' ', 1) if dst.find('&') != -1: background = True dst = dst.replace('&', '').strip() if dst == '.': dst = os.getcwd() # check if src still has options and user asked for -f options = src.split(' ') if len(options) > 1 and options[0] == '-f': overwrite = True else: overwrite = False except: traceback.print_exc() return try: debug = get_ipython().debug except: debug = 0 if not arg: print_error("Usage: cp <options> source_file target_{file,directory}") pat = pat_se orig = src.split(' ')[-1] if os.path.exists(orig) and not pat.match(dst): if background: cmd = 'cp %s' % orig_arg subprocess.call(cmd, shell=True) else: run("cp %s %s" % (src, dst)) else: try: status = copy_lfn(orig, dst, debug, background, overwrite) print_status(status) except: traceback.print_exc()
def cms_root(arg): """ cmssh command to run ROOT within cmssh Examples: cmssh> root -l """ pcre_init = pkg_init('external/pcre') gcc_init = pkg_init('external/gcc') root_init = pkg_init('lcg/root') pkgs_init = '%s %s %s' % (pcre_init, gcc_init, root_init) cmd = '%s root -l %s' % (pkgs_init, arg.strip()) run(cmd)
def cms_xrdcp(arg): """ cmssh command to run ROOT xrdcp via cmssh shell Examples: cmssh> xrdcp /a/b/c.root file:////tmp.file.root """ dyld_path = os.environ.get('DYLD_LIBRARY_PATH', None) root_path = os.environ['DEFAULT_ROOT'] if dyld_path: os.environ['DYLD_LIBRARY_PATH'] = os.path.join(root_path, 'lib') cmd = '%s/xrdcp %s' % (os.path.join(root_path, 'bin'), arg.strip()) run(cmd) if dyld_path: os.environ['DYLD_LIBRARY_PATH'] = dyld_path
def cmsexe(cmd): """ Execute given command within CMSSW environment """ vdir = os.environ.get('VO_CMS_SW_DIR', None) arch = os.environ.get('SCRAM_ARCH', None) if not vdir or not arch: msg = 'Unable to identify CMSSW environment, please run first: ' msg = msg_red(msg) msg += msg_blue('cmsrel <rel>\n') releases = os.listdir(os.environ['CMSSW_RELEASES']) msg += '\nInstalled releases: ' + msg_green(', '.join(releases)) print msg return cmd = "eval `scramv1 runtime -sh`; %s" % cmd run(cmd, shell=True, call=True)
def cms_install(rel): """ cmssh command to install given CMSSW release. Examples: cmssh> install CMSSW_5_2_4 """ rel = rel.strip() pat = pat_release if not pat.match(rel): msg = 'Fail to validate release name "%s"' % rel print_error(msg) msg = 'Please check the you provide correct release name,' msg += ' e.g. CMSSW_X_Y_Z<_patchN>' print msg return # check if we have stand-alone installation if os.environ.get('CMSSH_CMSSW', None): msg = '\nYou are not allowed to install new release, ' msg += 'since cmssh was installed with system CMSSW install area' print msg return # check if given release/architecture is in place status = check_release_arch(rel) if status != 'ok': msg = '\nCheck release architecture status: %s' % status print msg return print "Searching for %s" % rel script = get_apt_init(os.environ['SCRAM_ARCH']) cmd = 'source %s; apt-cache search %s | grep -v -i fwlite' % (script, rel) run(cmd) if rel.lower().find('patch') != -1: print "Installing cms+cmssw-patch+%s ..." % rel cmd = 'source %s; apt-get install cms+cmssw-patch+%s' % (script, rel) else: print "Installing cms+cmssw+%s ..." % rel cmd = 'source %s; apt-get install cms+cmssw+%s' % (script, rel) subprocess.call(cmd, shell=True) # use subprocess due to apt-get interactive feature if platform() == 'osx': idir = '%s/%s/cms/cmssw/%s' \ % (os.environ['VO_CMS_SW_DIR'], os.environ['SCRAM_ARCH'], rel) fix_so(idir) print "Create user area for %s release ..." % rel cmsrel(rel)
def cms_vomsinit(_arg=None): """ cmssh command which executes voms-proxy-init on behalf of the user Examples: cmssh> vomsinit By default it applies the following options -rfc -voms cms:/cms -key <userkey.pem> -cert <usercert.pem> """ cert = os.path.join(os.environ['HOME'], '.globus/usercert.pem') with working_pem(PEMMGR.pem) as key: run("voms-proxy-destroy") cmd = "voms-proxy-init -rfc -voms cms:/cms -key %s -cert %s" % (key, cert) run(cmd) userdn = os.environ.get('USER_DN', '') if not userdn: cmd = "voms-proxy-info -identity" stdout, stderr = execmd(cmd) os.environ['USER_DN'] = stdout.replace('\n', '')
def cms_install(rel): """ cmssh command to install given CMSSW release. Examples: cmssh> install CMSSW_5_2_4 """ rel = rel.strip() pat = pat_release if not pat.match(rel): msg = 'Fail to validate release name "%s"' % rel print_error(msg) msg = 'Please check the you provide correct release name,' msg += ' e.g. CMSSW_X_Y_Z<_patchN>' print msg return # check if we have stand-alone installation if os.path.islink(os.environ['VO_CMS_SW_DIR']): msg = '\nYou are not allowed to install new release, ' msg += 'since cmssh was installed with system CMSSW install area' print msg return # check if given release/architecture is in place status = check_release_arch(rel) if status != 'ok': msg = '\nCheck release architecture status: %s' % status print msg return print "Searching for %s" % rel script = get_apt_init(os.environ['SCRAM_ARCH']) cmd = 'source %s; apt-cache search %s | grep -v -i fwlite' % (script, rel) run(cmd) if rel.lower().find('patch') != -1: print "Installing cms+cmssw-patch+%s ..." % rel cmd = 'source %s; apt-get install cms+cmssw-patch+%s' % (script, rel) else: print "Installing cms+cmssw+%s ..." % rel cmd = 'source %s; apt-get install cms+cmssw+%s' % (script, rel) subprocess.call( cmd, shell=True) # use subprocess due to apt-get interactive feature print "Create user area for %s release ..." % rel cmsrel(rel)
def bootstrap(arch): "Bootstrap new architecture" swdir = os.environ['VO_CMS_SW_DIR'] arch = os.environ['SCRAM_ARCH'] cmd = 'sh -x %s/bootstrap.sh setup -path %s -arch %s' % (swdir, swdir, arch) if unsupported_linux(): cmd += ' -unsupported_distribution_hack' sdir = os.path.join(os.environ['CMSSH_ROOT'], 'CMSSW') debug = 0 msg = 'Bootstrap %s ...' % arch # run bootstrap command in subprocess.call since it invokes # wget/curl and it can be spawned into serate process, therefore # subprocess.Popen will not catch it run(cmd, sdir, 'bootstrap.log', msg, debug, shell=True, call=True) cmd = 'source `find %s/%s/external/apt -name init.sh | tail -1`; ' \ % (swdir, arch) cmd += 'apt-get install external+fakesystem+1.0; ' cmd += 'apt-get update; ' msg = 'Initialize %s apt repository ...' % arch run(cmd, sdir, msg=msg, debug=debug, shell=True)
def cms_mkdir(arg): """ cmssh mkdir command creates directory on local filesystem or remote CMS storage element. Examples: cmssh> mkdir foo cmssh> mkdir T3_US_Cornell:/store/user/user_name/foo """ arg = arg.strip() try: debug = get_ipython().debug except: debug = 0 if not arg: print_error("Usage: mkdir <options> dir") if arg.find(':') == -1: # not a SE:dir pattern run("mkdir %s" % arg) else: try: status = mkdir(arg, verbose=debug) print_status(status) except: traceback.print_exc()
def cms_rmdir(arg): """ cmssh rmdir command removes directory from local file system or CMS storage element. Examples: cmssh> rmdir foo cmssh> rmdir T3_US_Cornell:/store/user/user_name/foo """ arg = arg.strip() try: debug = get_ipython().debug except: debug = 0 if not arg: print_error("Usage: rmdir <options> dir") if os.path.exists(arg): run("rmdir %s" % arg) else: try: status = rmdir(arg, verbose=debug) print_status(status) except: traceback.print_exc()
def read_pem_via_openssl(): """ Create user key pem content via passless key (created by openssl). This function creates temporary file in user .globus area in order to load passless key. """ globus_dir = os.path.join(os.environ['HOME'], '.globus') fname = os.path.join(globus_dir, 'cmssh.x509pk_u%s' % os.getuid()) mode = stat.S_IRUSR try: cmd = '/usr/bin/openssl rsa -in %s/.globus/userkey.pem -out %s' \ % (os.environ['HOME'], fname) print # extra empty line before we read user key run(cmd) os.chmod(fname, mode) with open(fname, 'r') as userkey: PEMMGR.pem = userkey.read() except: traceback.print_exc() try: os.remove(fname) except: traceback.print_exc()
def cmsrel(rel): """ cmssh release setup command, it setups CMSSW environment and creates user based directory structure. Examples: cmssh> cmsrel # reset CMSSW environment to cmssh one cmssh> cmsrel CMSSW_5_2_4 """ ipython = get_ipython() rel = rel.strip() if not rel or rel in ['reset', 'clear', 'clean']: path = os.environ['CMSSH_ROOT'] for idir in ['external', 'lib', 'root']: pdir = os.path.join(path, 'install/lib/release_%s' % idir) if os.path.islink(pdir): os.remove(pdir) if os.path.isdir(pdir): shutil.rmtree(pdir) os.makedirs(pdir) # Set cmssh prompt prompt = 'cms-sh' ipython.prompt_manager.in_template = '%s|\#> ' % prompt return # check if given release name is installed on user system rel_arch = None for arch in cms_architectures(): rel_dir = '%s/cms/cmssw/%s' % (arch, rel) if os.path.isdir(os.path.join(os.environ['VO_CMS_SW_DIR'], rel_dir)): rel_arch = arch break if not rel_arch: msg = 'Release ' + msg_red(rel) msg += ' is not yet installed on your system.\n' msg += 'Use ' + msg_green('releases') msg += ' command to list available releases.\n' msg += 'Use ' + msg_green('install %s' % rel) msg += ' command to install given release.' print msg return # set release architecture os.environ['SCRAM_ARCH'] = rel_arch # setup environment cmssw_dir = os.environ.get('CMSSW_RELEASES', os.getcwd()) if not os.path.isdir(cmssw_dir): os.makedirs(cmssw_dir) root = os.environ['CMSSH_ROOT'] idir = os.environ['CMSSH_INSTALL_DIR'] base = os.path.realpath('%s/CMSSW' % root) path = '%s/%s/cms/cmssw/%s' % (base, rel_arch, rel) os.environ['CMSSW_BASE'] = os.path.join(cmssw_dir, rel) os.environ['CMSSW_RELEASE_BASE'] = path for pkg in ['FWCore', 'DataFormats']: pdir = '%s/%s' % (idir, pkg) if os.path.exists(pdir): shutil.rmtree(pdir) os.mkdir(pdir) touch(os.path.join(pdir, '__init__.py')) pkgs = ['Framework', 'GuiBrowsers', 'Integration', 'MessageLogger', 'MessageService', 'Modules', 'ParameterSet', 'PythonUtilities', 'Services', 'Utilities'] for pkg in pkgs: link = '%s/src/FWCore/%s/python' % (path, pkg) dst = '%s/FWCore/%s' % (idir, pkg) os.symlink(link, dst) link = '%s/src/DataFormats/FWLite/python' % path dst = '%s/DataFormats/FWLite' % idir os.symlink(link, dst) for lib in ['external', 'lib']: link = '%s/%s/%s' % (path, lib, rel_arch) dst = '%s/install/lib/release_%s' % (root, lib) if os.path.islink(dst): os.remove(dst) else: shutil.rmtree(dst) os.symlink(link, dst) # switch to given release os.environ['CMSSW_VERSION'] = rel os.environ['CMSSW_WORKAREA'] = os.path.join(cmssw_dir, rel) if os.path.isdir(os.path.join(cmssw_dir, rel + '/src')): os.chdir(os.path.join(cmssw_dir, rel + '/src')) else: os.chdir(cmssw_dir) cmd = "scramv1 project CMSSW %s" % rel run(cmd) os.chdir(os.path.join(rel, 'src')) # get ROOT from run-time environment cmd = 'eval `scramv1 runtime -sh`; env | grep ^ROOTSYS=' stdout, _stderr = execmd(cmd) rootsys = stdout.replace('\n', '').replace('ROOTSYS=', '') dst = '%s/install/lib/release_root' % root if os.path.exists(dst): if os.path.islink(dst): os.remove(dst) else: shutil.rmtree(dst) os.symlink(rootsys, dst) # set edm utils for given release ipython = get_ipython() rdir = '%s/bin/%s' % (rel_dir, rel_arch) reldir = os.path.join(os.environ['VO_CMS_SW_DIR'], rdir) for name in os.listdir(reldir): fname = os.path.join(reldir, name) if name.find('edm') == 0 and os.path.isfile(fname): # we use Magic(cmd).execute we don't need # to add scramv1 command in front of edm one, since # execute method will run in current shell environment # old command for reference: # cmd = "eval `scramv1 runtime -sh`; %s" % fname cmd = fname ipython.register_magic_function(Magic(cmd).execute, 'line', name) # Set cmssh prompt ipython.prompt_manager.in_template = '%s|\#> ' % rel # final message print "%s is ready, cwd: %s" % (rel, os.getcwd())
def execute(self, args=''): "Execute given command in current shell environment" cmd = '%s %s' % (self.cmd, args.strip()) run(cmd)
def cmsrel(rel): """ cmssh release setup command, it setups CMSSW environment and creates user based directory structure. Examples: cmssh> cmsrel # reset CMSSW environment to cmssh one cmssh> cmsrel CMSSW_5_2_4 """ ipython = get_ipython() rel = rel.strip() if not rel or rel in ['reset', 'clear', 'clean']: path = os.environ['CMSSH_ROOT'] for idir in ['external', 'lib', 'root']: pdir = os.path.join(path, 'install/lib/release_%s' % idir) if os.path.islink(pdir): os.remove(pdir) if os.path.isdir(pdir): shutil.rmtree(pdir) os.makedirs(pdir) # Set cmssh prompt prompt = 'cms-sh' ipython.prompt_manager.in_template = '%s|\#> ' % prompt return # check if given release name is installed on user system rel_arch = None for arch in cms_architectures(): rel_dir = '%s/cms/cmssw/%s' % (arch, rel) if os.path.isdir(os.path.join(os.environ['VO_CMS_SW_DIR'], rel_dir)): rel_arch = arch break if not rel_arch: msg = 'Release ' + msg_red(rel) msg += ' is not yet installed on your system.\n' msg += 'Use ' + msg_green('releases') msg += ' command to list available releases.\n' msg += 'Use ' + msg_green('install %s' % rel) msg += ' command to install given release.' print msg return # set release architecture os.environ['SCRAM_ARCH'] = rel_arch # setup environment cmssw_dir = os.environ.get('CMSSW_RELEASES', os.getcwd()) if not os.path.isdir(cmssw_dir): os.makedirs(cmssw_dir) root = os.environ['CMSSH_ROOT'] idir = os.environ['CMSSH_INSTALL_DIR'] base = os.path.realpath('%s/CMSSW' % root) path = '%s/%s/cms/cmssw/%s' % (base, rel_arch, rel) os.environ['CMSSW_BASE'] = os.path.join(cmssw_dir, rel) os.environ['CMSSW_RELEASE_BASE'] = path for pkg in ['FWCore', 'DataFormats']: pdir = '%s/%s' % (idir, pkg) if os.path.exists(pdir): shutil.rmtree(pdir) os.mkdir(pdir) touch(os.path.join(pdir, '__init__.py')) pkgs = [ 'Framework', 'GuiBrowsers', 'Integration', 'MessageLogger', 'MessageService', 'Modules', 'ParameterSet', 'PythonUtilities', 'Services', 'Utilities' ] for pkg in pkgs: link = '%s/src/FWCore/%s/python' % (path, pkg) dst = '%s/FWCore/%s' % (idir, pkg) os.symlink(link, dst) link = '%s/src/DataFormats/FWLite/python' % path dst = '%s/DataFormats/FWLite' % idir os.symlink(link, dst) for lib in ['external', 'lib']: link = '%s/%s/%s' % (path, lib, rel_arch) dst = '%s/install/lib/release_%s' % (root, lib) if os.path.islink(dst): os.remove(dst) else: shutil.rmtree(dst) os.symlink(link, dst) # switch to given release os.environ['CMSSW_VERSION'] = rel os.environ['CMSSW_WORKAREA'] = os.path.join(cmssw_dir, rel) if os.path.isdir(os.path.join(cmssw_dir, rel + '/src')): os.chdir(os.path.join(cmssw_dir, rel + '/src')) else: os.chdir(cmssw_dir) cmd = "scramv1 project CMSSW %s" % rel run(cmd) os.chdir(os.path.join(rel, 'src')) # get ROOT from run-time environment cmd = 'eval `scramv1 runtime -sh`; env | grep ^ROOTSYS=' stdout, _stderr = execmd(cmd) rootsys = stdout.replace('\n', '').replace('ROOTSYS=', '') dst = '%s/install/lib/release_root' % root if os.path.exists(dst): if os.path.islink(dst): os.remove(dst) else: shutil.rmtree(dst) os.symlink(rootsys, dst) # set edm utils for given release ipython = get_ipython() rdir = '%s/bin/%s' % (rel_dir, rel_arch) reldir = os.path.join(os.environ['VO_CMS_SW_DIR'], rdir) for name in os.listdir(reldir): fname = os.path.join(reldir, name) if name.find('edm') == 0 and os.path.isfile(fname): # we use Magic(cmd).execute we don't need # to add scramv1 command in front of edm one, since # execute method will run in current shell environment # old command for reference: # cmd = "eval `scramv1 runtime -sh`; %s" % fname cmd = fname ipython.register_magic_function(Magic(cmd).execute, 'line', name) # Set cmssh prompt ipython.prompt_manager.in_template = '%s|\#> ' % rel # final message print "%s is ready, cwd: %s" % (rel, os.getcwd())
def cms_ls(arg): """ cmssh ls command lists local files/dirs/CMS storate elements or CMS entities (se, site, dataset, block, run, release, file). Examples: cmssh> ls # UNIX command cmssh> ls -l local_file cmssh> ls T3_US_Cornell:/store/user/valya cmssh> ls run=160915 """ arg = arg.strip() res = [] try: debug = get_ipython().debug except: debug = 0 orig_arg = arg if orig_arg.find('|') != -1: arg, flt = orig_arg.split('|', 1) arg = arg.strip() else: flt = None startswith = None entities = \ ['se', 'site', 'lfn', 'dataset', 'block', 'run', 'release', 'file'] for item in entities: if arg.startswith(item + '='): startswith = item if os.path.isfile(orig_arg) or os.path.isdir(orig_arg): cmd = 'ls ' + orig_arg run(cmd, shell=True) elif pat_se.match(arg): arg = arg.replace('site=', '') res = list_se(arg, debug) elif pat_site.match(arg): arg = arg.replace('site=', '') res = site_info(arg, debug) elif pat_lfn.match(arg): arg = arg.replace('file=', '') arg = arg.replace('lfn=', '') res = file_info(arg, debug) elif pat_block.match(arg): arg = arg.replace('block=', '') res = block_info(arg, debug) elif pat_dataset.match(arg): arg = arg.replace('dataset=', '') try: res = dataset_info(arg, debug) except IndexError: msg = "Given pattern '%s' does not exist on local filesystem or in DBS" % arg print_error(msg) elif pat_run.match(arg): arg = arg.replace('run=', '') res = run_info(arg, debug) elif pat_release.match(arg): arg = arg.replace('release=', '') res = release_info(arg, debug) elif startswith: msg = 'No pattern is allowed for %s look-up' % startswith print_error(msg) else: cmd = 'ls ' + orig_arg run(cmd, shell=True) if res: RESMGR.assign(res) list_results(res, debug=True, flt=flt)