From a2169ab9936151d40c48322a4464d0003bf8242b Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Fri, 10 Feb 2023 16:57:00 +0100 Subject: [PATCH 1/5] add pre-commit config files --- .clang-format | 2 +- .cmake-format.yaml | 6 ++++++ .pre-commit-config.yaml | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 .cmake-format.yaml create mode 100644 .pre-commit-config.yaml diff --git a/.clang-format b/.clang-format index 686e2d9..f01f570 100644 --- a/.clang-format +++ b/.clang-format @@ -7,4 +7,4 @@ DerivePointerAlignment: false PointerAlignment: Left IndentWidth: 4 -ColumnLimit : 110 +ColumnLimit: 110 diff --git a/.cmake-format.yaml b/.cmake-format.yaml new file mode 100644 index 0000000..5b703c9 --- /dev/null +++ b/.cmake-format.yaml @@ -0,0 +1,6 @@ +# https://cmake-format.readthedocs.io/en/latest/configuration.html + +format: + tab_size: 4 + separate_ctrl_name_with_space: true + separate_fn_name_with_space: false diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..87c5b7f --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,35 @@ +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.4.0 + hooks: + - id: check-added-large-files + - id: check-case-conflict + - id: check-merge-conflict + - id: check-symlinks + - id: check-yaml + - id: check-xml + - id: requirements-txt-fixer + - id: end-of-file-fixer + exclude: external/.*$ + - id: mixed-line-ending + exclude: external/.*$ + - id: trailing-whitespace + exclude: external/.*$ + + - repo: https://github.com/pre-commit/mirrors-clang-format + rev: v15.0.6 + hooks: + - id: clang-format + exclude: external/.*$ + + - repo: https://github.com/cheshirekow/cmake-format-precommit + rev: v0.6.13 + hooks: + - id: cmake-format + additional_dependencies: [ pyyaml ] + # - id: cmake-lint + + - repo: https://github.com/psf/black + rev: 22.12.0 + hooks: + - id: black From 2373b5d3e92c602c74b5943f4563d4ea04736663 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Fri, 10 Feb 2023 17:09:14 +0100 Subject: [PATCH 2/5] run pre-commit on all files --- CMakeLists.txt | 45 ++--- inc/TRestGeant4ParticleSourceDecay0.h | 2 +- macros/REST_Geant4_ListIsotopes.C | 4 +- pipeline/clang-format/clang-format.sh | 1 - pipeline/clang-format/clangformattest.sh | 1 - pipeline/validateLibrary.py | 224 ++++++++++++----------- src/TRestGeant4ParticleSourceDecay0.cxx | 2 +- 7 files changed, 143 insertions(+), 136 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e19706f..0acf451 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,30 +4,29 @@ add_definitions(-DLIBRARY_VERSION="${LibraryVersion}") if (${REST_DECAY0} MATCHES "ON") add_definitions(-DUSE_Decay0) # TODO Issue #6 at rest-framework - #---------------------------------------------------------------------------- - # Find package Decay0 and ROOT - # find_package(BxDecay0 1.0.9 CONFIG COMPONENTS manager REQUIRED) + # ---------------------------------------------------------------------------- + # Find package Decay0 and ROOT find_package(BxDecay0 1.0.9 CONFIG COMPONENTS + # manager REQUIRED) # - # find_package was not working properly for me in 1.0.9. So I was using - # bxdecay0-config. But in case Decay0 flag is enabled and we do not - # have a bxdecay0 installation we might run into problems + # find_package was not working properly for me in 1.0.9. So I was using + # bxdecay0-config. But in case Decay0 flag is enabled and we do not have a + # bxdecay0 installation we might run into problems # execute_process( - COMMAND bxdecay0-config --version - OUTPUT_VARIABLE BxDecay0_VERSION - OUTPUT_STRIP_TRAILING_WHITESPACE) + COMMAND bxdecay0-config --version + OUTPUT_VARIABLE BxDecay0_VERSION + OUTPUT_STRIP_TRAILING_WHITESPACE) execute_process( - COMMAND bxdecay0-config --incdir - OUTPUT_VARIABLE BxDecay0_INCLUDE_DIRS - OUTPUT_STRIP_TRAILING_WHITESPACE) + COMMAND bxdecay0-config --incdir + OUTPUT_VARIABLE BxDecay0_INCLUDE_DIRS + OUTPUT_STRIP_TRAILING_WHITESPACE) execute_process( - COMMAND bxdecay0-config --libdir - OUTPUT_VARIABLE BxDecay0_LIBRARY_DIRS - OUTPUT_STRIP_TRAILING_WHITESPACE) - + COMMAND bxdecay0-config --libdir + OUTPUT_VARIABLE BxDecay0_LIBRARY_DIRS + OUTPUT_STRIP_TRAILING_WHITESPACE) if (NOT DEFINED BxDecay0_VERSION) message(FATAL_ERROR "bxdecay0 not found! Put its binary dir to PATH!") @@ -41,7 +40,9 @@ if (${REST_DECAY0} MATCHES "ON") set(external_libs "${external_libs} -L${BxDecay0_LIBRARY_DIRS} -lBxDecay0") set(feature_added "Decay0") - set(feature_added ${feature_added} PARENT_SCOPE) + set(feature_added + ${feature_added} + PARENT_SCOPE) else () set(REST_DECAY0 OFF) set(excludes ${excludes} TRestGeant4ParticleSourceDecay0) @@ -51,13 +52,15 @@ if (NOT ${REST_EVE} MATCHES "ON") set(excludes ${excludes} TRestGeant4EventViewer) endif () -COMPILELIB("") +compilelib("") file(GLOB_RECURSE MAC "${CMAKE_CURRENT_SOURCE_DIR}/macros/*") -INSTALL(FILES ${MAC} DESTINATION ./macros/geant4) +install(FILES ${MAC} DESTINATION ./macros/geant4) set(rest_macros ${rest_macros} "restGeant4_ViewEvent") set(rest_macros ${rest_macros} "restGeant4_ViewGeometry") -set(rest_macros ${rest_macros} PARENT_SCOPE) +set(rest_macros + ${rest_macros} + PARENT_SCOPE) -ADD_LIBRARY_TEST() +add_library_test() diff --git a/inc/TRestGeant4ParticleSourceDecay0.h b/inc/TRestGeant4ParticleSourceDecay0.h index cb02445..b892c93 100644 --- a/inc/TRestGeant4ParticleSourceDecay0.h +++ b/inc/TRestGeant4ParticleSourceDecay0.h @@ -33,4 +33,4 @@ class TRestGeant4ParticleSourceDecay0 : public TRestGeant4ParticleSource { ~TRestGeant4ParticleSourceDecay0() { delete fDecay0Model; } ClassDefOverride(TRestGeant4ParticleSourceDecay0, 1); }; -#endif \ No newline at end of file +#endif diff --git a/macros/REST_Geant4_ListIsotopes.C b/macros/REST_Geant4_ListIsotopes.C index a972a26..b7014f3 100644 --- a/macros/REST_Geant4_ListIsotopes.C +++ b/macros/REST_Geant4_ListIsotopes.C @@ -70,8 +70,8 @@ Int_t REST_Geant4_ListIsotopes(TString fName, TString fOutName) { } } if (kaux != 1) { - // cout<<"new isotope "<GetTrack(i).GetParticleName()<<" in track "<GetTrack(i).GetParticleName()<<" in track + // "<GetTrack(i).GetParticleName(); ni[k] = 1; // cout<<"k "<= 0: - print ("OK") + print("OK") return else: - print( "Problem found at class : " + className ) - print( "SetLibraryVersion was NOT found at Initialization!" ) + print("Problem found at class : " + className) + print("SetLibraryVersion was NOT found at Initialization!") sys.exit(1) return + def getObservablePositions(data): obsposes = {} pos = 0 - str = "SETOBSERVABLEVALUE(\"" - while(pos < len(data)): - pos1 = data.find(str,pos) - if(pos1 == -1): - break + str = 'SETOBSERVABLEVALUE("' + while pos < len(data): + pos1 = data.find(str, pos) + if pos1 == -1: + break pos1 += len(str) - pos2 = data.find("\"",pos1) - if(pos2 == -1): - break + pos2 = data.find('"', pos1) + if pos2 == -1: + break name = data[pos1:pos2] - if(not name in obsposes): + if not name in obsposes: obsposes[name] = pos1 pos = pos2 + 1 @@ -60,115 +61,120 @@ def getMethodDefinition(text): counter = 1 start = initPos + 1 - while(counter > 0): - pos1 = text.find("{",start) - pos2 = text.find("}",start) + while counter > 0: + pos1 = text.find("{", start) + pos2 = text.find("}", start) endPosition = pos2 + 1 - if(pos1 != -1 and pos2 != -1): - if(pos1 < pos2): + if pos1 != -1 and pos2 != -1: + if pos1 < pos2: counter = counter + 1 start = pos1 + 1 - if(pos2 < pos1): + if pos2 < pos1: counter = counter - 1 start = pos2 + 1 elif pos1 != -1: - print ("Big error!!") + print("Big error!!") else: - counter = counter - 1 - start = pos2 + 1 + counter = counter - 1 + start = pos2 + 1 return text[initPos:endPosition].upper() -def removeCppComment(strInput) : - state = 0; - strOutput = '' - strRemoved = '' - - for c in strInput : - if state == 0 and c == '/' : # ex. [/] - state = 1 - elif state == 1 and c == '*' : # ex. [/*] - state = 2 - elif state == 1 and c == '/' : # ex. [#] - state = 4 - elif state == 1 : # ex. [ or 5/3] - state = 0 - - elif state == 3 and c == '*': # ex. [/*he**] - state = 3 - elif state == 2 and c == '*': # ex. [/*he*] - state = 3 - elif state == 2: # ex. [/*heh] - state = 2 - - elif state == 3 and c == '/': # ex. [/*heh*/] - state = 0 - elif state == 3: # ex. [/*heh*e] - state = 2 - - elif state == 4 and c == '\\': # ex. [//hehe\] - state = 9 - elif state == 9 and c == '\\': # ex. [//hehe\\\\\] - state = 9 - elif state == 9: # ex. [//hehe\ or //hehe\a] - state = 4 - elif state == 4 and c == '\n': # ex. [//hehe] - state = 0 - - elif state == 0 and c == '\'': # ex. ['] - state = 5 - elif state == 5 and c == '\\': # ex. ['\] - state = 6 - elif state == 6: # ex. ['\n or '\' or '\t etc.] - state = 5 - elif state == 5 and c == '\'': # ex. ['\n' or '\'' or '\t' ect.] - state = 0 - - elif state == 0 and c == '\"': # ex. ["] - state = 7 - elif state == 7 and c == '\\': # ex. ["\] - state = 8 - elif state == 8: # ex. ["\n or "\" or "\t ect.] - state = 7 - elif state == 7 and c == '\"': # ex. ["\n" or "\"" or "\t" ect.] - state = 0 - - if (state == 0 and c != '/') or state == 5 or\ - state == 6 or state == 7 or state == 8 : - strOutput += c - else: - # removed chareters - strRemoved += c - - return strOutput +def removeCppComment(strInput): + state = 0 + strOutput = "" + strRemoved = "" + + for c in strInput: + if state == 0 and c == "/": # ex. [/] + state = 1 + elif state == 1 and c == "*": # ex. [/*] + state = 2 + elif state == 1 and c == "/": # ex. [#] + state = 4 + elif state == 1: # ex. [ or 5/3] + state = 0 + + elif state == 3 and c == "*": # ex. [/*he**] + state = 3 + elif state == 2 and c == "*": # ex. [/*he*] + state = 3 + elif state == 2: # ex. [/*heh] + state = 2 + + elif state == 3 and c == "/": # ex. [/*heh*/] + state = 0 + elif state == 3: # ex. [/*heh*e] + state = 2 + + elif state == 4 and c == "\\": # ex. [//hehe\] + state = 9 + elif state == 9 and c == "\\": # ex. [//hehe\\\\\] + state = 9 + elif state == 9: # ex. [//hehe\ or //hehe\a] + state = 4 + elif state == 4 and c == "\n": # ex. [//hehe] + state = 0 + + elif state == 0 and c == "'": # ex. ['] + state = 5 + elif state == 5 and c == "\\": # ex. ['\] + state = 6 + elif state == 6: # ex. ['\n or '\' or '\t etc.] + state = 5 + elif state == 5 and c == "'": # ex. ['\n' or '\'' or '\t' ect.] + state = 0 + + elif state == 0 and c == '"': # ex. ["] + state = 7 + elif state == 7 and c == "\\": # ex. ["\] + state = 8 + elif state == 8: # ex. ["\n or "\" or "\t ect.] + state = 7 + elif state == 7 and c == '"': # ex. ["\n" or "\"" or "\t" ect.] + state = 0 + + if ( + (state == 0 and c != "/") + or state == 5 + or state == 6 + or state == 7 + or state == 8 + ): + strOutput += c + else: + # removed chareters + strRemoved += c + + return strOutput files = [] # r=root, d=directories, f = files for r, d, f in os.walk(sys.argv[1]): - for file in f: - validate = 0 - if '.cxx' in file: -# print ( file ) - with open(os.path.join(r, file)) as fin: - if '::InitFromConfigFile' in fin.read(): - validate = 1 - with open(os.path.join(r, file)) as fin: - if '::LoadDefaultConfig' in fin.read(): - validate = 1 - with open(os.path.join(r, file)) as fin: - if '::Initialize' in fin.read(): - validate = validate + 1 - if validate == 2: - files.append(os.path.join(r, file)) - validateClass(os.path.join(r, file)) - -#for f in files: + for file in f: + validate = 0 + if ".cxx" in file: + # print ( file ) + with open(os.path.join(r, file)) as fin: + if "::InitFromConfigFile" in fin.read(): + validate = 1 + with open(os.path.join(r, file)) as fin: + if "::LoadDefaultConfig" in fin.read(): + validate = 1 + with open(os.path.join(r, file)) as fin: + if "::Initialize" in fin.read(): + validate = validate + 1 + if validate == 2: + files.append(os.path.join(r, file)) + validateClass(os.path.join(r, file)) + +# for f in files: # print(f) -#validateProcess(sys.argv[1]); +# validateProcess(sys.argv[1]); sys.exit(0) diff --git a/src/TRestGeant4ParticleSourceDecay0.cxx b/src/TRestGeant4ParticleSourceDecay0.cxx index de03721..8713e82 100644 --- a/src/TRestGeant4ParticleSourceDecay0.cxx +++ b/src/TRestGeant4ParticleSourceDecay0.cxx @@ -145,4 +145,4 @@ void TRestGeant4ParticleSourceDecay0::Update() { } // cout << endl; -} \ No newline at end of file +} From ee6c55739c801de86bd825f78916c663eab8b32c Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Fri, 10 Feb 2023 17:12:26 +0100 Subject: [PATCH 3/5] add pre-commit files check to workflow --- .github/workflows/validation.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/validation.yml b/.github/workflows/validation.yml index 453aa3f..b37b72b 100644 --- a/.github/workflows/validation.yml +++ b/.github/workflows/validation.yml @@ -43,6 +43,10 @@ jobs: with: cmake-flags: "-DCMAKE_INSTALL_PREFIX=${{ env.REST_PATH }} -DCMAKE_BUILD_TYPE=${{ env.CMAKE_BUILD_TYPE }} -DREST_WELCOME=ON -DRESTLIB_GEANT4=ON" branch: ${{ env.BRANCH_NAME }} + - name: Verify pre-commit config files match + run: | + cd $GITHUB_WORKSPACE + python ${{ env.REST_PATH }}/scripts/validatePreCommitConfig.py - name: Load REST libraries run: | source ${{ env.REST_PATH }}/thisREST.sh From 4cf60a1a1174747a794207dde50613ed7bd8eaaa Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Fri, 10 Feb 2023 17:33:34 +0100 Subject: [PATCH 4/5] run the validation script using curl --- .github/workflows/validation.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/validation.yml b/.github/workflows/validation.yml index b37b72b..323bd43 100644 --- a/.github/workflows/validation.yml +++ b/.github/workflows/validation.yml @@ -44,9 +44,7 @@ jobs: cmake-flags: "-DCMAKE_INSTALL_PREFIX=${{ env.REST_PATH }} -DCMAKE_BUILD_TYPE=${{ env.CMAKE_BUILD_TYPE }} -DREST_WELCOME=ON -DRESTLIB_GEANT4=ON" branch: ${{ env.BRANCH_NAME }} - name: Verify pre-commit config files match - run: | - cd $GITHUB_WORKSPACE - python ${{ env.REST_PATH }}/scripts/validatePreCommitConfig.py + run: curl https://raw.githubusercontent.com/rest-for-physics/framework/master/scripts/validatePreCommitConfig.py | sudo python - - name: Load REST libraries run: | source ${{ env.REST_PATH }}/thisREST.sh From 95b5bca19918882c608d0a96dcb4140972fe37b7 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Fri, 10 Feb 2023 17:36:18 +0100 Subject: [PATCH 5/5] make sure the directory is correct --- .github/workflows/validation.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/validation.yml b/.github/workflows/validation.yml index 323bd43..f1057db 100644 --- a/.github/workflows/validation.yml +++ b/.github/workflows/validation.yml @@ -44,7 +44,9 @@ jobs: cmake-flags: "-DCMAKE_INSTALL_PREFIX=${{ env.REST_PATH }} -DCMAKE_BUILD_TYPE=${{ env.CMAKE_BUILD_TYPE }} -DREST_WELCOME=ON -DRESTLIB_GEANT4=ON" branch: ${{ env.BRANCH_NAME }} - name: Verify pre-commit config files match - run: curl https://raw.githubusercontent.com/rest-for-physics/framework/master/scripts/validatePreCommitConfig.py | sudo python - + run: | + cd $GITHUB_WORKSPACE + curl https://raw.githubusercontent.com/rest-for-physics/framework/master/scripts/validatePreCommitConfig.py | python - name: Load REST libraries run: | source ${{ env.REST_PATH }}/thisREST.sh