[Replicant] [vendor_replicant-scripts] [PATCH v4 2/3] replicant_prepare_patch.py: wrap git commands in a class

Denis 'GNUtoo' Carikli GNUtoo at cyberdimension.org
Thu Oct 8 15:19:38 UTC 2020


Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo at cyberdimension.org>
---
 patches/replicant_prepare_patch.py | 169 ++++++++++++++---------------
 1 file changed, 83 insertions(+), 86 deletions(-)

diff --git a/patches/replicant_prepare_patch.py b/patches/replicant_prepare_patch.py
index 209c81d..5ed3311 100755
--- a/patches/replicant_prepare_patch.py
+++ b/patches/replicant_prepare_patch.py
@@ -18,7 +18,7 @@ import os, sys
 import re
 
 import configparser
-from sh import echo, git
+import sh
 
 # Settings
 cover_mail_template = """Hi,
@@ -35,13 +35,11 @@ git show {commit}
 {signature}
 """
 
-
 def usage(progname):
     print('{} <git_revision> [nr_patches] <patches_serie_revision>'.format(
         progname))
     sys.exit(1)
 
-
 def get_config():
     # This should implement the XDG Base Directory Specification which is
     # available here:
@@ -91,109 +89,108 @@ def get_config():
     config.read_file(config_file)
     return config
 
+class GitRepo(object):
+    def __init__(self, config):
+        self.config = config
 
-def get_repo_url(config):
-    output = git('remote', 'get-url', config['local']['git_remote'])
-    return output
-
-
-def get_repo_name(config):
-    output = git('remote', 'get-url', config['local']['git_remote'])
-    output = os.path.basename(str(output))
-    output = re.sub(os.linesep, '', output)
-    output = re.sub('\.git', '', output)
+    def get_repo_url(self):
+        output = sh.git('remote', 'get-url', self.config['local']['git_remote'])
+        return output
 
-    return output
+    def get_repo_name(self):
+        output = sh.git('remote', 'get-url', config['local']['git_remote'])
+        output = os.path.basename(str(output))
+        output = re.sub(os.linesep, '', output)
+        output = re.sub('\.git', '', output)
 
-# We want to generate a prefix to have the project name in it.
-# Examples:
-# - [libsamsung-ipc][PATCH] Fix IPC_SEC_LOCK_INFOMATION typo
-# - [device_samsung_i9300][PATCH] Add scripts to disable the modem
-# The revision is handled separately with git format-patch's -v<num> switch
-def get_subject_prefix(config):
-    repo_name = get_repo_name(config)
+        return output
 
-    # Try to autodetect the project name:
-    # external_libsamsung-ipc -> libsamsung-ipc
-    # device_samsung_i9300 -> device_samsung_i9300
-    dirs = repo_name.split('_')
+    # We want to generate a prefix to have the project name in it.
+    # Examples:
+    # - [libsamsung-ipc][PATCH] Fix IPC_SEC_LOCK_INFOMATION typo
+    # - [device_samsung_i9300][PATCH] Add scripts to disable the modem
+    # The revision is handled separately with git format-patch's -v<num> switch
+    def get_subject_prefix(self):
+        repo_name = self.get_repo_name()
 
-    project_name = None
-    if dirs[0] == "external":
-        project_name = dirs[-1]
-    elif dirs[0] == "hardware" and dirs[1] == "replicant":
-        project_name = dirs[-1]
-    elif dirs[0] == "device":
-        project_name =repo_name
-    else:
-        project_name =repo_name
+        # Try to autodetect the project name:
+        # external_libsamsung-ipc -> libsamsung-ipc
+        # device_samsung_i9300 -> device_samsung_i9300
+        dirs = repo_name.split('_')
 
-    if project_name == None:
-        return None
-
-    return '{project}] [PATCH'.format(project=project_name)
+        project_name = None
+        if dirs[0] == "external":
+            project_name = dirs[-1]
+        elif dirs[0] == "hardware" and dirs[1] == "replicant":
+            project_name = dirs[-1]
+        elif dirs[0] == "device":
+            project_name =repo_name
+        else:
+            project_name =repo_name
 
-def generate_patches(config, git_revision, nr_patches, patches_revision):
-    subject_prefix = get_subject_prefix(config)
+        if project_name == None:
+            return None
 
-    git_arguments = ['format-patch', git_revision, '-{}'.format(nr_patches)]
+        return '{project}] [PATCH'.format(project=project_name)
 
-    if subject_prefix != None:
-        git_arguments.append('--subject-prefix={}'.format(subject_prefix))
+    def generate_patches(self, git_revision, nr_patches, patches_revision):
+        subject_prefix = self.get_subject_prefix()
 
-    if patches_revision != None:
-        git_arguments.append('-v{}'.format(patches_revision))
+        git_arguments = ['format-patch', git_revision, '-{}'.format(nr_patches)]
 
-    patches = git(*git_arguments).split(os.linesep)
+        if subject_prefix != None:
+            git_arguments.append('--subject-prefix={}'.format(subject_prefix))
 
-    patches.remove('')
+        if patches_revision != None:
+            git_arguments.append('-v{}'.format(patches_revision))
 
-    return patches
+        patches = sh.git(*git_arguments).split(os.linesep)
 
+        patches.remove('')
 
-def generate_cover_mail_text(config, commit, nr_patches, repo):
-    cgit_url = 'https://git.replicant.us'
+        return patches
 
-    web_url = '{base}/contrib/{user}/{repo}/commit/?id={commit}'.format(
-        base=cgit_url, user=config['project']['username'], repo=repo,
-        commit=commit)
+    def generate_cover_mail_text(self, commit, nr_patches, repo):
+        cgit_url = 'https://git.replicant.us'
 
-    clone_repo = '{base}/{user}/{repo}'.format(
-        base=cgit_url, user=config['project']['username'], repo=repo)
+        web_url = '{base}/contrib/{user}/{repo}/commit/?id={commit}'.format(
+            base=cgit_url, user=self.config['project']['username'], repo=repo,
+            commit=commit)
 
-    signature = config['local']['mail_signature']
+        clone_repo = '{base}/{user}/{repo}'.format(
+            base=cgit_url, user=self.config['project']['username'], repo=repo)
 
-    patch = 'patch'
-    it = 'it'
-    if nr_patches > 1:
-        patch = 'patches'
-        it = 'them'
+        signature = self.config['local']['mail_signature']
 
-    return cover_mail_template.format(patch=patch,
-                                      it=it,
-                                      web_url=web_url,
-                                      clone_repo=clone_repo,
-                                      commit=commit,
-                                      repo_name=repo,
-                                      signature=signature)
+        patch = 'patch'
+        it = 'it'
+        if nr_patches > 1:
+            patch = 'patches'
+            it = 'them'
 
-def generate_git_send_email_command(config, git_revision, patches):
-    command = ['send-email',
-               '--compose',
-               '--to={}'.format(config['project']['mailing_list'])]
+        return cover_mail_template.format(patch=patch,
+                                          it=it,
+                                          web_url=web_url,
+                                          clone_repo=clone_repo,
+                                          commit=commit,
+                                          repo_name=repo,
+                                          signature=signature)
 
-    command += patches
+    def generate_git_send_email_command(self, git_revision, patches):
+        command = ['send-email',
+                   '--compose',
+                   '--to={}'.format(self.config['project']['mailing_list'])]
 
-    return command
+        command += patches
 
+        return command
 
-def get_git_revision(git_revision):
-    output = git('--no-pager', 'log', '--oneline', git_revision, '-1',
-                 '--format=%H')
-    revision = re.sub(os.linesep, '', str(output))
-
-    return revision
+    def get_git_revision(self, git_revision):
+        output = sh.git('--no-pager', 'log', '--oneline', git_revision, '-1',
+                        '--format=%H')
+        revision = re.sub(os.linesep, '', str(output))
 
+        return revision
 
 if __name__ == '__main__':
     nr_patches = 1
@@ -214,11 +211,10 @@ if __name__ == '__main__':
         print('Failed to find a configuration file')
         sys.exit(1)
 
-    git_revision = get_git_revision(sys.argv[1])
-
+    repo = GitRepo(config)
+    git_revision = repo.get_git_revision(sys.argv[1])
 
-    patches = generate_patches(config, git_revision, nr_patches,
-                               patches_revision)
+    patches = repo.generate_patches(git_revision, nr_patches, patches_revision)
 
     print('patches:')
     print('--------')
@@ -229,9 +225,10 @@ if __name__ == '__main__':
     print('git command:')
     print('------------')
     print('git ' + ' '.join(
-        generate_git_send_email_command(config, git_revision, patches)))
+        repo.generate_git_send_email_command(git_revision, patches)))
 
     print()
     print('Cover mail:')
     print('-----------')
-    print(generate_cover_mail_text(config, git_revision, nr_patches, get_repo_name(config)))
+    print(repo.generate_cover_mail_text(git_revision, nr_patches,
+                                        repo.get_repo_name()))
-- 
2.28.0



More information about the Replicant mailing list