From 1f9a07c54e9a18002759255abf89ea4849797b7c Mon Sep 17 00:00:00 2001 From: Andrii Lytovchenko Date: Tue, 15 Feb 2022 00:50:04 +0100 Subject: [PATCH 1/3] fix: python 3 things --- src/COMDIRAC/Interfaces/Utilities/DConfigCache.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/COMDIRAC/Interfaces/Utilities/DConfigCache.py b/src/COMDIRAC/Interfaces/Utilities/DConfigCache.py index d2cf2d5..748767e 100644 --- a/src/COMDIRAC/Interfaces/Utilities/DConfigCache.py +++ b/src/COMDIRAC/Interfaces/Utilities/DConfigCache.py @@ -2,7 +2,6 @@ import stat import time import re - import pickle from DIRAC.Core.Utilities.DIRACScript import DIRACScript as Script From de49a7bf36726876f4821d02ad1aca648b3cfebf Mon Sep 17 00:00:00 2001 From: Andrii Lytovchenko Date: Tue, 15 Feb 2022 00:51:17 +0100 Subject: [PATCH 2/3] fix: python 3 things --- setup.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/setup.py b/setup.py index dcded7c..a45dce1 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,3 @@ -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - from setuptools import setup # This is required to allow editable pip installs while using the declarative configuration (setup.cfg) From aa51c228267d36e9b45fc65ac9cf330b675a024c Mon Sep 17 00:00:00 2001 From: Andrii Lytovchenko Date: Tue, 15 Feb 2022 20:55:56 +0100 Subject: [PATCH 3/3] refactor: use DIRAC.Core.Base.Script.Script --- .../Interfaces/Utilities/DCommands.py | 6 +-- .../Interfaces/Utilities/DConfigCache.py | 6 +-- src/COMDIRAC/Interfaces/scripts/dcd.py | 29 ++++------- src/COMDIRAC/Interfaces/scripts/dchgrp.py | 42 ++++----------- src/COMDIRAC/Interfaces/scripts/dchmod.py | 38 ++++---------- src/COMDIRAC/Interfaces/scripts/dchown.py | 38 ++++---------- src/COMDIRAC/Interfaces/scripts/dconfig.py | 32 ++++-------- src/COMDIRAC/Interfaces/scripts/dfind.py | 41 +++++---------- src/COMDIRAC/Interfaces/scripts/dget.py | 8 +-- src/COMDIRAC/Interfaces/scripts/dgetenv.py | 46 ++++------------ src/COMDIRAC/Interfaces/scripts/dinit.py | 23 ++------ src/COMDIRAC/Interfaces/scripts/dinput.py | 52 +++++++------------ src/COMDIRAC/Interfaces/scripts/dkill.py | 24 +++------ src/COMDIRAC/Interfaces/scripts/dlogging.py | 19 ++----- src/COMDIRAC/Interfaces/scripts/dls.py | 29 ++++------- src/COMDIRAC/Interfaces/scripts/dmeta.py | 13 ++--- src/COMDIRAC/Interfaces/scripts/dmkdir.py | 33 +++--------- src/COMDIRAC/Interfaces/scripts/doutput.py | 22 ++------ src/COMDIRAC/Interfaces/scripts/dput.py | 7 ++- src/COMDIRAC/Interfaces/scripts/dpwd.py | 21 +------- src/COMDIRAC/Interfaces/scripts/drepl.py | 29 +++-------- src/COMDIRAC/Interfaces/scripts/dreplicas.py | 23 ++------ src/COMDIRAC/Interfaces/scripts/drm.py | 34 ++++-------- src/COMDIRAC/Interfaces/scripts/drmdir.py | 34 +++--------- src/COMDIRAC/Interfaces/scripts/dsetenv.py | 24 +++------ src/COMDIRAC/Interfaces/scripts/dsize.py | 7 ++- src/COMDIRAC/Interfaces/scripts/dstat.py | 18 ++----- src/COMDIRAC/Interfaces/scripts/dsub.py | 39 ++++++-------- src/COMDIRAC/Interfaces/scripts/dunsetenv.py | 23 +++----- 29 files changed, 216 insertions(+), 544 deletions(-) diff --git a/src/COMDIRAC/Interfaces/Utilities/DCommands.py b/src/COMDIRAC/Interfaces/Utilities/DCommands.py index fbb7ff0..17ad6ec 100644 --- a/src/COMDIRAC/Interfaces/Utilities/DCommands.py +++ b/src/COMDIRAC/Interfaces/Utilities/DCommands.py @@ -92,7 +92,7 @@ def listFormatJSON(summaries, headers=None, sortKeys=None): return json.dumps(l) -class ArrayFormatter(object): +class ArrayFormatter: fmts = {"csv": listFormatCSV, "pretty": listFormatPretty, "json": listFormatJSON} def __init__(self, outputFormat): @@ -140,7 +140,7 @@ def dictFormat(self, dict_, headers=None, sort=None): # ------------------------------- -class DConfig(object): +class DConfig: def __init__(self, configDir=None, configFilename="dcommands.conf"): try: self.config = ConfigParser(allow_no_value=True) @@ -700,7 +700,7 @@ def createCatalog(): return FileCatalog() -class DCatalog(object): +class DCatalog: """ DIRAC File Catalog helper """ diff --git a/src/COMDIRAC/Interfaces/Utilities/DConfigCache.py b/src/COMDIRAC/Interfaces/Utilities/DConfigCache.py index 748767e..7040a6f 100644 --- a/src/COMDIRAC/Interfaces/Utilities/DConfigCache.py +++ b/src/COMDIRAC/Interfaces/Utilities/DConfigCache.py @@ -1,10 +1,10 @@ import os +import re import stat import time -import re import pickle -from DIRAC.Core.Utilities.DIRACScript import DIRACScript as Script +from DIRAC.Core.Base.Script import Script from DIRAC.ConfigurationSystem.Client.ConfigurationData import gConfigurationData @@ -25,7 +25,7 @@ def proxy_lcg_protocols_if_missing(): ) -class ConfigCache(object): +class ConfigCache: @classmethod def cacheFilePrefix(cls): return "DSession.configCache" diff --git a/src/COMDIRAC/Interfaces/scripts/dcd.py b/src/COMDIRAC/Interfaces/scripts/dcd.py index 20b2909..8532919 100644 --- a/src/COMDIRAC/Interfaces/scripts/dcd.py +++ b/src/COMDIRAC/Interfaces/scripts/dcd.py @@ -1,7 +1,10 @@ #! /usr/bin/env python - """ Change current DIRAC File Catalog working directory + +Examples: + $ dcd /dirac/user + $ dcd """ from COMDIRAC.Interfaces import critical from COMDIRAC.Interfaces import DSession @@ -9,25 +12,14 @@ from COMDIRAC.Interfaces import pathFromArgument from COMDIRAC.Interfaces import ConfigCache -from DIRAC.Core.Utilities.DIRACScript import DIRACScript as Script +from DIRAC.Core.Base.Script import Script @Script() def main(): - Script.setUsageMessage( - "\n".join( - [ - __doc__.split("\n")[1], - "Usage:", - " %s [Path]" % Script.scriptName, - "Arguments:", - " Path: path to new working directory (defaults to home directory)", - "", - "Examples:", - " $ dcd /dirac/user", - " $ dcd", - ] - ) + Script.registerArgument( + "Path: path to new working directory (defaults to home directory)", + mandatory=False, ) configCache = ConfigCache() @@ -36,14 +28,11 @@ def main(): args = Script.getPositionalArgs() - import DIRAC - session = DSession() if len(args) > 1: print("Error: too many arguments provided\n%s:" % Script.scriptName) - Script.showHelp() - DIRAC.exit(-1) + Script.showHelp(exitCode=-1) if len(args): arg = pathFromArgument(session, args[0]) diff --git a/src/COMDIRAC/Interfaces/scripts/dchgrp.py b/src/COMDIRAC/Interfaces/scripts/dchgrp.py index 9cfedda..c7e562d 100644 --- a/src/COMDIRAC/Interfaces/scripts/dchgrp.py +++ b/src/COMDIRAC/Interfaces/scripts/dchgrp.py @@ -1,17 +1,17 @@ #! /usr/bin/env python -######################################################################## -# $HeadURL$ -######################################################################## - """ Change file owner's group + +Examples: + $ dchown atsareg ././some_lfn_file + $ dchown -R pgay ./ """ from COMDIRAC.Interfaces import ConfigCache -from DIRAC.Core.Utilities.DIRACScript import DIRACScript as Script +from DIRAC.Core.Base.Script import Script from DIRAC import S_OK -class Params(object): +class Params: def __init__(self): self.recursive = False @@ -27,46 +27,24 @@ def getRecursive(self): def main(): params = Params() - Script.setUsageMessage( - "\n".join( - [ - __doc__.split("\n")[1], - "Usage:", - " %s [options] group Path..." % Script.scriptName, - "Arguments:", - " group: new group name", - " Path: path to file", - "", - "Examples:", - " $ dchown atsareg ././some_lfn_file", - " $ dchown -R pgay ./", - ] - ) - ) + Script.registerArgument(" group: new group name") + Script.registerArgument(["Path: path to file"]) Script.registerSwitch("R", "recursive", "recursive", params.setRecursive) configCache = ConfigCache() Script.parseCommandLine(ignoreErrors=True) configCache.cacheConfig() - args = Script.getPositionalArgs() + group, paths = Script.getPositionalArgs(group=True) - import DIRAC from DIRAC import gLogger from COMDIRAC.Interfaces import DSession from COMDIRAC.Interfaces import pathFromArgument session = DSession() - if len(args) < 2: - print("Error: not enough arguments provided\n%s:" % Script.scriptName) - Script.showHelp() - DIRAC.exit(-1) - - group = args[0] - lfns = [] - for path in args[1:]: + for path in paths: lfns.append(pathFromArgument(session, path)) from DIRAC.Resources.Catalog.FileCatalog import FileCatalog diff --git a/src/COMDIRAC/Interfaces/scripts/dchmod.py b/src/COMDIRAC/Interfaces/scripts/dchmod.py index 683a7a0..5a0bb41 100644 --- a/src/COMDIRAC/Interfaces/scripts/dchmod.py +++ b/src/COMDIRAC/Interfaces/scripts/dchmod.py @@ -1,13 +1,17 @@ #! /usr/bin/env python """ Change file mode bits + +Examples: + $ dchmod 755 ././some_lfn_file + $ dchmod -R 700 ./ """ from COMDIRAC.Interfaces import ConfigCache -from DIRAC.Core.Utilities.DIRACScript import DIRACScript as Script +from DIRAC.Core.Base.Script import Script from DIRAC import S_OK -class Params(object): +class Params: def __init__(self): self.recursive = False @@ -23,46 +27,24 @@ def getRecursive(self): def main(): params = Params() - Script.setUsageMessage( - "\n".join( - [ - __doc__.split("\n")[1], - "Usage:", - " %s [options] mode Path..." % Script.scriptName, - "Arguments:", - " mode: octal mode bits", - " Path: path to file", - "", - "Examples:", - " $ dchmod 755 ././some_lfn_file", - " $ dchmod -R 700 ./", - ] - ) - ) + Script.registerArgument(" mode: octal mode bits") + Script.registerArgument(["Path: path to file"]) Script.registerSwitch("R", "recursive", "recursive", params.setRecursive) configCache = ConfigCache() Script.parseCommandLine(ignoreErrors=True) configCache.cacheConfig() - args = Script.getPositionalArgs() + mode, paths = Script.getPositionalArgs(group=True) - import DIRAC from DIRAC import gLogger from COMDIRAC.Interfaces import DSession from COMDIRAC.Interfaces import pathFromArgument session = DSession() - if len(args) < 2: - print("Error: not enough arguments provided\n%s:" % Script.scriptName) - Script.showHelp() - DIRAC.exit(-1) - - mode = args[0] - lfns = [] - for path in args[1:]: + for path in paths: lfns.append(pathFromArgument(session, path)) from DIRAC.Resources.Catalog.FileCatalog import FileCatalog diff --git a/src/COMDIRAC/Interfaces/scripts/dchown.py b/src/COMDIRAC/Interfaces/scripts/dchown.py index dc753dd..5181511 100644 --- a/src/COMDIRAC/Interfaces/scripts/dchown.py +++ b/src/COMDIRAC/Interfaces/scripts/dchown.py @@ -1,13 +1,17 @@ #! /usr/bin/env python """ Change file owner + +Examples: + $ dchown atsareg ././some_lfn_file + $ dchown -R pgay ./ """ from COMDIRAC.Interfaces import ConfigCache -from DIRAC.Core.Utilities.DIRACScript import DIRACScript as Script +from DIRAC.Core.Base.Script import Script from DIRAC import S_OK -class Params(object): +class Params: def __init__(self): self.recursive = False @@ -23,46 +27,24 @@ def getRecursive(self): def main(): params = Params() - Script.setUsageMessage( - "\n".join( - [ - __doc__.split("\n")[1], - "Usage:", - " %s [options] owner Path..." % Script.scriptName, - "Arguments:", - " owner: new owner name", - " Path: path to file", - "", - "Examples:", - " $ dchown atsareg ././some_lfn_file", - " $ dchown -R pgay ./", - ] - ) - ) + Script.registerArgument(" owner: new owner name") + Script.registerArgument(["Path: path to file"]) Script.registerSwitch("R", "recursive", "recursive", params.setRecursive) configCache = ConfigCache() Script.parseCommandLine(ignoreErrors=True) configCache.cacheConfig() - args = Script.getPositionalArgs() + owner, paths = Script.getPositionalArgs(group=True) - import DIRAC from DIRAC import gLogger from COMDIRAC.Interfaces import DSession from COMDIRAC.Interfaces import pathFromArgument session = DSession() - if len(args) < 2: - print("Error: not enough arguments provided\n%s:" % Script.scriptName) - Script.showHelp() - DIRAC.exit(-1) - - owner = args[0] - lfns = [] - for path in args[1:]: + for path in paths: lfns.append(pathFromArgument(session, path)) from DIRAC.Resources.Catalog.FileCatalog import FileCatalog diff --git a/src/COMDIRAC/Interfaces/scripts/dconfig.py b/src/COMDIRAC/Interfaces/scripts/dconfig.py index 99ac14d..532655e 100644 --- a/src/COMDIRAC/Interfaces/scripts/dconfig.py +++ b/src/COMDIRAC/Interfaces/scripts/dconfig.py @@ -1,11 +1,10 @@ #! /usr/bin/env python - """ configure DCommands """ import DIRAC from DIRAC import S_OK -from DIRAC.Core.Utilities.DIRACScript import DIRACScript as Script +from DIRAC.Core.Base.Script import Script from COMDIRAC.Interfaces import ( DConfig, @@ -16,7 +15,7 @@ from COMDIRAC.Interfaces import getDNFromProxy -class Params(object): +class Params: def __init__(self): self.minimal = False self.guessProfile = False @@ -40,22 +39,13 @@ def getGuessProfiles(self): def main(): params = Params() - Script.setUsageMessage( - "\n".join( - [ - __doc__.split("\n")[1], - "Usage:", - " %s [options] [section[.option[=value]]]..." % Script.scriptName, - "Arguments:", - " without argument: display whole configuration content", - "++ OR ++", - " section: display all options in section", - "++ OR ++", - " section.option: display option", - "++ OR ++", - " section.option=value: set option value", - ] - ) + Script.registerArgument( + [ + "section[.option[=value]]: section: display all options in section\n" + " section.option: display option\n" + " section.option=value: set option value" + ], + mandatory=False, ) Script.registerSwitch( "m", "minimal", "verify and fill minimal configuration", params.setMinimal @@ -77,12 +67,12 @@ def main(): Script.enableCS() result = getDNFromProxy() if not result["OK"]: - print("ERROR: %s" % result["Message"]) + print("ERROR:", result["Message"]) DIRAC.exit(2) dn = result["Value"] result = guessProfilesFromCS(dn) if not result["OK"]: - print("ERROR: %s" % result["Message"]) + print("ERROR:", result["Message"]) DIRAC.exit(2) profiles = result["Value"] diff --git a/src/COMDIRAC/Interfaces/scripts/dfind.py b/src/COMDIRAC/Interfaces/scripts/dfind.py index d612dde..b7697d8 100644 --- a/src/COMDIRAC/Interfaces/scripts/dfind.py +++ b/src/COMDIRAC/Interfaces/scripts/dfind.py @@ -1,10 +1,11 @@ #! /usr/bin/env python - """ find files in the FileCatalog + +Examples: + $ dfind . "some_integer_metadata>1" """ -import DIRAC -from DIRAC.Core.Utilities.DIRACScript import DIRACScript as Script +from DIRAC.Core.Base.Script import Script @Script() @@ -15,37 +16,23 @@ def main(): from COMDIRAC.Interfaces import ConfigCache - Script.setUsageMessage( - "\n".join( - [ - __doc__.split("\n")[1], - "Usage:", - " %s [options] lfn metaspec..." % Script.scriptName, - "Arguments:", - " lfn: directory entry in the FileCatalog", - ' metaspec: metadata index specifcation (of the form: "meta=value" or "meta1"', - ] - ) - ) - configCache = ConfigCache() + Script.registerArgument(" lfn: directory entry in the FileCatalog") + Script.registerArgument( + [ + 'metaspec: metadata index specifcation (of the form: "meta=value" or "meta 0: + if argss: try: optlist, arguments = getopt.getopt(argss, short_opts, long_opts) except getopt.GetoptError as e: diff --git a/src/COMDIRAC/Interfaces/scripts/dmeta.py b/src/COMDIRAC/Interfaces/scripts/dmeta.py index dfbf1e2..89fc939 100644 --- a/src/COMDIRAC/Interfaces/scripts/dmeta.py +++ b/src/COMDIRAC/Interfaces/scripts/dmeta.py @@ -1,5 +1,4 @@ #! /usr/bin/env python - """ manipulate metadata in the FileCatalog """ @@ -11,7 +10,7 @@ from COMDIRAC.Interfaces import pathFromArgument -class DMetaCommand(object): +class DMetaCommand: def run(self, lfn, metas): raise NotImplementedError @@ -61,14 +60,14 @@ def run(self, lfn, metas): print(meta + "=" + metadict[meta]) -from DIRAC.Core.Utilities.DIRACScript import DIRACScript as Script +from DIRAC.Core.Base.Script import Script @Script() def main(): from COMDIRAC.Interfaces import ConfigCache - class Params(object): + class Params: def __init__(self): self.index = False self.listIndex = False @@ -186,16 +185,14 @@ def getListIndex(self): if len(args) < 2: print("Error: Not enough arguments provided\n%s:" % Script.scriptName) - Script.showHelp() - DIRAC.exit(-1) + Script.showHelp(exitCode=-1) command = args[0] if command not in meta_commands.keys(): print('Error: Unknown dmeta command "%s"' % command) print("%s:" % Script.scriptName) - Script.showHelp() - DIRAC.exit(-1) + Script.showHelp(exitCode=-1) command = meta_commands[command] diff --git a/src/COMDIRAC/Interfaces/scripts/dmkdir.py b/src/COMDIRAC/Interfaces/scripts/dmkdir.py index 067c32d..732ed21 100644 --- a/src/COMDIRAC/Interfaces/scripts/dmkdir.py +++ b/src/COMDIRAC/Interfaces/scripts/dmkdir.py @@ -1,11 +1,12 @@ #! /usr/bin/env python - """ create a directory in the FileCatalog + +Examples: + $ dmkdir ./some_lfn_dir """ -import DIRAC from COMDIRAC.Interfaces import ConfigCache -from DIRAC.Core.Utilities.DIRACScript import DIRACScript as Script +from DIRAC.Core.Base.Script import Script @Script() @@ -14,43 +15,23 @@ def main(): from COMDIRAC.Interfaces import createCatalog from COMDIRAC.Interfaces import pathFromArguments - Script.setUsageMessage( - "\n".join( - [ - __doc__.split("\n")[1], - "Usage:", - " %s Path..." % Script.scriptName, - "Arguments:", - " Path: path to new directory", - "", - "Examples:", - " $ dmkdir ./some_lfn_dir", - ] - ) - ) - configCache = ConfigCache() + Script.registerArgument(["Path: path to new directory"]) Script.parseCommandLine(ignoreErrors=True) configCache.cacheConfig() args = Script.getPositionalArgs() session = DSession() - - if len(args) < 1: - print("Error: No argument provided\n%s:" % Script.scriptName) - Script.showHelp() - DIRAC.exit(-1) - catalog = createCatalog() result = catalog.createDirectory(pathFromArguments(session, args)) if result["OK"]: if result["Value"]["Failed"]: for p in result["Value"]["Failed"]: - print('ERROR - "%s": %s' % (p, result["Value"]["Failed"][p])) + print(f'ERROR - "{p}":', result["Value"]["Failed"][p]) else: - print("ERROR: %s" % result["Message"]) + print("ERROR:", result["Message"]) if __name__ == "__main__": diff --git a/src/COMDIRAC/Interfaces/scripts/doutput.py b/src/COMDIRAC/Interfaces/scripts/doutput.py index 83ca3fa..50088d9 100644 --- a/src/COMDIRAC/Interfaces/scripts/doutput.py +++ b/src/COMDIRAC/Interfaces/scripts/doutput.py @@ -4,12 +4,12 @@ import DIRAC from DIRAC import S_OK from COMDIRAC.Interfaces import ConfigCache -from DIRAC.Core.Utilities.DIRACScript import DIRACScript as Script +from DIRAC.Core.Base.Script import Script import os -class Params(object): +class Params: def __init__(self): self.outputDir = None self.outputData = False @@ -74,18 +74,7 @@ def getInputFile(self): def main(): params = Params() - Script.setUsageMessage( - "\n".join( - [ - __doc__.split("\n")[1], - "Usage:", - " %s [option|cfgfile] ... JobID ..." % Script.scriptName, - "Arguments:", - " JobID: DIRAC Job ID", - ] - ) - ) - + Script.registerArgument(["JobID: DIRAC Job ID"], mandatory=False) Script.registerSwitch( "D:", "OutputDir=", "destination directory", params.setOutputDir ) @@ -155,8 +144,7 @@ def main(): for arg in args: if os.path.isdir(os.path.join(outputDir, arg)) and not params.getNoJobDir(): print( - "Output for job %s already retrieved, remove the output directory to redownload" - % arg + f"Output for job {arg} already retrieved, remove the output directory to redownload" ) else: jobs.append(arg) @@ -201,7 +189,7 @@ def main(): exitCode = 2 for error in errors: - print("ERROR: %s" % error) + print("ERROR:", error) if params.getVerbose(): for j, d in inputs.items(): diff --git a/src/COMDIRAC/Interfaces/scripts/dput.py b/src/COMDIRAC/Interfaces/scripts/dput.py index 1bcf404..38d5c78 100644 --- a/src/COMDIRAC/Interfaces/scripts/dput.py +++ b/src/COMDIRAC/Interfaces/scripts/dput.py @@ -9,7 +9,7 @@ import DIRAC from DIRAC import S_OK -from DIRAC.Core.Utilities.DIRACScript import DIRACScript as Script +from DIRAC.Core.Base.Script import Script @Script() @@ -22,7 +22,7 @@ def main(): from COMDIRAC.Interfaces import ConfigCache - class Params(object): + class Params: def __init__(self): self.destinationSE = False self.recursive = False @@ -83,8 +83,7 @@ def getRecursive(self): if len(args) < 1: error("Error: No argument provided\n%s:" % Script.scriptName) - Script.showHelp() - DIRAC.exit(0) + Script.showHelp(exitCode=-1) # local file localPath = args[0] diff --git a/src/COMDIRAC/Interfaces/scripts/dpwd.py b/src/COMDIRAC/Interfaces/scripts/dpwd.py index a49089e..cf24c29 100644 --- a/src/COMDIRAC/Interfaces/scripts/dpwd.py +++ b/src/COMDIRAC/Interfaces/scripts/dpwd.py @@ -1,9 +1,8 @@ #! /usr/bin/env python - """ print DCommands working directory """ -from DIRAC.Core.Utilities.DIRACScript import DIRACScript as Script +from DIRAC.Core.Base.Script import Script @Script() @@ -11,27 +10,11 @@ def main(): from COMDIRAC.Interfaces import DSession from COMDIRAC.Interfaces import ConfigCache - Script.setUsageMessage( - "\n".join( - [ - __doc__.split("\n")[1], - "Usage:", - " %s [options]" % Script.scriptName, - ] - ) - ) - configCache = ConfigCache() Script.parseCommandLine(ignoreErrors=True) configCache.cacheConfig() - session = DSession() - - args = Script.getPositionalArgs() - - ret = session.getCwd() - - print(ret) + print(DSession().getCwd()) if __name__ == "__main__": diff --git a/src/COMDIRAC/Interfaces/scripts/drepl.py b/src/COMDIRAC/Interfaces/scripts/drepl.py index 8b962fd..5dfe410 100644 --- a/src/COMDIRAC/Interfaces/scripts/drepl.py +++ b/src/COMDIRAC/Interfaces/scripts/drepl.py @@ -15,10 +15,13 @@ * first(N) - replicate file to N first SEs listed in option "replication_ses" * random(N) - replicatefile to N randomly chosen SEs from the list in option "replication_ses" +Examples + $ drepl ./some_lfn_file + $ drepl -D SOME-DESTINATION-SE-disk ./some_lfn_file """ import DIRAC from DIRAC import S_OK -from DIRAC.Core.Utilities.DIRACScript import DIRACScript as Script +from DIRAC.Core.Base.Script import Script @Script() @@ -26,10 +29,9 @@ def main(): from COMDIRAC.Interfaces import error from COMDIRAC.Interfaces import DSession from COMDIRAC.Interfaces import pathFromArguments - from COMDIRAC.Interfaces import ConfigCache - class Params(object): + class Params: def __init__(self): self.destinationSE = False self.sourceSE = False @@ -50,21 +52,7 @@ def getSourceSE(self): params = Params() - Script.setUsageMessage( - "\n".join( - [ - __doc__.split("\n")[1], - "Usage:", - " %s [options] lfn..." % Script.scriptName, - "Arguments:", - " lfn: file entry in the FileCatalog", - "", - "Examples", - " $ drepl ./some_lfn_file", - " $ drepl -D SOME-DESTINATION-SE-disk ./some_lfn_file", - ] - ) - ) + Script.registerArgument(["lfn: file entry in the FileCatalog"]) Script.registerSwitch( "D:", "destination-se=", @@ -89,11 +77,6 @@ def getSourceSE(self): dirac = Dirac() - if len(args) < 1: - error("Error: No argument provided\n%s:" % Script.scriptName) - Script.showHelp() - DIRAC.exit(-1) - # default lfn: same file name as local_path lfns = pathFromArguments(session, args) diff --git a/src/COMDIRAC/Interfaces/scripts/dreplicas.py b/src/COMDIRAC/Interfaces/scripts/dreplicas.py index 4e4edf0..7e937ad 100644 --- a/src/COMDIRAC/Interfaces/scripts/dreplicas.py +++ b/src/COMDIRAC/Interfaces/scripts/dreplicas.py @@ -1,10 +1,9 @@ #! /usr/bin/env python - """ -list replicas for files in the FileCatalog +Print list replicas for files in the FileCatalog """ import DIRAC -from DIRAC.Core.Utilities.DIRACScript import DIRACScript as Script +from DIRAC.Core.Base.Script import Script @Script() @@ -17,19 +16,8 @@ def main(): from COMDIRAC.Interfaces import ConfigCache - Script.setUsageMessage( - "\n".join( - [ - __doc__.split("\n")[1], - "Usage:", - " %s lfn..." % Script.scriptName, - "Arguments:", - " lfn: logical file name", - ] - ) - ) - configCache = ConfigCache() + Script.registerArgument(["lfn: logical file name"]) Script.parseCommandLine(ignoreErrors=True) configCache.cacheConfig() @@ -38,11 +26,6 @@ def main(): session = DSession() catalog = DCatalog() - if len(args) < 1: - error("No argument provided\n%s:" % Script.scriptName) - Script.showHelp() - DIRAC.exit(-1) - exitCode = 0 for arg in args: diff --git a/src/COMDIRAC/Interfaces/scripts/drm.py b/src/COMDIRAC/Interfaces/scripts/drm.py index af72985..25c96e9 100644 --- a/src/COMDIRAC/Interfaces/scripts/drm.py +++ b/src/COMDIRAC/Interfaces/scripts/drm.py @@ -1,22 +1,22 @@ #! /usr/bin/env python - """ -remove files from the FileCatalog (and all replicas from Storage Elements) +Remove files from the FileCatalog (and all replicas from Storage Elements) + +Examples: + $ drm ./some_lfn_file """ import os import DIRAC from DIRAC import S_OK -from DIRAC.Core.Utilities.DIRACScript import DIRACScript as Script +from DIRAC.Core.Base.Script import Script @Script() def main(): from COMDIRAC.Interfaces import DSession from COMDIRAC.Interfaces import DCatalog - from COMDIRAC.Interfaces import pathFromArgument - from COMDIRAC.Interfaces import ConfigCache lfnFileName = "" @@ -40,21 +40,7 @@ def setDirFlag(arg): rmDirFlag = True return S_OK() - Script.setUsageMessage( - "\n".join( - [ - __doc__.split("\n")[1], - "Usage:", - " %s [options] [lfn]..." % Script.scriptName, - "Arguments:", - " lfn: logical file name", - "", - "Examples:", - " $ drm ./some_lfn_file", - ] - ) - ) - + Script.registerArgument(["lfn: logical file name"], mandatory=False) Script.registerSwitch( "F:", "lfnFile=", "file containing a list of LFNs", setLfnFileName ) @@ -72,10 +58,9 @@ def setDirFlag(arg): session = DSession() catalog = DCatalog() - if len(args) < 1 and not lfnFileName: + if not args and not lfnFileName: print("Error: No argument provided\n%s:" % Script.scriptName) - Script.showHelp() - DIRAC.exit(-1) + Script.showHelp(exitCode=-1) lfns = set() for path in args: @@ -105,14 +90,13 @@ def setDirFlag(arg): exitCode = 0 goodCounter = 0 badCounter = 0 - failed = {} for lfn in lfns: if rmDirFlag and not catalog.isFile(lfn): result = returnSingleResult(dm.cleanLogicalDirectory(lfn)) if result["OK"]: goodCounter += 1 else: - print("ERROR: %s" % result["Message"]) + print("ERROR:", result["Message"]) badCounter += 1 exitCode = 3 else: diff --git a/src/COMDIRAC/Interfaces/scripts/drmdir.py b/src/COMDIRAC/Interfaces/scripts/drmdir.py index 8e2abdc..dd18b63 100644 --- a/src/COMDIRAC/Interfaces/scripts/drmdir.py +++ b/src/COMDIRAC/Interfaces/scripts/drmdir.py @@ -1,11 +1,12 @@ #! /usr/bin/env python - """ remove FileCatalog directories. Attention ! This command does not remove directories and files on the physical storage. + +Examples: + $ drmdir ./some_lfn_directory """ -import DIRAC -from DIRAC.Core.Utilities.DIRACScript import DIRACScript as Script +from DIRAC.Core.Base.Script import Script @Script() @@ -13,46 +14,25 @@ def main(): from COMDIRAC.Interfaces import DSession from COMDIRAC.Interfaces import createCatalog from COMDIRAC.Interfaces import pathFromArguments - from COMDIRAC.Interfaces import ConfigCache - Script.setUsageMessage( - "\n".join( - [ - __doc__.split("\n")[1], - "Usage:", - " %s [Path]..." % Script.scriptName, - "Arguments:", - " Path: directory path", - "", - "Examples:", - " $ drmdir ./some_lfn_directory", - ] - ) - ) - configCache = ConfigCache() + Script.registerArgument(["Path: directory path"]) Script.parseCommandLine(ignoreErrors=True) configCache.cacheConfig() args = Script.getPositionalArgs() session = DSession() - - if len(args) < 1: - print("Error: No argument provided\n%s:" % Script.scriptName) - Script.showHelp() - DIRAC.exit(-1) - catalog = createCatalog() result = catalog.removeDirectory(pathFromArguments(session, args)) if result["OK"]: if result["Value"]["Failed"]: for p in result["Value"]["Failed"]: - print('ERROR - "%s": %s' % (p, result["Value"]["Failed"][p])) + print(f'ERROR - "{p}":', result["Value"]["Failed"][p]) else: - print("ERROR: %s" % result["Message"]) + print("ERROR:", result["Message"]) if __name__ == "__main__": diff --git a/src/COMDIRAC/Interfaces/scripts/dsetenv.py b/src/COMDIRAC/Interfaces/scripts/dsetenv.py index a5ef2e6..257a448 100644 --- a/src/COMDIRAC/Interfaces/scripts/dsetenv.py +++ b/src/COMDIRAC/Interfaces/scripts/dsetenv.py @@ -1,32 +1,22 @@ #! /usr/bin/env python - """ register DCommands session environment variables """ -from DIRAC.Core.Utilities.DIRACScript import DIRACScript as Script +from DIRAC.Core.Base.Script import Script @Script() def main(): from COMDIRAC.Interfaces import DSession - from COMDIRAC.Interfaces import ConfigCache - Script.setUsageMessage( - "\n".join( - [ - __doc__.split("\n")[1], - "Usage:", - " %s [[section.]option=value]..." % Script.scriptName, - "Arguments:", - ' section: section (defaults to "session:environment")', - " option: option name", - " value: value to be set", - ] - ) - ) - configCache = ConfigCache() + Script.registerArgument( + '[section.]option=value: section: section (defaults to "session:environment")\n' + " option: option name\n" + " value: value to be set", + mandatory=False, + ) Script.parseCommandLine(ignoreErrors=True) configCache.cacheConfig() diff --git a/src/COMDIRAC/Interfaces/scripts/dsize.py b/src/COMDIRAC/Interfaces/scripts/dsize.py index 33c9f37..162370d 100644 --- a/src/COMDIRAC/Interfaces/scripts/dsize.py +++ b/src/COMDIRAC/Interfaces/scripts/dsize.py @@ -1,12 +1,11 @@ #! /usr/bin/env python - """ print FileCatalog file or directory disk usage """ from signal import signal, SIGPIPE, SIG_DFL from DIRAC import S_OK -from DIRAC.Core.Utilities.DIRACScript import DIRACScript as Script +from DIRAC.Core.Base.Script import Script @Script() @@ -14,13 +13,12 @@ def main(): from COMDIRAC.Interfaces import DSession from COMDIRAC.Interfaces import createCatalog from COMDIRAC.Interfaces import pathFromArguments - from COMDIRAC.Interfaces import ConfigCache # broken pipe default behaviour signal(SIGPIPE, SIG_DFL) - class Params(object): + class Params: def __init__(self): self.long = False self.rawFiles = False @@ -52,6 +50,7 @@ def getRawFiles(self): ] ) ) + Script.registerArgument("path: file/directory path", mandatory=False) Script.registerSwitch("l", "long", "detailled listing", params.setLong) Script.registerSwitch("f", "raw-files", "reverse sort order", params.setRawFiles) diff --git a/src/COMDIRAC/Interfaces/scripts/dstat.py b/src/COMDIRAC/Interfaces/scripts/dstat.py index 176acf1..b1b1a38 100644 --- a/src/COMDIRAC/Interfaces/scripts/dstat.py +++ b/src/COMDIRAC/Interfaces/scripts/dstat.py @@ -1,5 +1,4 @@ #! /usr/bin/env python - """ Retrieve status of DIRAC jobs """ @@ -8,7 +7,7 @@ from DIRAC import exit as DIRACExit, S_OK, S_ERROR from COMDIRAC.Interfaces import DSession from COMDIRAC.Interfaces import ConfigCache -from DIRAC.Core.Utilities.DIRACScript import DIRACScript as Script +from DIRAC.Core.Base.Script import Script from DIRAC.Core.Utilities.Time import toString, date, day from DIRAC.WorkloadManagementSystem.Client.JobMonitoringClient import ( JobMonitoringClient, @@ -89,7 +88,7 @@ def chunks(l, n): ] -class Params(object): +class Params: def __init__(self): self.__session = None self.user = None @@ -173,16 +172,7 @@ def getInputFile(self): def main(): params = Params() - Script.setUsageMessage( - "\n".join( - [ - __doc__.split("\n")[1], - "Usage:", - " %s [option|cfgfile] " % Script.scriptName, - "Arguments:", - ] - ) - ) + Script.registerArgument(["JobID: DIRAC Job ID"], mandatory=False) Script.registerSwitch("u:", "User=", "job owner", params.setUser) Script.registerSwitch( "S:", @@ -281,7 +271,7 @@ def main(): for chunk in chunks(jobs, 1000): result = getJobSummary(chunk) if not result["OK"]: - print("ERROR: %s" % result["Message"]) + print("ERROR:", result["Message"]) DIRACExit(2) # filter on job statuses diff --git a/src/COMDIRAC/Interfaces/scripts/dsub.py b/src/COMDIRAC/Interfaces/scripts/dsub.py index 9bee8db..5183294 100644 --- a/src/COMDIRAC/Interfaces/scripts/dsub.py +++ b/src/COMDIRAC/Interfaces/scripts/dsub.py @@ -1,9 +1,8 @@ #! /usr/bin/env python - """ - Submit jobs to DIRAC WMS +Submit jobs to DIRAC WMS - Default JDL can be configured from session in the "JDL" option +Default JDL can be configured from session in the "JDL" option """ import os.path import sys @@ -14,7 +13,7 @@ from DIRAC import exit as DIRACexit from COMDIRAC.Interfaces import ConfigCache from COMDIRAC.Interfaces import pathFromArgument -from DIRAC.Core.Utilities.DIRACScript import DIRACScript as Script +from DIRAC.Core.Base.Script import Script from DIRAC.Core.Utilities.ClassAd.ClassAdLight import ClassAd classAdJob = None @@ -61,7 +60,7 @@ def classAdAppendToSandbox(classAd, f, sbName): classAdJob.insertAttributeVectorString(sbName, sb) -class Params(object): +class Params: def __init__(self): self.__session = None self.attribs = {} @@ -338,24 +337,20 @@ def main(): params = Params() - Script.setUsageMessage( - "\n".join( - [ - __doc__.split("\n")[1], - "Usage:", - " %s [option|cfgfile] [executable [--] [arguments...]]" - % Script.scriptName, - "Arguments:", - " executable: command to be run inside the job. ", - " If a relative path, local file will be included in InputSandbox", - " If no executable is given and JDL (provided or default) doesn't contain one,", - " standard input will be read for executable contents", - " arguments: arguments to pass to executable", - " if some arguments are to begin with a dash '-', prepend '--' before them", - ] - ) + Script.registerArgument( + "executable: command to be run inside the job.\n" + " If a relative path, local file will be included in InputSandbox\n" + " If no executable is given and JDL (provided or default) doesn't contain one,\n" + " standard input will be read for executable contents", + mandatory=False, + ) + Script.registerArgument( + [ + "arguments: arguments to pass to executable\n" + " if some arguments are to begin with a dash '-', prepend '--' before them" + ], + mandatory=False, ) - Script.registerSwitch("J:", "JDL=", "JDL file or inline", params.setJDL) Script.registerSwitch("N:", "JobName=", "job name", params.setName) Script.registerSwitch( diff --git a/src/COMDIRAC/Interfaces/scripts/dunsetenv.py b/src/COMDIRAC/Interfaces/scripts/dunsetenv.py index 2b3985a..250c3e2 100644 --- a/src/COMDIRAC/Interfaces/scripts/dunsetenv.py +++ b/src/COMDIRAC/Interfaces/scripts/dunsetenv.py @@ -1,30 +1,21 @@ #! /usr/bin/env python - """ -remove DCommands session environment variables +Remove DCommands session environment variables """ from COMDIRAC.Interfaces import DSession -from DIRAC.Core.Utilities.DIRACScript import DIRACScript as Script +from DIRAC.Core.Base.Script import Script @Script() def main(): from COMDIRAC.Interfaces import ConfigCache - Script.setUsageMessage( - "\n".join( - [ - __doc__.split("\n")[1], - "Usage:", - " %s [[section.]option]..." % Script.scriptName, - "Arguments:", - ' section: section (default: "session:environment")', - " option: option name", - ] - ) - ) - configCache = ConfigCache() + Script.registerArgument( + '[section.]option=value: section: section (defaults to "session:environment")\n' + " option: option name", + mandatory=False, + ) Script.parseCommandLine(ignoreErrors=True) configCache.cacheConfig()