From 8fa8f13fd8d8d7e0f2cc2e2738339f25365ba70b Mon Sep 17 00:00:00 2001
From: V3n3RiX <venerix@redcorelinux.org>
Date: Sat, 6 Jul 2019 17:41:14 +0100
Subject: libsisyphus : use pygit2 instead of subprocess to change branches,
 add options to use gitlab.com or pagure.io as remote

---
 src/backend/libsisyphus.py       | 180 ++++++++++++++++++++++++++++-----------
 src/frontend/cli/sisyphus-cli.py |  14 ++-
 2 files changed, 142 insertions(+), 52 deletions(-)

diff --git a/src/backend/libsisyphus.py b/src/backend/libsisyphus.py
index 0f5857e..73b9b91 100644
--- a/src/backend/libsisyphus.py
+++ b/src/backend/libsisyphus.py
@@ -11,6 +11,7 @@ import urllib3
 import io
 import wget
 import shutil
+import pygit2
 
 gentooEbuildDir = '/usr/ports/gentoo'
 redcoreEbuildDir = '/usr/ports/redcore'
@@ -507,8 +508,8 @@ def setActiveMirror(mirror):
                 mirrorList[i]['isActive'] = False
         writeMirrorCfg(mirrorList)
 
-@animation.wait('resetting environment')
-def resetPortageEnv():
+@animation.wait('resetting branch configuration')
+def resetBranch():
     if os.path.isdir(gentooEbuildDir):
         for files in os.listdir(gentooEbuildDir):
             if os.path.isfile(os.path.join(gentooEbuildDir, files)):
@@ -536,77 +537,156 @@ def resetPortageEnv():
     else:
         os.makedirs(portageConfigDir)
 
-def setPortageEnvStable():
+@animation.wait('injecting gentoo linux portage tree - branch master')
+def setGitlabMasterStage1():
     if not os.path.isdir(os.path.join(gentooEbuildDir, '.git')):
-        print("\ninjecting Gentoo Linux portage tree (master)\n")
-        os.chdir(gentooEbuildDir)
-        subprocess.call(['git', 'init', '-q'])
-        subprocess.call(['git', 'remote', 'add', 'origin', 'https://pagure.io/redcore/portage.git'])
-        subprocess.call(['git', 'pull', 'origin', 'master'])
-        subprocess.call(['git', 'branch', '-u', 'origin/master', 'master'])
+        repo_url = 'https://gitlab.com/redcore/portage.git'
+        repo_path = '/usr/ports/gentoo'
+        repo_branch = 'master'
+        pygit2.clone_repository(repo_url, repo_path, checkout_branch=repo_branch, bare=False)
 
+@animation.wait('injecting redcore linux ebuild tree - branch master')
+def setGitlabMasterStage2():
     if not os.path.isdir(os.path.join(redcoreEbuildDir, '.git')):
-        print("\ninjecting Redcore Linux overlay (master)\n")
-        os.chdir(redcoreEbuildDir)
-        subprocess.call(['git', 'init', '-q'])
-        subprocess.call(['git', 'remote', 'add', 'origin', 'https://pagure.io/redcore/redcore-desktop.git'])
-        subprocess.call(['git', 'pull', 'origin', 'master'])
-        subprocess.call(['git', 'branch', '-u', 'origin/master', 'master'])
+        repo_url = 'https://gitlab.com/redcore/redcore-desktop.git'
+        repo_path = '/usr/ports/redcore'
+        repo_branch = 'master'
+        pygit2.clone_repository(repo_url, repo_path, checkout_branch=repo_branch, bare=False)
 
+@animation.wait('injecting redcore linux portage configuration - branch master')
+def setGitlabMasterStage3():
     if not os.path.isdir(os.path.join(portageConfigDir, '.git')):
-        print("\ninjecting Redcore Linux portage configuration (master)\n")
-        os.chdir(portageConfigDir)
-        subprocess.call(['git', 'init', '-q'])
-        subprocess.call(['git', 'remote', 'add', 'origin', 'https://pagure.io/redcore/redcore-build.git'])
-        subprocess.call(['git', 'pull', 'origin', 'master'])
-        subprocess.call(['git', 'branch', '-u', 'origin/master', 'master'])
-
-def setPortageEnvTesting():
+        repo_url = 'https://gitlab.com/redcore/redcore-build.git'
+        repo_path = '/opt/redcore-build'
+        repo_branch = 'master'
+        pygit2.clone_repository(repo_url, repo_path, checkout_branch=repo_branch, bare=False)
+
+def setGitlabMaster():
+    setGitlabMasterStage1()
+    setGitlabMasterStage2()
+    setGitlabMasterStage3()
+
+@animation.wait('injecting gentoo linux portage tree - branch master')
+def setPagureMasterStage1():
     if not os.path.isdir(os.path.join(gentooEbuildDir, '.git')):
-        print("\ninjecting Gentoo Linux portage tree (next)\n")
-        os.chdir(gentooEbuildDir)
-        subprocess.call(['git', 'init', '-q'])
-        subprocess.call(['git', 'remote', 'add', 'origin', 'https://pagure.io/redcore/portage.git'])
-        subprocess.call(['git', 'pull', 'origin', 'next'])
-        subprocess.call(['git', 'branch', '-u', 'origin/next', 'master'])
+        repo_url = 'https://pagure.io/redcore/portage.git'
+        repo_path = '/usr/ports/gentoo'
+        repo_branch = 'master'
+        pygit2.clone_repository(repo_url, repo_path, checkout_branch=repo_branch, bare=False)
 
+@animation.wait('injecting redcore linux ebuild tree - branch master')
+def setPagureMasterStage2():
     if not os.path.isdir(os.path.join(redcoreEbuildDir, '.git')):
-        print("\ninjecting Redcore Linux overlay (next)\n")
-        os.chdir(redcoreEbuildDir)
-        subprocess.call(['git', 'init', '-q'])
-        subprocess.call(['git', 'remote', 'add', 'origin', 'https://pagure.io/redcore/redcore-desktop.git'])
-        subprocess.call(['git', 'pull', 'origin', 'next'])
-        subprocess.call(['git', 'branch', '-u', 'origin/next', 'master'])
+        repo_url = 'https://pagure.io/redcore/redcore-desktop.git'
+        repo_path = '/usr/ports/redcore'
+        repo_branch = 'master'
+        pygit2.clone_repository(repo_url, repo_path, checkout_branch=repo_branch, bare=False)
 
+@animation.wait('injecting redcore linux portage configuration - branch master')
+def setPagureMasterStage3():
     if not os.path.isdir(os.path.join(portageConfigDir, '.git')):
-        print("\ninjecting Redcore Linux portage configuration (next)\n")
-        os.chdir(portageConfigDir)
-        subprocess.call(['git', 'init', '-q'])
-        subprocess.call(['git', 'remote', 'add', 'origin', 'https://pagure.io/redcore/redcore-build.git'])
-        subprocess.call(['git', 'pull', 'origin', 'next'])
-        subprocess.call(['git', 'branch', '-u', 'origin/next', 'master'])
+        repo_url = 'https://pagure.io/redcore/redcore-build.git'
+        repo_path = '/opt/redcore-build'
+        repo_branch = 'master'
+        pygit2.clone_repository(repo_url, repo_path, checkout_branch=repo_branch, bare=False)
+
+def setPagureMaster():
+    setPagureMasterStage1()
+    setPagureMasterStage2()
+    setPagureMasterStage3()
+
+@animation.wait('injecting gentoo linux portage tree - branch next')
+def setGitlabNextStage1():
+    if not os.path.isdir(os.path.join(gentooEbuildDir, '.git')):
+        repo_url = 'https://gitlab.com/redcore/portage.git'
+        repo_path = '/usr/ports/gentoo'
+        repo_branch = 'next'
+        pygit2.clone_repository(repo_url, repo_path, checkout_branch=repo_branch, bare=False)
+
+@animation.wait('injecting redcore linux ebuild tree - branch next')
+def setGitlabNextStage2():
+    if not os.path.isdir(os.path.join(redcoreEbuildDir, '.git')):
+        repo_url = 'https://gitlab.com/redcore/redcore-desktop.git'
+        repo_path = '/usr/ports/redcore'
+        repo_branch = 'next'
+        pygit2.clone_repository(repo_url, repo_path, checkout_branch=repo_branch, bare=False)
+
+@animation.wait('injecting redcore linux portage configuration - branch next')
+def setGitlabNextStage3():
+    if not os.path.isdir(os.path.join(portageConfigDir, '.git')):
+        repo_url = 'https://gitlab.com/redcore/redcore-build.git'
+        repo_path = '/opt/redcore-build'
+        repo_branch = 'next'
+        pygit2.clone_repository(repo_url, repo_path, checkout_branch=repo_branch, bare=False)
+
+def setGitlabNext():
+    setGitlabNextStage1()
+    setGitlabNextStage2()
+    setGitlabNextStage3()
+
+@animation.wait('injecting gentoo linux portage tree - branch next')
+def setPagureNextStage1():
+    if not os.path.isdir(os.path.join(gentooEbuildDir, '.git')):
+        repo_url = 'https://pagure.io/redcore/portage.git'
+        repo_path = '/usr/ports/gentoo'
+        repo_branch = 'next'
+        pygit2.clone_repository(repo_url, repo_path, checkout_branch=repo_branch, bare=False)
+
+@animation.wait('injecting redcore linux ebuild tree - branch next')
+def setPagureNextStage2():
+    if not os.path.isdir(os.path.join(redcoreEbuildDir, '.git')):
+        repo_url = 'https://pagure.io/redcore/redcore-desktop.git'
+        repo_path = '/usr/ports/redcore'
+        repo_branch = 'next'
+        pygit2.clone_repository(repo_url, repo_path, checkout_branch=repo_branch, bare=False)
+
+@animation.wait('injecting redcore linux portage configuration - branch next')
+def setPagureNextStage3():
+    if not os.path.isdir(os.path.join(portageConfigDir, '.git')):
+        repo_url = 'https://pagure.io/redcore/redcore-build.git'
+        repo_path = '/opt/redcore-build'
+        repo_branch = 'next'
+        pygit2.clone_repository(repo_url, repo_path, checkout_branch=repo_branch, bare=False)
 
+def setPagureNext():
+    setPagureNextStage1()
+    setPagureNextStage2()
+    setPagureNextStage3()
+
+@animation.wait('setting up hardened profile')
 def setHardenedProfile():
     subprocess.call(['eselect', 'profile', 'set', 'default/linux/amd64/17.0/hardened'])
     subprocess.call(['env-update'])
 
-def resetPortage():
-    resetPortageEnv()
-
+@animation.wait('adjusting MAKEOPTS')
 def setJobs():
     subprocess.call(['/usr/share/sisyphus/helpers/set_jobs'])
 
-def setupStable():
+def injectGitlabMaster():
+    checkRoot()
+    resetBranch()
+    setGitlabMaster()
+    setHardenedProfile()
+    setJobs()
+
+def injectPagureMaster():
+    checkRoot()
+    resetBranch()
+    setPagureMaster()
+    setHardenedProfile()
+    setJobs()
+
+def injectGitlabNext():
     checkRoot()
-    resetPortageEnv()
-    setPortageEnvStable()
+    resetBranch()
+    setGitlabNext()
     setHardenedProfile()
     setJobs()
 
-def setupTesting():
+def injectPagureNext():
     checkRoot()
-    resetPortageEnv()
-    setPortageEnvTesting()
+    resetBranch()
+    setPagureNext()
     setHardenedProfile()
     setJobs()
 
diff --git a/src/frontend/cli/sisyphus-cli.py b/src/frontend/cli/sisyphus-cli.py
index 13c84f2..1755666 100755
--- a/src/frontend/cli/sisyphus-cli.py
+++ b/src/frontend/cli/sisyphus-cli.py
@@ -51,9 +51,19 @@ if "__main__" == __name__:
                 showHelp()
         elif "--branch" in sys.argv[1:]:
             if "--master" in sys.argv[2:]:
-                setupStable()
+                if "--gitlab" in sys.argv[3:]:
+                    injectGitlabMaster()
+                elif "--pagure" in sys.argv[3:]:
+                    injectPagureMaster()
+                else:
+                    showHelp()
             elif "--next" in sys.argv[2:]:
-                setupTesting()
+                if "--gitlab" in sys.argv[3:]:
+                    injectGitlabNext()
+                elif "--pagure" in sys.argv[3:]:
+                    injectPagureNext()
+                else:
+                    showHelp()
             else:
                 showHelp()
         elif "--help" in sys.argv[1:]:
-- 
cgit v1.2.3